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.h"
38 static gboolean g_local_directory_monitor_cancel (GDirectoryMonitor* monitor);
39 static void mounts_changed (GUnixMountMonitor *mount_monitor, gpointer user_data);
41 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_DIRECTORY_MONITOR)
44 g_local_directory_monitor_finalize (GObject* object)
46 GLocalDirectoryMonitor* local_monitor;
47 local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
49 g_free (local_monitor->dirname);
51 if (local_monitor->mount_monitor)
53 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
54 g_object_unref (local_monitor->mount_monitor);
55 local_monitor->mount_monitor = NULL;
58 if (G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize)
59 (*G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize) (object);
63 g_local_directory_monitor_set_property (GObject *object,
74 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
80 g_local_directory_monitor_constructor (GType type,
81 guint n_construct_properties,
82 GObjectConstructParam *construct_properties)
85 GLocalDirectoryMonitorClass *klass;
86 GObjectClass *parent_class;
87 GLocalDirectoryMonitor *local_monitor;
88 const gchar *dirname = NULL;
91 klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
92 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
93 obj = parent_class->constructor (type,
94 n_construct_properties,
95 construct_properties);
97 local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
99 for (i = 0; i < n_construct_properties; i++)
101 if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
103 g_assert (G_VALUE_HOLDS_STRING (construct_properties[i].value));
104 dirname = g_value_get_string (construct_properties[i].value);
109 local_monitor->dirname = g_strdup (dirname);
111 if (!klass->mount_notify)
115 /* Emulate unmount detection */
117 mount = g_get_unix_mount_at (local_monitor->dirname, NULL);
119 local_monitor->was_mounted = mount != NULL;
122 g_unix_mount_free (mount);
124 local_monitor->mount_monitor = g_unix_mount_monitor_new ();
125 g_signal_connect (local_monitor->mount_monitor, "mounts_changed",
126 G_CALLBACK (mounts_changed), local_monitor);
133 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
135 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
136 GDirectoryMonitorClass *dir_monitor_class = G_DIRECTORY_MONITOR_CLASS (klass);
138 gobject_class->finalize = g_local_directory_monitor_finalize;
139 gobject_class->set_property = g_local_directory_monitor_set_property;
140 gobject_class->constructor = g_local_directory_monitor_constructor;
142 dir_monitor_class->cancel = g_local_directory_monitor_cancel;
144 g_object_class_install_property (gobject_class, PROP_DIRNAME,
145 g_param_spec_string ("dirname", "Directory name", "Directory to monitor",
146 NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
148 klass->mount_notify = FALSE;
152 g_local_directory_monitor_init (GLocalDirectoryMonitor* local_monitor)
157 mounts_changed (GUnixMountMonitor *mount_monitor,
160 GLocalDirectoryMonitor *local_monitor = user_data;
165 /* Emulate unmount detection */
167 mount = g_get_unix_mount_at (local_monitor->dirname, NULL);
169 is_mounted = mount != NULL;
172 g_unix_mount_free (mount);
174 if (local_monitor->was_mounted != is_mounted)
176 if (local_monitor->was_mounted && !is_mounted)
178 file = g_file_new_for_path (local_monitor->dirname);
179 g_directory_monitor_emit_event (G_DIRECTORY_MONITOR (local_monitor),
181 G_FILE_MONITOR_EVENT_UNMOUNTED);
182 g_object_unref (file);
184 local_monitor->was_mounted = is_mounted;
189 _compare_monitor_class_by_prio (gconstpointer a,
193 GType *type1 = (GType *) a, *type2 = (GType *) b;
194 GLocalDirectoryMonitorClass *klass1, *klass2;
197 klass1 = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (*type1));
198 klass2 = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (*type2));
200 ret = klass1->prio - klass2->prio;
202 g_type_class_unref (klass1);
203 g_type_class_unref (klass2);
208 extern GType g_inotify_directory_monitor_get_type (void);
211 get_default_local_directory_monitor (gpointer data)
213 GType *monitor_impls, chosen_type;
214 guint n_monitor_impls;
215 GType *ret = (GType *) data;
218 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
219 /* Register Inotify monitor */
220 g_inotify_directory_monitor_get_type ();
223 g_io_modules_ensure_loaded (GIO_MODULE_DIR);
225 monitor_impls = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR,
228 chosen_type = G_TYPE_INVALID;
230 g_qsort_with_data (monitor_impls,
233 _compare_monitor_class_by_prio,
236 for (i = n_monitor_impls - 1; i >= 0 && chosen_type == G_TYPE_INVALID; i--)
238 GLocalDirectoryMonitorClass *klass;
240 klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
242 if (klass->is_supported())
243 chosen_type = monitor_impls[i];
245 g_type_class_unref (klass);
248 g_free (monitor_impls);
255 * g_local_directory_monitor_new:
256 * @dirname: filename of the directory to monitor.
257 * @flags: #GFileMonitorFlags.
259 * Returns: new #GDirectoryMonitor for the given @dirname.
262 g_local_directory_monitor_new (const char* dirname,
263 GFileMonitorFlags flags)
265 static GOnce once_init = G_ONCE_INIT;
266 static GType monitor_type = G_TYPE_INVALID;
268 g_once (&once_init, get_default_local_directory_monitor, &monitor_type);
270 if (monitor_type != G_TYPE_INVALID)
271 return G_DIRECTORY_MONITOR (g_object_new (monitor_type, "dirname", dirname, NULL));
277 g_local_directory_monitor_cancel (GDirectoryMonitor* monitor)
279 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
281 if (local_monitor->mount_monitor)
283 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
284 g_object_unref (local_monitor->mount_monitor);
285 local_monitor->mount_monitor = NULL;