README: add deprecation notice
[nautilus-actions.git] / src / io-gconf / fma-gconf-provider.c
blob56e6dcb503945e3ce9a05233cd0225e68bb153ad
1 /*
2 * FileManager-Actions
3 * A file-manager extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006-2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009-2015 Pierre Wieser and others (see AUTHORS)
9 * FileManager-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.
14 * FileManager-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.
19 * You should have received a copy of the GNU General Public License
20 * along with FileManager-Actions; see the file COPYING. If not, see
21 * <http://www.gnu.org/licenses/>.
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)
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
34 #include <glib/gi18n.h>
35 #include <string.h>
37 #include <api/fma-ifactory-provider.h>
38 #include <api/fma-iio-provider.h>
39 #include <api/fma-gconf-monitor.h>
41 #include "fma-gconf-provider.h"
42 #include "fma-gconf-reader.h"
43 #include "fma-gconf-writer.h"
44 #include "fma-gconf-keys.h"
46 /* private class data
48 struct _FMAGConfProviderClassPrivate {
49 void *empty; /* so that gcc -pedantic is happy */
52 static GType st_module_type = 0;
53 static GObjectClass *st_parent_class = NULL;
55 #ifdef FMA_ENABLE_DEPRECATED
56 static gint st_burst_timeout = 100; /* burst timeout in msec */
57 #endif
59 static void class_init( FMAGConfProviderClass *klass );
60 static void instance_init( GTypeInstance *instance, gpointer klass );
61 static void instance_dispose( GObject *object );
62 static void instance_finalize( GObject *object );
64 static void iio_provider_iface_init( FMAIIOProviderInterface *iface );
65 static gchar *iio_provider_get_id( const FMAIIOProvider *provider );
66 static gchar *iio_provider_get_name( const FMAIIOProvider *provider );
67 static guint iio_provider_get_version( const FMAIIOProvider *provider );
69 static void ifactory_provider_iface_init( FMAIFactoryProviderInterface *iface );
70 static guint ifactory_provider_get_version( const FMAIFactoryProvider *provider );
72 #ifdef FMA_ENABLE_DEPRECATED
73 static GList *install_monitors( FMAGConfProvider *provider );
74 static void config_path_changed_cb( GConfClient *client, guint cnxn_id, GConfEntry *entry, FMAGConfProvider *provider );
75 static gboolean config_path_changed_trigger_interface( FMAGConfProvider *provider );
76 static gulong time_val_diff( const GTimeVal *recent, const GTimeVal *old );
77 #endif
79 GType
80 fma_gconf_provider_get_type( void )
82 return( st_module_type );
85 void
86 fma_gconf_provider_register_type( GTypeModule *module )
88 static const gchar *thisfn = "fma_gconf_provider_register_type";
90 static GTypeInfo info = {
91 sizeof( FMAGConfProviderClass ),
92 NULL,
93 NULL,
94 ( GClassInitFunc ) class_init,
95 NULL,
96 NULL,
97 sizeof( FMAGConfProvider ),
99 ( GInstanceInitFunc ) instance_init
102 static const GInterfaceInfo iio_provider_iface_info = {
103 ( GInterfaceInitFunc ) iio_provider_iface_init,
104 NULL,
105 NULL
108 static const GInterfaceInfo ifactory_provider_iface_info = {
109 ( GInterfaceInitFunc ) ifactory_provider_iface_init,
110 NULL,
111 NULL
114 g_debug( "%s", thisfn );
116 st_module_type = g_type_module_register_type( module, G_TYPE_OBJECT, "FMAGConfProvider", &info, 0 );
118 g_type_module_add_interface( module, st_module_type, FMA_TYPE_IIO_PROVIDER, &iio_provider_iface_info );
120 g_type_module_add_interface( module, st_module_type, FMA_TYPE_IFACTORY_PROVIDER, &ifactory_provider_iface_info );
123 static void
124 class_init( FMAGConfProviderClass *klass )
126 static const gchar *thisfn = "fma_gconf_provider_class_init";
127 GObjectClass *object_class;
129 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
131 st_parent_class = g_type_class_peek_parent( klass );
133 object_class = G_OBJECT_CLASS( klass );
134 object_class->dispose = instance_dispose;
135 object_class->finalize = instance_finalize;
137 klass->private = g_new0( FMAGConfProviderClassPrivate, 1 );
140 static void
141 instance_init( GTypeInstance *instance, gpointer klass )
143 static const gchar *thisfn = "fma_gconf_provider_instance_init";
144 FMAGConfProvider *self;
146 g_return_if_fail( FMA_IS_GCONF_PROVIDER( instance ));
148 g_debug( "%s: instance=%p (%s), klass=%p",
149 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
151 self = FMA_GCONF_PROVIDER( instance );
153 self->private = g_new0( FMAGConfProviderPrivate, 1 );
155 self->private->dispose_has_run = FALSE;
157 self->private->gconf = gconf_client_get_default();
159 #ifdef FMA_ENABLE_DEPRECATED
160 self->private->monitors = install_monitors( self );
161 #endif
164 static void
165 instance_dispose( GObject *object )
167 static const gchar *thisfn = "fma_gconf_provider_instance_dispose";
168 FMAGConfProvider *self;
170 g_return_if_fail( FMA_IS_GCONF_PROVIDER( object ));
172 self = FMA_GCONF_PROVIDER( object );
174 if( !self->private->dispose_has_run ){
176 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
178 self->private->dispose_has_run = TRUE;
180 #ifdef FMA_ENABLE_DEPRECATED
181 /* release the GConf monitoring */
182 fma_gconf_monitor_release_monitors( self->private->monitors );
183 #endif
185 /* release the GConf connexion */
186 g_object_unref( self->private->gconf );
188 /* chain up to the parent class */
189 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
190 G_OBJECT_CLASS( st_parent_class )->dispose( object );
195 static void
196 instance_finalize( GObject *object )
198 static const gchar *thisfn =" fma_gconf_provider_instance_finalize";
199 FMAGConfProvider *self;
201 g_return_if_fail( FMA_IS_GCONF_PROVIDER( object ));
203 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
205 self = FMA_GCONF_PROVIDER( object );
207 g_free( self->private );
209 /* chain call to parent class */
210 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
211 G_OBJECT_CLASS( st_parent_class )->finalize( object );
215 static void
216 iio_provider_iface_init( FMAIIOProviderInterface *iface )
218 static const gchar *thisfn = "fma_gconf_provider_iio_provider_iface_init";
220 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
222 iface->get_id = iio_provider_get_id;
223 iface->get_name = iio_provider_get_name;
224 iface->get_version = iio_provider_get_version;
225 iface->read_items = fma_gconf_reader_iio_provider_read_items;
226 iface->is_willing_to_write = fma_gconf_writer_iio_provider_is_willing_to_write;
227 iface->is_able_to_write = fma_gconf_writer_iio_provider_is_able_to_write;
228 #ifdef FMA_ENABLE_DEPRECATED
229 iface->write_item = fma_gconf_writer_iio_provider_write_item;
230 iface->delete_item = fma_gconf_writer_iio_provider_delete_item;
231 #else
232 iface->write_item = NULL;
233 iface->delete_item = NULL;
234 #endif
235 iface->duplicate_data = NULL;
238 static gchar *
239 iio_provider_get_id( const FMAIIOProvider *provider )
241 return( g_strdup( "fma-gconf" ));
244 static gchar *
245 iio_provider_get_name( const FMAIIOProvider *provider )
247 return( g_strdup( _( "FileManager-Actions GConf I/O Provider" )));
250 static guint
251 iio_provider_get_version( const FMAIIOProvider *provider )
253 return( 1 );
256 static void
257 ifactory_provider_iface_init( FMAIFactoryProviderInterface *iface )
259 static const gchar *thisfn = "fma_gconf_provider_ifactory_provider_iface_init";
261 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
263 iface->get_version = ifactory_provider_get_version;
264 iface->read_start = fma_gconf_reader_read_start;
265 iface->read_data = fma_gconf_reader_read_data;
266 iface->read_done = fma_gconf_reader_read_done;
267 #ifdef FMA_ENABLE_DEPRECATED
268 iface->write_start = fma_gconf_writer_write_start;
269 iface->write_data = fma_gconf_writer_write_data;
270 iface->write_done = fma_gconf_writer_write_done;
271 #else
272 iface->write_start = NULL;
273 iface->write_data = NULL;
274 iface->write_done = NULL;
275 #endif
278 static guint
279 ifactory_provider_get_version( const FMAIFactoryProvider *provider )
281 return( 1 );
284 #ifdef FMA_ENABLE_DEPRECATED
285 static GList *
286 install_monitors( FMAGConfProvider *provider )
288 GList *list = NULL;
290 g_return_val_if_fail( FMA_IS_GCONF_PROVIDER( provider ), NULL );
291 g_return_val_if_fail( FMA_IS_IIO_PROVIDER( provider ), NULL );
292 g_return_val_if_fail( !provider->private->dispose_has_run, NULL );
294 /* monitor the configurations/ directory which contains all menus,
295 * actions and profiles definitions
297 list = g_list_prepend( list,
298 fma_gconf_monitor_new(
299 FMA_GCONF_CONFIGURATIONS_PATH,
300 ( GConfClientNotifyFunc ) config_path_changed_cb,
301 provider ));
303 list = g_list_prepend( list,
304 fma_gconf_monitor_new(
305 FMA_GCONF_SCHEMAS_PATH,
306 ( GConfClientNotifyFunc ) config_path_changed_cb,
307 provider ));
309 return( list );
313 * this callback is triggered each time a value is changed under our
314 * configurations/ directory ; as each object has several entries which
315 * describe it, this callback is triggered several times for each object
316 * update
318 * up to and including 1.10.1, the user interface took care of writing
319 * a special key in GConf at the end of each update operations ;
320 * as GConf monitored only this special key, it triggered this callback
321 * once for each global update operation
323 * this special key was of the form xxx:yyyyyyyy-yyyy-yyyy-..., where :
324 * xxx was a sequential number (inside of the ui session)
325 * yyyyyyyy-yyyy-yyyy-... was the uuid of the involved action
327 * this was a sort of hack which simplified a lot the notification
328 * system, but didn't take into account any modification which might
329 * come from outside of the ui
331 * if the modification is made elsewhere (an action is imported as a
332 * xml file in gconf, or gconf is directly edited), we'd have to rely
333 * only on the standard monitor (GConf watch) mechanism
335 * this is what we do below, in three phases:
336 * - first, GConf underlying subsystem advertises us, through the watch
337 * mechanism, of each and every modification ; this leads us to be
338 * triggered for each new/modified/deleted _entry_
339 * - as we want trigger the FMAIIOProvider interface only once for each
340 * update operation (i.e. once for each flow of individual notifications),
341 * then we install a timer in order to wait for all
342 * entries have been modified
343 * - when a [burst_timeout] reasonable delay has elapsed without having
344 * received any new individual notification, then we can assume that
345 * we have reached the end of the flow and that we can now trigger
346 * the FMAIIOProvider interface
348 * Note that we used to try to send one notification per modified object.
349 * This cannot work as we are not sure at all that we will received
350 * individual notifications themselves grouped by object.
352 static void
353 config_path_changed_cb( GConfClient *client, guint cnxn_id, GConfEntry *entry, FMAGConfProvider *provider )
355 g_return_if_fail( FMA_IS_GCONF_PROVIDER( provider ));
356 g_return_if_fail( FMA_IS_IIO_PROVIDER( provider ));
358 if( !provider->private->dispose_has_run ){
360 g_get_current_time( &provider->private->last_event );
362 if( !provider->private->event_source_id ){
363 provider->private->event_source_id =
364 g_timeout_add(
365 st_burst_timeout,
366 ( GSourceFunc ) config_path_changed_trigger_interface,
367 provider );
373 * this timer is set when we receive the first event of a serie
374 * we continue to loop until last event is older that the st_burst_timeout
375 * delay (in msec)
376 * there is no race condition here as we are not multithreaded
377 * or .. is there ?
379 static gboolean
380 config_path_changed_trigger_interface( FMAGConfProvider *provider )
382 static const gchar *thisfn = "fma_gconf_provider_config_path_changed_trigger_interface";
383 GTimeVal now;
384 gulong diff;
385 gulong timeout_usec = 1000*st_burst_timeout;
387 g_get_current_time( &now );
388 diff = time_val_diff( &now, &provider->private->last_event );
389 if( diff < timeout_usec ){
390 return( TRUE );
393 /* last individual notification is older that the st_burst_timeout
394 * so triggers the FMAIIOProvider interface and destroys this timeout
396 g_debug( "%s: triggering FMAIIOProvider interface for provider=%p (%s)",
397 thisfn, ( void * ) provider, G_OBJECT_TYPE_NAME( provider ));
399 fma_iio_provider_item_changed( FMA_IIO_PROVIDER( provider ));
400 provider->private->event_source_id = 0;
401 return( FALSE );
405 * returns the difference in microseconds.
407 static gulong
408 time_val_diff( const GTimeVal *recent, const GTimeVal *old )
410 gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
411 microsec += recent->tv_usec - old->tv_usec;
412 return( microsec );
414 #endif /* FMA_ENABLE_DEPRECATED */