source: trunk/gtk/actions.c @ 7252

Last change on this file since 7252 was 7252, checked in by charles, 12 years ago

(gtk) #1300: "pause all" button in the right-click menu of the system tray icon

  • Property svn:keywords set to Date Rev Author Id
File size: 10.8 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.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 7252 2008-12-03 05:15:17Z 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    { "remove-torrent", GTK_STOCK_REMOVE, NULL, "Delete", N_( "Remove torrent" ), G_CALLBACK( action_cb ) },
124    { "delete-torrent", GTK_STOCK_DELETE, N_( "_Delete Files and Remove" ), "<shift>Delete", NULL, G_CALLBACK( action_cb ) },
125    { "new-torrent", GTK_STOCK_NEW, N_( "_New..." ), NULL, N_( "Create a torrent" ), G_CALLBACK( action_cb ) },
126    { "quit", GTK_STOCK_QUIT, N_( "_Quit" ), NULL, NULL, G_CALLBACK( action_cb ) },
127    { "select-all", GTK_STOCK_SELECT_ALL, N_( "Select _All" ), "<control>A", NULL, G_CALLBACK( action_cb ) },
128    { "deselect-all", NULL, N_( "Dese_lect All" ), "<shift><control>A", NULL, G_CALLBACK( action_cb ) },
129    { "edit-preferences", GTK_STOCK_PREFERENCES, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
130    { "show-torrent-properties", GTK_STOCK_PROPERTIES, NULL, "<alt>Return", N_( "Torrent properties" ), G_CALLBACK( action_cb ) },
131    { "open-torrent-folder",  GTK_STOCK_OPEN, N_( "_Open Folder" ), NULL, NULL, G_CALLBACK( action_cb ) },
132    { "show-about-dialog", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
133    { "help", GTK_STOCK_HELP, N_( "_Contents" ), "F1", NULL, G_CALLBACK( action_cb ) },
134    { "update-tracker", GTK_STOCK_NETWORK, N_( "Ask Tracker for _More Peers" ), NULL, NULL, G_CALLBACK( action_cb ) },
135    { "present-main-window", NULL, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
136};
137
138typedef struct
139{
140    const guint8*   raw;
141    const char *    name;
142}
143BuiltinIconInfo;
144
145static const BuiltinIconInfo my_fallback_icons[] =
146{
147    { tr_icon_logo, "transmission"      },
148    { tr_icon_lock, "transmission-lock" }
149};
150
151static void
152register_my_icons( void )
153{
154    int              i;
155    const int        n = G_N_ELEMENTS( my_fallback_icons );
156    GtkIconFactory * factory = gtk_icon_factory_new( );
157    GtkIconTheme *   theme = gtk_icon_theme_get_default( );
158
159    gtk_icon_factory_add_default( factory );
160
161    for( i = 0; i < n; ++i )
162    {
163        const char * name = my_fallback_icons[i].name;
164
165        if( !gtk_icon_theme_has_icon( theme, name ) )
166        {
167            int          width;
168            GdkPixbuf *  p;
169            GtkIconSet * icon_set;
170
171            p =
172                gdk_pixbuf_new_from_inline( -1, my_fallback_icons[i].raw,
173                                            FALSE,
174                                            NULL );
175            width = gdk_pixbuf_get_width( p );
176            icon_set = gtk_icon_set_new_from_pixbuf( p );
177            gtk_icon_theme_add_builtin_icon( name, width, p );
178            gtk_icon_factory_add( factory, name, icon_set );
179
180            g_object_unref( p );
181            gtk_icon_set_unref( icon_set );
182        }
183    }
184
185    g_object_unref ( G_OBJECT ( factory ) );
186}
187
188static GtkUIManager * myUIManager = NULL;
189
190void
191actions_set_core( TrCore * core )
192{
193    myCore = core;
194}
195
196void
197actions_init( GtkUIManager * ui_manager,
198              gpointer       callback_user_data )
199{
200    int              i, n;
201    int              active;
202    const char *     match;
203    const int        n_entries = G_N_ELEMENTS( entries );
204    GtkActionGroup * action_group;
205
206    myUIManager = ui_manager;
207
208    register_my_icons( );
209
210    action_group = myGroup = gtk_action_group_new( "Actions" );
211    gtk_action_group_set_translation_domain( action_group, NULL );
212
213
214    match = pref_string_get( PREF_KEY_SORT_MODE );
215    for( i = 0, n = G_N_ELEMENTS( sort_radio_entries ), active = -1;
216         active == -1 && i < n; ++i )
217        if( !strcmp( sort_radio_entries[i].name, match ) )
218            active = i;
219
220    gtk_action_group_add_radio_actions( action_group,
221                                        sort_radio_entries,
222                                        G_N_ELEMENTS( sort_radio_entries ),
223                                        active,
224                                        G_CALLBACK( sort_changed_cb ),
225                                        NULL );
226
227    gtk_action_group_add_toggle_actions( action_group,
228                                         show_toggle_entries,
229                                         G_N_ELEMENTS( show_toggle_entries ),
230                                         callback_user_data );
231
232    for( i = 0, n = G_N_ELEMENTS( pref_toggle_entries ); i < n; ++i )
233        pref_toggle_entries[i].is_active =
234            pref_flag_get( pref_toggle_entries[i].name );
235
236    gtk_action_group_add_toggle_actions( action_group,
237                                         pref_toggle_entries,
238                                         G_N_ELEMENTS( pref_toggle_entries ),
239                                         callback_user_data );
240
241    gtk_action_group_add_actions( action_group,
242                                  entries, n_entries,
243                                  callback_user_data );
244
245    gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
246    g_object_unref ( G_OBJECT( action_group ) );
247}
248
249/****
250*****
251****/
252
253static GHashTable * key_to_action = NULL;
254
255static void
256ensure_action_map_loaded( GtkUIManager * uim )
257{
258    GList * l;
259
260    if( key_to_action != NULL )
261        return;
262
263    key_to_action =
264        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL );
265
266    for( l = gtk_ui_manager_get_action_groups( uim ); l != NULL;
267         l = l->next )
268    {
269        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
270        GList *          ait, *actions = gtk_action_group_list_actions(
271            action_group );
272        for( ait = actions; ait != NULL; ait = ait->next )
273        {
274            GtkAction *  action = GTK_ACTION( ait->data );
275            const char * name = gtk_action_get_name( action );
276            g_hash_table_insert( key_to_action, g_strdup( name ), action );
277        }
278        g_list_free( actions );
279    }
280}
281
282static GtkAction*
283get_action( const char* name )
284{
285    ensure_action_map_loaded( myUIManager );
286    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
287}
288
289void
290action_activate( const char * name )
291{
292    GtkAction * action = get_action( name );
293
294    g_assert( action != NULL );
295    gtk_action_activate( action );
296}
297
298void
299action_sensitize( const char * name,
300                  gboolean     b )
301{
302    GtkAction * action = get_action( name );
303
304    g_assert( action != NULL );
305    g_object_set( action, "sensitive", b, NULL );
306}
307
308void
309action_toggle( const char * name,
310               gboolean     b )
311{
312    GtkAction * action = get_action( name );
313
314    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION( action ), b );
315}
316
317GtkWidget*
318action_get_widget( const char * path )
319{
320    return gtk_ui_manager_get_widget( myUIManager, path );
321}
322
Note: See TracBrowser for help on using the repository browser.