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 "giomodule-priv.h"
42 static gboolean g_local_directory_monitor_cancel (GFileMonitor *monitor);
43 static void mounts_changed (GUnixMountMonitor *mount_monitor,
46 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
49 g_local_directory_monitor_finalize (GObject *object)
51 GLocalDirectoryMonitor *local_monitor;
52 local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
54 g_free (local_monitor->dirname);
56 if (local_monitor->mount_monitor)
58 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
59 g_object_unref (local_monitor->mount_monitor);
60 local_monitor->mount_monitor = NULL;
63 G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize (object);
67 g_local_directory_monitor_set_property (GObject *object,
72 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
77 local_monitor->dirname = g_value_dup_string (value);
81 local_monitor->flags = g_value_get_flags (value);
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor)
93 GLocalDirectoryMonitorClass *class;
95 class = G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor);
97 if (!class->mount_notify && (local_monitor->flags & G_FILE_MONITOR_WATCH_MOUNTS))
100 /*claim everything was mounted */
101 local_monitor->was_mounted = TRUE;
103 GUnixMountEntry *mount;
105 /* Emulate unmount detection */
107 mount = g_unix_mount_at (local_monitor->dirname, NULL);
109 local_monitor->was_mounted = mount != NULL;
112 g_unix_mount_free (mount);
114 local_monitor->mount_monitor = g_unix_mount_monitor_new ();
115 g_signal_connect_object (local_monitor->mount_monitor, "mounts-changed",
116 G_CALLBACK (mounts_changed), local_monitor, 0);
121 class->start (local_monitor);
125 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
127 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
128 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
130 gobject_class->finalize = g_local_directory_monitor_finalize;
131 gobject_class->set_property = g_local_directory_monitor_set_property;
133 file_monitor_class->cancel = g_local_directory_monitor_cancel;
135 g_object_class_install_property (gobject_class,
137 g_param_spec_string ("dirname",
138 P_("Directory name"),
139 P_("Directory to monitor"),
141 G_PARAM_CONSTRUCT_ONLY|
143 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
144 g_object_class_install_property (gobject_class,
146 g_param_spec_flags ("flags",
149 G_TYPE_FILE_MONITOR_FLAGS,
151 G_PARAM_CONSTRUCT_ONLY|
153 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
155 klass->mount_notify = FALSE;
159 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
164 mounts_changed (GUnixMountMonitor *mount_monitor,
167 GLocalDirectoryMonitor *local_monitor = user_data;
169 GUnixMountEntry *mount;
174 /* Emulate unmount detection */
176 mount = g_unix_mount_at (local_monitor->dirname, NULL);
178 is_mounted = mount != NULL;
181 g_unix_mount_free (mount);
183 /*claim everything was mounted */
187 if (local_monitor->was_mounted != is_mounted)
189 if (local_monitor->was_mounted && !is_mounted)
191 file = g_file_new_for_path (local_monitor->dirname);
192 g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
194 G_FILE_MONITOR_EVENT_UNMOUNTED);
195 g_object_unref (file);
197 local_monitor->was_mounted = is_mounted;
202 _g_local_directory_monitor_new (const char *dirname,
203 GFileMonitorFlags flags,
204 GMainContext *context,
205 gboolean is_remote_fs,
209 GFileMonitor *monitor = NULL;
210 GType type = G_TYPE_INVALID;
213 type = _g_io_module_get_default_type (G_NFS_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
214 "GIO_USE_FILE_MONITOR",
215 G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
217 if (type == G_TYPE_INVALID)
218 type = _g_io_module_get_default_type (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
219 "GIO_USE_FILE_MONITOR",
220 G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
222 if (type != G_TYPE_INVALID)
223 monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, "context", context, NULL));
225 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
226 _("Unable to find default local directory monitor type"));
228 if (monitor && do_start)
229 g_local_directory_monitor_start (G_LOCAL_DIRECTORY_MONITOR (monitor));
235 g_local_directory_monitor_cancel (GFileMonitor *monitor)
237 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
239 if (local_monitor->mount_monitor)
241 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
242 g_object_unref (local_monitor->mount_monitor);
243 local_monitor->mount_monitor = NULL;