source: trunk/gtk/actions.c @ 12354

Last change on this file since 12354 was 12354, checked in by jordan, 11 years ago

(trunk gtk) transmission-gtk requires GTK+ 2.8 or higher, so remove the `#if GTK_CHECK_VERSION(2,8,0)' checks because we already know the answer.

  • Property svn:keywords set to Date Rev Author Id
File size: 11.8 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
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 12354 2011-04-12 11:13:41Z jordan $
11 */
12
13#include <string.h>
14
15#include <glib/gi18n.h>
16#include <gtk/gtk.h>
17
18#include <libtransmission/transmission.h>
19
20#include "actions.h"
21#include "conf.h"
22#include "tr-core.h"
23#include "tr-prefs.h"
24
25#include "icon-lock.h"
26#include "icon-logo-24.h"
27#include "icon-logo-48.h"
28#include "icon-ratio.h"
29#include "icon-turtle.h"
30#include "icon-utilities.h"
31
32#define UNUSED G_GNUC_UNUSED
33
34static TrCore * myCore = NULL;
35static GtkActionGroup * myGroup = NULL;
36
37static void
38action_cb( GtkAction * a, gpointer user_data )
39{
40    gtr_actions_handler( gtk_action_get_name( a ), user_data );
41}
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 Rati_o" ),     NULL, NULL, 3 },
53    { "sort-by-state",     NULL, N_( "Sort by Stat_e" ),     NULL, NULL, 4 },
54    { "sort-by-age",       NULL, N_( "Sort by A_ge" ),       NULL, NULL, 5 },
55    { "sort-by-time-left", NULL, N_( "Sort by Time _Left" ), NULL, NULL, 6 },
56    { "sort-by-size",      NULL, N_( "Sort by Si_ze" ),      NULL, NULL, 7 }
57};
58
59static void
60sort_changed_cb( GtkAction            * action UNUSED,
61                 GtkRadioAction *              current,
62                 gpointer user_data            UNUSED )
63{
64    const char * key = PREF_KEY_SORT_MODE;
65    const int    i = gtk_radio_action_get_current_value( current );
66    const char * val = sort_radio_entries[i].name;
67
68    gtr_core_set_pref( myCore, key, val );
69}
70
71static GtkToggleActionEntry show_toggle_entries[] =
72{
73    { "toggle-main-window", NULL, N_( "_Show Transmission" ), NULL, NULL, G_CALLBACK( action_cb ), TRUE },
74    { "toggle-message-log", NULL, N_( "Message _Log" ), NULL, NULL, G_CALLBACK( action_cb ), FALSE }
75};
76
77static void
78toggle_pref_cb( GtkToggleAction *  action,
79                gpointer user_data UNUSED )
80{
81    const char *   key = gtk_action_get_name( GTK_ACTION( action ) );
82    const gboolean val = gtk_toggle_action_get_active( action );
83
84    gtr_core_set_pref_bool( myCore, key, val );
85}
86
87static GtkToggleActionEntry  pref_toggle_entries[] =
88{
89    { "alt-speed-enabled", NULL, N_( "Enable Alternative Speed _Limits" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
90    { "compact-view",      NULL, N_( "_Compact View" ), "<alt>C", NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
91    { "sort-reversed",     NULL, N_( "Re_verse Sort Order" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
92    { "show-filterbar",    NULL, N_( "_Filterbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
93    { "show-statusbar",    NULL, N_( "_Statusbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE },
94    { "show-toolbar",      NULL, N_( "_Toolbar" ), NULL, NULL, G_CALLBACK( toggle_pref_cb ), FALSE }
95};
96
97static GtkActionEntry entries[] =
98{
99    { "file-menu", NULL, N_( "_File" ), NULL, NULL, NULL  },
100    { "torrent-menu", NULL, N_( "_Torrent" ), NULL, NULL, NULL  },
101    { "view-menu", NULL, N_( "_View" ), NULL, NULL, NULL  },
102    { "sort-menu", NULL, N_( "_Sort Torrents By" ), NULL, NULL, NULL },
103    { "edit-menu", NULL, N_( "_Edit" ), NULL, NULL, NULL },
104    { "help-menu", NULL, N_( "_Help" ), NULL, NULL, NULL },
105    { "copy-magnet-link-to-clipboard",  GTK_STOCK_COPY, N_("Copy _Magnet Link to Clipboard" ), "", NULL,  G_CALLBACK( action_cb ) },
106    { "open-torrent-from-url",  GTK_STOCK_OPEN, N_("Open _URL..." ), "<control>U", N_( "Open URL..." ),  G_CALLBACK( action_cb ) },
107    { "open-torrent-toolbar",  GTK_STOCK_OPEN, NULL, NULL, N_( "Open a torrent" ),  G_CALLBACK( action_cb ) },
108    { "open-torrent-menu", GTK_STOCK_OPEN, NULL, NULL, N_( "Open a torrent" ), G_CALLBACK( action_cb ) },
109    { "start-torrent", GTK_STOCK_MEDIA_PLAY, N_( "_Start" ), "<control>S", N_( "Start torrent" ), G_CALLBACK( action_cb ) },
110    { "show-stats", NULL, N_( "_Statistics" ), NULL, NULL, G_CALLBACK( action_cb ) },
111    { "donate", NULL, N_( "_Donate" ), NULL, NULL, G_CALLBACK( action_cb ) },
112    { "verify-torrent", NULL, N_( "_Verify Local Data" ), "<control>V", NULL, G_CALLBACK( action_cb ) },
113    { "pause-torrent", GTK_STOCK_MEDIA_PAUSE, N_( "_Pause" ), "<control>P", N_( "Pause torrent" ), G_CALLBACK( action_cb ) },
114    { "pause-all-torrents", GTK_STOCK_MEDIA_PAUSE, N_( "_Pause All" ), NULL, N_( "Pause all torrents" ), G_CALLBACK( action_cb ) },
115    { "start-all-torrents", GTK_STOCK_MEDIA_PLAY, N_( "_Start All" ), NULL, N_( "Start all torrents" ), G_CALLBACK( action_cb ) },
116    { "relocate-torrent", NULL, N_("Set _Location..." ), NULL, NULL, G_CALLBACK( action_cb ) },
117    { "remove-torrent", GTK_STOCK_REMOVE, NULL, "Delete", N_( "Remove torrent" ), G_CALLBACK( action_cb ) },
118    { "delete-torrent", GTK_STOCK_DELETE, N_( "_Delete Files and Remove" ), "<shift>Delete", NULL, G_CALLBACK( action_cb ) },
119    { "new-torrent", GTK_STOCK_NEW, N_( "_New..." ), NULL, N_( "Create a torrent" ), G_CALLBACK( action_cb ) },
120    { "quit", GTK_STOCK_QUIT, N_( "_Quit" ), NULL, NULL, G_CALLBACK( action_cb ) },
121    { "select-all", GTK_STOCK_SELECT_ALL, N_( "Select _All" ), "<control>A", NULL, G_CALLBACK( action_cb ) },
122    { "deselect-all", NULL, N_( "Dese_lect All" ), "<shift><control>A", NULL, G_CALLBACK( action_cb ) },
123    { "edit-preferences", GTK_STOCK_PREFERENCES, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
124    { "show-torrent-properties", GTK_STOCK_PROPERTIES, NULL, "<alt>Return", N_( "Torrent properties" ), G_CALLBACK( action_cb ) },
125    { "open-torrent-folder",  GTK_STOCK_OPEN, N_( "Open Fold_er" ), "<control>E", NULL, G_CALLBACK( action_cb ) },
126    { "show-about-dialog", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK( action_cb ) },
127    { "help", GTK_STOCK_HELP, N_( "_Contents" ), "F1", NULL, G_CALLBACK( action_cb ) },
128    { "update-tracker", GTK_STOCK_NETWORK, N_( "Ask Tracker for _More Peers" ), NULL, NULL, G_CALLBACK( action_cb ) },
129};
130
131typedef struct
132{
133    const guint8*   raw;
134    const char *    name;
135}
136BuiltinIconInfo;
137
138static const BuiltinIconInfo my_fallback_icons[] =
139{
140    { tr_icon_logo_48,  WINDOW_ICON          },
141    { tr_icon_logo_24,  TRAY_ICON            },
142    { tr_icon_logo_48,  NOTIFICATION_ICON    },
143    { tr_icon_lock,     "transmission-lock"  },
144    { utilities_icon,   "utilities"          },
145    { blue_turtle,      "alt-speed-on"       },
146    { grey_turtle,      "alt-speed-off"      },
147    { ratio_icon,       "ratio"              }
148};
149
150static void
151register_my_icons( void )
152{
153    int              i;
154    const int        n = G_N_ELEMENTS( my_fallback_icons );
155    GtkIconFactory * factory = gtk_icon_factory_new( );
156    GtkIconTheme *   theme = gtk_icon_theme_get_default( );
157
158    gtk_icon_factory_add_default( factory );
159
160    for( i = 0; i < n; ++i )
161    {
162        const char * name = my_fallback_icons[i].name;
163
164        if( !gtk_icon_theme_has_icon( theme, name ) )
165        {
166            int          width;
167            GdkPixbuf *  p;
168            GtkIconSet * icon_set;
169
170            p =
171                gdk_pixbuf_new_from_inline( -1, my_fallback_icons[i].raw,
172                                            FALSE,
173                                            NULL );
174            width = gdk_pixbuf_get_width( p );
175            icon_set = gtk_icon_set_new_from_pixbuf( p );
176            gtk_icon_theme_add_builtin_icon( name, width, p );
177            gtk_icon_factory_add( factory, name, icon_set );
178
179            g_object_unref( p );
180            gtk_icon_set_unref( icon_set );
181        }
182    }
183
184    g_object_unref ( G_OBJECT ( factory ) );
185}
186
187static GtkUIManager * myUIManager = NULL;
188
189void
190gtr_actions_set_core( TrCore * core )
191{
192    myCore = core;
193}
194
195void
196gtr_actions_init( GtkUIManager * ui_manager, gpointer callback_user_data )
197{
198    int              i, n;
199    int              active;
200    const char *     match;
201    const int        n_entries = G_N_ELEMENTS( entries );
202    GtkActionGroup * action_group;
203
204    myUIManager = ui_manager;
205
206    register_my_icons( );
207
208    action_group = myGroup = gtk_action_group_new( "Actions" );
209    gtk_action_group_set_translation_domain( action_group, NULL );
210
211
212    match = gtr_pref_string_get( PREF_KEY_SORT_MODE );
213    for( i = 0, n = G_N_ELEMENTS( sort_radio_entries ), active = -1;
214         active == -1 && i < n; ++i )
215        if( !strcmp( sort_radio_entries[i].name, match ) )
216            active = i;
217
218    gtk_action_group_add_radio_actions( action_group,
219                                        sort_radio_entries,
220                                        G_N_ELEMENTS( sort_radio_entries ),
221                                        active,
222                                        G_CALLBACK( sort_changed_cb ),
223                                        NULL );
224
225    gtk_action_group_add_toggle_actions( action_group,
226                                         show_toggle_entries,
227                                         G_N_ELEMENTS( show_toggle_entries ),
228                                         callback_user_data );
229
230    for( i = 0, n = G_N_ELEMENTS( pref_toggle_entries ); i < n; ++i )
231        pref_toggle_entries[i].is_active =
232            gtr_pref_flag_get( pref_toggle_entries[i].name );
233
234    gtk_action_group_add_toggle_actions( action_group,
235                                         pref_toggle_entries,
236                                         G_N_ELEMENTS( pref_toggle_entries ),
237                                         callback_user_data );
238
239    gtk_action_group_add_actions( action_group,
240                                  entries, n_entries,
241                                  callback_user_data );
242
243    gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 );
244    g_object_unref ( G_OBJECT( action_group ) );
245}
246
247/****
248*****
249****/
250
251static GHashTable * key_to_action = NULL;
252
253static void
254ensure_action_map_loaded( GtkUIManager * uim )
255{
256    GList * l;
257
258    if( key_to_action != NULL )
259        return;
260
261    key_to_action =
262        g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL );
263
264    for( l = gtk_ui_manager_get_action_groups( uim ); l != NULL;
265         l = l->next )
266    {
267        GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data );
268        GList *          ait, *actions = gtk_action_group_list_actions(
269            action_group );
270        for( ait = actions; ait != NULL; ait = ait->next )
271        {
272            GtkAction *  action = GTK_ACTION( ait->data );
273            const char * name = gtk_action_get_name( action );
274            g_hash_table_insert( key_to_action, g_strdup( name ), action );
275        }
276        g_list_free( actions );
277    }
278}
279
280static GtkAction*
281get_action( const char* name )
282{
283    ensure_action_map_loaded( myUIManager );
284    return ( GtkAction* ) g_hash_table_lookup( key_to_action, name );
285}
286
287void
288gtr_action_activate( const char * name )
289{
290    GtkAction * action = get_action( name );
291
292    g_assert( action != NULL );
293    gtk_action_activate( action );
294}
295
296void
297gtr_action_set_sensitive( const char * name, gboolean b )
298{
299    GtkAction * action = get_action( name );
300
301    g_assert( action != NULL );
302    g_object_set( action, "sensitive", b, NULL );
303}
304
305void
306gtr_action_set_important( const char * name, gboolean b )
307{
308    GtkAction * action = get_action( name );
309
310    g_assert( action != NULL );
311    g_object_set( action, "is-important", b, NULL );
312}
313
314void
315gtr_action_set_toggled( const char * name, gboolean b )
316{
317    GtkAction * action = get_action( name );
318
319    gtk_toggle_action_set_active( GTK_TOGGLE_ACTION( action ), b );
320}
321
322GtkWidget*
323gtr_action_get_widget( const char * path )
324{
325    return gtk_ui_manager_get_widget( myUIManager, path );
326}
327
Note: See TracBrowser for help on using the repository browser.