1 | /* |
---|
2 | * This file Copyright (C) 2007 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 3392 2007-10-13 13:51:38Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <string.h> |
---|
14 | #include <glib/gi18n.h> |
---|
15 | #include <gtk/gtk.h> |
---|
16 | #include <libtransmission/transmission.h> |
---|
17 | #include "torrent-inspector.h" |
---|
18 | #include "lock.h" |
---|
19 | #include "logo.h" |
---|
20 | |
---|
21 | #define UNUSED G_GNUC_UNUSED |
---|
22 | |
---|
23 | extern void doAction (const char * action_name, gpointer user_data ); |
---|
24 | |
---|
25 | static GtkActionGroup * myGroup = 0; |
---|
26 | |
---|
27 | static void action_cb ( GtkAction * a, gpointer user_data ) |
---|
28 | { |
---|
29 | doAction ( gtk_action_get_name(a), user_data ); |
---|
30 | } |
---|
31 | |
---|
32 | #if !GTK_CHECK_VERSION(2,8,0) |
---|
33 | #define GTK_STOCK_INFO GTK_STOCK_PROPERTIES |
---|
34 | #endif |
---|
35 | |
---|
36 | #if !GTK_CHECK_VERSION(2,10,0) |
---|
37 | #define GTK_STOCK_SELECT_ALL NULL |
---|
38 | #endif |
---|
39 | |
---|
40 | static GtkRadioActionEntry priority_toggle_entries[] = |
---|
41 | { |
---|
42 | { "priority-high", NULL, N_("_High"), NULL, NULL, TR_PRI_HIGH }, |
---|
43 | { "priority-normal", NULL, N_("_Normal"), NULL, NULL, TR_PRI_NORMAL }, |
---|
44 | { "priority-low", NULL, N_("_Low"), NULL, NULL, TR_PRI_LOW } |
---|
45 | }; |
---|
46 | |
---|
47 | extern void set_selected_file_priority ( tr_priority_t ); |
---|
48 | |
---|
49 | static void |
---|
50 | priority_changed_cb (GtkAction *action UNUSED, GtkRadioAction *current) |
---|
51 | { |
---|
52 | const int priority = gtk_radio_action_get_current_value (current); |
---|
53 | set_selected_file_priority ( priority ); |
---|
54 | } |
---|
55 | |
---|
56 | static GtkToggleActionEntry show_toggle_entries[] = |
---|
57 | { |
---|
58 | { "toggle-main-window", NULL, |
---|
59 | N_("Show _Main Window"), NULL, NULL, G_CALLBACK(action_cb), TRUE }, |
---|
60 | { "toggle-debug-window", NULL, |
---|
61 | N_("Show _Debug Window"), NULL, NULL, G_CALLBACK(action_cb), FALSE } |
---|
62 | }; |
---|
63 | |
---|
64 | static GtkActionEntry entries[] = |
---|
65 | { |
---|
66 | { "torrent-menu", NULL, N_("_Torrent"), NULL, NULL, NULL }, |
---|
67 | { "edit-menu", NULL, N_("_Edit"), NULL, NULL, NULL }, |
---|
68 | { "help-menu", NULL, N_("_Help"), NULL, NULL, NULL }, |
---|
69 | { "priority-menu", NULL, N_("_Priority"), NULL, NULL, NULL }, |
---|
70 | { "add-torrent", GTK_STOCK_OPEN, NULL, NULL, N_("Open Torrent"), G_CALLBACK(action_cb) }, |
---|
71 | { "start-torrent", GTK_STOCK_MEDIA_PLAY, |
---|
72 | N_("_Start"), "<control>S", NULL, G_CALLBACK(action_cb) }, |
---|
73 | { "verify-torrent", NULL, |
---|
74 | N_("_Verify Local Files"), NULL, NULL, G_CALLBACK(action_cb) }, |
---|
75 | { "pause-torrent", GTK_STOCK_MEDIA_PAUSE, |
---|
76 | N_("_Pause"), "<control>P", NULL, G_CALLBACK(action_cb) }, |
---|
77 | { "remove-torrent", GTK_STOCK_REMOVE, |
---|
78 | N_("_Remove"), "<control>R", NULL, G_CALLBACK(action_cb) }, |
---|
79 | { "create-torrent", GTK_STOCK_NEW, |
---|
80 | N_("Create _New Torrent"), NULL, NULL, G_CALLBACK(action_cb) }, |
---|
81 | { "close", GTK_STOCK_CLOSE, |
---|
82 | N_("_Close"), "<control>W", NULL, G_CALLBACK(action_cb) }, |
---|
83 | { "quit", GTK_STOCK_QUIT, |
---|
84 | N_("_Quit"), "<control>Q", NULL, G_CALLBACK(action_cb) }, |
---|
85 | { "select-all", GTK_STOCK_SELECT_ALL, |
---|
86 | N_("Select _All"), "<control>A", NULL, G_CALLBACK(action_cb) }, |
---|
87 | { "unselect-all", NULL, |
---|
88 | N_("Dese_lect All"), "<shift><control>A", NULL, G_CALLBACK(action_cb) }, |
---|
89 | { "edit-preferences", GTK_STOCK_PREFERENCES, |
---|
90 | NULL, NULL, NULL, G_CALLBACK(action_cb) }, |
---|
91 | { "show-torrent-details", GTK_STOCK_INFO, |
---|
92 | N_("_Details"), "<alt>Return", N_("Torrent Details"), G_CALLBACK(action_cb) }, |
---|
93 | { "show-about-dialog", GTK_STOCK_ABOUT, |
---|
94 | N_("_About Transmission"), NULL, NULL, G_CALLBACK(action_cb) }, |
---|
95 | { "update-tracker", GTK_STOCK_NETWORK, |
---|
96 | N_("Ask Tracker for _More Peers"), NULL, NULL, G_CALLBACK(action_cb) } |
---|
97 | }; |
---|
98 | |
---|
99 | static void |
---|
100 | ensure_tooltip (GtkActionEntry * e) |
---|
101 | { |
---|
102 | if( !e->tooltip && e->label ) |
---|
103 | { |
---|
104 | const char * src; |
---|
105 | char *tgt; |
---|
106 | e->tooltip = g_malloc( strlen( e->label ) + 1 ); |
---|
107 | for( src=e->label, tgt=(char*)e->tooltip; *src; ++src ) |
---|
108 | if( *src != '_' ) |
---|
109 | *tgt++ = *src; |
---|
110 | *tgt++ = '\0'; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | typedef struct |
---|
115 | { |
---|
116 | const guint8* raw; |
---|
117 | const char * name; |
---|
118 | } |
---|
119 | BuiltinIconInfo; |
---|
120 | |
---|
121 | /* only one icon now... but room to grow ;) */ |
---|
122 | const BuiltinIconInfo my_builtin_icons [] = |
---|
123 | { |
---|
124 | { tr_icon_logo, "transmission-logo" }, |
---|
125 | { tr_icon_lock, "transmission-lock" } |
---|
126 | }; |
---|
127 | |
---|
128 | static void |
---|
129 | register_my_icons ( void ) |
---|
130 | { |
---|
131 | int i; |
---|
132 | const int n = G_N_ELEMENTS( my_builtin_icons ); |
---|
133 | GtkIconFactory * factory = gtk_icon_factory_new (); |
---|
134 | gtk_icon_factory_add_default( factory ); |
---|
135 | |
---|
136 | for( i=0; i<n; ++i ) |
---|
137 | { |
---|
138 | GdkPixbuf * p; |
---|
139 | int width; |
---|
140 | GtkIconSet * icon_set; |
---|
141 | |
---|
142 | p = gdk_pixbuf_new_from_inline( -1, my_builtin_icons[i].raw, FALSE, 0 ); |
---|
143 | width = gdk_pixbuf_get_width( p ); |
---|
144 | gtk_icon_theme_add_builtin_icon (my_builtin_icons[i].name, width, p ); |
---|
145 | |
---|
146 | icon_set = gtk_icon_set_new_from_pixbuf( p ); |
---|
147 | gtk_icon_factory_add( factory, my_builtin_icons[i].name, icon_set ); |
---|
148 | |
---|
149 | g_object_unref( p ); |
---|
150 | gdk_pixbuf_unref( p ); |
---|
151 | gtk_icon_set_unref (icon_set); |
---|
152 | } |
---|
153 | |
---|
154 | g_object_unref (G_OBJECT (factory)); |
---|
155 | } |
---|
156 | |
---|
157 | static GtkUIManager * myUIManager = NULL; |
---|
158 | |
---|
159 | void |
---|
160 | actions_init( GtkUIManager * ui_manager, gpointer callback_user_data ) |
---|
161 | { |
---|
162 | int i; |
---|
163 | const int n_entries = G_N_ELEMENTS( entries ); |
---|
164 | GtkActionGroup * action_group; |
---|
165 | |
---|
166 | myUIManager = ui_manager; |
---|
167 | |
---|
168 | register_my_icons (); |
---|
169 | |
---|
170 | for( i=0; i<n_entries; ++i ) |
---|
171 | ensure_tooltip (&entries[i]); |
---|
172 | |
---|
173 | action_group = myGroup = gtk_action_group_new( "Actions" ); |
---|
174 | gtk_action_group_set_translation_domain( action_group, NULL ); |
---|
175 | |
---|
176 | gtk_action_group_add_radio_actions( action_group, |
---|
177 | priority_toggle_entries, |
---|
178 | G_N_ELEMENTS(priority_toggle_entries), |
---|
179 | TR_PRI_NORMAL, |
---|
180 | G_CALLBACK(priority_changed_cb), NULL); |
---|
181 | |
---|
182 | gtk_action_group_add_toggle_actions ( action_group, |
---|
183 | show_toggle_entries, |
---|
184 | G_N_ELEMENTS(show_toggle_entries), |
---|
185 | callback_user_data ); |
---|
186 | |
---|
187 | gtk_action_group_add_actions( action_group, |
---|
188 | entries, n_entries, |
---|
189 | callback_user_data ); |
---|
190 | |
---|
191 | gtk_ui_manager_insert_action_group( ui_manager, action_group, 0 ); |
---|
192 | g_object_unref (G_OBJECT(action_group)); |
---|
193 | } |
---|
194 | |
---|
195 | /**** |
---|
196 | ***** |
---|
197 | ****/ |
---|
198 | |
---|
199 | static GHashTable * key_to_action = NULL; |
---|
200 | |
---|
201 | static void |
---|
202 | ensure_action_map_loaded (GtkUIManager * uim) |
---|
203 | { |
---|
204 | GList * l; |
---|
205 | |
---|
206 | if ( key_to_action != NULL ) |
---|
207 | return; |
---|
208 | |
---|
209 | key_to_action = |
---|
210 | g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL); |
---|
211 | |
---|
212 | for( l=gtk_ui_manager_get_action_groups(uim); l!=NULL; l=l->next ) |
---|
213 | { |
---|
214 | GtkActionGroup * action_group = GTK_ACTION_GROUP( l->data ); |
---|
215 | GList *ait, *actions = gtk_action_group_list_actions( action_group ); |
---|
216 | for( ait=actions; ait!=NULL; ait=ait->next ) |
---|
217 | { |
---|
218 | GtkAction * action = GTK_ACTION( ait->data ); |
---|
219 | const char * name = gtk_action_get_name( action ); |
---|
220 | g_hash_table_insert( key_to_action, g_strdup(name), action ); |
---|
221 | } |
---|
222 | g_list_free( actions ); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | static GtkAction* |
---|
227 | get_action( const char* name ) |
---|
228 | { |
---|
229 | ensure_action_map_loaded( myUIManager ); |
---|
230 | return ( GtkAction* ) g_hash_table_lookup( key_to_action, name ); |
---|
231 | } |
---|
232 | |
---|
233 | void |
---|
234 | action_activate ( const char * name ) |
---|
235 | { |
---|
236 | GtkAction * action = get_action( name ); |
---|
237 | g_assert( action != NULL ); |
---|
238 | gtk_action_activate( action ); |
---|
239 | } |
---|
240 | |
---|
241 | void |
---|
242 | action_sensitize( const char * name, gboolean b ) |
---|
243 | { |
---|
244 | GtkAction * action = get_action( name ); |
---|
245 | g_assert( action != NULL ); |
---|
246 | g_object_set( action, "sensitive", b, NULL ); |
---|
247 | } |
---|
248 | |
---|
249 | void |
---|
250 | action_toggle( const char * name, gboolean b ) |
---|
251 | { |
---|
252 | GtkAction * action = get_action( name ); |
---|
253 | gtk_toggle_action_set_active( GTK_TOGGLE_ACTION(action), b ); |
---|
254 | } |
---|
255 | |
---|
256 | GtkWidget* |
---|
257 | action_get_widget( const char * path ) |
---|
258 | { |
---|
259 | return gtk_ui_manager_get_widget( myUIManager, path ); |
---|
260 | } |
---|