Changeset 9290


Ignore:
Timestamp:
Oct 11, 2009, 6:23:31 AM (13 years ago)
Author:
charles
Message:

(trunk gtk) #1978: Rescaning for new torrents at torrent creation time

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/tr-core.c

    r9130 r9290  
    459459}
    460460
    461 static int
    462 tr_strcmp( const void * a,
    463            const void * b )
    464 {
    465     if( a && b ) return strcmp( a, b );
    466     if( a ) return 1;
    467     if( b ) return -1;
    468     return 0;
    469 }
    470 
    471461#ifdef HAVE_GIO
     462
     463struct watchdir_file
     464{
     465    char * filename;
     466    time_t mtime;
     467};
     468
     469static int
     470compare_watchdir_file_to_filename( const void * a, const void * filename )
     471{
     472    return strcmp( ((const struct watchdir_file*)a)->filename, filename );
     473}
     474
     475static void
     476watchdir_file_update_mtime( struct watchdir_file * file )
     477{
     478    GFile * gfile = g_file_new_for_path( file->filename );
     479    GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL );
     480
     481    file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED );
     482
     483    g_object_unref( G_OBJECT( info ) );
     484    g_object_unref( G_OBJECT( gfile ) );
     485}
     486
     487static struct watchdir_file*
     488watchdir_file_new( const char * filename )
     489{
     490    struct watchdir_file * f;
     491
     492    f = g_new( struct watchdir_file, 1 );
     493    f->filename = g_strdup( filename );
     494    watchdir_file_update_mtime( f );
     495
     496    return f;
     497}
     498
     499static void
     500watchdir_file_free( struct watchdir_file * f )
     501{
     502    g_free( f->filename );
     503    g_free( f );
     504}
     505   
    472506static gboolean
    473507watchFolderIdle( gpointer gcore )
    474508{
     509    GSList * l;
     510    GSList * addme = NULL;
     511    GSList * monitor_files = NULL;
    475512    TrCore * core = TR_CORE( gcore );
    476 
     513    const time_t now = time( NULL );
     514    struct TrCorePrivate * p = core->priv;
     515
     516    /* of the monitor_files, make a list of those that haven't
     517     * changed lately, since they should be ready to add */
     518    for( l=p->monitor_files; l!=NULL; l=l->next ) {
     519        struct watchdir_file * f = l->data;
     520        watchdir_file_update_mtime( f );
     521        if( f->mtime + 2 >= now )
     522            monitor_files = g_slist_prepend( monitor_files, f );
     523        else {
     524            addme = g_slist_prepend( addme, g_strdup( f->filename ) );
     525            watchdir_file_free( f );
     526        }
     527    }
     528
     529    /* add the torrents from that list */
    477530    core->priv->adding_from_watch_dir = TRUE;
    478     tr_core_add_list_defaults( core, core->priv->monitor_files, TRUE );
     531    tr_core_add_list_defaults( core, addme, TRUE );
    479532    core->priv->adding_from_watch_dir = FALSE;
    480533
    481     /* cleanup */
    482     core->priv->monitor_files = NULL;
     534    /* update the monitor_files list */
     535    g_slist_free( p->monitor_files );
     536    p->monitor_files = monitor_files;
     537
     538    /* if monitor_files is nonempty, keep checking every second */
     539    if( core->priv->monitor_files )
     540        return TRUE;
    483541    core->priv->monitor_idle_tag = 0;
    484542    return FALSE;
    485 }
    486 
    487 static void
    488 maybeAddTorrent( TrCore *     core,
    489                 const char * filename )
     543
     544}
     545
     546static void
     547maybeAddTorrent( TrCore * core, const char * filename )
    490548{
    491549    const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" );
     
    495553        struct TrCorePrivate * p = core->priv;
    496554
    497         if( !g_slist_find_custom( p->monitor_files, filename,
    498                                   (GCompareFunc)strcmp ) )
    499             p->monitor_files =
    500                 g_slist_append( p->monitor_files, g_strdup( filename ) );
     555        if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) )
     556            p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) );
     557
    501558        if( !p->monitor_idle_tag )
    502559            p->monitor_idle_tag = gtr_timeout_add_seconds( 1, watchFolderIdle, core );
     
    539596        g_dir_close( dir );
    540597    }
     598}
     599
     600static int
     601tr_strcmp( const void * a,
     602           const void * b )
     603{
     604    if( a && b ) return strcmp( a, b );
     605    if( a ) return 1;
     606    if( b ) return -1;
     607    return 0;
    541608}
    542609
Note: See TracChangeset for help on using the changeset viewer.