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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "glocaldirectorymonitor.h"
24 #include "gunixmounts.h"
25 #include "giomodule-priv.h"
40 static gboolean g_local_directory_monitor_cancel (GFileMonitor *monitor);
41 static void mounts_changed (GUnixMountMonitor *mount_monitor,
44 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_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 G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize (object);
65 g_local_directory_monitor_set_property (GObject *object,
70 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
75 local_monitor->dirname = g_value_dup_string (value);
79 local_monitor->flags = g_value_get_flags (value);
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
89 g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor)
91 GLocalDirectoryMonitorClass *class;
93 class = G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor);
95 if (!class->mount_notify && (local_monitor->flags & G_FILE_MONITOR_WATCH_MOUNTS))
98 /*claim everything was mounted */
99 local_monitor->was_mounted = TRUE;
101 GUnixMountEntry *mount;
103 /* Emulate unmount detection */
105 mount = g_unix_mount_at (local_monitor->dirname, NULL);
107 local_monitor->was_mounted = mount != NULL;
110 g_unix_mount_free (mount);
112 local_monitor->mount_monitor = g_unix_mount_monitor_new ();
113 g_signal_connect_object (local_monitor->mount_monitor, "mounts-changed",
114 G_CALLBACK (mounts_changed), local_monitor, 0);
119 class->start (local_monitor);
123 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
125 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
126 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
128 gobject_class->finalize = g_local_directory_monitor_finalize;
129 gobject_class->set_property = g_local_directory_monitor_set_property;
131 file_monitor_class->cancel = g_local_directory_monitor_cancel;
133 g_object_class_install_property (gobject_class,
135 g_param_spec_string ("dirname",
136 P_("Directory name"),
137 P_("Directory to monitor"),
139 G_PARAM_CONSTRUCT_ONLY|
141 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
142 g_object_class_install_property (gobject_class,
144 g_param_spec_flags ("flags",
147 G_TYPE_FILE_MONITOR_FLAGS,
149 G_PARAM_CONSTRUCT_ONLY|
151 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
153 klass->mount_notify = FALSE;
157 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
162 mounts_changed (GUnixMountMonitor *mount_monitor,
165 GLocalDirectoryMonitor *local_monitor = user_data;
167 GUnixMountEntry *mount;
172 /* Emulate unmount detection */
174 mount = g_unix_mount_at (local_monitor->dirname, NULL);
176 is_mounted = mount != NULL;
179 g_unix_mount_free (mount);
181 /*claim everything was mounted */
185 if (local_monitor->was_mounted != is_mounted)
187 if (local_monitor->was_mounted && !is_mounted)
189 file = g_file_new_for_path (local_monitor->dirname);
190 g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
192 G_FILE_MONITOR_EVENT_UNMOUNTED);
193 g_object_unref (file);
195 local_monitor->was_mounted = is_mounted;
200 _g_local_directory_monitor_new (const char *dirname,
201 GFileMonitorFlags flags,
202 GMainContext *context,
203 gboolean is_remote_fs,
207 GFileMonitor *monitor = NULL;
208 GType type = G_TYPE_INVALID;
211 type = _g_io_module_get_default_type (G_NFS_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
212 "GIO_USE_FILE_MONITOR",
213 G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
215 if (type == G_TYPE_INVALID)
216 type = _g_io_module_get_default_type (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
217 "GIO_USE_FILE_MONITOR",
218 G_STRUCT_OFFSET (GLocalDirectoryMonitorClass, is_supported));
220 if (type != G_TYPE_INVALID)
221 monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, "context", context, NULL));
223 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
224 _("Unable to find default local directory monitor type"));
226 if (monitor && do_start)
227 g_local_directory_monitor_start (G_LOCAL_DIRECTORY_MONITOR (monitor));
233 g_local_directory_monitor_cancel (GFileMonitor *monitor)
235 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
237 if (local_monitor->mount_monitor)
239 g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
240 g_object_unref (local_monitor->mount_monitor);
241 local_monitor->mount_monitor = NULL;