Define new 'pivot-prop-loadable' property
[nautilus-actions.git] / src / io-gconf / nagp-gconf-provider.c
blobc15f8ca7ff825012710598ea88bf6d0f4b937148
1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program 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.
14 * This Program 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
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <glib/gi18n.h>
36 #include <string.h>
38 #include <api/na-ifactory-provider.h>
39 #include <api/na-iio-provider.h>
40 #include <api/na-gconf-monitor.h>
42 #include "nagp-gconf-provider.h"
43 #include "nagp-reader.h"
44 #include "nagp-writer.h"
45 #include "nagp-keys.h"
47 /* private class data
49 struct _NagpGConfProviderClassPrivate {
50 void *empty; /* so that gcc -pedantic is happy */
53 static GType st_module_type = 0;
54 static GObjectClass *st_parent_class = NULL;
56 #ifndef NA_DISABLE_DEPRECATED
57 static gint st_burst_timeout = 100; /* burst timeout in msec */
58 #endif
60 static void class_init( NagpGConfProviderClass *klass );
61 static void instance_init( GTypeInstance *instance, gpointer klass );
62 static void instance_dispose( GObject *object );
63 static void instance_finalize( GObject *object );
65 static void iio_provider_iface_init( NAIIOProviderInterface *iface );
66 static gchar *iio_provider_get_id( const NAIIOProvider *provider );
67 static gchar *iio_provider_get_name( const NAIIOProvider *provider );
68 static guint iio_provider_get_version( const NAIIOProvider *provider );
70 static void ifactory_provider_iface_init( NAIFactoryProviderInterface *iface );
71 static guint ifactory_provider_get_version( const NAIFactoryProvider *provider );
73 #ifndef NA_DISABLE_DEPRECATED
74 static GList *install_monitors( NagpGConfProvider *provider );
75 static void config_path_changed_cb( GConfClient *client, guint cnxn_id, GConfEntry *entry, NagpGConfProvider *provider );
76 static gboolean config_path_changed_trigger_interface( NagpGConfProvider *provider );
77 static gulong time_val_diff( const GTimeVal *recent, const GTimeVal *old );
78 #endif
80 GType
81 nagp_gconf_provider_get_type( void )
83 return( st_module_type );
86 void
87 nagp_gconf_provider_register_type( GTypeModule *module )
89 static const gchar *thisfn = "nagp_gconf_provider_register_type";
91 static GTypeInfo info = {
92 sizeof( NagpGConfProviderClass ),
93 NULL,
94 NULL,
95 ( GClassInitFunc ) class_init,
96 NULL,
97 NULL,
98 sizeof( NagpGConfProvider ),
100 ( GInstanceInitFunc ) instance_init
103 static const GInterfaceInfo iio_provider_iface_info = {
104 ( GInterfaceInitFunc ) iio_provider_iface_init,
105 NULL,
106 NULL
109 static const GInterfaceInfo ifactory_provider_iface_info = {
110 ( GInterfaceInitFunc ) ifactory_provider_iface_init,
111 NULL,
112 NULL
115 g_debug( "%s", thisfn );
117 st_module_type = g_type_module_register_type( module, G_TYPE_OBJECT, "NagpGConfProvider", &info, 0 );
119 g_type_module_add_interface( module, st_module_type, NA_IIO_PROVIDER_TYPE, &iio_provider_iface_info );
121 g_type_module_add_interface( module, st_module_type, NA_IFACTORY_PROVIDER_TYPE, &ifactory_provider_iface_info );
124 static void
125 class_init( NagpGConfProviderClass *klass )
127 static const gchar *thisfn = "nagp_gconf_provider_class_init";
128 GObjectClass *object_class;
130 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
132 st_parent_class = g_type_class_peek_parent( klass );
134 object_class = G_OBJECT_CLASS( klass );
135 object_class->dispose = instance_dispose;
136 object_class->finalize = instance_finalize;
138 klass->private = g_new0( NagpGConfProviderClassPrivate, 1 );
141 static void
142 instance_init( GTypeInstance *instance, gpointer klass )
144 static const gchar *thisfn = "nagp_gconf_provider_instance_init";
145 NagpGConfProvider *self;
147 g_return_if_fail( NAGP_IS_GCONF_PROVIDER( instance ));
149 g_debug( "%s: instance=%p (%s), klass=%p",
150 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
152 self = NAGP_GCONF_PROVIDER( instance );
154 self->private = g_new0( NagpGConfProviderPrivate, 1 );
156 self->private->dispose_has_run = FALSE;
158 self->private->gconf = gconf_client_get_default();
160 #ifndef NA_DISABLE_DEPRECATED
161 self->private->monitors = install_monitors( self );
162 #endif
165 static void
166 instance_dispose( GObject *object )
168 static const gchar *thisfn = "nagp_gconf_provider_instance_dispose";
169 NagpGConfProvider *self;
171 g_return_if_fail( NAGP_IS_GCONF_PROVIDER( object ));
173 self = NAGP_GCONF_PROVIDER( object );
175 if( !self->private->dispose_has_run ){
177 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
179 self->private->dispose_has_run = TRUE;
181 #ifndef NA_DISABLE_DEPRECATED
182 /* release the GConf monitoring */
183 na_gconf_monitor_release_monitors( self->private->monitors );
184 #endif
186 /* release the GConf connexion */
187 g_object_unref( self->private->gconf );
189 /* chain up to the parent class */
190 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
191 G_OBJECT_CLASS( st_parent_class )->dispose( object );
196 static void
197 instance_finalize( GObject *object )
199 static const gchar *thisfn =" nagp_gconf_provider_instance_finalize";
200 NagpGConfProvider *self;
202 g_return_if_fail( NAGP_IS_GCONF_PROVIDER( object ));
204 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
206 self = NAGP_GCONF_PROVIDER( object );
208 g_free( self->private );
210 /* chain call to parent class */
211 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
212 G_OBJECT_CLASS( st_parent_class )->finalize( object );
216 static void
217 iio_provider_iface_init( NAIIOProviderInterface *iface )
219 static const gchar *thisfn = "nagp_gconf_provider_iio_provider_iface_init";
221 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
223 iface->get_id = iio_provider_get_id;
224 iface->get_name = iio_provider_get_name;
225 iface->get_version = iio_provider_get_version;
226 iface->read_items = nagp_iio_provider_read_items;
227 iface->is_willing_to_write = nagp_iio_provider_is_willing_to_write;
228 iface->is_able_to_write = nagp_iio_provider_is_able_to_write;
229 #ifndef NA_DISABLE_DEPRECATED
230 iface->write_item = nagp_iio_provider_write_item;
231 iface->delete_item = nagp_iio_provider_delete_item;
232 #else
233 iface->write_item = NULL;
234 iface->delete_item = NULL;
235 #endif
236 iface->duplicate_data = NULL;
239 static gchar *
240 iio_provider_get_id( const NAIIOProvider *provider )
242 return( g_strdup( "na-gconf" ));
245 static gchar *
246 iio_provider_get_name( const NAIIOProvider *provider )
248 return( g_strdup( _( "Nautilus-Actions GConf I/O Provider" )));
251 static guint
252 iio_provider_get_version( const NAIIOProvider *provider )
254 return( 1 );
257 static void
258 ifactory_provider_iface_init( NAIFactoryProviderInterface *iface )
260 static const gchar *thisfn = "nagp_gconf_provider_ifactory_provider_iface_init";
262 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
264 iface->get_version = ifactory_provider_get_version;
265 iface->read_start = nagp_reader_read_start;
266 iface->read_data = nagp_reader_read_data;
267 iface->read_done = nagp_reader_read_done;
268 #ifndef NA_DISABLE_DEPRECATED
269 iface->write_start = nagp_writer_write_start;
270 iface->write_data = nagp_writer_write_data;
271 iface->write_done = nagp_writer_write_done;
272 #else
273 iface->write_start = NULL;
274 iface->write_data = NULL;
275 iface->write_done = NULL;
276 #endif
279 static guint
280 ifactory_provider_get_version( const NAIFactoryProvider *provider )
282 return( 1 );
285 #ifndef NA_DISABLE_DEPRECATED
286 static GList *
287 install_monitors( NagpGConfProvider *provider )
289 GList *list = NULL;
291 g_return_val_if_fail( NAGP_IS_GCONF_PROVIDER( provider ), NULL );
292 g_return_val_if_fail( NA_IS_IIO_PROVIDER( provider ), NULL );
293 g_return_val_if_fail( !provider->private->dispose_has_run, NULL );
295 /* monitor the configurations/ directory which contains all menus,
296 * actions and profiles definitions
298 list = g_list_prepend( list,
299 na_gconf_monitor_new(
300 NAGP_CONFIGURATIONS_PATH,
301 ( GConfClientNotifyFunc ) config_path_changed_cb,
302 provider ));
304 list = g_list_prepend( list,
305 na_gconf_monitor_new(
306 NAGP_SCHEMAS_PATH,
307 ( GConfClientNotifyFunc ) config_path_changed_cb,
308 provider ));
310 return( list );
314 * this callback is triggered each time a value is changed under our
315 * configurations/ directory ; as each object has several entries which
316 * describe it, this callback is triggered several times for each object
317 * update
319 * up to and including 1.10.1, the user interface took care of writing
320 * a special key in GConf at the end of each update operations ;
321 * as GConf monitored only this special key, it triggered this callback
322 * once for each global update operation
324 * this special key was of the form xxx:yyyyyyyy-yyyy-yyyy-..., where :
325 * xxx was a sequential number (inside of the ui session)
326 * yyyyyyyy-yyyy-yyyy-... was the uuid of the involved action
328 * this was a sort of hack which simplified a lot the notification
329 * system, but didn't take into account any modification which might
330 * come from outside of the ui
332 * if the modification is made elsewhere (an action is imported as a
333 * xml file in gconf, or gconf is directly edited), we'd have to rely
334 * only on the standard monitor (GConf watch) mechanism
336 * this is what we do below, in three phases:
337 * - first, GConf underlying subsystem advertises us, through the watch
338 * mechanism, of each and every modification ; this leads us to be
339 * triggered for each new/modified/deleted _entry_
340 * - as we want trigger the NAIIOProvider interface only once for each
341 * update operation (i.e. once for each flow of individual notifications),
342 * then we install a timer in order to wait for all
343 * entries have been modified
344 * - when a [burst_timeout] reasonable delay has elapsed without having
345 * received any new individual notification, then we can assume that
346 * we have reached the end of the flow and that we can now trigger
347 * the NAIIOProvider interface
349 * Note that we used to try to send one notification per modified object.
350 * This cannot work as we are not sure at all that we will received
351 * individual notifications themselves grouped by object.
353 static void
354 config_path_changed_cb( GConfClient *client, guint cnxn_id, GConfEntry *entry, NagpGConfProvider *provider )
356 g_return_if_fail( NAGP_IS_GCONF_PROVIDER( provider ));
357 g_return_if_fail( NA_IS_IIO_PROVIDER( provider ));
359 if( !provider->private->dispose_has_run ){
361 g_get_current_time( &provider->private->last_event );
363 if( !provider->private->event_source_id ){
364 provider->private->event_source_id =
365 g_timeout_add(
366 st_burst_timeout,
367 ( GSourceFunc ) config_path_changed_trigger_interface,
368 provider );
374 * this timer is set when we receive the first event of a serie
375 * we continue to loop until last event is older that the st_burst_timeout
376 * delay (in msec)
377 * there is no race condition here as we are not multithreaded
378 * or .. is there ?
380 static gboolean
381 config_path_changed_trigger_interface( NagpGConfProvider *provider )
383 static const gchar *thisfn = "nagp_gconf_provider_config_path_changed_trigger_interface";
384 GTimeVal now;
385 gulong diff;
386 gulong timeout_usec = 1000*st_burst_timeout;
388 g_get_current_time( &now );
389 diff = time_val_diff( &now, &provider->private->last_event );
390 if( diff < timeout_usec ){
391 return( TRUE );
394 /* last individual notification is older that the st_burst_timeout
395 * so triggers the NAIIOProvider interface and destroys this timeout
397 g_debug( "%s: triggering NAIIOProvider interface for provider=%p (%s)",
398 thisfn, ( void * ) provider, G_OBJECT_TYPE_NAME( provider ));
400 na_iio_provider_item_changed( NA_IIO_PROVIDER( provider ));
401 provider->private->event_source_id = 0;
402 return( FALSE );
406 * returns the difference in microseconds.
408 static gulong
409 time_val_diff( const GTimeVal *recent, const GTimeVal *old )
411 gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
412 microsec += recent->tv_usec - old->tv_usec;
413 return( microsec );
415 #endif /* NA_DISABLE_DEPRECATED */