source: trunk/gtk/actions.c @ 8238

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

(trunk gtk) #1522: Lack of fast bw limit settings and display

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