source: branches/1.5x/gtk/actions.c @ 8205

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

(1.5x gtk) minor backports:
(1) remove dead code
(2) minor improvements to the filterbar buttons
(3) various minor formatting changes to reduce the diffs between 1.52 and trunk

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