Changeset 3771 for trunk/gtk/main.c


Ignore:
Timestamp:
Nov 9, 2007, 4:11:10 PM (15 years ago)
Author:
charles
Message:

sync our shutdown code to BentMyWookie?'s latest tr_close() changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/main.c

    r3587 r3771  
    111111};
    112112
    113 struct exitdata {
    114     struct cbdata * cbdata;
    115     time_t          started;
    116     guint           timer;
    117 };
    118 
    119 #define CBDATA_PTR              "callback-data-pointer"
     113#define CBDATA_PTR "callback-data-pointer"
    120114
    121115static GtkUIManager * myUIManager = NULL;
     
    134128static void
    135129wannaquit( void * vdata );
    136 static gboolean
    137 exitcheck(gpointer gdata);
    138130static void
    139131setupdrag(GtkWidget *widget, struct cbdata *data);
     
    256248
    257249    /* initialize gtk */
     250    g_thread_init( NULL );
    258251    gtk_init_with_args( &argc, &argv, _("[torrent files]"), entries, domain, NULL );
    259252    myUIManager = gtk_ui_manager_new ();
     
    413406}
    414407
     408static gpointer
     409quitThreadFunc( gpointer gdata )
     410{
     411    struct cbdata * cbdata = gdata;
     412
     413    tr_close( tr_core_handle( cbdata->core ) );
     414
     415    /* shutdown the gui */
     416    if( cbdata->prefs )
     417        gtk_widget_destroy( GTK_WIDGET( cbdata->prefs ) );
     418    if( cbdata->wind )
     419        gtk_widget_destroy( GTK_WIDGET( cbdata->wind ) );
     420    g_object_unref( cbdata->core );
     421    if( cbdata->icon )
     422        g_object_unref( cbdata->icon );
     423    if( cbdata->errqueue ) {
     424        g_list_foreach( cbdata->errqueue, (GFunc)g_free, NULL );
     425        g_list_free( cbdata->errqueue );
     426    }
     427    g_free( cbdata );
     428
     429    /* exit the gtk main loop */
     430    gtk_main_quit( );
     431    return NULL;
     432}
     433
    415434static void
    416435wannaquit( void * vdata )
    417436{
    418   struct cbdata * data;
    419   struct exitdata *edata;
    420 
    421   data = vdata;
    422   if( data->closing )
    423   {
    424       return;
    425   }
    426   data->closing = TRUE;
    427 
    428   /* stop the update timer */
    429   if(0 < data->timer)
    430     g_source_remove(data->timer);
    431   data->timer = 0;
    432 
    433   /* pause torrents and stop nat traversal */
    434   tr_core_shutdown( data->core );
    435 
    436   /* set things up to wait for torrents to stop */
    437   edata = g_new0(struct exitdata, 1);
    438   edata->cbdata = data;
    439   edata->started = time(NULL);
    440   /* check if torrents are still running */
    441   if(exitcheck(edata)) {
    442     /* yes, start the exit timer and disable widgets */
    443     edata->timer = g_timeout_add(EXIT_CHECK_INTERVAL, exitcheck, edata);
    444     if( NULL != data->wind )
    445     {
    446         gtk_widget_set_sensitive( GTK_WIDGET( data->wind ), FALSE );
    447     }
    448   }
    449 }
    450 
    451 static gboolean
    452 exitcheck( gpointer gdata )
    453 {
    454     struct exitdata    * edata;
    455     struct cbdata      * cbdata;
    456 
    457     edata  = gdata;
    458     cbdata = edata->cbdata;
    459 
    460     /* keep waiting until we're ready to quit or we hit the exit timeout */
    461     if( time( NULL ) - edata->started < TRACKER_EXIT_TIMEOUT )
    462     {
    463         if( !tr_core_quiescent( cbdata->core ) )
    464         {
    465             updatemodel( cbdata );
    466             return TRUE;
    467         }
    468     }
    469 
    470     /* exit otherwise */
    471     if( 0 < edata->timer )
    472     {
    473         g_source_remove( edata->timer );
    474     }
    475     g_free( edata );
    476     /* note that cbdata->prefs holds a reference to cbdata->core, and
    477        it's destruction may trigger callbacks that use cbdata->core */
    478     if( NULL != cbdata->prefs )
    479     {
    480         gtk_widget_destroy( GTK_WIDGET( cbdata->prefs ) );
    481     }
    482     if( NULL != cbdata->wind )
    483     {
    484         gtk_widget_destroy( GTK_WIDGET( cbdata->wind ) );
    485     }
    486     g_object_unref( cbdata->core );
    487     if( NULL != cbdata->icon )
    488     {
    489         g_object_unref( cbdata->icon );
    490     }
    491     g_assert( 0 == cbdata->timer );
    492     if( NULL != cbdata->errqueue )
    493     {
    494         g_list_foreach( cbdata->errqueue, (GFunc) g_free, NULL );
    495         g_list_free( cbdata->errqueue );
    496     }
    497     g_free( cbdata );
    498     gtk_main_quit();
    499 
    500     return FALSE;
     437    struct cbdata * cbdata = vdata;
     438
     439    /* stop the update timer */
     440    if( cbdata->timer ) {
     441        g_source_remove( cbdata->timer );
     442        cbdata->timer = 0;
     443    }
     444
     445    /* freeze the gui */
     446    if( cbdata->wind )
     447        gtk_widget_set_sensitive( GTK_WIDGET( cbdata->wind ), FALSE );
     448
     449    /* shut down libT */
     450    g_thread_create( quitThreadFunc, vdata, TRUE, NULL );
    501451}
    502452
Note: See TracChangeset for help on using the changeset viewer.