source: trunk/gtk/actions.c @ 5994

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

#981: add tr_stat.dateAdded to libT; add "sort by age" to gtk+ client

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