source: branches/1.5x/gtk/actions.c @ 7910

Last change on this file since 7910 was 7910, checked in by charles, 13 years ago

(1.5x gtk) #1846: GTK client has no pause all / resume all buttons

  • Property svn:keywords set to Date Rev Author Id
File size: 10.9 KB
Line 
1/*
2 * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: actions.c 7910 2009-02-18 18:40:36Z charles $
11 */
12
13#include <string.h>
14#include <glib/gi18n.h>
15#include <gtk/gtk.h>
16#include <libtransmission/transmission.h>
17#include "actions.h"
18#include "conf.h"
19#include "tr-core.h"
20#include "tr-prefs.h"
21#include "lock.h"
22#include "logo.h"
23
24#define UNUSED G_GNUC_UNUSED
25
26static TrCore *         myCore = NULL;
27
28static GtkActionGroup * myGroup = NULL;
29
30static void
31action_cb( GtkAction * a,
32           gpointer    user_data )
33{
34    doAction ( gtk_action_get_name( a ), user_data );
35}
36
37#if !GTK_CHECK_VERSION( 2, 8, 0 )
38 #define GTK_STOCK_INFO GTK_STOCK_PROPERTIES
39#endif
40
41#if !GTK_CHECK_VERSION( 2, 10, 0 )
42 #define GTK_STOCK_SELECT_ALL NULL
43#endif
44
45static GtkRadioActionEntry sort_radio_entries[] =
46{
47    { "sort-by-activity", NULL,      N_( "Sort by _Activity" ),        NULL,
48      NULL, 0 },
49    { "sort-by-name",     NULL,        N_( "Sort by _Name" ),     NULL,
50      NULL, 1 },
51    { "sort-by-progress", NULL,        N_( "Sort by _Progress" ), NULL,
52      NULL, 2 },
53    { "sort-by-ratio",    NULL,        N_( "Sort by _Ratio" ),    NULL,
54      NULL, 3 },
55    { "sort-by-state",    NULL,        N_( "Sort by _State" ),    NULL,
56      NULL, 4 },
57    { "sort-by-tracker",  NULL,        N_( "Sort by _Tracker" ),  NULL,
58      NULL, 5 },
59    { "sort-by-age",      NULL,        N_( "Sort by A_ge" ),      NULL,
60      NULL, 6 }
61};
62
63static void
64sort_changed_cb( GtkAction            * action UNUSED,
65                 GtkRadioAction *              current,
66                 gpointer user_data            UNUSED )
67{
68    const char * key = PREF_KEY_SORT_MODE;
69    const int    i = gtk_radio_action_get_current_value( current );
70    const char * val = sort_radio_entries[i].name;
71
72    tr_core_set_pref( myCore, key, val );
73}
74
75static GtkToggleActionEntry show_toggle_entries[] =
76{
77    { "toggle-main-window", NULL,
78      N_( "_Main Window" ), NULL, NULL, G_CALLBACK( action_cb ), TRUE },
79    { "toggle-message-log", NULL,
80      N_( "Message _Log" ), NULL, NULL, G_CALLBACK( action_cb ), FALSE }
81};
82
83static void
84toggle_pref_cb( GtkToggleAction *  action,
85                gpointer user_data UNUSED )
86{
87    const char *   key = gtk_action_get_name( GTK_ACTION( action ) );
88    const gboolean val = gtk_toggle_action_get_active( action );
89
90    tr_core_set_pref_bool( myCore, key, val );
91}
92
93static GtkToggleActionEntry  pref_toggle_entries[] =
94{
95    { "minimal-view",          NULL,
96      N_( "_Minimal View" ), "<alt>M", NULL, G_CALLBACK( toggle_pref_cb ),
97      FALSE },
98    { "sort-reversed",         NULL,
99      N_( "_Reverse Sort Order" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ),
100      FALSE },
101    { "show-filterbar",        NULL,
102      N_( "_Filterbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
103    { "show-statusbar",        NULL,
104      N_( "_Statusbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
105    { "show-toolbar",          NULL,
106      N_( "_Toolbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE }
107};
108
109static GtkActionEntry        entries[] =
110{
111    { "torrent-menu", NULL, N_( "_Torrent" ), NULL, NULL, NULL  },
112    { "view-menu", NULL, N_( "_View" ), NULL, NULL, NULL  },
113    { "sort-menu", NULL, N_( "_Sort Torrents By" ), NULL, NULL, NULL },
114    { "edit-menu", NULL, N_( "_Edit" ), NULL, NULL, NULL },
115    { "help-menu", NULL, N_( "_Help" ), NULL, NULL, NULL },
116    { "add-torrent-toolbar",  GTK_STOCK_ADD, NULL, NULL, N_( "Add a torrent" ),  G_CALLBACK( action_cb ) },
117    { "add-torrent-menu", GTK_STOCK_ADD, N_( "_Add..." ), "<control>D", N_( "Add a torrent" ), G_CALLBACK( action_cb ) },
118    { "start-torrent", GTK_STOCK_MEDIA_PLAY, N_( "_Start" ), "<control>S", N_( "Start torrent" ), G_CALLBACK( action_cb ) },
119    { "show-stats", NULL, N_( "_Statistics" ), NULL, NULL, G_CALLBACK( action_cb ) },
120    { "verify-torrent", NULL, N_( "_Verify Local Data" ), NULL, NULL, G_CALLBACK( action_cb ) },
121    { "pause-torrent", GTK_STOCK_MEDIA_PAUSE, N_( "_Pause" ), "<control>P", N_( "Pause torrent" ), G_CALLBACK( action_cb ) },
122    { "pause-all-torrents", GTK_STOCK_MEDIA_PAUSE, N_( "_Pause All" ), NULL, N_( "Pause all torrents" ), G_CALLBACK( action_cb ) },
123    { "start-all-torrents", GTK_STOCK_MEDIA_PLAY, N_( "_Start All" ), NULL, N_( "Start all torrents" ), G_CALLBACK( action_cb ) },
124    { "remove-torrent", GTK_STOCK_REMOVE, NULL, "Delete", N_( "Remove torrent" ), G_CALLBACK( action_cb ) },
125    { "delete-torrent", GTK_STOCK_DELETE, N_( "_Delete Files and Remove" ), "<shift>Delete", NULL, G_CALLBACK( action_cb ) },
126    { "new-torrent", GTK_STOCK_NEW, N_( "_New..." ), NULL, N_( "Create a torrent" ), G_CALLBACK( action_cb ) },
127    { "quit", GTK_STOCK_QUIT, N_( "_Quit" ), NULL, NULL, G_CALLBACK( action_cb ) },
128    { "select-all", GTK_STOCK_SELECT_ALL, N_( "Select _All" ), "<control>A", NULL, G_CALLBACK( action_cb ) },
129    { "deselect-all", NULL, N_( "Dese_lect All" ), "<shift><control>A", NULL, G_CALLBACK( action_cb ) },
130    { "edit-preferences", GTK_STOCK_PREFERENCES, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
131    { "show-torrent-properties", GTK_STOCK_PROPERTIES, NULL, "<alt>Return", N_( "Torrent properties" ), G_CALLBACK( action_cb ) },
132    { "open-torrent-folder",  GTK_STOCK_OPEN, N_( "_Open Folder" ), NULL, NULL, G_CALLBACK( action_cb ) },
133    { "show-about-dialog", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
134    { "help", GTK_STOCK_HELP, N_( "_Contents" ), "F1", NULL, G_CALLBACK( action_cb ) },
135    { "update-tracker", GTK_STOCK_NETWORK, N_( "Ask Tracker for _More Peers" ), NULL, NULL, G_CALLBACK( action_cb ) },
136    { "present-main-window", NULL, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
137};
138
139typedef struct
140{
141    const guint8*   raw;
142    const char *    name;
143}
144BuiltinIconInfo;
145
146static const BuiltinIconInfo my_fallback_icons[] =
147{
148    { tr_icon_logo, "transmission"      },
149    { tr_icon_lock, "transmission-lock" }
150};
151
152static void
153register_my_icons( void )
154{
155    int              i;
156    const int        n = G_N_ELEMENTS( my_fallback_icons );
157    GtkIconFactory * factory = gtk_icon_factory_new( );
158    GtkIconTheme *   theme = gtk_icon_theme_get_default( );
159
160    gtk_icon_factory_add_default( factory );
161
162    for( i = 0; i < n; ++i )
163    {
164        const char * name = my_fallback_icons[i].name;
165
166        if( !gtk_icon_theme_has_icon( theme, name ) )
167        {
168            int          width;
169            GdkPixbuf *  p;
170            GtkIconSet * icon_set;
171
172            p =
173                gdk_pixbuf_new_from_inline( -1, my_fallback_icons[i].raw,
174                                            FALSE,
175                                            NULL );
176            width = gdk_pixbuf_get_width( p );
177            icon_set = gtk_icon_set_new_from_pixbuf( p );
178            gtk_icon_theme_add_builtin_icon( name, width, p );
179            gtk_icon_factory_add( factory, name, icon_set );
180
181            g_object_unref( p );
182            gtk_icon_set_unref( icon_set );
183        }
184    }
185
186    g_object_unref ( G_OBJECT ( factory ) );
187}
188
189static GtkUIManager * myUIManager = NULL;
190
191void
192actions_set_core( TrCore * core )
193{
194    myCore = core;
195}
196
197void
198actions_init( GtkUIManager * ui_manager,
199              gpointer       callback_user_data )
200{
201    int              i, n;
202    int              active;
203    const char *     match;
204    const int        n_entries = G_N_ELEMENTS( entries );
205    GtkActionGroup * action_group;
206
207    myUIManager = ui_manager;
208
209    register_my_icons( );
210
211    action_group = myGroup = gtk_action_group_new( "Actions" );
212    gtk_action_group_set_translation_domain( action_group, NULL );
213
214
215    match = pref_string_get( PREF_KEY_SORT_MODE );
216    for( i = 0, n = G_N_ELEMENTS( sort_radio_entries ), active = -1;
217         active == -1 && i < n; ++i )
218        if( !strcmp( sort_radio_entries[i].name, match ) )
219            active = i;
220
221    gtk_action_group_add_radio_actions( action_group,
222                                        sort_radio_entries,
223                                        G_N_ELEMENTS( sort_radio_entries ),
224                                        active,
225                                        G_CALLBACK( sort_changed_cb ),
226                                        NULL );
227
228    gtk_action_group_add_toggle_actions( action_group,
229                                         show_toggle_entries,
230                                         G_N_ELEMENTS( show_toggle_entries ),
231                                         callback_user_data );
232
233    for( i = 0, n = G_N_ELEMENTS( pref_toggle_entries ); i < n; ++i )
234        pref_toggle_entries[i].is_active =
235            pref_flag_get( pref_toggle_entries[i].name );
236
237    gtk_action_group_add_toggle_actions( action_group,
238                                         pref_toggle_entries,
239                                         G_N_ELEMENTS( pref_toggle_entries ),
240                                         callback_user_data );
241
242    gtk_action_group_add_actions( action_group,
243                                  entries, n_entries,
244                                  callback_user_data );
245
246    gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
247    g_object_unref ( G_OBJECT( action_group ) );
248}
249
250/****
251*****
252****/
253
254static GHashTable * key_to_action = NULL;
255
256static void
257ensure_action_map_loaded( GtkUIManager * uim )
258{
259    GList * l;
260
261    if( key_to_action != NULL )
262        return;
263
264    key_to_action =
265        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL );
266
267    for( l = gtk_ui_manager_get_action_groups( uim ); l != NULL;
268         l = l->next )
269    {
270        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
271        GList *          ait, *actions = gtk_action_group_list_actions(
272            action_group );
273        for( ait = actions; ait != NULL; ait = ait->next )
274        {
275            GtkAction *  action = GTK_ACTION( ait->data );
276            const char * name = gtk_action_get_name( action );
277            g_hash_table_insert( key_to_action, g_strdup( name ), action );
278        }
279        g_list_free( actions );
280    }
281}
282
283static GtkAction*
284get_action( const char* name )
285{
286    ensure_action_map_loaded( myUIManager );
287    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
288}
289
290void
291action_activate( const char * name )
292{
293    GtkAction * action = get_action( name );
294
295    g_assert( action != NULL );
296    gtk_action_activate( action );
297}
298
299void
300action_sensitize( const char * name,
301                  gboolean     b )
302{
303    GtkAction * action = get_action( name );
304
305    g_assert( action != NULL );
306    g_object_set( action, "sensitive", b, NULL );
307}
308
309void
310action_toggle( const char * name,
311               gboolean     b )
312{
313    GtkAction * action = get_action( name );
314
315    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION( action ), b );
316}
317
318GtkWidget*
319action_get_widget( const char * path )
320{
321    return gtk_ui_manager_get_widget( myUIManager, path );
322}
323
Note: See TracBrowser for help on using the repository browser.