source: trunk/gtk/actions.c @ 8389

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

(trunk) #920: add "move data" to libT so all clients can use it

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