source: trunk/gtk/actions.c @ 5180

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

(gtk) #748: hig improvements to the `remove torrent' confirmation dialog.

  • Property svn:keywords set to Date Rev Author Id
File size: 10.1 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 5180 2008-03-02 15:54:58Z 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, N_("_Open"), NULL,
104    N_("Open an existing torrent"),
105    G_CALLBACK(action_cb) },
106  { "open-torrent-menu", GTK_STOCK_OPEN, N_("_Open..."), "<control>O",
107    N_("Open an existing torrent"),
108    G_CALLBACK(action_cb) },
109  { "start-torrent", GTK_STOCK_MEDIA_PLAY,
110    N_("_Start"), "<control>S", NULL, G_CALLBACK(action_cb) },
111  { "show-stats", NULL, N_("_Statistics"), NULL, NULL, G_CALLBACK(action_cb) },
112  { "verify-torrent", NULL,
113    N_("_Verify Local Data"), NULL, NULL, G_CALLBACK(action_cb) },
114  { "pause-torrent", GTK_STOCK_MEDIA_PAUSE,
115    N_("_Pause"), "<control>P", NULL, G_CALLBACK(action_cb) },
116  { "remove-torrent", GTK_STOCK_REMOVE,
117    N_("_Remove"), "Delete", NULL, G_CALLBACK(action_cb) },
118  { "delete-torrent", GTK_STOCK_DELETE,
119    N_("_Delete Files"), "<control>Delete", NULL, G_CALLBACK(action_cb) },
120  { "new-torrent", GTK_STOCK_NEW, N_("_New..."), NULL,
121    N_("Create a new torrent"),
122    G_CALLBACK(action_cb) },
123  { "close", GTK_STOCK_CLOSE, N_("_Close"), "<control>W",
124    N_("Close main window"),
125    G_CALLBACK(action_cb) },
126  { "quit", GTK_STOCK_QUIT,
127    N_("_Quit"), "<control>Q", NULL, G_CALLBACK(action_cb) },
128  { "select-all", GTK_STOCK_SELECT_ALL,
129    N_("Select _All"), "<control>A", NULL, G_CALLBACK(action_cb) },
130  { "unselect-all", NULL,
131    N_("Dese_lect All"), "<shift><control>A", NULL, G_CALLBACK(action_cb) },
132  { "edit-preferences", GTK_STOCK_PREFERENCES,
133    NULL, NULL, NULL, G_CALLBACK(action_cb) },
134  { "show-torrent-details", GTK_STOCK_INFO,
135    N_("_Details"), "<alt>Return", N_("Torrent Details"), G_CALLBACK(action_cb) },
136  { "show-about-dialog", GTK_STOCK_ABOUT,
137    N_("_About Transmission"), NULL, NULL, G_CALLBACK(action_cb) },
138  { "update-tracker", GTK_STOCK_NETWORK,
139    N_("Ask Tracker for _More Peers"), NULL, NULL, G_CALLBACK(action_cb) }
140};
141
142static void
143ensure_tooltip (GtkActionEntry * e)
144{
145    if( !e->tooltip && e->label )
146    {
147        const char * src;
148        char *tgt;
149        e->tooltip = g_malloc( strlen( e->label ) + 1 );
150        for( src=e->label, tgt=(char*)e->tooltip; *src; ++src )
151            if( *src != '_' )
152                *tgt++ = *src;
153        *tgt++ = '\0';
154    }
155}
156
157typedef struct
158{
159   const guint8* raw;
160   const char * name;
161}
162BuiltinIconInfo;
163
164const BuiltinIconInfo my_fallback_icons [] =
165{
166    { tr_icon_logo, "transmission" },
167    { tr_icon_lock, "transmission-lock" }
168};
169
170static void
171register_my_icons ( void )
172{
173    int i;
174    const int n = G_N_ELEMENTS( my_fallback_icons );
175    GtkIconFactory * factory = gtk_icon_factory_new( );
176    GtkIconTheme * theme = gtk_icon_theme_get_default( );
177    gtk_icon_factory_add_default( factory );
178
179    for( i=0; i<n; ++i )
180    {
181        const char * name = my_fallback_icons[i].name;
182
183        if( !gtk_icon_theme_has_icon( theme, name ) )
184        {
185            int width;
186            GdkPixbuf * p;
187            GtkIconSet * icon_set;
188
189            p = gdk_pixbuf_new_from_inline( -1, my_fallback_icons[i].raw, FALSE, 0 );
190            width = gdk_pixbuf_get_width( p );
191            icon_set = gtk_icon_set_new_from_pixbuf( p );
192            gtk_icon_theme_add_builtin_icon( name, width, p );
193            gtk_icon_factory_add( factory, name, icon_set );
194
195            g_object_unref( p );
196            gtk_icon_set_unref( icon_set );
197        }
198    }
199
200    g_object_unref (G_OBJECT (factory));
201}
202
203static GtkUIManager * myUIManager = NULL;
204
205void
206actions_set_core( TrCore * core )
207{
208    myCore = core;
209}
210
211void
212actions_init( GtkUIManager * ui_manager, gpointer callback_user_data )
213{
214  int i, n;
215  int active;
216  char * match;
217  const int n_entries = G_N_ELEMENTS( entries );
218  GtkActionGroup * action_group;
219
220  myUIManager = ui_manager;
221
222  register_my_icons ();
223
224  for( i=0; i<n_entries; ++i )
225    ensure_tooltip (&entries[i]);
226
227  action_group = myGroup = gtk_action_group_new( "Actions" );
228  gtk_action_group_set_translation_domain( action_group, NULL );
229
230
231  match = pref_string_get( PREF_KEY_SORT_MODE );
232  for( i=0, n=G_N_ELEMENTS(sort_radio_entries), active=-1; active==-1 && i<n; ++i )
233      if( !strcmp( sort_radio_entries[i].name, match ) )
234          active = i;
235
236  gtk_action_group_add_radio_actions( action_group,
237                                      sort_radio_entries,
238                                      G_N_ELEMENTS(sort_radio_entries),
239                                      active,
240                                      G_CALLBACK(sort_changed_cb),
241                                      NULL );
242
243  gtk_action_group_add_toggle_actions( action_group, 
244                                       show_toggle_entries, 
245                                       G_N_ELEMENTS(show_toggle_entries), 
246                                       callback_user_data );
247
248  for( i=0, n=G_N_ELEMENTS(pref_toggle_entries); i<n; ++i )
249    pref_toggle_entries[i].is_active =
250      pref_flag_get( pref_toggle_entries[i].name );
251
252  gtk_action_group_add_toggle_actions( action_group, 
253                                       pref_toggle_entries, 
254                                       G_N_ELEMENTS(pref_toggle_entries), 
255                                       callback_user_data );
256
257  gtk_action_group_add_actions( action_group,
258                                entries, n_entries,
259                                callback_user_data );
260
261  gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
262  g_object_unref (G_OBJECT(action_group));
263  g_free( match );
264}
265
266/****
267*****
268****/
269
270static GHashTable * key_to_action = NULL;
271
272static void
273ensure_action_map_loaded (GtkUIManager * uim)
274{
275    GList * l;
276
277    if ( key_to_action != NULL )
278        return;
279
280    key_to_action =
281        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL);
282
283    for( l=gtk_ui_manager_get_action_groups(uim); l!=NULL; l=l->next )
284    {
285        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
286        GList *ait, *actions = gtk_action_group_list_actions( action_group );
287        for( ait=actions; ait!=NULL; ait=ait->next )
288        {
289            GtkAction * action = GTK_ACTION( ait->data );
290            const char * name = gtk_action_get_name( action );
291            g_hash_table_insert( key_to_action, g_strdup(name), action );
292        }
293        g_list_free( actions );
294    }
295}
296
297static GtkAction*
298get_action( const char* name )
299{
300    ensure_action_map_loaded( myUIManager );
301    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
302}
303
304void
305action_activate ( const char * name )
306{
307    GtkAction * action = get_action( name );
308    g_assert( action != NULL );
309    gtk_action_activate( action );
310}
311
312void
313action_sensitize( const char * name, gboolean b )
314{
315    GtkAction * action = get_action( name );
316    g_assert( action != NULL );
317    g_object_set( action, "sensitive", b, NULL );
318}
319
320void
321action_toggle( const char * name, gboolean b )
322{
323    GtkAction * action = get_action( name );
324    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION(action), b );
325}
326
327GtkWidget*
328action_get_widget( const char * path )
329{
330    return gtk_ui_manager_get_widget( myUIManager, path );
331}
Note: See TracBrowser for help on using the repository browser.