source: trunk/gtk/actions.c @ 5209

Last change on this file since 5209 was 5209, checked in by charles, 14 years ago

more i18n work.

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