1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "glocaldirectorymonitor.h"
26 #include "gunixmounts.h"
27 #include "gdirectorymonitor.h"
28 #include "giomodule-priv.h"
41 static gboolean g_local_directory_monitor_cancel (GDirectoryMonitor* monitor);
42 static void mounts_changed (GUnixMountMonitor *mount_monitor, gpointer user_data);
44 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_DIRECTORY_MONITOR)
47 g_local_directory_monitor_finalize (GObject* object)
49 GLocalDirectoryMonitor* local_monitor;
50 local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
52 g_free (local_monitor->dirname);
54 if (local_monitor->mount_monitor)
56 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
57 g_object_unref (local_monitor->mount_monitor);
58 local_monitor->mount_monitor = NULL;
61 if (G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize)
62 (*G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize) (object);
66 g_local_directory_monitor_set_property (GObject *object,
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83 g_local_directory_monitor_constructor (GType type,
84 guint n_construct_properties,
85 GObjectConstructParam *construct_properties)
88 GLocalDirectoryMonitorClass *klass;
89 GObjectClass *parent_class;
90 GLocalDirectoryMonitor *local_monitor;
91 const gchar *dirname = NULL;
94 klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
95 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
96 obj = parent_class->constructor (type,
97 n_construct_properties,
98 construct_properties);
100 local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
102 for (i = 0; i < n_construct_properties; i++)
104 if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
106 g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
107 dirname = g_value_get_string (construct_properties[i].value);
112 local_monitor->dirname = g_strdup (dirname);
114 if (!klass->mount_notify)
117 g_warning ("G_OS_WIN32: no mount emulation");
119 GUnixMountEntry *mount;
121 /* Emulate unmount detection */
123 mount = g_unix_mount_at (local_monitor->dirname, NULL);
125 local_monitor->was_mounted = mount != NULL;
128 g_unix_mount_free (mount);
130 local_monitor->mount_monitor = g_unix_mount_monitor_new ();
131 g_signal_connect (local_monitor->mount_monitor, "mounts_changed",
132 G_CALLBACK (mounts_changed), local_monitor);
140 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
142 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
143 GDirectoryMonitorClass *dir_monitor_class = G_DIRECTORY_MONITOR_CLASS (klass);
145 gobject_class->finalize = g_local_directory_monitor_finalize;
146 gobject_class->set_property = g_local_directory_monitor_set_property;
147 gobject_class->constructor = g_local_directory_monitor_constructor;
149 dir_monitor_class->cancel = g_local_directory_monitor_cancel;
151 g_object_class_install_property (gobject_class,
153 g_param_spec_string ("dirname",
154 P_("Directory name"),
155 P_("Directory to monitor"),
157 G_PARAM_CONSTRUCT_ONLY|
159 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
161 klass->mount_notify = FALSE;
165 g_local_directory_monitor_init (GLocalDirectoryMonitor* local_monitor)
170 mounts_changed (GUnixMountMonitor *mount_monitor,
173 GLocalDirectoryMonitor *local_monitor = user_data;
174 GUnixMountEntry *mount;
178 /* Emulate unmount detection */
181 g_warning ("G_OS_WIN32: no mount emulation");
183 mount = g_unix_mount_at (local_monitor->dirname, NULL);
185 is_mounted = mount != NULL;
188 g_unix_mount_free (mount);
191 if (local_monitor->was_mounted != is_mounted)
193 if (local_monitor->was_mounted && !is_mounted)
195 file = g_file_new_for_path (local_monitor->dirname);
196 g_directory_monitor_emit_event (G_DIRECTORY_MONITOR (local_monitor),
198 G_FILE_MONITOR_EVENT_UNMOUNTED);
199 g_object_unref (file);
201 local_monitor->was_mounted = is_mounted;
206 _compare_monitor_type_by_prio (gconstpointer _a,
210 const GType *a = _a, *b = _b;
215 private_q = g_quark_from_static_string ("gio-prio");
217 prio_a = GPOINTER_TO_INT (g_type_get_qdata (*a, private_q));
218 prio_b = GPOINTER_TO_INT (g_type_get_qdata (*b, private_q));
220 ret = prio_b - prio_a;
226 get_default_local_directory_monitor (gpointer data)
228 GType *monitor_impls;
229 guint n_monitor_impls;
231 GLocalDirectoryMonitorClass *chosen_class;
232 GLocalDirectoryMonitorClass **ret = data;
234 _g_io_modules_ensure_loaded ();
236 monitor_impls = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR,
239 g_qsort_with_data (monitor_impls,
242 _compare_monitor_type_by_prio,
246 for (i = 0; i < n_monitor_impls; i++)
248 GLocalDirectoryMonitorClass *klass;
250 klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
252 if (klass->is_supported())
254 chosen_class = klass;
258 g_type_class_unref (klass);
261 g_free (monitor_impls);
266 return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
269 return (gpointer)G_TYPE_INVALID;
273 * g_local_directory_monitor_new:
274 * @dirname: filename of the directory to monitor.
275 * @flags: #GFileMonitorFlags.
277 * Returns: new #GDirectoryMonitor for the given @dirname.
280 _g_local_directory_monitor_new (const char* dirname,
281 GFileMonitorFlags flags)
283 static GOnce once_init = G_ONCE_INIT;
284 GTypeClass *type_class;
285 GDirectoryMonitor *monitor;
289 g_once (&once_init, get_default_local_directory_monitor, &type_class);
290 type = (GType)once_init.retval;
293 if (type != G_TYPE_INVALID)
294 monitor = G_DIRECTORY_MONITOR (g_object_new (type, "dirname", dirname, NULL));
296 /* This is non-null on first pass here. Unref the class now.
297 * This is to avoid unloading the module and then loading it
298 * again which would happen if we unrefed the class
299 * before creating the monitor.
303 g_type_class_unref (type_class);
309 g_local_directory_monitor_cancel (GDirectoryMonitor* monitor)
311 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
313 if (local_monitor->mount_monitor)
315 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
316 g_object_unref (local_monitor->mount_monitor);
317 local_monitor->mount_monitor = NULL;
323 #define __G_LOCAL_DIRECTORY_MONITOR_C__
324 #include "gioaliasdef.c"