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