Changeset 12071


Ignore:
Timestamp:
Mar 3, 2011, 7:17:57 AM (11 years ago)
Author:
jordan
Message:

(trunk gtk) some tr-core cleanup

Variable and function name cleanup; regrouping related functions together, etc.

Location:
trunk/gtk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/main.c

    r12068 r12071  
    16611661{
    16621662    int id;
    1663     GSList ** data = ( GSList** ) gdata;
     1663    GSList ** data = gdata;
    16641664
    16651665    gtk_tree_model_get( model, iter, MC_TORRENT_ID, &id, -1 );
  • trunk/gtk/tr-core.c

    r12068 r12071  
    7171static guint core_signals[LAST_SIGNAL] = { 0 };
    7272
    73 static void maybe_inhibit_hibernation( TrCore * core );
     73static void core_maybe_inhibit_hibernation( TrCore * core );
    7474
    7575static gboolean our_instance_adds_remote_torrents = FALSE;
     
    215215}
    216216
    217 /***
    218 ****
    219 ***/
    220 
    221 static GtkTreeModel *
    222 core_raw_model( TrCore * core )
    223 {
    224     return core_is_disposed( core ) ? NULL : core->priv->raw_model;
    225 }
    226 
    227 GtkTreeModel *
    228 gtr_core_model( TrCore * core )
    229 {
    230     return core_is_disposed( core ) ? NULL : core->priv->sorted_model;
    231 }
    232 
    233 tr_session *
    234 gtr_core_session( TrCore * core )
    235 {
    236     return core_is_disposed( core ) ? NULL : core->priv->session;
    237 }
    238 
    239 /***
    240 ****
    241 ***/
    242 
    243 static tr_bool
    244 core_is_busy( TrCore * core )
    245 {
    246     return core->priv->busy_count > 0;
    247 }
    248 
    249 static void
    250 core_emit_busy( TrCore * core )
    251 {
    252     g_signal_emit( core, core_signals[BUSY_SIGNAL], 0, core_is_busy( core ) );
    253 }
    254 
    255 static void
    256 core_add_to_busy( TrCore * core, int addMe )
    257 {
    258     const tr_bool wasBusy = core_is_busy( core );
    259 
    260     core->priv->busy_count += addMe;
    261 
    262     if( wasBusy != core_is_busy( core ) )
    263         core_emit_busy( core );
    264 }
    265 
    266 static void core_inc_busy( TrCore * core ) { core_add_to_busy( core, 1 ); }
    267 static void core_dec_busy( TrCore * core ) { core_add_to_busy( core, -1 ); }
    268 
    269 /***
    270 ****  SORTING
    271 ***/
    272 
    273 static gboolean
    274 isValidETA( int t )
    275 {
    276     return ( t != TR_ETA_NOT_AVAIL ) && ( t != TR_ETA_UNKNOWN );
    277 }
    278 
    279 static int
    280 compareETA( int a, int b )
    281 {
    282     const gboolean a_valid = isValidETA( a );
    283     const gboolean b_valid = isValidETA( b );
    284 
    285     if( !a_valid && !b_valid ) return 0;
    286     if( !a_valid ) return -1;
    287     if( !b_valid ) return 1;
    288     return a < b ? 1 : -1;
    289 }
    290 
    291 static int
    292 compareDouble( double a, double b )
    293 {
    294     if( a < b ) return -1;
    295     if( a > b ) return 1;
    296     return 0;
    297 }
    298 
    299 static int
    300 compareUint64( uint64_t a, uint64_t b )
    301 {
    302     if( a < b ) return -1;
    303     if( a > b ) return 1;
    304     return 0;
    305 }
    306 
    307 static int
    308 compareInt_( int a, int b )
    309 {
    310     if( a < b ) return -1;
    311     if( a > b ) return 1;
    312     return 0;
    313 }
    314 
    315 static int
    316 compareRatio( double a, double b )
    317 {
    318     if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0;
    319     if( (int)a == TR_RATIO_INF ) return 1;
    320     if( (int)b == TR_RATIO_INF ) return -1;
    321     return compareDouble( a, b );
    322 }
    323 
    324 static int
    325 compareTime( time_t a, time_t b )
    326 {
    327     if( a < b ) return -1;
    328     if( a > b ) return 1;
    329     return 0;
    330 }
    331 
    332 static int
    333 compareByName( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data UNUSED )
    334 {
    335     int ret = 0;
    336 
    337     if( !ret ) {
    338         char *ca, *cb;
    339         gtk_tree_model_get( m, a, MC_NAME_COLLATED, &ca, -1 );
    340         gtk_tree_model_get( m, b, MC_NAME_COLLATED, &cb, -1 );
    341         ret = gtr_strcmp0( ca, cb );
    342         g_free( cb );
    343         g_free( ca );
    344     }
    345 
    346     return ret;
    347 }
    348 
    349 static int
    350 compareByRatio( GtkTreeModel* m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
    351 {
    352     int ret = 0;
    353     tr_torrent *ta, *tb;
    354     const tr_stat *sa, *sb;
    355 
    356     gtk_tree_model_get( m, a, MC_TORRENT, &ta, -1 );
    357     sa = tr_torrentStatCached( ta );
    358     gtk_tree_model_get( m, b, MC_TORRENT, &tb, -1 );
    359     sb = tr_torrentStatCached( tb );
    360 
    361     if( !ret ) ret = compareRatio( sa->ratio, sb->ratio );
    362     if( !ret ) ret = compareByName( m, a, b, user_data );
    363     return ret;
    364 }
    365 
    366 static int
    367 compareByActivity( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
    368 {
    369     int ret = 0;
    370     tr_torrent *ta, *tb;
    371     const tr_stat *sa, *sb;
    372     double aUp, aDown, bUp, bDown;
    373 
    374     gtk_tree_model_get( m, a, MC_SPEED_UP, &aUp,
    375                               MC_SPEED_DOWN, &aDown,
    376                               MC_TORRENT, &ta,
    377                               -1 );
    378     gtk_tree_model_get( m, b, MC_SPEED_UP, &bUp,
    379                               MC_SPEED_DOWN, &bDown,
    380                               MC_TORRENT, &tb,
    381                               -1 );
    382     sa = tr_torrentStatCached( ta );
    383     sb = tr_torrentStatCached( tb );
    384 
    385     if( !ret ) ret = compareDouble( aUp+aDown, bUp+bDown );
    386     if( !ret ) ret = compareUint64( sa->uploadedEver, sb->uploadedEver );
    387     if( !ret ) ret = compareByName( m, a, b, user_data );
    388     return ret;
    389 }
    390 
    391 static int
    392 compareByAge( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
    393 {
    394     int ret = 0;
    395     tr_torrent *ta, *tb;
    396 
    397     gtk_tree_model_get( m, a, MC_TORRENT, &ta, -1 );
    398     gtk_tree_model_get( m, b, MC_TORRENT, &tb, -1 );
    399 
    400     if( !ret ) ret = compareTime( tr_torrentStatCached( ta )->addedDate, tr_torrentStatCached( tb )->addedDate );
    401     if( !ret ) ret = compareByName( m, a, b, user_data );
    402     return ret;
    403 }
    404 
    405 static int
    406 compareBySize( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
    407 {
    408     int ret = 0;
    409     tr_torrent *t;
    410     const tr_info *ia, *ib;
    411 
    412     gtk_tree_model_get( m, a, MC_TORRENT, &t, -1 );
    413     ia = tr_torrentInfo( t );
    414     gtk_tree_model_get( m, b, MC_TORRENT, &t, -1 );
    415     ib = tr_torrentInfo( t );
    416 
    417     if( !ret ) ret = compareUint64( ia->totalSize, ib->totalSize );
    418     if( !ret ) ret = compareByName( m, a, b, user_data );
    419     return ret;
    420 }
    421 
    422 static int
    423 compareByProgress( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
    424 {
    425     int ret = 0;
    426     tr_torrent * t;
    427     const tr_stat *sa, *sb;
    428 
    429     gtk_tree_model_get( m, a, MC_TORRENT, &t, -1 );
    430     sa = tr_torrentStatCached( t );
    431     gtk_tree_model_get( m, b, MC_TORRENT, &t, -1 );
    432     sb = tr_torrentStatCached( t );
    433 
    434     if( !ret ) ret = compareDouble( sa->percentComplete, sb->percentComplete );
    435     if( !ret ) ret = compareDouble( sa->seedRatioPercentDone, sb->seedRatioPercentDone );
    436     if( !ret ) ret = compareByRatio( m, a, b, user_data );
    437     return ret;
    438 }
    439 
    440 static int
    441 compareByETA( GtkTreeModel * m, GtkTreeIter  * a, GtkTreeIter  * b, gpointer user_data )
    442 {
    443     int ret = 0;
    444     tr_torrent *ta, *tb;
    445 
    446     gtk_tree_model_get( m, a, MC_TORRENT, &ta, -1 );
    447     gtk_tree_model_get( m, b, MC_TORRENT, &tb, -1 );
    448 
    449     if( !ret ) ret = compareETA( tr_torrentStatCached( ta )->eta, tr_torrentStatCached( tb )->eta );
    450     if( !ret ) ret = compareByName( m, a, b, user_data );
    451     return ret;
    452 }
    453 
    454 static int
    455 compareByState( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
    456 {
    457     int ret = 0;
    458     int sa, sb;
    459 
    460     gtk_tree_model_get( m, a, MC_ACTIVITY, &sa, -1 );
    461     gtk_tree_model_get( m, b, MC_ACTIVITY, &sb, -1 );
    462 
    463     if( !ret ) ret = compareInt_( sa, sb );
    464     if( !ret ) ret = compareByProgress( m, a, b, user_data );
    465     return ret;
    466 }
    467 
    468 static void
    469 setSort( TrCore * core, const char * mode, gboolean isReversed  )
    470 {
    471     const int col = MC_TORRENT;
    472     GtkTreeIterCompareFunc sort_func;
    473     GtkSortType type = isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
    474     GtkTreeSortable * sortable = GTK_TREE_SORTABLE( gtr_core_model( core ) );
    475 
    476     if( !strcmp( mode, "sort-by-activity" ) )
    477         sort_func = compareByActivity;
    478     else if( !strcmp( mode, "sort-by-age" ) )
    479         sort_func = compareByAge;
    480     else if( !strcmp( mode, "sort-by-progress" ) )
    481         sort_func = compareByProgress;
    482     else if( !strcmp( mode, "sort-by-time-left" ) )
    483         sort_func = compareByETA;
    484     else if( !strcmp( mode, "sort-by-ratio" ) )
    485         sort_func = compareByRatio;
    486     else if( !strcmp( mode, "sort-by-state" ) )
    487         sort_func = compareByState;
    488     else if( !strcmp( mode, "sort-by-size" ) )
    489         sort_func = compareBySize;
    490     else {
    491         sort_func = compareByName;
    492         type = isReversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
    493     }
    494 
    495     gtk_tree_sortable_set_sort_func( sortable, col, sort_func, NULL, NULL );
    496     gtk_tree_sortable_set_sort_column_id( sortable, col, type );
    497 }
    498 
    499 static void
    500 gtr_core_apply_defaults( tr_ctor * ctor )
    501 {
    502     if( tr_ctorGetPaused( ctor, TR_FORCE, NULL ) )
    503         tr_ctorSetPaused( ctor, TR_FORCE, !gtr_pref_flag_get( TR_PREFS_KEY_START ) );
    504 
    505     if( tr_ctorGetDeleteSource( ctor, NULL ) )
    506         tr_ctorSetDeleteSource( ctor,
    507                                gtr_pref_flag_get( TR_PREFS_KEY_TRASH_ORIGINAL ) );
    508 
    509     if( tr_ctorGetPeerLimit( ctor, TR_FORCE, NULL ) )
    510         tr_ctorSetPeerLimit( ctor, TR_FORCE,
    511                              gtr_pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) );
    512 
    513     if( tr_ctorGetDownloadDir( ctor, TR_FORCE, NULL ) )
    514     {
    515         const char * path = gtr_pref_string_get( TR_PREFS_KEY_DOWNLOAD_DIR );
    516         tr_ctorSetDownloadDir( ctor, TR_FORCE, path );
    517     }
    518 }
    519 
    520 #ifdef HAVE_GIO
    521 
    522 struct watchdir_file
    523 {
    524     char * filename;
    525     time_t mtime;
    526 };
    527 
    528 static int
    529 compare_watchdir_file_to_filename( const void * a, const void * filename )
    530 {
    531     return strcmp( ((const struct watchdir_file*)a)->filename, filename );
    532 }
    533 
    534 static void
    535 watchdir_file_update_mtime( struct watchdir_file * file )
    536 {
    537     GFile * gfile = g_file_new_for_path( file->filename );
    538     GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL );
    539 
    540     file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED );
    541 
    542     g_object_unref( G_OBJECT( info ) );
    543     g_object_unref( G_OBJECT( gfile ) );
    544 }
    545 
    546 static struct watchdir_file*
    547 watchdir_file_new( const char * filename )
    548 {
    549     struct watchdir_file * f;
    550 
    551     f = g_new( struct watchdir_file, 1 );
    552     f->filename = g_strdup( filename );
    553     watchdir_file_update_mtime( f );
    554 
    555     return f;
    556 }
    557 
    558 static void
    559 watchdir_file_free( struct watchdir_file * f )
    560 {
    561     g_free( f->filename );
    562     g_free( f );
    563 }
    564 
    565 static gboolean
    566 watchFolderIdle( gpointer gcore )
    567 {
    568     GSList * l;
    569     GSList * addme = NULL;
    570     GSList * monitor_files = NULL;
    571     TrCore * core = TR_CORE( gcore );
    572     const time_t now = time( NULL );
    573     struct TrCorePrivate * p = core->priv;
    574 
    575     /* of the monitor_files, make a list of those that haven't
    576      * changed lately, since they should be ready to add */
    577     for( l=p->monitor_files; l!=NULL; l=l->next ) {
    578         struct watchdir_file * f = l->data;
    579         watchdir_file_update_mtime( f );
    580         if( f->mtime + 2 >= now )
    581             monitor_files = g_slist_prepend( monitor_files, f );
    582         else {
    583             addme = g_slist_prepend( addme, g_strdup( f->filename ) );
    584             watchdir_file_free( f );
    585         }
    586     }
    587 
    588     /* add the torrents from that list */
    589     core->priv->adding_from_watch_dir = TRUE;
    590     gtr_core_add_list_defaults( core, addme, TRUE );
    591     core->priv->adding_from_watch_dir = FALSE;
    592 
    593     /* update the monitor_files list */
    594     g_slist_free( p->monitor_files );
    595     p->monitor_files = monitor_files;
    596 
    597     /* if monitor_files is nonempty, keep checking every second */
    598     if( core->priv->monitor_files )
    599         return TRUE;
    600     core->priv->monitor_idle_tag = 0;
    601     return FALSE;
    602 
    603 }
    604 
    605 static void
    606 maybeAddTorrent( TrCore * core, const char * filename )
    607 {
    608     const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" );
    609 
    610     if( isTorrent )
    611     {
    612         struct TrCorePrivate * p = core->priv;
    613 
    614         if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) )
    615             p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) );
    616 
    617         if( !p->monitor_idle_tag )
    618             p->monitor_idle_tag = gtr_timeout_add_seconds( 1, watchFolderIdle, core );
    619     }
    620 }
    621 
    622 static void
    623 watchFolderChanged( GFileMonitor       * monitor    UNUSED,
    624                     GFile *                         file,
    625                     GFile              * other_type UNUSED,
    626                     GFileMonitorEvent               event_type,
    627                     gpointer                        core )
    628 {
    629     if( event_type == G_FILE_MONITOR_EVENT_CREATED )
    630     {
    631         char * filename = g_file_get_path( file );
    632         maybeAddTorrent( core, filename );
    633         g_free( filename );
    634     }
    635 }
    636 
    637 static void
    638 scanWatchDir( TrCore * core )
    639 {
    640     const gboolean isEnabled = gtr_pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED );
    641 
    642     if( isEnabled )
    643     {
    644         const char * dirname = gtr_pref_string_get( PREF_KEY_DIR_WATCH );
    645         GDir * dir = g_dir_open( dirname, 0, NULL );
    646 
    647         if( dir != NULL )
    648         {
    649             const char * basename;
    650             while(( basename = g_dir_read_name( dir )))
    651             {
    652                 char * filename = g_build_filename( dirname, basename, NULL );
    653                 maybeAddTorrent( core, filename );
    654                 g_free( filename );
    655             }
    656 
    657             g_dir_close( dir );
    658         }
    659     }
    660 }
    661 
    662 static void
    663 updateWatchDir( TrCore * core )
    664 {
    665     const char *           filename = gtr_pref_string_get( PREF_KEY_DIR_WATCH );
    666     const gboolean         isEnabled = gtr_pref_flag_get(
    667         PREF_KEY_DIR_WATCH_ENABLED );
    668     struct TrCorePrivate * p = TR_CORE( core )->priv;
    669 
    670     if( p->monitor && ( !isEnabled || gtr_strcmp0( filename, p->monitor_path ) ) )
    671     {
    672         g_signal_handler_disconnect( p->monitor, p->monitor_tag );
    673         g_free( p->monitor_path );
    674         g_file_monitor_cancel( p->monitor );
    675         g_object_unref( G_OBJECT( p->monitor ) );
    676         p->monitor_path = NULL;
    677         p->monitor = NULL;
    678         p->monitor_tag = 0;
    679     }
    680 
    681     if( isEnabled && !p->monitor )
    682     {
    683         GFile *        file = g_file_new_for_path( filename );
    684         GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL );
    685         scanWatchDir( core );
    686         p->monitor = m;
    687         p->monitor_path = g_strdup( filename );
    688         p->monitor_tag = g_signal_connect( m, "changed",
    689                                            G_CALLBACK(
    690                                                watchFolderChanged ), core );
    691     }
    692 }
    693 
    694 #endif
    695 
    696 static void
    697 prefsChanged( TrCore *      core,
    698               const char *  key,
    699               gpointer data UNUSED )
    700 {
    701     if( !strcmp( key, PREF_KEY_SORT_MODE )
    702       || !strcmp( key, PREF_KEY_SORT_REVERSED ) )
    703     {
    704         const char * mode = gtr_pref_string_get( PREF_KEY_SORT_MODE );
    705         gboolean     isReversed = gtr_pref_flag_get( PREF_KEY_SORT_REVERSED );
    706         setSort( core, mode, isReversed );
    707     }
    708     else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_GLOBAL ) )
    709     {
    710         const uint16_t val = gtr_pref_int_get( key );
    711         tr_sessionSetPeerLimit( gtr_core_session( core ), val );
    712     }
    713     else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_TORRENT ) )
    714     {
    715         const uint16_t val = gtr_pref_int_get( key );
    716         tr_sessionSetPeerLimitPerTorrent( gtr_core_session( core ), val );
    717     }
    718     else if( !strcmp( key, PREF_KEY_INHIBIT_HIBERNATION ) )
    719     {
    720         maybe_inhibit_hibernation( core );
    721     }
    722 #ifdef HAVE_GIO
    723     else if( !strcmp( key, PREF_KEY_DIR_WATCH )
    724            || !strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) )
    725     {
    726         updateWatchDir( core );
    727     }
    728 #endif
    729 }
    730 
    731 static void
    732 gtr_core_init( GTypeInstance * instance, gpointer g_class UNUSED )
     217static void
     218core_init( GTypeInstance * instance, gpointer g_class UNUSED )
    733219{
    734220    GtkListStore * store;
     
    794280            sizeof( TrCore ),
    795281            0,                    /* n_preallocs */
    796             gtr_core_init,        /* instance_init */
     282            core_init,            /* instance_init */
    797283            NULL,
    798284        };
     
    803289}
    804290
     291/***
     292****
     293***/
     294
     295static inline void
     296core_emit_blocklist_udpated( TrCore * core, int ruleCount )
     297{
     298    g_signal_emit( core, core_signals[BLOCKLIST_SIGNAL], 0, ruleCount );
     299}
     300
     301static inline void
     302core_emit_port_tested( TrCore * core, gboolean isOpen )
     303{
     304    g_signal_emit( core, core_signals[PORT_SIGNAL], 0, isOpen );
     305}
     306
     307static inline void
     308core_emit_err( TrCore * core, enum tr_core_err type, const char * msg )
     309{
     310    g_signal_emit( core, core_signals[ADD_ERROR_SIGNAL], 0, type, msg );
     311}
     312
     313static inline void
     314core_emit_busy( TrCore * core, gboolean is_busy )
     315{
     316    g_signal_emit( core, core_signals[BUSY_SIGNAL], 0, is_busy );
     317}
     318
     319static inline void
     320core_emit_prefs_changed( TrCore * core, const char * key )
     321{
     322    g_signal_emit( core, core_signals[PREFS_SIGNAL], 0, key );
     323}
     324
     325/***
     326****
     327***/
     328
     329static GtkTreeModel *
     330core_raw_model( TrCore * core )
     331{
     332    return core_is_disposed( core ) ? NULL : core->priv->raw_model;
     333}
     334
     335GtkTreeModel *
     336gtr_core_model( TrCore * core )
     337{
     338    return core_is_disposed( core ) ? NULL : core->priv->sorted_model;
     339}
     340
     341tr_session *
     342gtr_core_session( TrCore * core )
     343{
     344    return core_is_disposed( core ) ? NULL : core->priv->session;
     345}
     346
     347/***
     348****
     349***/
     350
     351static tr_bool
     352core_is_busy( TrCore * core )
     353{
     354    return core->priv->busy_count > 0;
     355}
     356
     357static void
     358core_add_to_busy( TrCore * core, int addMe )
     359{
     360    const tr_bool wasBusy = core_is_busy( core );
     361
     362    core->priv->busy_count += addMe;
     363
     364    if( wasBusy != core_is_busy( core ) )
     365        core_emit_busy( core, core_is_busy( core ) );
     366}
     367
     368static void core_inc_busy( TrCore * core ) { core_add_to_busy( core, 1 ); }
     369static void core_dec_busy( TrCore * core ) { core_add_to_busy( core, -1 ); }
     370
     371/***
     372****
     373****  SORTING THE MODEL
     374****
     375***/
     376
     377static gboolean
     378is_valid_eta( int t )
     379{
     380    return ( t != TR_ETA_NOT_AVAIL ) && ( t != TR_ETA_UNKNOWN );
     381}
     382
     383static int
     384compare_eta( int a, int b )
     385{
     386    const gboolean a_valid = is_valid_eta( a );
     387    const gboolean b_valid = is_valid_eta( b );
     388
     389    if( !a_valid && !b_valid ) return 0;
     390    if( !a_valid ) return -1;
     391    if( !b_valid ) return 1;
     392    return a < b ? 1 : -1;
     393}
     394
     395static int
     396compare_double( double a, double b )
     397{
     398    if( a < b ) return -1;
     399    if( a > b ) return 1;
     400    return 0;
     401}
     402
     403static int
     404compare_uint64( uint64_t a, uint64_t b )
     405{
     406    if( a < b ) return -1;
     407    if( a > b ) return 1;
     408    return 0;
     409}
     410
     411static int
     412compare_int( int a, int b )
     413{
     414    if( a < b ) return -1;
     415    if( a > b ) return 1;
     416    return 0;
     417}
     418
     419static int
     420compare_ratio( double a, double b )
     421{
     422    if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0;
     423    if( (int)a == TR_RATIO_INF ) return 1;
     424    if( (int)b == TR_RATIO_INF ) return -1;
     425    return compare_double( a, b );
     426}
     427
     428static int
     429compare_time( time_t a, time_t b )
     430{
     431    if( a < b ) return -1;
     432    if( a > b ) return 1;
     433    return 0;
     434}
     435
     436static int
     437compare_by_name( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data UNUSED )
     438{
     439    int ret = 0;
     440
     441    if( !ret ) {
     442        char *ca, *cb;
     443        gtk_tree_model_get( m, a, MC_NAME_COLLATED, &ca, -1 );
     444        gtk_tree_model_get( m, b, MC_NAME_COLLATED, &cb, -1 );
     445        ret = gtr_strcmp0( ca, cb );
     446        g_free( cb );
     447        g_free( ca );
     448    }
     449
     450    return ret;
     451}
     452
     453static int
     454compare_by_ratio( GtkTreeModel* m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     455{
     456    int ret = 0;
     457    tr_torrent *ta, *tb;
     458    const tr_stat *sa, *sb;
     459
     460    gtk_tree_model_get( m, a, MC_TORRENT, &ta, -1 );
     461    sa = tr_torrentStatCached( ta );
     462    gtk_tree_model_get( m, b, MC_TORRENT, &tb, -1 );
     463    sb = tr_torrentStatCached( tb );
     464
     465    if( !ret ) ret = compare_ratio( sa->ratio, sb->ratio );
     466    if( !ret ) ret = compare_by_name( m, a, b, user_data );
     467    return ret;
     468}
     469
     470static int
     471compare_by_activity( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     472{
     473    int ret = 0;
     474    tr_torrent *ta, *tb;
     475    const tr_stat *sa, *sb;
     476    double aUp, aDown, bUp, bDown;
     477
     478    gtk_tree_model_get( m, a, MC_SPEED_UP, &aUp,
     479                              MC_SPEED_DOWN, &aDown,
     480                              MC_TORRENT, &ta,
     481                              -1 );
     482    gtk_tree_model_get( m, b, MC_SPEED_UP, &bUp,
     483                              MC_SPEED_DOWN, &bDown,
     484                              MC_TORRENT, &tb,
     485                              -1 );
     486    sa = tr_torrentStatCached( ta );
     487    sb = tr_torrentStatCached( tb );
     488
     489    if( !ret ) ret = compare_double( aUp+aDown, bUp+bDown );
     490    if( !ret ) ret = compare_uint64( sa->uploadedEver, sb->uploadedEver );
     491    if( !ret ) ret = compare_by_name( m, a, b, user_data );
     492    return ret;
     493}
     494
     495static int
     496compare_by_age( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     497{
     498    int ret = 0;
     499    tr_torrent *ta, *tb;
     500
     501    gtk_tree_model_get( m, a, MC_TORRENT, &ta, -1 );
     502    gtk_tree_model_get( m, b, MC_TORRENT, &tb, -1 );
     503
     504    if( !ret ) ret = compare_time( tr_torrentStatCached( ta )->addedDate, tr_torrentStatCached( tb )->addedDate );
     505    if( !ret ) ret = compare_by_name( m, a, b, user_data );
     506    return ret;
     507}
     508
     509static int
     510compare_by_size( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     511{
     512    int ret = 0;
     513    tr_torrent *t;
     514    const tr_info *ia, *ib;
     515
     516    gtk_tree_model_get( m, a, MC_TORRENT, &t, -1 );
     517    ia = tr_torrentInfo( t );
     518    gtk_tree_model_get( m, b, MC_TORRENT, &t, -1 );
     519    ib = tr_torrentInfo( t );
     520
     521    if( !ret ) ret = compare_uint64( ia->totalSize, ib->totalSize );
     522    if( !ret ) ret = compare_by_name( m, a, b, user_data );
     523    return ret;
     524}
     525
     526static int
     527compare_by_progress( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     528{
     529    int ret = 0;
     530    tr_torrent * t;
     531    const tr_stat *sa, *sb;
     532
     533    gtk_tree_model_get( m, a, MC_TORRENT, &t, -1 );
     534    sa = tr_torrentStatCached( t );
     535    gtk_tree_model_get( m, b, MC_TORRENT, &t, -1 );
     536    sb = tr_torrentStatCached( t );
     537
     538    if( !ret ) ret = compare_double( sa->percentComplete, sb->percentComplete );
     539    if( !ret ) ret = compare_double( sa->seedRatioPercentDone, sb->seedRatioPercentDone );
     540    if( !ret ) ret = compare_by_ratio( m, a, b, user_data );
     541    return ret;
     542}
     543
     544static int
     545compare_by_eta( GtkTreeModel * m, GtkTreeIter  * a, GtkTreeIter  * b, gpointer user_data )
     546{
     547    int ret = 0;
     548    tr_torrent *ta, *tb;
     549
     550    gtk_tree_model_get( m, a, MC_TORRENT, &ta, -1 );
     551    gtk_tree_model_get( m, b, MC_TORRENT, &tb, -1 );
     552
     553    if( !ret ) ret = compare_eta( tr_torrentStatCached( ta )->eta, tr_torrentStatCached( tb )->eta );
     554    if( !ret ) ret = compare_by_name( m, a, b, user_data );
     555    return ret;
     556}
     557
     558static int
     559compare_by_state( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     560{
     561    int ret = 0;
     562    int sa, sb;
     563
     564    gtk_tree_model_get( m, a, MC_ACTIVITY, &sa, -1 );
     565    gtk_tree_model_get( m, b, MC_ACTIVITY, &sb, -1 );
     566
     567    if( !ret ) ret = compare_int( sa, sb );
     568    if( !ret ) ret = compare_by_progress( m, a, b, user_data );
     569    return ret;
     570}
     571
     572static void
     573core_set_sort_mode( TrCore * core, const char * mode, gboolean is_reversed  )
     574{
     575    const int col = MC_TORRENT;
     576    GtkTreeIterCompareFunc sort_func;
     577    GtkSortType type = is_reversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
     578    GtkTreeSortable * sortable = GTK_TREE_SORTABLE( gtr_core_model( core ) );
     579
     580    if( !strcmp( mode, "sort-by-activity" ) )
     581        sort_func = compare_by_activity;
     582    else if( !strcmp( mode, "sort-by-age" ) )
     583        sort_func = compare_by_age;
     584    else if( !strcmp( mode, "sort-by-progress" ) )
     585        sort_func = compare_by_progress;
     586    else if( !strcmp( mode, "sort-by-time-left" ) )
     587        sort_func = compare_by_eta;
     588    else if( !strcmp( mode, "sort-by-ratio" ) )
     589        sort_func = compare_by_ratio;
     590    else if( !strcmp( mode, "sort-by-state" ) )
     591        sort_func = compare_by_state;
     592    else if( !strcmp( mode, "sort-by-size" ) )
     593        sort_func = compare_by_size;
     594    else {
     595        sort_func = compare_by_name;
     596        type = is_reversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
     597    }
     598
     599    gtk_tree_sortable_set_sort_func( sortable, col, sort_func, NULL, NULL );
     600    gtk_tree_sortable_set_sort_column_id( sortable, col, type );
     601}
     602
     603/***
     604****
     605****  WATCHDIR
     606****
     607***/
     608
     609#ifdef HAVE_GIO
     610
     611struct watchdir_file
     612{
     613    char * filename;
     614    time_t mtime;
     615};
     616
     617static int
     618compare_watchdir_file_to_filename( const void * a, const void * filename )
     619{
     620    return strcmp( ((const struct watchdir_file*)a)->filename, filename );
     621}
     622
     623static void
     624watchdir_file_update_mtime( struct watchdir_file * file )
     625{
     626    GFile * gfile = g_file_new_for_path( file->filename );
     627    GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL );
     628
     629    file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED );
     630
     631    g_object_unref( G_OBJECT( info ) );
     632    g_object_unref( G_OBJECT( gfile ) );
     633}
     634
     635static struct watchdir_file*
     636watchdir_file_new( const char * filename )
     637{
     638    struct watchdir_file * f;
     639
     640    f = g_new( struct watchdir_file, 1 );
     641    f->filename = g_strdup( filename );
     642    watchdir_file_update_mtime( f );
     643
     644    return f;
     645}
     646
     647static void
     648watchdir_file_free( struct watchdir_file * f )
     649{
     650    g_free( f->filename );
     651    g_free( f );
     652}
     653
     654static gboolean
     655core_watchdir_idle( gpointer gcore )
     656{
     657    GSList * l;
     658    GSList * addme = NULL;
     659    GSList * monitor_files = NULL;
     660    TrCore * core = TR_CORE( gcore );
     661    const time_t now = time( NULL );
     662    struct TrCorePrivate * p = core->priv;
     663
     664    /* of the monitor_files, make a list of those that haven't
     665     * changed lately, since they should be ready to add */
     666    for( l=p->monitor_files; l!=NULL; l=l->next ) {
     667        struct watchdir_file * f = l->data;
     668        watchdir_file_update_mtime( f );
     669        if( f->mtime + 2 >= now )
     670            monitor_files = g_slist_prepend( monitor_files, f );
     671        else {
     672            addme = g_slist_prepend( addme, g_strdup( f->filename ) );
     673            watchdir_file_free( f );
     674        }
     675    }
     676
     677    /* add the torrents from that list */
     678    core->priv->adding_from_watch_dir = TRUE;
     679    gtr_core_add_list_defaults( core, addme, TRUE );
     680    core->priv->adding_from_watch_dir = FALSE;
     681
     682    /* update the monitor_files list */
     683    g_slist_free( p->monitor_files );
     684    p->monitor_files = monitor_files;
     685
     686    /* if monitor_files is nonempty, keep checking every second */
     687    if( core->priv->monitor_files )
     688        return TRUE;
     689    core->priv->monitor_idle_tag = 0;
     690    return FALSE;
     691
     692}
     693
     694static void
     695core_watchdir_monitor_file( TrCore * core, const char * filename )
     696{
     697    const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" );
     698
     699    if( isTorrent )
     700    {
     701        struct TrCorePrivate * p = core->priv;
     702
     703        if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) )
     704            p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) );
     705
     706        if( !p->monitor_idle_tag )
     707            p->monitor_idle_tag = gtr_timeout_add_seconds( 1, core_watchdir_idle, core );
     708    }
     709}
     710
     711static void
     712on_file_changed_in_watchdir( GFileMonitor       * monitor UNUSED,
     713                             GFile              * file,
     714                             GFile              * other_type UNUSED,
     715                             GFileMonitorEvent    event_type,
     716                             gpointer             core )
     717{
     718    if( event_type == G_FILE_MONITOR_EVENT_CREATED )
     719    {
     720        char * filename = g_file_get_path( file );
     721        core_watchdir_monitor_file( core, filename );
     722        g_free( filename );
     723    }
     724}
     725
     726static void
     727core_watchdir_scan( TrCore * core )
     728{
     729    const gboolean is_enabled = gtr_pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED );
     730
     731    if( is_enabled )
     732    {
     733        const char * dirname = gtr_pref_string_get( PREF_KEY_DIR_WATCH );
     734        GDir * dir = g_dir_open( dirname, 0, NULL );
     735
     736        if( dir != NULL )
     737        {
     738            const char * basename;
     739            while(( basename = g_dir_read_name( dir )))
     740            {
     741                char * filename = g_build_filename( dirname, basename, NULL );
     742                core_watchdir_monitor_file( core, filename );
     743                g_free( filename );
     744            }
     745
     746            g_dir_close( dir );
     747        }
     748    }
     749}
     750
     751static void
     752core_watchdir_update( TrCore * core )
     753{
     754    const char * filename = gtr_pref_string_get( PREF_KEY_DIR_WATCH );
     755    const gboolean is_enabled = gtr_pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED );
     756    struct TrCorePrivate * p = TR_CORE( core )->priv;
     757
     758    if( p->monitor && ( !is_enabled || gtr_strcmp0( filename, p->monitor_path ) ) )
     759    {
     760        g_signal_handler_disconnect( p->monitor, p->monitor_tag );
     761        g_free( p->monitor_path );
     762        g_file_monitor_cancel( p->monitor );
     763        g_object_unref( G_OBJECT( p->monitor ) );
     764        p->monitor_path = NULL;
     765        p->monitor = NULL;
     766        p->monitor_tag = 0;
     767    }
     768
     769    if( is_enabled && !p->monitor )
     770    {
     771        GFile * file = g_file_new_for_path( filename );
     772        GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL );
     773        core_watchdir_scan( core );
     774        p->monitor = m;
     775        p->monitor_path = g_strdup( filename );
     776        p->monitor_tag = g_signal_connect( m, "changed",
     777                                           G_CALLBACK( on_file_changed_in_watchdir ), core );
     778    }
     779}
     780
     781#endif
     782
     783/***
     784****
     785***/
     786
     787static void
     788on_pref_changed( TrCore * core, const char * key, gpointer data UNUSED )
     789{
     790    if( !strcmp( key, PREF_KEY_SORT_MODE )
     791      || !strcmp( key, PREF_KEY_SORT_REVERSED ) )
     792    {
     793        const char * mode = gtr_pref_string_get( PREF_KEY_SORT_MODE );
     794        gboolean is_reversed = gtr_pref_flag_get( PREF_KEY_SORT_REVERSED );
     795        core_set_sort_mode( core, mode, is_reversed );
     796    }
     797    else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_GLOBAL ) )
     798    {
     799        const uint16_t val = gtr_pref_int_get( key );
     800        tr_sessionSetPeerLimit( gtr_core_session( core ), val );
     801    }
     802    else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_TORRENT ) )
     803    {
     804        const uint16_t val = gtr_pref_int_get( key );
     805        tr_sessionSetPeerLimitPerTorrent( gtr_core_session( core ), val );
     806    }
     807    else if( !strcmp( key, PREF_KEY_INHIBIT_HIBERNATION ) )
     808    {
     809        core_maybe_inhibit_hibernation( core );
     810    }
     811#ifdef HAVE_GIO
     812    else if( !strcmp( key, PREF_KEY_DIR_WATCH )
     813           || !strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) )
     814    {
     815        core_watchdir_update( core );
     816    }
     817#endif
     818}
     819
    805820/**
    806821***
     
    815830
    816831    /* init from prefs & listen to pref changes */
    817     prefsChanged( core, PREF_KEY_SORT_MODE, NULL );
    818     prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL );
    819     prefsChanged( core, PREF_KEY_DIR_WATCH_ENABLED, NULL );
    820     prefsChanged( core, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, NULL );
    821     prefsChanged( core, PREF_KEY_INHIBIT_HIBERNATION, NULL );
    822     g_signal_connect( core, "prefs-changed", G_CALLBACK( prefsChanged ), NULL );
     832    on_pref_changed( core, PREF_KEY_SORT_MODE, NULL );
     833    on_pref_changed( core, PREF_KEY_SORT_REVERSED, NULL );
     834    on_pref_changed( core, PREF_KEY_DIR_WATCH_ENABLED, NULL );
     835    on_pref_changed( core, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, NULL );
     836    on_pref_changed( core, PREF_KEY_INHIBIT_HIBERNATION, NULL );
     837    g_signal_connect( core, "prefs-changed", G_CALLBACK( on_pref_changed ), NULL );
    823838
    824839    return core;
     
    837852    }
    838853}
     854
     855/***
     856****
     857****  ADDING TORRENTS
     858****
     859***/
    839860
    840861static char*
     
    884905
    885906static char *
    886 torrentTrackerString( tr_torrent * tor )
     907build_torrent_tracker_string( tr_torrent * tor )
    887908{
    888909    int i;
     
    900921
    901922static gboolean
    902 isTorrentActive( const tr_stat * st )
     923is_torrent_active( const tr_stat * st )
    903924{
    904925    return ( st->peersSendingToUs > 0 )
     
    908929
    909930void
    910 gtr_core_add_torrent( TrCore * core, tr_torrent * tor, gboolean doNotify )
     931gtr_core_add_torrent( TrCore * core, tr_torrent * tor, gboolean do_notify )
    911932{
    912933    if( tor != NULL )
    913934    {
     935        GtkTreeIter unused;
    914936        const tr_stat * st = tr_torrentStat( tor );
    915937        const char * name = tr_torrentName( tor );
    916938        char * collated = get_collated_name( tor );
    917         char *  trackers = torrentTrackerString( tor );
    918         GtkListStore *  store = GTK_LIST_STORE( core_raw_model( core ) );
    919         GtkTreeIter  unused;
     939        char * trackers = build_torrent_tracker_string( tor );
     940        GtkListStore * store = GTK_LIST_STORE( core_raw_model( core ) );
    920941
    921942        gtk_list_store_insert_with_values( store, &unused, 0,
     
    927948            MC_SPEED_DOWN,        st->pieceDownloadSpeed_KBps,
    928949            MC_RECHECK_PROGRESS,  st->recheckProgress,
    929             MC_ACTIVE,            isTorrentActive( st ),
     950            MC_ACTIVE,            is_torrent_active( st ),
    930951            MC_ACTIVITY,          st->activity,
    931952            MC_FINISHED,          st->finished,
     
    934955            -1 );
    935956
    936         if( doNotify )
     957        if( do_notify )
    937958            gtr_notify_torrent_added( name );
    938959
     
    945966}
    946967
    947 void
    948 gtr_core_load( TrCore * self, gboolean forcePaused )
    949 {
    950     int i;
    951     tr_ctor * ctor;
    952     tr_torrent ** torrents;
    953     int count = 0;
    954 
    955     ctor = tr_ctorNew( gtr_core_session( self ) );
    956     if( forcePaused )
    957         tr_ctorSetPaused( ctor, TR_FORCE, TRUE );
    958     tr_ctorSetPeerLimit( ctor, TR_FALLBACK,
    959                          gtr_pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) );
    960 
    961     torrents = tr_sessionLoadTorrents ( gtr_core_session( self ), ctor, &count );
    962     for( i=0; i<count; ++i )
    963         gtr_core_add_torrent( self, torrents[i], FALSE );
    964 
    965     tr_free( torrents );
    966     tr_ctorFree( ctor );
    967 }
    968 
    969 void
    970 gtr_core_clear( TrCore * self )
    971 {
    972     gtk_list_store_clear( GTK_LIST_STORE( core_raw_model( self ) ) );
    973 }
    974 
    975 /***
    976 ****
    977 ***/
    978 
    979 static void
    980 core_emit_blocklist_udpated( TrCore * core, int ruleCount )
    981 {
    982     g_signal_emit( core, core_signals[BLOCKLIST_SIGNAL], 0, ruleCount );
    983 }
    984 
    985 static void
    986 core_emit_port_tested( TrCore * core, gboolean isOpen )
    987 {
    988     g_signal_emit( core, core_signals[PORT_SIGNAL], 0, isOpen );
    989 }
    990 
    991 static void
    992 gtr_core_errsig( TrCore * core, enum tr_core_err type, const char * msg )
    993 {
    994     g_signal_emit( core, core_signals[ADD_ERROR_SIGNAL], 0, type, msg );
    995 }
    996 
    997968static tr_torrent *
    998 torrent_new_from_ctor( TrCore * core, tr_ctor * ctor )
     969core_create_new_torrent( TrCore * core, tr_ctor * ctor )
    999970{
    1000971    int errcode = 0;
    1001972    tr_torrent * tor;
    1002     uint8_t      doTrash = FALSE;
     973    uint8_t do_trash = FALSE;
    1003974    tr_session * session = gtr_core_session( core );
    1004975
    1005976    /* let the gtk client handle the removal, since libT
    1006977     * doesn't have any concept of the glib trash API */
    1007     tr_ctorGetDeleteSource( ctor, &doTrash );
     978    tr_ctorGetDeleteSource( ctor, &do_trash );
    1008979    tr_ctorSetDeleteSource( ctor, FALSE );
    1009980    tor = tr_torrentNew( ctor, &errcode );
    1010981
    1011     if( tor && doTrash )
     982    if( tor && do_trash )
    1012983    {
    1013984        const char * config = tr_sessionGetConfigDir( session );
     
    1023994}
    1024995
    1025 
    1026996static int
    1027 add_ctor( TrCore * core, tr_ctor * ctor, gboolean doPrompt, gboolean doNotify )
     997core_add_ctor( TrCore * core, tr_ctor * ctor, gboolean do_prompt, gboolean do_notify )
    1028998{
    1029999    tr_info inf;
     
    10401010             * don't want to be nagging users to clean up their watch dirs */
    10411011            if( !tr_ctorGetSourceFile(ctor) || !core->priv->adding_from_watch_dir )
    1042                 gtr_core_errsig( core, err, inf.name );
     1012                core_emit_err( core, err, inf.name );
    10431013            tr_metainfoFree( &inf );
    10441014            break;
    10451015
    10461016        default:
    1047             if( doPrompt )
     1017            if( do_prompt )
    10481018                g_signal_emit( core, core_signals[ADD_PROMPT_SIGNAL], 0, ctor );
    10491019            else
    1050                 gtr_core_add_torrent( core, torrent_new_from_ctor( core, ctor ), doNotify );
     1020                gtr_core_add_torrent( core, core_create_new_torrent( core, ctor ), do_notify );
    10511021            tr_metainfoFree( &inf );
    10521022            break;
     
    10571027}
    10581028
     1029static void
     1030core_apply_defaults( tr_ctor * ctor )
     1031{
     1032    if( tr_ctorGetPaused( ctor, TR_FORCE, NULL ) )
     1033        tr_ctorSetPaused( ctor, TR_FORCE, !gtr_pref_flag_get( TR_PREFS_KEY_START ) );
     1034
     1035    if( tr_ctorGetDeleteSource( ctor, NULL ) )
     1036        tr_ctorSetDeleteSource( ctor,
     1037                               gtr_pref_flag_get( TR_PREFS_KEY_TRASH_ORIGINAL ) );
     1038
     1039    if( tr_ctorGetPeerLimit( ctor, TR_FORCE, NULL ) )
     1040        tr_ctorSetPeerLimit( ctor, TR_FORCE,
     1041                             gtr_pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) );
     1042
     1043    if( tr_ctorGetDownloadDir( ctor, TR_FORCE, NULL ) )
     1044        tr_ctorSetDownloadDir( ctor, TR_FORCE,
     1045                               gtr_pref_string_get( TR_PREFS_KEY_DOWNLOAD_DIR ) );
     1046}
     1047
    10591048void
    10601049gtr_core_add_ctor( TrCore * core, tr_ctor * ctor )
    10611050{
    1062     const gboolean doPrompt = gtr_pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
    1063     const gboolean doNotify = FALSE;
    1064     gtr_core_apply_defaults( ctor );
    1065     add_ctor( core, ctor, doPrompt, doNotify );
     1051    const gboolean do_notify = FALSE;
     1052    const gboolean do_prompt = gtr_pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
     1053    core_apply_defaults( ctor );
     1054    core_add_ctor( core, ctor, do_prompt, do_notify );
    10661055}
    10671056
     
    10921081
    10931082        ctor = tr_ctorNew( session );
    1094         gtr_core_apply_defaults( ctor );
     1083        core_apply_defaults( ctor );
    10951084
    10961085        file_contents = tr_base64_decode( payload, -1, &file_length );
    10971086        tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length );
    1098         add_ctor( core, ctor, do_prompt, TRUE );
     1087        core_add_ctor( core, ctor, do_prompt, TRUE );
    10991088
    11001089        tr_free( file_contents );
     
    11321121    else
    11331122    {
    1134         const gboolean doPrompt = gtr_pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
    1135         const gboolean doNotify = FALSE;
    1136         const int err = add_ctor( data->core, data->ctor, doPrompt, doNotify );
     1123        const gboolean do_prompt = gtr_pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
     1124        const gboolean do_notify = FALSE;
     1125        const int err = core_add_ctor( data->core, data->ctor, do_prompt, do_notify );
    11371126
    11381127        if( err == TR_PARSE_ERR )
    1139             gtr_core_errsig( data->core, TR_PARSE_ERR, data->url );
     1128            core_emit_err( data->core, TR_PARSE_ERR, data->url );
    11401129
    11411130        gtr_core_torrents_added( data->core );
     
    11641153    data->response_code = response_code;
    11651154    data->ctor = tr_ctorNew( session );
    1166     gtr_core_apply_defaults( data->ctor );
     1155    core_apply_defaults( data->ctor );
    11671156    tr_ctorSetMetainfo( data->ctor, response, response_byte_count );
    11681157
     
    12131202add_filename( TrCore      * core,
    12141203              const char  * filename,
    1215               gboolean      doStart,
    1216               gboolean      doPrompt,
    1217               gboolean      doNotify )
     1204              gboolean      do_start,
     1205              gboolean      do_prompt,
     1206              gboolean      do_notify )
    12181207{
    12191208    tr_session * session = gtr_core_session( core );
     
    12321221        tr_ctor * ctor = tr_ctorNew( session );
    12331222        tr_ctorSetMetainfoFromFile( ctor, filename );
    1234         gtr_core_apply_defaults( ctor );
    1235         tr_ctorSetPaused( ctor, TR_FORCE, !doStart );
    1236 
    1237         err = add_ctor( core, ctor, doPrompt, doNotify );
     1223        core_apply_defaults( ctor );
     1224        tr_ctorSetPaused( ctor, TR_FORCE, !do_start );
     1225
     1226        err = core_add_ctor( core, ctor, do_prompt, do_notify );
    12381227        if( err == TR_PARSE_ERR )
    1239             gtr_core_errsig( core, TR_PARSE_ERR, filename );
     1228            core_emit_err( core, TR_PARSE_ERR, filename );
    12401229    }
    12411230    else if( gtr_is_hex_hashcode( filename ) )
     
    12451234}
    12461235
    1247 gboolean
    1248 gtr_core_present_window( TrCore      * core UNUSED,
    1249                          gboolean    * success,
    1250                          GError     ** err  UNUSED )
    1251 {
    1252     /* Setting the toggle-main-window GtkCheckMenuItem to
    1253        make sure its state is correctly set */
    1254     gtr_action_set_toggled( "toggle-main-window", TRUE);
    1255 
    1256     *success = TRUE;
    1257     return TRUE;
    1258 }
    1259 
    12601236void
    12611237gtr_core_add_list( TrCore    * core,
    1262                    GSList    * torrentFiles,
    1263                    gboolean    doStart,
    1264                    gboolean    doPrompt,
    1265                    gboolean    doNotify )
     1238                   GSList    * torrent_files,
     1239                   gboolean    do_start,
     1240                   gboolean    do_prompt,
     1241                   gboolean    do_notify )
    12661242{
    12671243    GSList * l;
    12681244
    1269     for( l=torrentFiles; l!=NULL; l=l->next )
     1245    for( l=torrent_files; l!=NULL; l=l->next )
    12701246    {
    12711247        char * filename = l->data;
    1272         add_filename( core, filename, doStart, doPrompt, doNotify );
     1248        add_filename( core, filename, do_start, do_prompt, do_notify );
    12731249        g_free( filename );
    12741250    }
     
    12761252    gtr_core_torrents_added( core );
    12771253
    1278     g_slist_free( torrentFiles );
    1279 }
    1280 
    1281 void
    1282 gtr_core_add_list_defaults( TrCore * core, GSList * torrentFiles, gboolean doNotify )
    1283 {
    1284     const gboolean doStart = gtr_pref_flag_get( TR_PREFS_KEY_START );
    1285     const gboolean doPrompt = gtr_pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
    1286 
    1287     gtr_core_add_list( core, torrentFiles, doStart, doPrompt, doNotify );
     1254    g_slist_free( torrent_files );
     1255}
     1256
     1257void
     1258gtr_core_add_list_defaults( TrCore * core, GSList * torrentFiles, gboolean do_notify )
     1259{
     1260    const gboolean do_start = gtr_pref_flag_get( TR_PREFS_KEY_START );
     1261    const gboolean do_prompt = gtr_pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
     1262
     1263    gtr_core_add_list( core, torrentFiles, do_start, do_prompt, do_notify );
    12881264}
    12891265
     
    12921268{
    12931269    gtr_core_update( self );
    1294     gtr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL );
     1270    core_emit_err( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL );
    12951271}
    12961272
     
    13311307        tr_torrentRemove( tor, delete_local_data, gtr_file_trash_or_remove );
    13321308    }
     1309}
     1310
     1311void
     1312gtr_core_load( TrCore * self, gboolean forcePaused )
     1313{
     1314    int i;
     1315    tr_ctor * ctor;
     1316    tr_torrent ** torrents;
     1317    int count = 0;
     1318
     1319    ctor = tr_ctorNew( gtr_core_session( self ) );
     1320    if( forcePaused )
     1321        tr_ctorSetPaused( ctor, TR_FORCE, TRUE );
     1322    tr_ctorSetPeerLimit( ctor, TR_FALLBACK,
     1323                         gtr_pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) );
     1324
     1325    torrents = tr_sessionLoadTorrents ( gtr_core_session( self ), ctor, &count );
     1326    for( i=0; i<count; ++i )
     1327        gtr_core_add_torrent( self, torrents[i], FALSE );
     1328
     1329    tr_free( torrents );
     1330    tr_ctorFree( ctor );
     1331}
     1332
     1333void
     1334gtr_core_clear( TrCore * self )
     1335{
     1336    gtk_list_store_clear( GTK_LIST_STORE( core_raw_model( self ) ) );
    13331337}
    13341338
     
    13751379    /* get the new states */
    13761380    st = tr_torrentStat( tor );
    1377     newActive = isTorrentActive( st );
     1381    newActive = is_torrent_active( st );
    13781382    newActivity = st->activity;
    13791383    newFinished = st->finished;
    13801384    newPriority = tr_torrentGetPriority( tor );
    1381     newTrackers = torrentTrackerString( tor );
     1385    newTrackers = build_torrent_tracker_string( tor );
    13821386    newUpSpeed = st->pieceUploadSpeed_KBps;
    13831387    newDownSpeed = st->pieceDownloadSpeed_KBps;
     
    14301434    gtk_tree_model_foreach( core_raw_model( core ), update_foreach, NULL );
    14311435
    1432     maybe_inhibit_hibernation( core );
     1436    core_maybe_inhibit_hibernation( core );
    14331437}
    14341438
     
    14421446get_hibernation_inhibit_proxy( void )
    14431447{
    1444     DBusGConnection * conn;
    14451448    GError * error = NULL;
    14461449    const char * name = "org.gnome.SessionManager";
    14471450    const char * path = "/org/gnome/SessionManager";
    14481451    const char * interface = "org.gnome.SessionManager";
    1449 
    1450     conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error );
     1452    DBusGConnection * conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error );
     1453
    14511454    if( error )
    14521455    {
     
    14621465gtr_inhibit_hibernation( guint * cookie )
    14631466{
    1464     gboolean     success = FALSE;
     1467    gboolean success = FALSE;
    14651468    DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
    14661469
     
    15521555
    15531556static void
    1554 maybe_inhibit_hibernation( TrCore * core )
     1557core_maybe_inhibit_hibernation( TrCore * core )
    15551558{
    15561559    /* hibernation is allowed if EITHER
     
    15691572core_commit_prefs_change( TrCore * core, const char * key )
    15701573{
    1571     g_signal_emit( core, core_signals[PREFS_SIGNAL], 0, key );
     1574    core_emit_prefs_changed( core, key );
    15721575    gtr_pref_save( gtr_core_session( core ) );
    15731576}
     
    17331736
    17341737static void
    1735 blocklistResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED )
     1738on_blocklist_response( TrCore * core, tr_benc * response, gpointer data UNUSED )
    17361739{
    17371740    tr_benc * args;
     
    17531756    const int tag = nextTag++;
    17541757    g_snprintf( buf, sizeof( buf ), "{ \"method\": \"blocklist-update\", \"tag\": %d }", tag );
    1755     core_send_rpc_request( core, buf, tag, blocklistResponseFunc, NULL );
     1758    core_send_rpc_request( core, buf, tag, on_blocklist_response, NULL );
    17561759}
    17571760
     
    18351838    }
    18361839}
     1840
     1841gboolean
     1842gtr_core_present_window( TrCore      * core UNUSED,
     1843                         gboolean    * success,
     1844                         GError     ** err  UNUSED )
     1845{
     1846    /* Setting the toggle-main-window GtkCheckMenuItem to
     1847       make sure its state is correctly set */
     1848    gtr_action_set_toggled( "toggle-main-window", TRUE);
     1849
     1850    *success = TRUE;
     1851    return TRUE;
     1852}
Note: See TracChangeset for help on using the changeset viewer.