source: trunk/gtk/actions.c @ 9989

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

(trunk gtk) #2780 "for themability, T needs unique icon names for window, system tray, and popup icons" -- added to trunk for 1.81

  • Property svn:keywords set to Date Rev Author Id
File size: 12.0 KB
Line 
1/*
2 * This file Copyright (C) 2007-2010 Mnemosyne LLC
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 9989 2010-01-21 20:51:48Z charles $
11 */
12
13#include <string.h>
14
15#include <glib/gi18n.h>
16#include <gtk/gtk.h>
17
18#include <libtransmission/transmission.h>
19
20#include "actions.h"
21#include "conf.h"
22#include "tr-core.h"
23#include "tr-prefs.h"
24#include "lock.h"
25
26#include "logo.h"
27#include "ratio-icon.h"
28#include "options-icon.h"
29#include "turtles.h"
30
31#define UNUSED G_GNUC_UNUSED
32
33static TrCore *         myCore = NULL;
34
35static GtkActionGroup * myGroup = NULL;
36
37static void
38action_cb( GtkAction * a,
39           gpointer    user_data )
40{
41    doAction ( gtk_action_get_name( a ), user_data );
42}
43
44#if !GTK_CHECK_VERSION( 2, 8, 0 )
45 #define GTK_STOCK_INFO GTK_STOCK_PROPERTIES
46#endif
47
48#if !GTK_CHECK_VERSION( 2, 10, 0 )
49 #define GTK_STOCK_SELECT_ALL NULL
50#endif
51
52static GtkRadioActionEntry sort_radio_entries[] =
53{
54    { "sort-by-activity",  NULL, N_( "Sort by _Activity" ),  NULL, NULL, 0 },
55    { "sort-by-name",      NULL, N_( "Sort by _Name" ),      NULL, NULL, 1 },
56    { "sort-by-progress",  NULL, N_( "Sort by _Progress" ),  NULL, NULL, 2 },
57    { "sort-by-ratio",     NULL, N_( "Sort by Rati_o" ),     NULL, NULL, 3 },
58    { "sort-by-state",     NULL, N_( "Sort by Stat_e" ),     NULL, NULL, 4 },
59    { "sort-by-tracker",   NULL, N_( "Sort by T_racker" ),   NULL, NULL, 5 },
60    { "sort-by-age",       NULL, N_( "Sort by A_ge" ),       NULL, NULL, 6 },
61    { "sort-by-time-left", NULL, N_( "Sort by Time _Left" ), NULL, NULL, 7 },
62    { "sort-by-size",      NULL, N_( "Sort by Si_ze" ),      NULL, NULL, 8 }
63};
64
65static void
66sort_changed_cb( GtkAction            * action UNUSED,
67                 GtkRadioAction *              current,
68                 gpointer user_data            UNUSED )
69{
70    const char * key = PREF_KEY_SORT_MODE;
71    const int    i = gtk_radio_action_get_current_value( current );
72    const char * val = sort_radio_entries[i].name;
73
74    tr_core_set_pref( myCore, key, val );
75}
76
77static GtkToggleActionEntry show_toggle_entries[] =
78{
79    { "toggle-main-window", NULL, N_( "_Show Transmission" ), NULL, NULL, G_CALLBACK( action_cb ), TRUE },
80    { "toggle-message-log", NULL, 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    { "alt-speed-enabled", NULL, N_( "Enable Temporary Speed _Limits" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
96    { "minimal-view",      NULL, N_( "_Minimal View" ), "<alt>M", NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
97    { "sort-reversed",     NULL, N_( "Re_verse Sort Order" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
98    { "show-filterbar",    NULL, N_( "_Filterbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
99    { "show-statusbar",    NULL, N_( "_Statusbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
100    { "show-toolbar",      NULL, N_( "_Toolbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE }
101};
102
103static GtkActionEntry entries[] =
104{
105    { "file-menu", NULL, N_( "_File" ), NULL, NULL, NULL  },
106    { "torrent-menu", NULL, N_( "_Torrent" ), NULL, NULL, NULL  },
107    { "view-menu", NULL, N_( "_View" ), NULL, NULL, NULL  },
108    { "sort-menu", NULL, N_( "_Sort Torrents By" ), NULL, NULL, NULL },
109    { "edit-menu", NULL, N_( "_Edit" ), NULL, NULL, NULL },
110    { "help-menu", NULL, N_( "_Help" ), NULL, NULL, NULL },
111    { "copy-magnet-link-to-clipboard",  GTK_STOCK_COPY, N_("Copy _Magnet Link to Clipboard" ), "<control>M", NULL,  G_CALLBACK( action_cb ) },
112    { "add-torrent-from-url",  GTK_STOCK_ADD, N_("Add _URL..." ), NULL, N_( "Add URL..." ),  G_CALLBACK( action_cb ) },
113    { "add-torrent-toolbar",  GTK_STOCK_ADD, NULL, NULL, N_( "Add a torrent" ),  G_CALLBACK( action_cb ) },
114    { "add-torrent-menu", GTK_STOCK_ADD, N_( "_Add File..." ), "<control>D", N_( "Add a torrent" ), G_CALLBACK( action_cb ) },
115    { "start-torrent", GTK_STOCK_MEDIA_PLAY, N_( "_Start" ), "<control>S", N_( "Start torrent" ), G_CALLBACK( action_cb ) },
116    { "show-stats", NULL, N_( "_Statistics" ), NULL, NULL, G_CALLBACK( action_cb ) },
117    { "donate", NULL, N_( "_Donate" ), NULL, NULL, G_CALLBACK( action_cb ) },
118    { "verify-torrent", NULL, N_( "_Verify Local Data" ), "<control>V", NULL, G_CALLBACK( action_cb ) },
119    { "pause-torrent", GTK_STOCK_MEDIA_PAUSE, N_( "_Pause" ), "<control>P", N_( "Pause torrent" ), G_CALLBACK( action_cb ) },
120    { "pause-all-torrents", GTK_STOCK_MEDIA_PAUSE, N_( "_Pause All" ), NULL, N_( "Pause all torrents" ), G_CALLBACK( action_cb ) },
121    { "start-all-torrents", GTK_STOCK_MEDIA_PLAY, N_( "_Start All" ), NULL, N_( "Start all torrents" ), G_CALLBACK( action_cb ) },
122    { "relocate-torrent", NULL, N_("Set _Location" ), NULL, NULL, 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, WINDOW_ICON         },
148    { tr_icon_logo, TRAY_ICON           },
149    { tr_icon_logo, NOTIFICATION_ICON   },
150    { tr_icon_lock, "transmission-lock" },
151    { options_icon, "options"           },
152    { blue_turtle,  "alt-speed-on"      },
153    { grey_turtle,  "alt-speed-off"     },
154    { ratio_icon,   "ratio"             }
155};
156
157static void
158register_my_icons( void )
159{
160    int              i;
161    const int        n = G_N_ELEMENTS( my_fallback_icons );
162    GtkIconFactory * factory = gtk_icon_factory_new( );
163    GtkIconTheme *   theme = gtk_icon_theme_get_default( );
164
165    gtk_icon_factory_add_default( factory );
166
167    for( i = 0; i < n; ++i )
168    {
169        const char * name = my_fallback_icons[i].name;
170
171        if( !gtk_icon_theme_has_icon( theme, name ) )
172        {
173            int          width;
174            GdkPixbuf *  p;
175            GtkIconSet * icon_set;
176
177            p =
178                gdk_pixbuf_new_from_inline( -1, my_fallback_icons[i].raw,
179                                            FALSE,
180                                            NULL );
181            width = gdk_pixbuf_get_width( p );
182            icon_set = gtk_icon_set_new_from_pixbuf( p );
183            gtk_icon_theme_add_builtin_icon( name, width, p );
184            gtk_icon_factory_add( factory, name, icon_set );
185
186            g_object_unref( p );
187            gtk_icon_set_unref( icon_set );
188        }
189    }
190
191    g_object_unref ( G_OBJECT ( factory ) );
192}
193
194static GtkUIManager * myUIManager = NULL;
195
196void
197actions_set_core( TrCore * core )
198{
199    myCore = core;
200}
201
202void
203actions_init( GtkUIManager * ui_manager,
204              gpointer       callback_user_data )
205{
206    int              i, n;
207    int              active;
208    const char *     match;
209    const int        n_entries = G_N_ELEMENTS( entries );
210    GtkActionGroup * action_group;
211
212    myUIManager = ui_manager;
213
214    register_my_icons( );
215
216    action_group = myGroup = gtk_action_group_new( "Actions" );
217    gtk_action_group_set_translation_domain( action_group, NULL );
218
219
220    match = pref_string_get( PREF_KEY_SORT_MODE );
221    for( i = 0, n = G_N_ELEMENTS( sort_radio_entries ), active = -1;
222         active == -1 && i < n; ++i )
223        if( !strcmp( sort_radio_entries[i].name, match ) )
224            active = i;
225
226    gtk_action_group_add_radio_actions( action_group,
227                                        sort_radio_entries,
228                                        G_N_ELEMENTS( sort_radio_entries ),
229                                        active,
230                                        G_CALLBACK( sort_changed_cb ),
231                                        NULL );
232
233    gtk_action_group_add_toggle_actions( action_group,
234                                         show_toggle_entries,
235                                         G_N_ELEMENTS( show_toggle_entries ),
236                                         callback_user_data );
237
238    for( i = 0, n = G_N_ELEMENTS( pref_toggle_entries ); i < n; ++i )
239        pref_toggle_entries[i].is_active =
240            pref_flag_get( pref_toggle_entries[i].name );
241
242    gtk_action_group_add_toggle_actions( action_group,
243                                         pref_toggle_entries,
244                                         G_N_ELEMENTS( pref_toggle_entries ),
245                                         callback_user_data );
246
247    gtk_action_group_add_actions( action_group,
248                                  entries, n_entries,
249                                  callback_user_data );
250
251    gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
252    g_object_unref ( G_OBJECT( action_group ) );
253}
254
255/****
256*****
257****/
258
259static GHashTable * key_to_action = NULL;
260
261static void
262ensure_action_map_loaded( GtkUIManager * uim )
263{
264    GList * l;
265
266    if( key_to_action != NULL )
267        return;
268
269    key_to_action =
270        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL );
271
272    for( l = gtk_ui_manager_get_action_groups( uim ); l != NULL;
273         l = l->next )
274    {
275        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
276        GList *          ait, *actions = gtk_action_group_list_actions(
277            action_group );
278        for( ait = actions; ait != NULL; ait = ait->next )
279        {
280            GtkAction *  action = GTK_ACTION( ait->data );
281            const char * name = gtk_action_get_name( action );
282            g_hash_table_insert( key_to_action, g_strdup( name ), action );
283        }
284        g_list_free( actions );
285    }
286}
287
288static GtkAction*
289get_action( const char* name )
290{
291    ensure_action_map_loaded( myUIManager );
292    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
293}
294
295void
296action_activate( const char * name )
297{
298    GtkAction * action = get_action( name );
299
300    g_assert( action != NULL );
301    gtk_action_activate( action );
302}
303
304void
305action_sensitize( const char * name,
306                  gboolean     b )
307{
308    GtkAction * action = get_action( name );
309
310    g_assert( action != NULL );
311    g_object_set( action, "sensitive", b, NULL );
312}
313
314void
315action_set_important( const char * name, gboolean b )
316{
317    GtkAction * action = get_action( name );
318
319    g_assert( action != NULL );
320    g_object_set( action, "is-important", b, NULL );
321}
322
323void
324action_toggle( const char * name,
325               gboolean     b )
326{
327    GtkAction * action = get_action( name );
328
329    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION( action ), b );
330}
331
332GtkWidget*
333action_get_widget( const char * path )
334{
335    return gtk_ui_manager_get_widget( myUIManager, path );
336}
337
Note: See TracBrowser for help on using the repository browser.