File: fma-gconf-provider.c

package info (click to toggle)
filemanager-actions 3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,428 kB
  • sloc: ansic: 48,857; xml: 17,767; sh: 4,662; makefile: 1,375; lisp: 9
file content (414 lines) | stat: -rw-r--r-- 12,907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * FileManager-Actions
 * A file-manager extension which offers configurable context menu actions.
 *
 * Copyright (C) 2005 The GNOME Foundation
 * Copyright (C) 2006-2008 Frederic Ruaudel and others (see AUTHORS)
 * Copyright (C) 2009-2015 Pierre Wieser and others (see AUTHORS)
 *
 * FileManager-Actions is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * FileManager-Actions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FileManager-Actions; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Frederic Ruaudel <grumz@grumz.net>
 *   Rodrigo Moya <rodrigo@gnome-db.org>
 *   Pierre Wieser <pwieser@trychlos.org>
 *   ... and many others (see AUTHORS)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>
#include <string.h>

#include <api/fma-ifactory-provider.h>
#include <api/fma-iio-provider.h>
#include <api/fma-gconf-monitor.h>

#include "fma-gconf-provider.h"
#include "fma-gconf-reader.h"
#include "fma-gconf-writer.h"
#include "fma-gconf-keys.h"

/* private class data
 */
struct _FMAGConfProviderClassPrivate {
	void *empty;						/* so that gcc -pedantic is happy */
};

static GType         st_module_type = 0;
static GObjectClass *st_parent_class = NULL;

#ifdef FMA_ENABLE_DEPRECATED
static gint          st_burst_timeout = 100;		/* burst timeout in msec */
#endif

static void     class_init( FMAGConfProviderClass *klass );
static void     instance_init( GTypeInstance *instance, gpointer klass );
static void     instance_dispose( GObject *object );
static void     instance_finalize( GObject *object );

static void     iio_provider_iface_init( FMAIIOProviderInterface *iface );
static gchar   *iio_provider_get_id( const FMAIIOProvider *provider );
static gchar   *iio_provider_get_name( const FMAIIOProvider *provider );
static guint    iio_provider_get_version( const FMAIIOProvider *provider );

static void     ifactory_provider_iface_init( FMAIFactoryProviderInterface *iface );
static guint    ifactory_provider_get_version( const FMAIFactoryProvider *provider );

#ifdef FMA_ENABLE_DEPRECATED
static GList   *install_monitors( FMAGConfProvider *provider );
static void     config_path_changed_cb( GConfClient *client, guint cnxn_id, GConfEntry *entry, FMAGConfProvider *provider );
static gboolean config_path_changed_trigger_interface( FMAGConfProvider *provider );
static gulong   time_val_diff( const GTimeVal *recent, const GTimeVal *old );
#endif

GType
fma_gconf_provider_get_type( void )
{
	return( st_module_type );
}

void
fma_gconf_provider_register_type( GTypeModule *module )
{
	static const gchar *thisfn = "fma_gconf_provider_register_type";

	static GTypeInfo info = {
		sizeof( FMAGConfProviderClass ),
		NULL,
		NULL,
		( GClassInitFunc ) class_init,
		NULL,
		NULL,
		sizeof( FMAGConfProvider ),
		0,
		( GInstanceInitFunc ) instance_init
	};

	static const GInterfaceInfo iio_provider_iface_info = {
		( GInterfaceInitFunc ) iio_provider_iface_init,
		NULL,
		NULL
	};

	static const GInterfaceInfo ifactory_provider_iface_info = {
		( GInterfaceInitFunc ) ifactory_provider_iface_init,
		NULL,
		NULL
	};

	g_debug( "%s", thisfn );

	st_module_type = g_type_module_register_type( module, G_TYPE_OBJECT, "FMAGConfProvider", &info, 0 );

	g_type_module_add_interface( module, st_module_type, FMA_TYPE_IIO_PROVIDER, &iio_provider_iface_info );

	g_type_module_add_interface( module, st_module_type, FMA_TYPE_IFACTORY_PROVIDER, &ifactory_provider_iface_info );
}

static void
class_init( FMAGConfProviderClass *klass )
{
	static const gchar *thisfn = "fma_gconf_provider_class_init";
	GObjectClass *object_class;

	g_debug( "%s: klass=%p", thisfn, ( void * ) klass );

	st_parent_class = g_type_class_peek_parent( klass );

	object_class = G_OBJECT_CLASS( klass );
	object_class->dispose = instance_dispose;
	object_class->finalize = instance_finalize;

	klass->private = g_new0( FMAGConfProviderClassPrivate, 1 );
}

static void
instance_init( GTypeInstance *instance, gpointer klass )
{
	static const gchar *thisfn = "fma_gconf_provider_instance_init";
	FMAGConfProvider *self;

	g_return_if_fail( FMA_IS_GCONF_PROVIDER( instance ));

	g_debug( "%s: instance=%p (%s), klass=%p",
			thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );

	self = FMA_GCONF_PROVIDER( instance );

	self->private = g_new0( FMAGConfProviderPrivate, 1 );

	self->private->dispose_has_run = FALSE;

	self->private->gconf = gconf_client_get_default();

#ifdef FMA_ENABLE_DEPRECATED
	self->private->monitors = install_monitors( self );
#endif
}

static void
instance_dispose( GObject *object )
{
	static const gchar *thisfn = "fma_gconf_provider_instance_dispose";
	FMAGConfProvider *self;

	g_return_if_fail( FMA_IS_GCONF_PROVIDER( object ));

	self = FMA_GCONF_PROVIDER( object );

	if( !self->private->dispose_has_run ){

		g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));

		self->private->dispose_has_run = TRUE;

#ifdef FMA_ENABLE_DEPRECATED
		/* release the GConf monitoring */
		fma_gconf_monitor_release_monitors( self->private->monitors );
#endif

		/* release the GConf connexion */
		g_object_unref( self->private->gconf );

		/* chain up to the parent class */
		if( G_OBJECT_CLASS( st_parent_class )->dispose ){
			G_OBJECT_CLASS( st_parent_class )->dispose( object );
		}
	}
}

static void
instance_finalize( GObject *object )
{
	static const gchar *thisfn =" fma_gconf_provider_instance_finalize";
	FMAGConfProvider *self;

	g_return_if_fail( FMA_IS_GCONF_PROVIDER( object ));

	g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));

	self = FMA_GCONF_PROVIDER( object );

	g_free( self->private );

	/* chain call to parent class */
	if( G_OBJECT_CLASS( st_parent_class )->finalize ){
		G_OBJECT_CLASS( st_parent_class )->finalize( object );
	}
}

static void
iio_provider_iface_init( FMAIIOProviderInterface *iface )
{
	static const gchar *thisfn = "fma_gconf_provider_iio_provider_iface_init";

	g_debug( "%s: iface=%p", thisfn, ( void * ) iface );

	iface->get_id = iio_provider_get_id;
	iface->get_name = iio_provider_get_name;
	iface->get_version = iio_provider_get_version;
	iface->read_items = fma_gconf_reader_iio_provider_read_items;
	iface->is_willing_to_write = fma_gconf_writer_iio_provider_is_willing_to_write;
	iface->is_able_to_write = fma_gconf_writer_iio_provider_is_able_to_write;
#ifdef FMA_ENABLE_DEPRECATED
	iface->write_item = fma_gconf_writer_iio_provider_write_item;
	iface->delete_item = fma_gconf_writer_iio_provider_delete_item;
#else
	iface->write_item = NULL;
	iface->delete_item = NULL;
#endif
	iface->duplicate_data = NULL;
}

static gchar *
iio_provider_get_id( const FMAIIOProvider *provider )
{
	return( g_strdup( "fma-gconf" ));
}

static gchar *
iio_provider_get_name( const FMAIIOProvider *provider )
{
	return( g_strdup( _( "FileManager-Actions GConf I/O Provider" )));
}

static guint
iio_provider_get_version( const FMAIIOProvider *provider )
{
	return( 1 );
}

static void
ifactory_provider_iface_init( FMAIFactoryProviderInterface *iface )
{
	static const gchar *thisfn = "fma_gconf_provider_ifactory_provider_iface_init";

	g_debug( "%s: iface=%p", thisfn, ( void * ) iface );

	iface->get_version = ifactory_provider_get_version;
	iface->read_start = fma_gconf_reader_read_start;
	iface->read_data = fma_gconf_reader_read_data;
	iface->read_done = fma_gconf_reader_read_done;
#ifdef FMA_ENABLE_DEPRECATED
	iface->write_start = fma_gconf_writer_write_start;
	iface->write_data = fma_gconf_writer_write_data;
	iface->write_done = fma_gconf_writer_write_done;
#else
	iface->write_start = NULL;
	iface->write_data = NULL;
	iface->write_done = NULL;
#endif
}

static guint
ifactory_provider_get_version( const FMAIFactoryProvider *provider )
{
	return( 1 );
}

#ifdef FMA_ENABLE_DEPRECATED
static GList *
install_monitors( FMAGConfProvider *provider )
{
	GList *list = NULL;

	g_return_val_if_fail( FMA_IS_GCONF_PROVIDER( provider ), NULL );
	g_return_val_if_fail( FMA_IS_IIO_PROVIDER( provider ), NULL );
	g_return_val_if_fail( !provider->private->dispose_has_run, NULL );

	/* monitor the configurations/ directory which contains all menus,
	 * actions and profiles definitions
	 */
	list = g_list_prepend( list,
			fma_gconf_monitor_new(
					FMA_GCONF_CONFIGURATIONS_PATH,
					( GConfClientNotifyFunc ) config_path_changed_cb,
					provider ));

	list = g_list_prepend( list,
			fma_gconf_monitor_new(
					FMA_GCONF_SCHEMAS_PATH,
					( GConfClientNotifyFunc ) config_path_changed_cb,
					provider ));

	return( list );
}

/*
 * this callback is triggered each time a value is changed under our
 * configurations/ directory ; as each object has several entries which
 * describe it, this callback is triggered several times for each object
 * update
 *
 * up to and including 1.10.1, the user interface took care of writing
 * a special key in GConf at the end of each update operations ;
 * as GConf monitored only this special key, it triggered this callback
 * once for each global update operation
 *
 * this special key was of the form xxx:yyyyyyyy-yyyy-yyyy-..., where :
 *    xxx was a sequential number (inside of the ui session)
 *    yyyyyyyy-yyyy-yyyy-... was the uuid of the involved action
 *
 * this was a sort of hack which simplified a lot the notification
 * system, but didn't take into account any modification which might
 * come from outside of the ui
 *
 * if the modification is made elsewhere (an action is imported as a
 * xml file in gconf, or gconf is directly edited), we'd have to rely
 * only on the standard monitor (GConf watch) mechanism
 *
 * this is what we do below, in three phases:
 * - first, GConf underlying subsystem advertises us, through the watch
 *   mechanism, of each and every modification ; this leads us to be
 *   triggered for each new/modified/deleted _entry_
 * - as we want trigger the FMAIIOProvider interface only once for each
 *   update operation (i.e. once for each flow of individual notifications),
 *   then we install a timer in order to wait for all
 *   entries have been modified
 * - when a [burst_timeout] reasonable delay has elapsed without having
 *   received any new individual notification, then we can assume that
 *   we have reached the end of the flow and that we can now trigger
 *   the FMAIIOProvider interface
 *
 * Note that we used to try to send one notification per modified object.
 * This cannot work as we are not sure at all that we will received
 * individual notifications themselves grouped by object.
 */
static void
config_path_changed_cb( GConfClient *client, guint cnxn_id, GConfEntry *entry, FMAGConfProvider *provider )
{
	g_return_if_fail( FMA_IS_GCONF_PROVIDER( provider ));
	g_return_if_fail( FMA_IS_IIO_PROVIDER( provider ));

	if( !provider->private->dispose_has_run ){

		g_get_current_time( &provider->private->last_event );

		if( !provider->private->event_source_id ){
			provider->private->event_source_id =
				g_timeout_add(
						st_burst_timeout,
						( GSourceFunc ) config_path_changed_trigger_interface,
						provider );
		}
	}
}

/*
 * this timer is set when we receive the first event of a series
 * we continue to loop until last event is older that the st_burst_timeout
 * delay (in msec)
 * there is no race condition here as we are not multithreaded
 * or .. is there ?
 */
static gboolean
config_path_changed_trigger_interface( FMAGConfProvider *provider )
{
	static const gchar *thisfn = "fma_gconf_provider_config_path_changed_trigger_interface";
	GTimeVal now;
	gulong diff;
	gulong timeout_usec = 1000*st_burst_timeout;

	g_get_current_time( &now );
	diff = time_val_diff( &now, &provider->private->last_event );
	if( diff < timeout_usec ){
		return( TRUE );
	}

	/* last individual notification is older that the st_burst_timeout
	 * so triggers the FMAIIOProvider interface and destroys this timeout
	 */
	g_debug( "%s: triggering FMAIIOProvider interface for provider=%p (%s)",
			thisfn, ( void * ) provider, G_OBJECT_TYPE_NAME( provider ));

	fma_iio_provider_item_changed( FMA_IIO_PROVIDER( provider ));
	provider->private->event_source_id = 0;
	return( FALSE );
}

/*
 * returns the difference in microseconds.
 */
static gulong
time_val_diff( const GTimeVal *recent, const GTimeVal *old )
{
	gulong microsec = 1000000 * ( recent->tv_sec - old->tv_sec );
	microsec += recent->tv_usec  - old->tv_usec;
	return( microsec );
}
#endif /* FMA_ENABLE_DEPRECATED */