1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
4 *
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006-2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009-2014 Pierre Wieser and others (see AUTHORS)
8 *
9 * Nautilus-Actions is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * Nautilus-Actions is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Nautilus-Actions; see the file COPYING. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * Authors:
24 * Frederic Ruaudel <grumz@grumz.net>
25 * Rodrigo Moya <rodrigo@gnome-db.org>
26 * Pierre Wieser <pwieser@trychlos.org>
27 * ... and many others (see AUTHORS)
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <glib/gi18n.h>
35
36 #include <api/na-object-api.h>
37
38 #include "nact-main-tab.h"
39 #include "nact-match-list.h"
40 #include "nact-imimetypes-tab.h"
41
42 /* private interface data
43 */
44 struct _NactIMimetypesTabInterfacePrivate {
45 void *empty; /* so that gcc -pedantic is happy */
46 };
47
48 /* the identifier of this notebook page in the Match dialog
49 */
50 #define ITAB_NAME "mimetypes"
51
52 static guint st_initializations = 0; /* interface initialization count */
53
54 static GType register_type( void );
55 static void interface_base_init( NactIMimetypesTabInterface *klass );
56 static void interface_base_finalize( NactIMimetypesTabInterface *klass );
57
58 static void on_base_initialize_gtk( NactIMimetypesTab *instance, GtkWindow *toplevel, gpointer user_data );
59 static void on_base_initialize_window( NactIMimetypesTab *instance, gpointer user_data );
60
61 static void on_main_selection_changed( NactIMimetypesTab *instance, GList *selected_items, gpointer user_data );
62
63 static GSList *get_mimetypes( void *context );
64 static void set_mimetypes( void *context, GSList *filters );
65
66 static void on_instance_finalized( gpointer user_data, NactIMimetypesTab *instance );
67
68 GType
nact_imimetypes_tab_get_type(void)69 nact_imimetypes_tab_get_type( void )
70 {
71 static GType iface_type = 0;
72
73 if( !iface_type ){
74 iface_type = register_type();
75 }
76
77 return( iface_type );
78 }
79
80 static GType
register_type(void)81 register_type( void )
82 {
83 static const gchar *thisfn = "nact_imimetypes_tab_register_type";
84 GType type;
85
86 static const GTypeInfo info = {
87 sizeof( NactIMimetypesTabInterface ),
88 ( GBaseInitFunc ) interface_base_init,
89 ( GBaseFinalizeFunc ) interface_base_finalize,
90 NULL,
91 NULL,
92 NULL,
93 0,
94 0,
95 NULL
96 };
97
98 g_debug( "%s", thisfn );
99
100 type = g_type_register_static( G_TYPE_INTERFACE, "NactIMimetypesTab", &info, 0 );
101
102 g_type_interface_add_prerequisite( type, BASE_TYPE_WINDOW );
103
104 return( type );
105 }
106
107 static void
interface_base_init(NactIMimetypesTabInterface * klass)108 interface_base_init( NactIMimetypesTabInterface *klass )
109 {
110 static const gchar *thisfn = "nact_imimetypes_tab_interface_base_init";
111
112 if( !st_initializations ){
113
114 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
115
116 klass->private = g_new0( NactIMimetypesTabInterfacePrivate, 1 );
117 }
118
119 st_initializations += 1;
120 }
121
122 static void
interface_base_finalize(NactIMimetypesTabInterface * klass)123 interface_base_finalize( NactIMimetypesTabInterface *klass )
124 {
125 static const gchar *thisfn = "nact_imimetypes_tab_interface_base_finalize";
126
127 st_initializations -= 1;
128
129 if( !st_initializations ){
130
131 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
132
133 g_free( klass->private );
134 }
135 }
136
137 /*
138 * nact_imimetypes_tab_init:
139 * @instance: this #NactIMimetypesTab instance.
140 *
141 * Initialize the interface
142 * Connect to #BaseWindow signals
143 */
144 void
nact_imimetypes_tab_init(NactIMimetypesTab * instance)145 nact_imimetypes_tab_init( NactIMimetypesTab *instance )
146 {
147 static const gchar *thisfn = "nact_imimetypes_tab_init";
148
149 g_return_if_fail( NACT_IS_IMIMETYPES_TAB( instance ));
150
151 g_debug( "%s: instance=%p (%s)",
152 thisfn,
153 ( void * ) instance, G_OBJECT_TYPE_NAME( instance ));
154
155 base_window_signal_connect(
156 BASE_WINDOW( instance ),
157 G_OBJECT( instance ),
158 BASE_SIGNAL_INITIALIZE_GTK,
159 G_CALLBACK( on_base_initialize_gtk ));
160
161 base_window_signal_connect(
162 BASE_WINDOW( instance ),
163 G_OBJECT( instance ),
164 BASE_SIGNAL_INITIALIZE_WINDOW,
165 G_CALLBACK( on_base_initialize_window ));
166
167 nact_main_tab_init( NACT_MAIN_WINDOW( instance ), TAB_MIMETYPES );
168
169 g_object_weak_ref( G_OBJECT( instance ), ( GWeakNotify ) on_instance_finalized, NULL );
170 }
171
172 /*
173 * on_base_initialize_gtk:
174 * @window: this #NactIMimetypesTab instance.
175 *
176 * Initializes the tab widget at initial load.
177 */
178 static void
on_base_initialize_gtk(NactIMimetypesTab * instance,GtkWindow * toplevel,void * user_data)179 on_base_initialize_gtk( NactIMimetypesTab *instance, GtkWindow *toplevel, void *user_data )
180 {
181 static const gchar *thisfn = "nact_imimetypes_tab_on_base_initialize_gtk";
182
183 g_return_if_fail( NACT_IS_IMIMETYPES_TAB( instance ));
184
185 g_debug( "%s: instance=%p (%s), toplevel=%p, user_data=%p",
186 thisfn,
187 ( void * ) instance, G_OBJECT_TYPE_NAME( instance ),
188 ( void * ) toplevel,
189 ( void * ) user_data );
190
191 nact_match_list_init_with_args(
192 BASE_WINDOW( instance ),
193 ITAB_NAME,
194 TAB_MIMETYPES,
195 base_window_get_widget( BASE_WINDOW( instance ), "MimetypesTreeView" ),
196 base_window_get_widget( BASE_WINDOW( instance ), "AddMimetypeButton" ),
197 base_window_get_widget( BASE_WINDOW( instance ), "RemoveMimetypeButton" ),
198 ( pget_filters ) get_mimetypes,
199 ( pset_filters ) set_mimetypes,
200 NULL,
201 NULL,
202 MATCH_LIST_MUST_MATCH_ONE_OF,
203 _( "Mimetype filter" ),
204 TRUE );
205 }
206
207 /*
208 * on_base_initialize_window:
209 * @window: this #NactIMimetypesTab instance.
210 *
211 * Initializes the tab widget at each time the widget will be displayed.
212 * Connect signals and setup runtime values.
213 */
214 static void
on_base_initialize_window(NactIMimetypesTab * instance,void * user_data)215 on_base_initialize_window( NactIMimetypesTab *instance, void *user_data )
216 {
217 static const gchar *thisfn = "nact_imimetypes_tab_on_base_initialize_window";
218
219 g_return_if_fail( NACT_IS_IMIMETYPES_TAB( instance ));
220
221 g_debug( "%s: instance=%p (%s), user_data=%p",
222 thisfn,
223 ( void * ) instance, G_OBJECT_TYPE_NAME( instance ),
224 ( void * ) user_data );
225
226 base_window_signal_connect(
227 BASE_WINDOW( instance ),
228 G_OBJECT( instance ),
229 MAIN_SIGNAL_SELECTION_CHANGED,
230 G_CALLBACK( on_main_selection_changed ));
231 }
232
233 static void
on_main_selection_changed(NactIMimetypesTab * instance,GList * selected_items,gpointer user_data)234 on_main_selection_changed( NactIMimetypesTab *instance, GList *selected_items, gpointer user_data )
235 {
236 NAIContext *context;
237 gboolean editable;
238 gboolean enable_tab;
239
240 g_object_get( G_OBJECT( instance ),
241 MAIN_PROP_CONTEXT, &context, MAIN_PROP_EDITABLE, &editable,
242 NULL );
243
244 enable_tab = ( context != NULL );
245 nact_main_tab_enable_page( NACT_MAIN_WINDOW( instance ), TAB_MIMETYPES, enable_tab );
246 }
247
248 static GSList *
get_mimetypes(void * context)249 get_mimetypes( void *context )
250 {
251 return( na_object_get_mimetypes( context ));
252 }
253
254 static void
set_mimetypes(void * context,GSList * filters)255 set_mimetypes( void *context, GSList *filters )
256 {
257 na_object_set_mimetypes( context, filters );
258 }
259
260 static void
on_instance_finalized(gpointer user_data,NactIMimetypesTab * instance)261 on_instance_finalized( gpointer user_data, NactIMimetypesTab *instance )
262 {
263 static const gchar *thisfn = "nact_imimetypes_tab_on_instance_finalized";
264
265 g_debug( "%s: instance=%p, user_data=%p", thisfn, ( void * ) instance, ( void * ) user_data );
266 }
267