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"
40 static gboolean g_local_directory_monitor_cancel (GDirectoryMonitor* monitor);
41 static void mounts_changed (GUnixMountMonitor *mount_monitor, gpointer user_data);
43 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_DIRECTORY_MONITOR)
46 g_local_directory_monitor_finalize (GObject* object)
48 GLocalDirectoryMonitor* local_monitor;
49 local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
51 g_free (local_monitor->dirname);
53 if (local_monitor->mount_monitor)
55 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
56 g_object_unref (local_monitor->mount_monitor);
57 local_monitor->mount_monitor = NULL;
60 if (G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize)
61 (*G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize) (object);
65 g_local_directory_monitor_set_property (GObject *object,
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82 g_local_directory_monitor_constructor (GType type,
83 guint n_construct_properties,
84 GObjectConstructParam *construct_properties)
87 GLocalDirectoryMonitorClass *klass;
88 GObjectClass *parent_class;
89 GLocalDirectoryMonitor *local_monitor;
90 const gchar *dirname = NULL;
93 klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
94 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
95 obj = parent_class->constructor (type,
96 n_construct_properties,
97 construct_properties);
99 local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
101 for (i = 0; i < n_construct_properties; i++)
103 if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
105 g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
106 dirname = g_value_get_string (construct_properties[i].value);
111 local_monitor->dirname = g_strdup (dirname);
113 if (!klass->mount_notify)
116 g_warning ("G_OS_WIN32: no mount emulation");
118 GUnixMountEntry *mount;
120 /* Emulate unmount detection */
122 mount = g_get_unix_mount_at (local_monitor->dirname, NULL);
124 local_monitor->was_mounted = mount != NULL;
127 g_unix_mount_free (mount);
129 local_monitor->mount_monitor = g_unix_mount_monitor_new ();
130 g_signal_connect (local_monitor->mount_monitor, "mounts_changed",
131 G_CALLBACK (mounts_changed), local_monitor);
139 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
141 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
142 GDirectoryMonitorClass *dir_monitor_class = G_DIRECTORY_MONITOR_CLASS (klass);
144 gobject_class->finalize = g_local_directory_monitor_finalize;
145 gobject_class->set_property = g_local_directory_monitor_set_property;
146 gobject_class->constructor = g_local_directory_monitor_constructor;
148 dir_monitor_class->cancel = g_local_directory_monitor_cancel;
150 g_object_class_install_property (gobject_class, PROP_DIRNAME,
151 g_param_spec_string ("dirname", "Directory name", "Directory to monitor",
152 NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
154 klass->mount_notify = FALSE;
158 g_local_directory_monitor_init (GLocalDirectoryMonitor* local_monitor)
163 mounts_changed (GUnixMountMonitor *mount_monitor,
166 GLocalDirectoryMonitor *local_monitor = user_data;
167 GUnixMountEntry *mount;
171 /* Emulate unmount detection */
174 g_warning ("G_OS_WIN32: no mount emulation");
176 mount = g_get_unix_mount_at (local_monitor->dirname, NULL);
178 is_mounted = mount != NULL;
181 g_unix_mount_free (mount);
184 if (local_monitor->was_mounted != is_mounted)
186 if (local_monitor->was_mounted && !is_mounted)
188 file = g_file_new_for_path (local_monitor->dirname);
189 g_directory_monitor_emit_event (G_DIRECTORY_MONITOR (local_monitor),
191 G_FILE_MONITOR_EVENT_UNMOUNTED);
192 g_object_unref (file);
194 local_monitor->was_mounted = is_mounted;
199 _compare_monitor_class_by_prio (gconstpointer a,
203 GType *type1 = (GType *) a, *type2 = (GType *) b;
204 GLocalDirectoryMonitorClass *klass1, *klass2;
207 klass1 = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (*type1));
208 klass2 = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (*type2));
210 ret = klass1->prio - klass2->prio;
212 g_type_class_unref (klass1);
213 g_type_class_unref (klass2);
218 extern GType _g_inotify_directory_monitor_get_type (void);
221 get_default_local_directory_monitor (gpointer data)
223 GType *monitor_impls, chosen_type;
224 guint n_monitor_impls;
225 GType *ret = (GType *) data;
228 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
229 /* Register Inotify monitor */
230 _g_inotify_directory_monitor_get_type ();
233 _g_io_modules_ensure_loaded ();
235 monitor_impls = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR,
238 chosen_type = G_TYPE_INVALID;
240 g_qsort_with_data (monitor_impls,
243 _compare_monitor_class_by_prio,
246 for (i = n_monitor_impls - 1; i >= 0 && chosen_type == G_TYPE_INVALID; i--)
248 GLocalDirectoryMonitorClass *klass;
250 klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
252 if (klass->is_supported())
253 chosen_type = monitor_impls[i];
255 g_type_class_unref (klass);
258 g_free (monitor_impls);
265 * g_local_directory_monitor_new:
266 * @dirname: filename of the directory to monitor.
267 * @flags: #GFileMonitorFlags.
269 * Returns: new #GDirectoryMonitor for the given @dirname.
272 _g_local_directory_monitor_new (const char* dirname,
273 GFileMonitorFlags flags)
275 static GOnce once_init = G_ONCE_INIT;
276 static GType monitor_type = G_TYPE_INVALID;
278 g_once (&once_init, get_default_local_directory_monitor, &monitor_type);
280 if (monitor_type != G_TYPE_INVALID)
281 return G_DIRECTORY_MONITOR (g_object_new (monitor_type, "dirname", dirname, NULL));
287 g_local_directory_monitor_cancel (GDirectoryMonitor* monitor)
289 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
291 if (local_monitor->mount_monitor)
293 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
294 g_object_unref (local_monitor->mount_monitor);
295 local_monitor->mount_monitor = NULL;
301 #define __G_LOCAL_DIRECTORY_MONITOR_C__
302 #include "gioaliasdef.c"