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

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

(gtk 1.0x) #376: Use freedesktop icon spec

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