Axe the stupid torrent flags.

This commit is contained in:
Josh Elsasser 2007-05-23 19:26:29 +00:00
parent e52e736e91
commit eadedea754
11 changed files with 134 additions and 126 deletions

View File

@ -57,10 +57,11 @@ struct addcb {
struct dirdata
{
add_torrents_func_t addfunc;
void * cbdata;
GList * files;
guint flags;
add_torrents_func_t addfunc;
void * cbdata;
GList * files;
enum tr_torrent_action action;
gboolean paused;
};
struct quitdata
@ -213,8 +214,8 @@ addresp(GtkWidget *widget, gint resp, gpointer gdata) {
struct addcb *data = gdata;
GSList *files, *ii;
GList *stupidgtk;
int flags;
char *dir;
enum tr_torrent_action action;
if(GTK_RESPONSE_ACCEPT == resp) {
dir = NULL;
@ -224,9 +225,9 @@ addresp(GtkWidget *widget, gint resp, gpointer gdata) {
stupidgtk = NULL;
for(ii = files; NULL != ii; ii = ii->next)
stupidgtk = g_list_append(stupidgtk, ii->data);
flags = ( data->autostart ? TR_TORNEW_RUNNING : TR_TORNEW_PAUSED );
flags |= addactionflag( tr_prefs_get( PREF_ID_ADDSTD ) );
data->addfunc( data->data, NULL, stupidgtk, dir, flags );
action = toraddaction( tr_prefs_get( PREF_ID_ADDSTD ) );
data->addfunc( data->data, NULL, stupidgtk, dir,
action, !data->autostart );
if(NULL != dir)
g_free(dir);
g_slist_free(files);
@ -507,7 +508,7 @@ fmtpeercount( GtkLabel * label, int count )
void
promptfordir( GtkWindow * parent, add_torrents_func_t addfunc, void *cbdata,
GList * files, guint flags )
GList * files, enum tr_torrent_action act, gboolean paused )
{
struct dirdata * stuff;
GtkWidget * wind;
@ -516,7 +517,8 @@ promptfordir( GtkWindow * parent, add_torrents_func_t addfunc, void *cbdata,
stuff->addfunc = addfunc;
stuff->cbdata = cbdata;
stuff->files = dupstrlist( files );
stuff->flags = flags;
stuff->action = act;
stuff->paused = paused;
wind = gtk_file_chooser_dialog_new( _("Choose a directory"), parent,
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
@ -547,7 +549,8 @@ promptresp( GtkWidget * widget, gint resp, gpointer data )
dir = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( widget ) );
/* it seems that we will always get a directory */
g_assert( NULL != dir );
stuff->addfunc( stuff->cbdata, NULL, stuff->files, dir, stuff->flags );
stuff->addfunc( stuff->cbdata, NULL, stuff->files, dir,
stuff->action, stuff->paused );
g_free( dir );
}

View File

@ -40,7 +40,7 @@ makeinfowind( GtkWindow * parent, GtkTreeModel * model, GtkTreePath * path,
/* prompt for a download directory for torrents, then add them */
void
promptfordir( GtkWindow * parent, add_torrents_func_t addfunc, void *cbdata,
GList * files, guint flags );
GList * files, enum tr_torrent_action act, gboolean paused );
/* prompt if the user wants to quit, calls func with cbdata if they do */
void

View File

@ -462,7 +462,6 @@ srv_addfile(struct constate *con, const char *name SHUTUP, benc_val_t *val) {
struct constate_serv *srv = &con->u.serv;
GList *files;
int ii;
guint flags;
if(TYPE_LIST == val->type) {
files = NULL;
@ -471,8 +470,8 @@ srv_addfile(struct constate *con, const char *name SHUTUP, benc_val_t *val) {
/* XXX somehow escape invalid utf-8 */
g_utf8_validate(val->val.l.vals[ii].val.s.s, -1, NULL))
files = g_list_append(files, val->val.l.vals[ii].val.s.s);
flags = addactionflag( tr_prefs_get( PREF_ID_ADDIPC ) );
srv->addfunc( srv->cbdata, NULL, files, NULL, flags );
srv->addfunc( srv->cbdata, NULL, files, NULL,
toraddaction( tr_prefs_get( PREF_ID_ADDIPC ) ), FALSE );
g_list_free(files);
}
}

View File

@ -186,7 +186,7 @@ handleaction( struct cbdata *data, int action );
static void
addtorrents(void *vdata, void *state, GList *files,
const char *dir, guint flags);
const char *dir, enum tr_torrent_action act, gboolean paused);
static void
safepipe(void);
static void
@ -373,7 +373,6 @@ static void
appsetup( TrWindow * wind, benc_val_t * state, GList * args, gboolean paused )
{
struct cbdata * cbdata;
guint flags;
/* fill out cbdata */
cbdata = g_new0( struct cbdata, 1 );
@ -396,13 +395,8 @@ appsetup( TrWindow * wind, benc_val_t * state, GList * args, gboolean paused )
winsetup( cbdata, wind );
/* add torrents from command-line and saved state */
flags = addactionflag( tr_prefs_get( PREF_ID_ADDIPC ) );
g_assert( !( flags & ( TR_TORNEW_PAUSED | TR_TORNEW_RUNNING ) ) );
if( paused )
{
flags |= TR_TORNEW_PAUSED;
}
addtorrents( cbdata, state, args, NULL, flags );
addtorrents( cbdata, state, args, NULL,
toraddaction( tr_prefs_get( PREF_ID_ADDIPC ) ), paused );
/* start model update timer */
cbdata->timer = g_timeout_add( UPDATE_INTERVAL, updatemodel, cbdata );
@ -667,7 +661,7 @@ gotdrag(GtkWidget *widget SHUTUP, GdkDragContext *dc, gint x SHUTUP,
if( NULL != paths )
{
addtorrents( data, NULL, paths, NULL,
addactionflag( tr_prefs_get( PREF_ID_ADDSTD ) ) );
toraddaction( tr_prefs_get( PREF_ID_ADDSTD ) ), FALSE );
}
freestrlist(freeables);
g_free(files);
@ -1011,8 +1005,9 @@ handleaction( struct cbdata * data, int act )
}
static void
addtorrents(void *vdata, void *state, GList *files,
const char *dir, guint flags) {
addtorrents( void * vdata, void * state, GList * files, const char * dir,
enum tr_torrent_action act, gboolean paused )
{
struct cbdata *data = vdata;
const char * pref;
int added;
@ -1021,7 +1016,7 @@ addtorrents(void *vdata, void *state, GList *files,
if( NULL != state )
{
added += tr_core_load( data->core, state );
added += tr_core_load( data->core, state, paused );
}
if(NULL != files) {
@ -1030,14 +1025,14 @@ addtorrents(void *vdata, void *state, GList *files,
pref = tr_prefs_get( PREF_ID_ASKDIR );
if( NULL != pref && strbool( pref ) )
{
promptfordir( data->wind, addtorrents, data, files, flags );
promptfordir( data->wind, addtorrents, data, files, act, paused );
files = NULL;
}
dir = getdownloaddir();
}
for( files = g_list_first( files ); NULL != files; files = files->next )
{
if( tr_core_add_torrent( data->core, files->data, dir, flags ) )
if( tr_core_add_torrent( data->core, files->data, dir, act, paused ) )
{
added++;
}

View File

@ -432,7 +432,7 @@ tr_core_save( TrCore * self )
}
int
tr_core_load( TrCore * self, benc_val_t * state )
tr_core_load( TrCore * self, benc_val_t * state, gboolean forcepaused )
{
int ii, count;
char * errstr;
@ -450,7 +450,7 @@ tr_core_load( TrCore * self, benc_val_t * state )
{
errstr = NULL;
tor = tr_torrent_new_with_state( self->handle, state->val.l.vals + ii,
0, &errstr );
forcepaused, &errstr );
if( NULL == tor )
{
tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr );
@ -468,8 +468,8 @@ tr_core_load( TrCore * self, benc_val_t * state )
}
gboolean
tr_core_add_torrent( TrCore * self, const char * torrent, const char * dir,
guint flags )
tr_core_add_torrent( TrCore * self, const char * path, const char * dir,
enum tr_torrent_action act, gboolean paused )
{
TrTorrent * tor;
char * errstr;
@ -477,7 +477,7 @@ tr_core_add_torrent( TrCore * self, const char * torrent, const char * dir,
TR_IS_CORE( self );
errstr = NULL;
tor = tr_torrent_new( self->handle, torrent, dir, flags, &errstr );
tor = tr_torrent_new( self->handle, path, dir, act, paused, &errstr );
if( NULL == tor )
{
tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr );

View File

@ -32,6 +32,8 @@
#include "bencode.h"
#include "util.h"
#define TR_CORE_TYPE ( tr_core_get_type() )
#define TR_CORE( obj ) \
@ -108,13 +110,12 @@ tr_core_save( TrCore * self );
/* Load saved state, return number of torrents added. May trigger one
or more "error" signals with TR_CORE_ERR_ADD_TORRENT */
int
tr_core_load( TrCore * self, benc_val_t * state );
tr_core_load( TrCore * self, benc_val_t * state, gboolean forcepaused );
/* Add a torrent. Torrent, dir and flags arguments are the same as
tr_torrent_new(). May trigger "error" signal with TR_CORE_ERR_ADD_TORRENT */
/* Add a torrent. May trigger "error" signal with TR_CORE_ERR_ADD_TORRENT */
gboolean
tr_core_add_torrent( TrCore * self, const char * torrent, const char * dir,
guint flags );
tr_core_add_torrent( TrCore * self, const char * path, const char * dir,
enum tr_torrent_action act, gboolean paused );
/* Trigger "error" signal with TR_CORE_ERR_NO_MORE_TORRENTS */
void

View File

@ -854,13 +854,13 @@ makecombomodel( void )
/* create the model used by the two popup menus */
list = gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_INT );
gtk_list_store_append( list, &iter );
gtk_list_store_set( list, &iter, 1, 0, 0,
gtk_list_store_set( list, &iter, 1, TR_TOR_LEAVE, 0,
_("Use the torrent file where it is"), -1 );
gtk_list_store_append( list, &iter );
gtk_list_store_set( list, &iter, 1, TR_TORNEW_SAVE_COPY, 0,
gtk_list_store_set( list, &iter, 1, TR_TOR_COPY, 0,
_("Keep a copy of the torrent file"), -1 );
gtk_list_store_append( list, &iter );
gtk_list_store_set( list, &iter, 1, TR_TORNEW_SAVE_MOVE, 0,
gtk_list_store_set( list, &iter, 1, TR_TOR_MOVE, 0,
_("Keep a copy and remove the original"), -1 );
return GTK_TREE_MODEL( list );
@ -873,7 +873,7 @@ addwid_combo( TrPrefs * self, int id, GtkTooltips * tips,
GtkWidget * combo, * label;
GtkCellRenderer * rend;
GtkTreeIter iter;
guint prefsflag, modelflag;
enum tr_torrent_action prefsact, modelact;
g_assert( ALEN( defs ) > id && GTK_TYPE_COMBO_BOX == PTYPE( id ) );
combo = gtk_combo_box_new();
@ -885,13 +885,13 @@ addwid_combo( TrPrefs * self, int id, GtkTooltips * tips,
gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( combo ), rend, TRUE );
gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT( combo ), rend, "text", 0 );
prefsflag = addactionflag( tr_prefs_get( id ) );
prefsact = toraddaction( tr_prefs_get( id ) );
if( gtk_tree_model_get_iter_first( self->combomodel, &iter ) )
{
do
{
gtk_tree_model_get( self->combomodel, &iter, 1, &modelflag, -1 );
if( modelflag == prefsflag)
gtk_tree_model_get( self->combomodel, &iter, 1, &modelact, -1 );
if( modelact == prefsact )
{
gtk_combo_box_set_active_iter( GTK_COMBO_BOX( combo ), &iter );
break;
@ -912,7 +912,7 @@ combochosen( GtkWidget * widget, gpointer data )
TrPrefs * self;
GtkTreeIter iter;
GtkTreeModel * model;
guint flags;
enum tr_torrent_action action;
int id;
TR_IS_PREFS( data );
@ -920,9 +920,9 @@ combochosen( GtkWidget * widget, gpointer data )
if( gtk_combo_box_get_active_iter( GTK_COMBO_BOX( widget ), &iter ) )
{
model = gtk_combo_box_get_model( GTK_COMBO_BOX( widget ) );
gtk_tree_model_get( model, &iter, 1, &flags, -1 );
gtk_tree_model_get( model, &iter, 1, &action, -1 );
GETPREFID( widget, id );
savepref( self, id, addactionname( flags ) );
savepref( self, id, toractionname( action ) );
}
}

View File

@ -290,27 +290,38 @@ tr_torrent_info(TrTorrent *tor) {
return tr_torrentInfo(tor->handle);
}
static TrTorrent *
maketorrent( tr_torrent_t * handle, const char * dir, gboolean paused )
{
TrTorrent * tor;
tr_torrentDisablePex( handle,
!tr_prefs_get_bool_with_default( PREF_ID_PEX ) );
tor = g_object_new( TR_TORRENT_TYPE, "torrent-handle", handle,
"download-directory", dir, NULL);
g_object_set( tor, "paused", paused, NULL );
return tor;
}
TrTorrent *
tr_torrent_new( tr_handle_t * back, const char *torrent, const char *dir,
guint flags, char **err) {
enum tr_torrent_action act, gboolean paused, char **err )
{
TrTorrent *ret;
tr_torrent_t *handle;
int errcode, trflags;
gboolean boolval;
int errcode, flag;
g_assert(NULL != dir);
*err = NULL;
trflags = 0;
if((TR_TORNEW_SAVE_COPY|TR_TORNEW_SAVE_MOVE) & flags)
trflags |= TR_FLAG_SAVE;
flag = ( TR_TOR_COPY == act || TR_TOR_MOVE == act ? TR_FLAG_SAVE : 0 );
errcode = -1;
if(TR_TORNEW_LOAD_SAVED & flags)
handle = tr_torrentInitSaved(back, torrent, 0, &errcode);
else
handle = tr_torrentInit(back, torrent, NULL, trflags, &errcode);
handle = tr_torrentInit( back, torrent, NULL, flag, &errcode );
if(NULL == handle) {
switch(errcode) {
@ -327,17 +338,9 @@ tr_torrent_new( tr_handle_t * back, const char *torrent, const char *dir,
return NULL;
}
/* I should probably add a property for this but I've had enough
with adding useless gtk glue to this program */
boolval = tr_prefs_get_bool_with_default( PREF_ID_PEX );
tr_torrentDisablePex( handle, !boolval );
ret = maketorrent( handle, dir, paused );
ret = g_object_new(TR_TORRENT_TYPE, "torrent-handle", handle,
"download-directory", dir, NULL);
g_object_set(ret, "paused", (TR_TORNEW_PAUSED & flags ? TRUE : FALSE), NULL);
if(TR_TORNEW_SAVE_MOVE & flags)
if( TR_TOR_MOVE == act )
ret->delfile = g_strdup(torrent);
return ret;
@ -345,13 +348,13 @@ tr_torrent_new( tr_handle_t * back, const char *torrent, const char *dir,
TrTorrent *
tr_torrent_new_with_state( tr_handle_t * back, benc_val_t * state,
guint forcedflags, char ** err)
gboolean forcedpause, char ** err )
{
int ii;
tr_torrent_t * handle;
int ii, errcode;
benc_val_t *name, *data;
char *torrent, *hash, *dir;
gboolean hadpaused, paused;
guint flags;
gboolean paused;
*err = NULL;
@ -359,8 +362,7 @@ tr_torrent_new_with_state( tr_handle_t * back, benc_val_t * state,
return NULL;
torrent = hash = dir = NULL;
hadpaused = FALSE;
paused = FALSE; /* silence stupid compiler warning */
paused = FALSE;
for(ii = 0; ii + 1 < state->val.l.count; ii += 2) {
name = state->val.l.vals + ii;
@ -374,7 +376,6 @@ tr_torrent_new_with_state( tr_handle_t * back, benc_val_t * state,
else if(0 == strcmp("dir", name->val.s.s))
dir = data->val.s.s;
else if(0 == strcmp("paused", name->val.s.s)) {
hadpaused = TRUE;
paused = (data->val.i ? TRUE : FALSE);
}
}
@ -384,21 +385,28 @@ tr_torrent_new_with_state( tr_handle_t * back, benc_val_t * state,
(NULL == torrent && NULL == hash) || NULL == dir)
return NULL;
flags = 0;
if(hadpaused)
flags |= (paused ? TR_TORNEW_PAUSED : TR_TORNEW_RUNNING);
if(NULL != hash) {
flags |= TR_TORNEW_LOAD_SAVED;
torrent = hash;
}
forcedflags &= TR_TORNEW_PAUSED | TR_TORNEW_RUNNING;
if( forcedflags )
{
flags &= ~( TR_TORNEW_PAUSED | TR_TORNEW_RUNNING );
flags |= forcedflags;
if( NULL != hash )
handle = tr_torrentInitSaved(back, hash, 0, &errcode);
else
handle = tr_torrentInit(back, torrent, NULL, 0, &errcode);
if(NULL == handle) {
torrent = ( NULL == hash ? torrent : hash );
switch(errcode) {
case TR_EINVALID:
*err = g_strdup_printf(_("%s: not a valid torrent file"), torrent);
break;
case TR_EDUPLICATE:
*err = g_strdup_printf(_("%s: torrent is already open"), torrent);
break;
default:
*err = g_strdup(torrent);
break;
}
return NULL;
}
return tr_torrent_new( back, torrent, dir, flags, err );
return maketorrent( handle, dir, paused || forcedpause );
}
gboolean

View File

@ -30,6 +30,8 @@
#include "transmission.h"
#include "bencode.h"
#include "util.h"
/* boxed type for tr_tracker_info_t */
#define TR_TRACKER_BOXED_TYPE (tr_tracker_boxed_get_type ())
GType
@ -76,25 +78,15 @@ tr_torrent_stat(TrTorrent *tor);
tr_info_t *
tr_torrent_info(TrTorrent *tor);
/* explicitly start the torrent running or paused */
#define TR_TORNEW_PAUSED 0x01
#define TR_TORNEW_RUNNING 0x02
/* load a saved torrent file, torrent param is hash instead of filename */
#define TR_TORNEW_LOAD_SAVED 0x04
/* save a private copy of the torrent file */
#define TR_TORNEW_SAVE_COPY 0x08
/* save a private copy of the torrent file and remove the original */
#define TR_TORNEW_SAVE_MOVE 0x10
#ifdef TR_WANT_TORRENT_PRIVATE
TrTorrent *
tr_torrent_new( tr_handle_t * handle, const char *torrent, const char *dir,
guint flags, char **err);
tr_torrent_new( tr_handle_t * handle, const char * path, const char * dir,
enum tr_torrent_action act, gboolean paused, char ** err);
TrTorrent *
tr_torrent_new_with_state( tr_handle_t * handle, benc_val_t * state,
guint flags, char ** err );
gboolean forcepaused, char ** err );
gboolean
tr_torrent_get_state( TrTorrent * tor, benc_val_t * state );

View File

@ -268,29 +268,35 @@ checkfilenames(int argc, char **argv) {
return ret;
}
guint
addactionflag(const char *action) {
if(NULL == action)
return TR_TORNEW_SAVE_COPY;
else if(0 == strcmp("copy", action))
return TR_TORNEW_SAVE_COPY;
else if(0 == strcmp("move", action))
return TR_TORNEW_SAVE_MOVE;
else
return 0;
enum tr_torrent_action
toraddaction( const char * action )
{
if( NULL == action || 0 == strcmp( "copy", action ) )
{
return TR_TOR_COPY;
}
else if( 0 == strcmp( "move", action ) )
{
return TR_TOR_MOVE;
}
else
{
return TR_TOR_LEAVE;
}
}
const char *
addactionname( guint flag )
toractionname( enum tr_torrent_action action )
{
static char name[6];
snprintf( name, sizeof name, "%s",
( TR_TORNEW_SAVE_COPY & flag ? "copy" :
( TR_TORNEW_SAVE_MOVE & flag ? "move" :
"leave" ) ) );
return name;
switch( action )
{
case TR_TOR_COPY:
return "copy";
case TR_TOR_MOVE:
return "move";
default:
return "leave";
}
}
const char *

View File

@ -35,7 +35,11 @@
#define SHUTUP
#endif
typedef void (*add_torrents_func_t)(void*,void*,GList*,const char*,guint);
/* XXX this shouldn't be here */
enum tr_torrent_action { TR_TOR_LEAVE, TR_TOR_COPY, TR_TOR_MOVE };
typedef void (*add_torrents_func_t)(void*,void*,GList*,const char*,
enum tr_torrent_action,gboolean);
/* return number of items in array */
#define ALEN( a ) ( ( signed )( sizeof(a) / sizeof( (a)[0] ) ) )
@ -106,12 +110,12 @@ GList *
checkfilenames(int argc, char **argv);
/* returns the flag for an action string */
guint
addactionflag(const char *action);
enum tr_torrent_action
toraddaction( const char * action );
/* returns the action string for a flag */
const char *
addactionname(guint flag);
toractionname( enum tr_torrent_action action );
/* retrieve the global download directory */
const char *