Changeset 5238


Ignore:
Timestamp:
Mar 12, 2008, 2:11:37 AM (14 years ago)
Author:
charles
Message:

(gtk) tweak the "watch folder" feature: firefox likes to save a file, then remove it, then add it again. Add a slight delay in to allow for this without popping up a second "open torrent" dialog.

Location:
trunk/gtk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/main.c

    r5229 r5238  
    803803
    804804#if GTK_CHECK_VERSION(2,8,0)
     805    if( w )
    805806    if( cbdata->wind )
    806         gtk_window_set_urgency_hint( GTK_WINDOW( cbdata->wind ), TRUE );
     807        gtk_window_set_urgency_hint( GTK_WINDOW( w ), TRUE );
    807808    g_signal_connect( w, "focus-in-event",
    808809                      G_CALLBACK(on_main_window_focus_in),  cbdata );
  • trunk/gtk/tr-core.c

    r5232 r5238  
    4646    gulong             monitor_tag;
    4747    char             * monitor_path;
     48    GList            * monitor_files;
     49    guint              monitor_idle_tag;
    4850#endif
    4951    GtkTreeModel     * model;
     
    308310#ifdef HAVE_GIO
    309311static gboolean
    310 canAddTorrent( TrCore * core, const char * filename )
    311 {
    312     gboolean canAdd;
    313     tr_ctor * ctor = tr_ctorNew( core->priv->handle );
    314     tr_core_apply_defaults( ctor );
    315     tr_ctorSetMetainfoFromFile( ctor, filename );
    316     canAdd = !tr_torrentParse( core->priv->handle, ctor, NULL );
    317     tr_ctorFree( ctor );
    318     return canAdd;
     312watchFolderIdle( gpointer gcore )
     313{
     314    TrCore * core;
     315    tr_ctor * ctor;
     316
     317    /* add these files */
     318    core = TR_CORE( gcore );
     319    ctor = tr_ctorNew( core->priv->handle );
     320    tr_core_add_list( core, core->priv->monitor_files, ctor );
     321
     322    /* cleanup */
     323    core->priv->monitor_files = NULL;
     324    core->priv->monitor_idle_tag = 0;
     325    return FALSE;
    319326}
    320327
     
    324331                    GFile              * other_type UNUSED,
    325332                    GFileMonitorEvent    event_type,
    326                     gpointer             gcore )
     333                    gpointer             core )
    327334{
    328335    if( event_type == G_FILE_MONITOR_EVENT_CREATED )
    329336    {
    330         TrCore * core = TR_CORE( gcore );
    331337        char * filename = g_file_get_path( file );
    332338        const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" );
    333339
    334         if( isTorrent && canAddTorrent( core, filename ) )
     340        if( isTorrent )
    335341        {
    336             tr_ctor * ctor = tr_ctorNew( core->priv->handle );
    337             tr_core_add_list( core, g_list_append( NULL, g_strdup( filename ) ), ctor );
     342            struct TrCorePrivate * p = TR_CORE( core )->priv;
     343
     344            if( !g_list_find_custom( p->monitor_files, filename, (GCompareFunc)strcmp ) )
     345                p->monitor_files = g_list_append( p->monitor_files, g_strdup( filename ) );
     346            if( !p->monitor_idle_tag )
     347                p->monitor_idle_tag = g_timeout_add( 1000, watchFolderIdle, core );
    338348        }
    339349
     
    366376    char * filename = pref_string_get( PREF_KEY_DIR_WATCH );
    367377    const gboolean isEnabled = pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED );
    368 
    369     if( core->priv->monitor && ( !isEnabled || tr_strcmp( filename, core->priv->monitor_path ) ) )
    370     {
    371         g_signal_handler_disconnect( core->priv->monitor, core->priv->monitor_tag );
    372         g_free( core->priv->monitor_path );
    373         g_file_monitor_cancel( core->priv->monitor );
    374         g_object_unref( G_OBJECT( core->priv->monitor ) );
    375         core->priv->monitor_path = NULL;
    376         core->priv->monitor = NULL;
    377         core->priv->monitor_tag = 0;
    378     }
    379 
    380     if( isEnabled && !core->priv->monitor )
     378    struct TrCorePrivate * p = TR_CORE( core )->priv;
     379
     380    if( p->monitor && ( !isEnabled || tr_strcmp( filename, p->monitor_path ) ) )
     381    {
     382        g_signal_handler_disconnect( p->monitor, p->monitor_tag );
     383        g_free( p->monitor_path );
     384        g_file_monitor_cancel( p->monitor );
     385        g_object_unref( G_OBJECT( p->monitor ) );
     386        p->monitor_path = NULL;
     387        p->monitor = NULL;
     388        p->monitor_tag = 0;
     389    }
     390
     391    if( isEnabled && !p->monitor )
    381392    {
    382393        GFile * file = g_file_new_for_path( filename );
    383394        GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL );
    384395        scanWatchDir( core );
    385         core->priv->monitor = m;
    386         core->priv->monitor_path = g_strdup( filename );
    387         core->priv->monitor_tag = g_signal_connect( m, "changed", G_CALLBACK (watchFolderChanged), core );
     396        p->monitor = m;
     397        p->monitor_path = g_strdup( filename );
     398        p->monitor_tag = g_signal_connect( m, "changed",
     399                                           G_CALLBACK( watchFolderChanged ), core );
    388400    }
    389401
Note: See TracChangeset for help on using the changeset viewer.