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/>.
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)
36 #include "fma-desktop-monitor.h"
40 struct _FMADesktopMonitorClassPrivate
{
41 void *empty
; /* so that gcc -pedantic is happy */
44 /* private instance data
46 struct _FMADesktopMonitorPrivate
{
47 gboolean dispose_has_run
;
48 FMADesktopProvider
*provider
;
51 GFileMonitor
*monitor
;
55 static GObjectClass
*st_parent_class
= NULL
;
57 static GType
register_type( void );
58 static void class_init( FMADesktopMonitorClass
*klass
);
59 static void instance_init( GTypeInstance
*instance
, gpointer klass
);
60 static void instance_dispose( GObject
*object
);
61 static void instance_finalize( GObject
*object
);
63 static void on_monitor_changed( GFileMonitor
*monitor
, GFile
*file
, GFile
*other_file
, GFileMonitorEvent event_type
, FMADesktopMonitor
*my_monitor
);
66 fma_desktop_monitor_get_type( void )
68 static GType class_type
= 0;
71 class_type
= register_type();
80 static const gchar
*thisfn
= "fma_desktop_monitor_register_type";
83 static GTypeInfo info
= {
84 sizeof( FMADesktopMonitorClass
),
87 ( GClassInitFunc
) class_init
,
90 sizeof( FMADesktopMonitor
),
92 ( GInstanceInitFunc
) instance_init
95 g_debug( "%s", thisfn
);
97 type
= g_type_register_static( G_TYPE_OBJECT
, "FMADesktopMonitor", &info
, 0 );
103 class_init( FMADesktopMonitorClass
*klass
)
105 static const gchar
*thisfn
= "fma_desktop_monitor_class_init";
106 GObjectClass
*object_class
;
108 g_debug( "%s: klass=%p", thisfn
, ( void * ) klass
);
110 st_parent_class
= g_type_class_peek_parent( klass
);
112 object_class
= G_OBJECT_CLASS( klass
);
113 object_class
->dispose
= instance_dispose
;
114 object_class
->finalize
= instance_finalize
;
116 klass
->private = g_new0( FMADesktopMonitorClassPrivate
, 1 );
120 instance_init( GTypeInstance
*instance
, gpointer klass
)
122 static const gchar
*thisfn
= "fma_desktop_monitor_instance_init";
123 FMADesktopMonitor
*self
;
125 g_return_if_fail( FMA_IS_DESKTOP_MONITOR( instance
));
127 g_debug( "%s: instance=%p (%s), klass=%p",
128 thisfn
, ( void * ) instance
, G_OBJECT_TYPE_NAME( instance
), ( void * ) klass
);
130 self
= FMA_DESKTOP_MONITOR( instance
);
132 self
->private = g_new0( FMADesktopMonitorPrivate
, 1 );
134 self
->private->dispose_has_run
= FALSE
;
138 instance_dispose( GObject
*object
)
140 static const gchar
*thisfn
= "fma_desktop_monitor_instance_dispose";
141 FMADesktopMonitor
*self
;
143 g_return_if_fail( FMA_IS_DESKTOP_MONITOR( object
));
145 self
= FMA_DESKTOP_MONITOR( object
);
147 if( !self
->private->dispose_has_run
){
149 g_debug( "%s: object=%p (%s)", thisfn
, ( void * ) object
, G_OBJECT_TYPE_NAME( object
));
151 if( self
->private->handler
){
152 g_signal_handler_disconnect( self
->private->monitor
, self
->private->handler
);
155 if( self
->private->monitor
){
156 g_object_unref( self
->private->monitor
);
159 if( self
->private->file
){
160 g_object_unref( self
->private->file
);
163 self
->private->dispose_has_run
= TRUE
;
165 /* chain up to the parent class */
166 if( G_OBJECT_CLASS( st_parent_class
)->dispose
){
167 G_OBJECT_CLASS( st_parent_class
)->dispose( object
);
173 instance_finalize( GObject
*object
)
175 static const gchar
*thisfn
= "fma_desktop_monitor_instance_finalize";
176 FMADesktopMonitor
*self
;
178 g_return_if_fail( FMA_IS_DESKTOP_MONITOR( object
));
180 g_debug( "%s: object=%p (%s)", thisfn
, ( void * ) object
, G_OBJECT_TYPE_NAME( object
));
182 self
= FMA_DESKTOP_MONITOR( object
);
184 g_free( self
->private->name
);
186 g_free( self
->private );
188 /* chain call to parent class */
189 if( G_OBJECT_CLASS( st_parent_class
)->finalize
){
190 G_OBJECT_CLASS( st_parent_class
)->finalize( object
);
195 * fma_desktop_monitor_new:
196 * @provider: the #FMADesktopProvider instance.
197 * @path: the path of a directory to be monitored.
199 * Installs a new monitor on the given directory.
201 * Returns: a new #FMADesktopMonitor instance.
204 fma_desktop_monitor_new( const FMADesktopProvider
*provider
, const gchar
*path
)
206 static const gchar
*thisfn
= "fma_desktop_monitor_new";
207 FMADesktopMonitor
*monitor
;
208 GFileMonitorFlags flags
;
211 monitor
= g_object_new( FMA_TYPE_DESKTOP_MONITOR
, NULL
);
213 monitor
->private->provider
= FMA_DESKTOP_PROVIDER( provider
);
214 monitor
->private->name
= g_strdup( path
);
215 monitor
->private->file
= g_file_new_for_path( path
);
218 flags
= G_FILE_MONITOR_NONE
;
220 monitor
->private->monitor
= g_file_monitor_directory( monitor
->private->file
, flags
, NULL
, &error
);
222 g_warning( "%s: g_file_monitor: %s", thisfn
, error
->message
);
223 g_error_free( error
);
225 g_object_unref( monitor
);
229 g_return_val_if_fail( monitor
->private->monitor
, NULL
);
231 monitor
->private->handler
= g_signal_connect(
232 monitor
->private->monitor
, "changed", G_CALLBACK( on_monitor_changed
), monitor
);
238 * - an existing file is modified: n events on dir + m events on file
239 * - an existing file is deleted: 1 event on file + 1 event on dir
240 * - a new file is created: n events on the dir
241 * - a new directory is created: 1 event of this new dir
242 * - a directory is removed: 1 event of this new dir
243 * - an existing file is renamed: 1 event on file + n events on dir
244 * - the renamed file is modified: n events on dir
247 on_monitor_changed( GFileMonitor
*monitor
, GFile
*file
, GFile
*other_file
, GFileMonitorEvent event_type
, FMADesktopMonitor
*my_monitor
)
249 fma_desktop_provider_on_monitor_event( my_monitor
->private->provider
);