source: trunk/gtk/actions.c @ 8097

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

(trunk) more speedlimit work

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