NadpDesktopFile: do not try to import empty or not DES-EMA files
[nautilus-actions.git] / src / io-desktop / nadp-desktop-provider.c
blob9fbb38a952213187fe10c2f4f64e012a68eaeaba
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-core-utils.h>
39 #include <api/na-ifactory-provider.h>
41 #include "nadp-desktop-provider.h"
42 #include "nadp-keys.h"
43 #include "nadp-monitor.h"
44 #include "nadp-reader.h"
45 #include "nadp-writer.h"
47 /* private class data
49 struct _NadpDesktopProviderClassPrivate {
50 void *empty; /* so that gcc -pedantic is happy */
53 extern NAIExporterFormat nadp_formats[];
55 static GType st_module_type = 0;
56 static GObjectClass *st_parent_class = NULL;
57 static guint st_burst_timeout = 100; /* burst timeout in msec */
59 static void class_init( NadpDesktopProviderClass *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( NAIIOProviderInterface *iface );
65 static gchar *iio_provider_get_id( const NAIIOProvider *provider );
66 static gchar *iio_provider_get_name( const NAIIOProvider *provider );
67 static guint iio_provider_get_version( const NAIIOProvider *provider );
69 static void ifactory_provider_iface_init( NAIFactoryProviderInterface *iface );
70 static guint ifactory_provider_get_version( const NAIFactoryProvider *reader );
72 static void iimporter_iface_init( NAIImporterInterface *iface );
73 static guint iimporter_get_version( const NAIImporter *importer );
75 static void iexporter_iface_init( NAIExporterInterface *iface );
76 static guint iexporter_get_version( const NAIExporter *exporter );
77 static gchar *iexporter_get_name( const NAIExporter *exporter );
78 static const NAIExporterFormat *iexporter_get_formats( const NAIExporter *exporter );
80 static void on_monitor_timeout( NadpDesktopProvider *provider );
82 GType
83 nadp_desktop_provider_get_type( void )
85 return( st_module_type );
88 void
89 nadp_desktop_provider_register_type( GTypeModule *module )
91 static const gchar *thisfn = "nadp_desktop_provider_register_type";
93 static GTypeInfo info = {
94 sizeof( NadpDesktopProviderClass ),
95 NULL,
96 NULL,
97 ( GClassInitFunc ) class_init,
98 NULL,
99 NULL,
100 sizeof( NadpDesktopProvider ),
102 ( GInstanceInitFunc ) instance_init
105 static const GInterfaceInfo iio_provider_iface_info = {
106 ( GInterfaceInitFunc ) iio_provider_iface_init,
107 NULL,
108 NULL
111 static const GInterfaceInfo ifactory_provider_iface_info = {
112 ( GInterfaceInitFunc ) ifactory_provider_iface_init,
113 NULL,
114 NULL
117 static const GInterfaceInfo iimporter_iface_info = {
118 ( GInterfaceInitFunc ) iimporter_iface_init,
119 NULL,
120 NULL
123 static const GInterfaceInfo iexporter_iface_info = {
124 ( GInterfaceInitFunc ) iexporter_iface_init,
125 NULL,
126 NULL
129 g_debug( "%s", thisfn );
131 st_module_type = g_type_module_register_type( module, G_TYPE_OBJECT, "NadpDesktopProvider", &info, 0 );
133 g_type_module_add_interface( module, st_module_type, NA_IIO_PROVIDER_TYPE, &iio_provider_iface_info );
135 g_type_module_add_interface( module, st_module_type, NA_IFACTORY_PROVIDER_TYPE, &ifactory_provider_iface_info );
137 g_type_module_add_interface( module, st_module_type, NA_IIMPORTER_TYPE, &iimporter_iface_info );
139 g_type_module_add_interface( module, st_module_type, NA_IEXPORTER_TYPE, &iexporter_iface_info );
142 static void
143 class_init( NadpDesktopProviderClass *klass )
145 static const gchar *thisfn = "nadp_desktop_provider_class_init";
146 GObjectClass *object_class;
148 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
150 st_parent_class = g_type_class_peek_parent( klass );
152 object_class = G_OBJECT_CLASS( klass );
153 object_class->dispose = instance_dispose;
154 object_class->finalize = instance_finalize;
156 klass->private = g_new0( NadpDesktopProviderClassPrivate, 1 );
159 static void
160 instance_init( GTypeInstance *instance, gpointer klass )
162 static const gchar *thisfn = "nadp_desktop_provider_instance_init";
163 NadpDesktopProvider *self;
165 g_return_if_fail( NADP_IS_DESKTOP_PROVIDER( instance ));
167 g_debug( "%s: instance=%p (%s), klass=%p",
168 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
170 self = NADP_DESKTOP_PROVIDER( instance );
172 self->private = g_new0( NadpDesktopProviderPrivate, 1 );
174 self->private->dispose_has_run = FALSE;
175 self->private->monitors = NULL;
176 self->private->timeout.timeout = st_burst_timeout;
177 self->private->timeout.handler = ( NATimeoutFunc ) on_monitor_timeout;
178 self->private->timeout.user_data = self;
179 self->private->timeout.source_id = 0;
182 static void
183 instance_dispose( GObject *object )
185 static const gchar *thisfn = "nadp_desktop_provider_instance_dispose";
186 NadpDesktopProvider *self;
188 g_return_if_fail( NADP_IS_DESKTOP_PROVIDER( object ));
190 self = NADP_DESKTOP_PROVIDER( object );
192 if( !self->private->dispose_has_run ){
194 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
196 self->private->dispose_has_run = TRUE;
198 nadp_desktop_provider_release_monitors( self );
200 /* chain up to the parent class */
201 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
202 G_OBJECT_CLASS( st_parent_class )->dispose( object );
207 static void
208 instance_finalize( GObject *object )
210 static const gchar *thisfn = "nadp_desktop_provider_instance_finalize";
211 NadpDesktopProvider *self;
213 g_return_if_fail( NADP_IS_DESKTOP_PROVIDER( object ));
215 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
217 self = NADP_DESKTOP_PROVIDER( object );
219 g_free( self->private );
221 /* chain call to parent class */
222 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
223 G_OBJECT_CLASS( st_parent_class )->finalize( object );
227 static void
228 iio_provider_iface_init( NAIIOProviderInterface *iface )
230 static const gchar *thisfn = "nadp_desktop_provider_iio_provider_iface_init";
232 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
234 iface->get_version = iio_provider_get_version;
235 iface->get_id = iio_provider_get_id;
236 iface->get_name = iio_provider_get_name;
237 iface->read_items = nadp_iio_provider_read_items;
238 iface->is_willing_to_write = nadp_iio_provider_is_willing_to_write;
239 iface->is_able_to_write = nadp_iio_provider_is_able_to_write;
240 iface->write_item = nadp_iio_provider_write_item;
241 iface->delete_item = nadp_iio_provider_delete_item;
242 iface->duplicate_data = nadp_iio_provider_duplicate_data;
245 static guint
246 iio_provider_get_version( const NAIIOProvider *provider )
248 return( 1 );
251 static gchar *
252 iio_provider_get_id( const NAIIOProvider *provider )
254 return( g_strdup( "na-desktop" ));
257 static gchar *
258 iio_provider_get_name( const NAIIOProvider *provider )
260 return( g_strdup( _( "Nautilus-Actions Desktop I/O Provider" )));
263 static void
264 ifactory_provider_iface_init( NAIFactoryProviderInterface *iface )
266 static const gchar *thisfn = "nadp_desktop_provider_ifactory_provider_iface_init";
268 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
270 iface->get_version = ifactory_provider_get_version;
271 iface->read_start = nadp_reader_ifactory_provider_read_start;
272 iface->read_data = nadp_reader_ifactory_provider_read_data;
273 iface->read_done = nadp_reader_ifactory_provider_read_done;
274 iface->write_start = nadp_writer_ifactory_provider_write_start;
275 iface->write_data = nadp_writer_ifactory_provider_write_data;
276 iface->write_done = nadp_writer_ifactory_provider_write_done;
279 static guint
280 ifactory_provider_get_version( const NAIFactoryProvider *reader )
282 return( 1 );
285 static void
286 iimporter_iface_init( NAIImporterInterface *iface )
288 static const gchar *thisfn = "nadp_desktop_provider_iimporter_iface_init";
290 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
292 iface->get_version = iimporter_get_version;
293 iface->import_from_uri = nadp_reader_iimporter_import_from_uri;
296 static guint
297 iimporter_get_version( const NAIImporter *importer )
299 return( 1 );
302 static void
303 iexporter_iface_init( NAIExporterInterface *iface )
305 static const gchar *thisfn = "nadp_desktop_iexporter_iface_init";
307 g_debug( "%s: iface=%p", thisfn, ( void * ) iface );
309 iface->get_version = iexporter_get_version;
310 iface->get_name = iexporter_get_name;
311 iface->get_formats = iexporter_get_formats;
312 iface->to_file = nadp_writer_iexporter_export_to_file;
313 iface->to_buffer = nadp_writer_iexporter_export_to_buffer;
316 static guint
317 iexporter_get_version( const NAIExporter *exporter )
319 return( 1 );
322 static gchar *
323 iexporter_get_name( const NAIExporter *exporter )
325 return( g_strdup( "NA Desktop Exporter" ));
328 static const NAIExporterFormat *
329 iexporter_get_formats( const NAIExporter *exporter )
331 return( nadp_formats );
335 * nadp_desktop_provider_add_monitor:
336 * @provider: this #NadpDesktopProvider object.
337 * @dir: the path to the directory to be monitored. May not exist.
339 * Installs a GIO monitor on the given directory.
341 void
342 nadp_desktop_provider_add_monitor( NadpDesktopProvider *provider, const gchar *dir )
344 NadpMonitor *monitor;
346 g_return_if_fail( NADP_IS_DESKTOP_PROVIDER( provider ));
348 if( !provider->private->dispose_has_run ){
350 monitor = nadp_monitor_new( provider, dir );
351 provider->private->monitors = g_list_prepend( provider->private->monitors, monitor );
356 * nadp_desktop_provider_on_monitor_event:
357 * @provider: this #NadpDesktopProvider object.
359 * Factorize events received from GIO when monitoring desktop directories.
361 void
362 nadp_desktop_provider_on_monitor_event( NadpDesktopProvider *provider )
364 g_return_if_fail( NADP_IS_DESKTOP_PROVIDER( provider ));
366 if( !provider->private->dispose_has_run ){
368 na_timeout_event( &provider->private->timeout );
373 * nadp_desktop_provider_release_monitors:
374 * @provider: this #NadpDesktopProvider object.
376 * Release previously set desktop monitors.
378 void
379 nadp_desktop_provider_release_monitors( NadpDesktopProvider *provider )
381 g_return_if_fail( NADP_IS_DESKTOP_PROVIDER( provider ));
383 if( provider->private->monitors ){
385 g_list_foreach( provider->private->monitors, ( GFunc ) g_object_unref, NULL );
386 g_list_free( provider->private->monitors );
387 provider->private->monitors = NULL;
391 static void
392 on_monitor_timeout( NadpDesktopProvider *provider )
394 static const gchar *thisfn = "nadp_desktop_provider_on_monitor_timeout";
396 /* last individual notification is older that the st_burst_timeout
397 * so triggers the NAIIOProvider interface and destroys this timeout
399 g_debug( "%s: triggering NAIIOProvider interface for provider=%p (%s)",
400 thisfn, ( void * ) provider, G_OBJECT_TYPE_NAME( provider ));
402 na_iio_provider_item_changed( NA_IIO_PROVIDER( provider ));