source: trunk/gtk/actions.c @ 8420

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

(trunk gtk) #2090: Speed limit switch accessibility

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