source: branches/1.0x/gtk/actions.c @ 4969

Last change on this file since 4969 was 4969, checked in by charles, 15 years ago

(gtk 1.0x) #376 again: add a fallback logo for platforms that don't support the freedesktop icon scheme

  • Property svn:keywords set to Date Rev Author Id
File size: 10.8 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.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 4969 2008-02-07 20:27:15Z charles $
11 */
12
13#include <string.h>
14#include <glib/gi18n.h>
15#include <gtk/gtk.h>
16#include <libtransmission/transmission.h>
17#include "conf.h"
18#include "torrent-inspector.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
26extern void doAction (const char * action_name, gpointer user_data );
27
28static TrCore * myCore = 0;
29
30static GtkActionGroup * myGroup = 0;
31
32static void
33action_cb ( GtkAction * a, 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 priority_toggle_entries[] =
47{
48  { "priority-high", NULL, N_("_High"), NULL, NULL, TR_PRI_HIGH },
49  { "priority-normal", NULL, N_("_Normal"), NULL, NULL, TR_PRI_NORMAL },
50  { "priority-low", NULL, N_("_Low"), NULL, NULL, TR_PRI_LOW }
51};
52
53extern void set_selected_file_priority ( tr_priority_t ); 
54
55static void
56priority_changed_cb (GtkAction *action UNUSED, GtkRadioAction *current)
57{
58  const int priority = gtk_radio_action_get_current_value (current);
59  set_selected_file_priority ( priority );
60}
61
62static GtkRadioActionEntry sort_radio_entries[] =
63{
64  { "sort-by-activity",   NULL, N_("Sort by _Activity"),   NULL, NULL, 0 },
65  { "sort-by-date-added", NULL, N_("Sort by _Date Added"), NULL, NULL, 1 },
66  { "sort-by-name",       NULL, N_("Sort by _Name"),       NULL, NULL, 2 },
67  { "sort-by-progress",   NULL, N_("Sort by _Progress"),   NULL, NULL, 3 },
68  { "sort-by-state",      NULL, N_("Sort by _State"),      NULL, NULL, 4 },
69  { "sort-by-tracker",    NULL, N_("Sort by _Tracker"),    NULL, NULL, 5 }
70};
71
72static void
73sort_changed_cb( GtkAction            * action UNUSED,
74                 GtkRadioAction       * current,
75                 gpointer user_data     UNUSED )
76{
77    const char * key = PREF_KEY_SORT_MODE;
78    const int i = gtk_radio_action_get_current_value( current );
79    const char * val = sort_radio_entries[i].name;
80    tr_core_set_pref( myCore, key, val );
81}
82
83static GtkToggleActionEntry show_toggle_entries[] = 
84{
85  { "toggle-main-window", NULL, 
86    N_("Show _Main Window"), NULL, NULL, G_CALLBACK(action_cb), TRUE }, 
87  { "toggle-message-log", NULL,
88    N_("Show Message _Log"), NULL, NULL, G_CALLBACK(action_cb), FALSE }
89};
90
91static void
92toggle_pref_cb ( GtkToggleAction * action, gpointer user_data UNUSED )
93{
94    const char * key = gtk_action_get_name( GTK_ACTION( action ) );
95    const gboolean val = gtk_toggle_action_get_active( action );
96    tr_core_set_pref_bool( myCore, key, val );
97}
98
99static GtkToggleActionEntry pref_toggle_entries[] =
100{
101  { "minimal-view", NULL,
102    N_("_Minimal View"), "<alt>M", NULL, G_CALLBACK(toggle_pref_cb), FALSE },
103  { "sort-reversed", NULL,
104    N_("_Reverse Sort Order"), NULL, NULL, G_CALLBACK(toggle_pref_cb), FALSE },
105  { "show-filter-bar", NULL,
106    N_("Show _Filter Bar"), NULL, NULL, G_CALLBACK(toggle_pref_cb), FALSE },
107  { "show-status-bar", NULL,
108    N_("Show _Status Bar"), NULL, NULL, G_CALLBACK(toggle_pref_cb), FALSE },
109  { "show-toolbar", NULL,
110    N_("Show _Toolbar"), NULL, NULL, G_CALLBACK(toggle_pref_cb), FALSE }
111};
112
113static GtkActionEntry entries[] =
114{
115  { "torrent-menu", NULL, N_("_Torrent"), NULL, NULL, NULL },
116  { "view-menu", NULL, N_("_View"), NULL, NULL, NULL },
117  { "sort-menu", NULL, N_("_Sort Torrents By"), NULL, NULL, NULL },
118  { "edit-menu", NULL, N_("_Edit"), NULL, NULL, NULL },
119  { "help-menu", NULL, N_("_Help"), NULL, NULL, NULL },
120  { "priority-menu", NULL, N_("_Priority"), NULL, NULL, NULL },
121  { "add-torrent", GTK_STOCK_OPEN,
122    NULL, NULL, N_("Open Torrent"), G_CALLBACK(action_cb) },
123  { "start-torrent", GTK_STOCK_MEDIA_PLAY,
124    N_("_Start"), "<control>S", NULL, G_CALLBACK(action_cb) },
125  { "show-stats", NULL, N_("_Statistics"), NULL, NULL, G_CALLBACK(action_cb) },
126  { "verify-torrent", NULL,
127    N_("_Verify Local Data"), NULL, NULL, G_CALLBACK(action_cb) },
128  { "pause-torrent", GTK_STOCK_MEDIA_PAUSE,
129    N_("_Pause"), "<control>P", NULL, G_CALLBACK(action_cb) },
130  { "remove-torrent", GTK_STOCK_REMOVE,
131    N_("_Remove"), "<control>Delete", NULL, G_CALLBACK(action_cb) },
132  { "create-torrent", GTK_STOCK_NEW,
133    N_("Create _New Torrent"), NULL, NULL, G_CALLBACK(action_cb) },
134  { "close", GTK_STOCK_CLOSE,
135    N_("_Close"), "<control>W", NULL, G_CALLBACK(action_cb) },
136  { "quit", GTK_STOCK_QUIT,
137    N_("_Quit"), "<control>Q", NULL, G_CALLBACK(action_cb) },
138  { "select-all", GTK_STOCK_SELECT_ALL,
139    N_("Select _All"), "<control>A", NULL, G_CALLBACK(action_cb) },
140  { "unselect-all", NULL,
141    N_("Dese_lect All"), "<shift><control>A", NULL, G_CALLBACK(action_cb) },
142  { "edit-preferences", GTK_STOCK_PREFERENCES,
143    NULL, NULL, NULL, G_CALLBACK(action_cb) },
144  { "show-torrent-details", GTK_STOCK_INFO,
145    N_("_Details"), "<alt>Return", N_("Torrent Details"), G_CALLBACK(action_cb) },
146  { "show-about-dialog", GTK_STOCK_ABOUT,
147    N_("_About Transmission"), NULL, NULL, G_CALLBACK(action_cb) },
148  { "update-tracker", GTK_STOCK_NETWORK,
149    N_("Ask Tracker for _More Peers"), NULL, NULL, G_CALLBACK(action_cb) }
150};
151
152static void
153ensure_tooltip (GtkActionEntry * e)
154{
155    if( !e->tooltip && e->label )
156    {
157        const char * src;
158        char *tgt;
159        e->tooltip = g_malloc( strlen( e->label ) + 1 );
160        for( src=e->label, tgt=(char*)e->tooltip; *src; ++src )
161            if( *src != '_' )
162                *tgt++ = *src;
163        *tgt++ = '\0';
164    }
165}
166
167typedef struct
168{
169   const guint8* raw;
170   const char * name;
171}
172BuiltinIconInfo;
173
174const BuiltinIconInfo my_fallback_icons [] =
175{
176    { tr_icon_logo, "transmission" },
177    { tr_icon_lock, "transmission-lock" }
178};
179
180static void
181register_my_icons ( void )
182{
183    int i;
184    const int n = G_N_ELEMENTS( my_fallback_icons );
185    GtkIconFactory * factory = gtk_icon_factory_new( );
186    GtkIconTheme * theme = gtk_icon_theme_get_default( );
187    gtk_icon_factory_add_default( factory );
188
189    for( i=0; i<n; ++i )
190    {
191        const char * name = my_fallback_icons[i].name;
192
193        if( !gtk_icon_theme_has_icon( theme, name ) )
194        {
195            int width;
196            GdkPixbuf * p;
197            GtkIconSet * icon_set;
198
199            p = gdk_pixbuf_new_from_inline( -1, my_fallback_icons[i].raw, FALSE, 0 );
200            width = gdk_pixbuf_get_width( p );
201            icon_set = gtk_icon_set_new_from_pixbuf( p );
202            gtk_icon_theme_add_builtin_icon( name, width, p );
203            gtk_icon_factory_add( factory, name, icon_set );
204
205            g_object_unref( p );
206            gtk_icon_set_unref( icon_set );
207        }
208    }
209
210    g_object_unref (G_OBJECT (factory));
211}
212
213static GtkUIManager * myUIManager = NULL;
214
215void
216actions_set_core( TrCore * core )
217{
218    myCore = core;
219}
220
221void
222actions_init( GtkUIManager * ui_manager, gpointer callback_user_data )
223{
224  int i, n;
225  int active;
226  char * match;
227  const int n_entries = G_N_ELEMENTS( entries );
228  GtkActionGroup * action_group;
229
230  myUIManager = ui_manager;
231
232  register_my_icons ();
233
234  for( i=0; i<n_entries; ++i )
235    ensure_tooltip (&entries[i]);
236
237  action_group = myGroup = gtk_action_group_new( "Actions" );
238  gtk_action_group_set_translation_domain( action_group, NULL );
239
240  gtk_action_group_add_radio_actions( action_group,
241                                      priority_toggle_entries,
242                                      G_N_ELEMENTS(priority_toggle_entries),
243                                      TR_PRI_NORMAL,
244                                      G_CALLBACK(priority_changed_cb), NULL);
245
246
247  match = pref_string_get( PREF_KEY_SORT_MODE );
248  for( i=0, n=G_N_ELEMENTS(sort_radio_entries), active=-1; active==-1 && i<n; ++i )
249      if( !strcmp( sort_radio_entries[i].name, match ) )
250          active = i;
251
252  gtk_action_group_add_radio_actions( action_group,
253                                      sort_radio_entries,
254                                      G_N_ELEMENTS(sort_radio_entries),
255                                      active,
256                                      G_CALLBACK(sort_changed_cb),
257                                      NULL );
258
259  gtk_action_group_add_toggle_actions( action_group, 
260                                       show_toggle_entries, 
261                                       G_N_ELEMENTS(show_toggle_entries), 
262                                       callback_user_data );
263
264  for( i=0, n=G_N_ELEMENTS(pref_toggle_entries); i<n; ++i )
265    pref_toggle_entries[i].is_active =
266      pref_flag_get( pref_toggle_entries[i].name );
267
268  gtk_action_group_add_toggle_actions( action_group, 
269                                       pref_toggle_entries, 
270                                       G_N_ELEMENTS(pref_toggle_entries), 
271                                       callback_user_data );
272
273  gtk_action_group_add_actions( action_group,
274                                entries, n_entries,
275                                callback_user_data );
276
277  gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
278  g_object_unref (G_OBJECT(action_group));
279  g_free( match );
280}
281
282/****
283*****
284****/
285
286static GHashTable * key_to_action = NULL;
287
288static void
289ensure_action_map_loaded (GtkUIManager * uim)
290{
291    GList * l;
292
293    if ( key_to_action != NULL )
294        return;
295
296    key_to_action =
297        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL);
298
299    for( l=gtk_ui_manager_get_action_groups(uim); l!=NULL; l=l->next )
300    {
301        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
302        GList *ait, *actions = gtk_action_group_list_actions( action_group );
303        for( ait=actions; ait!=NULL; ait=ait->next )
304        {
305            GtkAction * action = GTK_ACTION( ait->data );
306            const char * name = gtk_action_get_name( action );
307            g_hash_table_insert( key_to_action, g_strdup(name), action );
308        }
309        g_list_free( actions );
310    }
311}
312
313static GtkAction*
314get_action( const char* name )
315{
316    ensure_action_map_loaded( myUIManager );
317    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
318}
319
320void
321action_activate ( const char * name )
322{
323    GtkAction * action = get_action( name );
324    g_assert( action != NULL );
325    gtk_action_activate( action );
326}
327
328void
329action_sensitize( const char * name, gboolean b )
330{
331    GtkAction * action = get_action( name );
332    g_assert( action != NULL );
333    g_object_set( action, "sensitive", b, NULL );
334}
335
336void
337action_toggle( const char * name, gboolean b )
338{
339    GtkAction * action = get_action( name );
340    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION(action), b );
341}
342
343GtkWidget*
344action_get_widget( const char * path )
345{
346    return gtk_ui_manager_get_widget( myUIManager, path );
347}
Note: See TracBrowser for help on using the repository browser.