Changeset 12671


Ignore:
Timestamp:
Aug 13, 2011, 5:47:32 AM (12 years ago)
Author:
jordan
Message:

(trunk gtk) simplify the details dialog manager

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/main.c

    r12670 r12671  
    114114
    115115    for( l=tmp; l!=NULL; l=l->next )
    116         g_string_append_printf( gstr, "%d ", (int)l->data );
     116        g_string_append_printf( gstr, "%d ", GPOINTER_TO_INT(l->data) );
    117117
    118118    g_slist_free( tmp );
     
    120120}
    121121
    122 struct DetailsDialogHandle
    123 {
    124     char * key;
    125     GtkWidget * dialog;
    126 };
    127 
     122static void
     123get_selected_torrent_ids_foreach( GtkTreeModel  * model,
     124                                  GtkTreePath   * p UNUSED,
     125                                  GtkTreeIter   * iter,
     126                                  gpointer        gdata )
     127{
     128    int id;
     129    GSList ** ids = gdata;
     130    gtk_tree_model_get( model, iter, MC_TORRENT_ID, &id, -1 );
     131    *ids = g_slist_append( *ids, GINT_TO_POINTER( id ) );
     132}
    128133static GSList*
    129 getSelectedTorrentIds( struct cbdata * data )
    130 {
    131     GList * l;
    132     GtkTreeModel * model;
     134get_selected_torrent_ids( struct cbdata * data )
     135{
    133136    GSList * ids = NULL;
    134     GList * paths = NULL;
    135     GtkTreeSelection * s = data->sel;
    136 
    137     /* build a list of the selected torrents' ids */
    138     for( paths=l=gtk_tree_selection_get_selected_rows(s,&model); l; l=l->next ) {
    139         GtkTreeIter iter;
    140         if( gtk_tree_model_get_iter( model, &iter, l->data ) ) {
    141             int id;
    142             gtk_tree_model_get( model, &iter, MC_TORRENT_ID, &id, -1 );
    143             ids = g_slist_append( ids, GINT_TO_POINTER( id ) );
    144         }
    145     }
    146 
    147     /* cleanup */
    148     g_list_foreach( paths, (GFunc)gtk_tree_path_free, NULL );
    149     g_list_free( paths );
     137    gtk_tree_selection_selected_foreach( data->sel,
     138                                         get_selected_torrent_ids_foreach,
     139                                         &ids );
    150140    return ids;
    151141}
    152142
    153 static struct DetailsDialogHandle*
    154 find_details_dialog_from_ids( struct cbdata * cbdata, GSList * ids )
    155 {
     143static void
     144on_details_dialog_closed( gpointer gdata, GObject * dead )
     145{
     146    struct cbdata * data = gdata;
     147
     148    data->details = g_slist_remove( data->details, dead );
     149}
     150
     151static void
     152show_details_dialog_for_selected_torrents( struct cbdata * data )
     153{
     154    GtkWidget * dialog = NULL;
    156155    GSList * l;
    157     struct DetailsDialogHandle * ret = NULL;
     156    GSList * ids = get_selected_torrent_ids( data );
    158157    char * key = get_details_dialog_key( ids );
    159158
    160     for( l=cbdata->details; l!=NULL && ret==NULL; l=l->next ) {
    161         struct DetailsDialogHandle * h = l->data;
    162         if( !strcmp( h->key, key ) )
    163             ret = h;
    164     }
    165 
     159    for( l=data->details; dialog==NULL && l!=NULL; l=l->next )
     160        if( !strcmp( key, g_object_get_data( l->data, "key" ) ) )
     161            dialog = l->data;
     162
     163    if( dialog == NULL )
     164    {
     165        dialog = gtr_torrent_details_dialog_new( GTK_WINDOW( data->wind ), data->core );
     166        gtr_torrent_details_dialog_set_torrents( dialog, ids );
     167        g_object_set_data_full( G_OBJECT( dialog ), "key", g_strdup( key ), g_free );
     168        g_object_weak_ref( G_OBJECT( dialog ), on_details_dialog_closed, data );
     169        data->details = g_slist_append( data->details, dialog );
     170        gtk_widget_show( dialog );
     171    }
     172
     173    gtr_window_present( GTK_WINDOW( dialog ) );
    166174    g_free( key );
    167     return ret;
    168 }
    169 
    170 static struct DetailsDialogHandle*
    171 find_details_dialog_from_widget( struct cbdata * cbdata, gpointer w )
    172 {
    173     GSList * l;
    174     struct DetailsDialogHandle * ret = NULL;
    175 
    176     for( l=cbdata->details; l!=NULL && ret==NULL; l=l->next ) {
    177         struct DetailsDialogHandle * h = l->data;
    178         if( h->dialog == w )
    179             ret = h;
    180     }
    181 
    182     return ret;
    183 }
    184 
    185 static void
    186 on_details_dialog_closed( gpointer gdata, GObject * dead )
    187 {
    188     struct cbdata * data = gdata;
    189     struct DetailsDialogHandle * h = find_details_dialog_from_widget( data, dead );
    190 
    191     if( h != NULL )
    192     {
    193         data->details = g_slist_remove( data->details, h );
    194         g_free( h->key );
    195         g_free( h );
    196     }
    197 }
    198 
    199 static void
    200 show_details_dialog_for_selected_torrents( struct cbdata * data )
    201 {
    202     GtkWidget * w;
    203     GSList * ids = getSelectedTorrentIds( data );
    204     struct DetailsDialogHandle * h = find_details_dialog_from_ids( data, ids );
    205 
    206     if( h != NULL )
    207         w = h->dialog;
    208     else {
    209         h = g_new( struct DetailsDialogHandle, 1 );
    210         h->key = get_details_dialog_key( ids );
    211         h->dialog = w = gtr_torrent_details_dialog_new( data->wind, data->core );
    212         gtr_torrent_details_dialog_set_torrents( w, ids );
    213         data->details = g_slist_append( data->details, h );
    214         g_object_weak_ref( G_OBJECT( w ), on_details_dialog_closed, data );
    215         gtk_widget_show( w );
    216     }
    217     gtr_window_present( GTK_WINDOW( w ) );
    218175    g_slist_free( ids );
    219176}
     
    676633    gtk_window_set_default_icon_name (MY_CONFIG_NAME);
    677634
    678 
    679635    /* parse the command line */
    680636    option_context = g_option_context_new( _( "[torrent files or urls]" ) );
     
    909865on_session_closed( gpointer gdata )
    910866{
     867    GSList * tmp;
    911868    struct cbdata * cbdata = gdata;
    912869
    913     /* shutdown the gui */
    914     while( cbdata->details != NULL ) {
    915         struct DetailsDialogHandle * h = cbdata->details->data;
    916         gtk_widget_destroy( h->dialog );
    917     }
     870    tmp = g_slist_copy( cbdata->details );
     871    g_slist_foreach( tmp, (GFunc)gtk_widget_destroy, NULL );
     872    g_slist_free( tmp );
    918873
    919874    if( cbdata->prefs )
     
    15511506    else if( !strcmp( action_name, "relocate-torrent" ) )
    15521507    {
    1553         GSList * ids = getSelectedTorrentIds( data );
     1508        GSList * ids = get_selected_torrent_ids( data );
    15541509        if( ids != NULL )
    15551510        {
Note: See TracChangeset for help on using the changeset viewer.