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.
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)
37 #include "nadp-monitor.h"
41 struct _NadpMonitorClassPrivate
{
42 void *empty
; /* so that gcc -pedantic is happy */
45 /* private instance data
47 struct _NadpMonitorPrivate
{
48 gboolean dispose_has_run
;
49 NadpDesktopProvider
*provider
;
52 GFileMonitor
*monitor
;
56 static GObjectClass
*st_parent_class
= NULL
;
58 static GType
register_type( void );
59 static void class_init( NadpMonitorClass
*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 on_monitor_changed( GFileMonitor
*monitor
, GFile
*file
, GFile
*other_file
, GFileMonitorEvent event_type
, NadpMonitor
*my_monitor
);
67 nadp_monitor_get_type( void )
69 static GType class_type
= 0;
72 class_type
= register_type();
81 static const gchar
*thisfn
= "nadp_monitor_register_type";
84 static GTypeInfo info
= {
85 sizeof( NadpMonitorClass
),
88 ( GClassInitFunc
) class_init
,
91 sizeof( NadpMonitor
),
93 ( GInstanceInitFunc
) instance_init
96 g_debug( "%s", thisfn
);
98 type
= g_type_register_static( G_TYPE_OBJECT
, "NadpMonitor", &info
, 0 );
104 class_init( NadpMonitorClass
*klass
)
106 static const gchar
*thisfn
= "nadp_monitor_class_init";
107 GObjectClass
*object_class
;
109 g_debug( "%s: klass=%p", thisfn
, ( void * ) klass
);
111 st_parent_class
= g_type_class_peek_parent( klass
);
113 object_class
= G_OBJECT_CLASS( klass
);
114 object_class
->dispose
= instance_dispose
;
115 object_class
->finalize
= instance_finalize
;
117 klass
->private = g_new0( NadpMonitorClassPrivate
, 1 );
121 instance_init( GTypeInstance
*instance
, gpointer klass
)
123 static const gchar
*thisfn
= "nadp_monitor_instance_init";
126 g_return_if_fail( NADP_IS_MONITOR( instance
));
128 g_debug( "%s: instance=%p (%s), klass=%p",
129 thisfn
, ( void * ) instance
, G_OBJECT_TYPE_NAME( instance
), ( void * ) klass
);
131 self
= NADP_MONITOR( instance
);
133 self
->private = g_new0( NadpMonitorPrivate
, 1 );
135 self
->private->dispose_has_run
= FALSE
;
139 instance_dispose( GObject
*object
)
141 static const gchar
*thisfn
= "nadp_monitor_instance_dispose";
144 g_return_if_fail( NADP_IS_MONITOR( object
));
146 self
= NADP_MONITOR( object
);
148 if( !self
->private->dispose_has_run
){
150 g_debug( "%s: object=%p (%s)", thisfn
, ( void * ) object
, G_OBJECT_TYPE_NAME( object
));
152 if( self
->private->handler
){
153 g_signal_handler_disconnect( self
->private->monitor
, self
->private->handler
);
156 if( self
->private->monitor
){
157 g_object_unref( self
->private->monitor
);
160 if( self
->private->file
){
161 g_object_unref( self
->private->file
);
164 self
->private->dispose_has_run
= TRUE
;
166 /* chain up to the parent class */
167 if( G_OBJECT_CLASS( st_parent_class
)->dispose
){
168 G_OBJECT_CLASS( st_parent_class
)->dispose( object
);
174 instance_finalize( GObject
*object
)
176 static const gchar
*thisfn
= "nadp_monitor_instance_finalize";
179 g_return_if_fail( NADP_IS_MONITOR( object
));
181 g_debug( "%s: object=%p (%s)", thisfn
, ( void * ) object
, G_OBJECT_TYPE_NAME( object
));
183 self
= NADP_MONITOR( object
);
185 g_free( self
->private->name
);
187 g_free( self
->private );
189 /* chain call to parent class */
190 if( G_OBJECT_CLASS( st_parent_class
)->finalize
){
191 G_OBJECT_CLASS( st_parent_class
)->finalize( object
);
197 * @provider: the #NadpDesktopProvider instance.
198 * @path: the path of a directory to be monitored.
200 * Installs a new monitor on the given directory.
202 * Returns: a new #NadpMonitor instance.
205 nadp_monitor_new( const NadpDesktopProvider
*provider
, const gchar
*path
)
207 static const gchar
*thisfn
= "nadp_monitor_new";
208 NadpMonitor
*monitor
;
209 GFileMonitorFlags flags
;
212 monitor
= g_object_new( NADP_MONITOR_TYPE
, NULL
);
214 monitor
->private->provider
= NADP_DESKTOP_PROVIDER( provider
);
215 monitor
->private->name
= g_strdup( path
);
216 monitor
->private->file
= g_file_new_for_path( path
);
219 flags
= G_FILE_MONITOR_NONE
;
221 monitor
->private->monitor
= g_file_monitor_directory( monitor
->private->file
, flags
, NULL
, &error
);
223 g_warning( "%s: g_file_monitor: %s", thisfn
, error
->message
);
224 g_error_free( error
);
226 g_object_unref( monitor
);
230 g_return_val_if_fail( monitor
->private->monitor
, NULL
);
232 monitor
->private->handler
= g_signal_connect(
233 monitor
->private->monitor
, "changed", G_CALLBACK( on_monitor_changed
), monitor
);
239 * - an existing file is modified: n events on dir + m events on file
240 * - an existing file is deleted: 1 event on file + 1 event on dir
241 * - a new file is created: n events on the dir
242 * - a new directory is created: 1 event of this new dir
243 * - a directory is removed: 1 event of this new dir
244 * - an existing file is renamed: 1 event on file + n events on dir
245 * - the renamed file is modified: n events on dir
248 on_monitor_changed( GFileMonitor
*monitor
, GFile
*file
, GFile
*other_file
, GFileMonitorEvent event_type
, NadpMonitor
*my_monitor
)
250 nadp_desktop_provider_on_monitor_event( my_monitor
->private->provider
);