GSettings: small internal refactor
[glib.git] / gio / glocaldirectorymonitor.c
blob942643e8922eec858d1994f3c246a0719aa6e37c
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
25 #include "glocaldirectorymonitor.h"
26 #include "gunixmounts.h"
27 #include "giomodule-priv.h"
28 #include "gfile.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
32 #include <string.h>
35 enum
37 PROP_0,
38 PROP_DIRNAME,
39 PROP_FLAGS
42 static gboolean g_local_directory_monitor_cancel (GFileMonitor *monitor);
43 static void mounts_changed (GUnixMountMonitor *mount_monitor,
44 gpointer user_data);
46 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
48 static void
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);
66 static void
67 g_local_directory_monitor_set_property (GObject *object,
68 guint property_id,
69 const GValue *value,
70 GParamSpec *pspec)
72 GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
74 switch (property_id)
76 case PROP_DIRNAME:
77 local_monitor->dirname = g_value_dup_string (value);
78 break;
80 case PROP_FLAGS:
81 local_monitor->flags = g_value_get_flags (value);
82 break;
84 default:
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86 break;
90 void
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))
99 #ifdef G_OS_WIN32
100 /*claim everything was mounted */
101 local_monitor->was_mounted = TRUE;
102 #else
103 GUnixMountEntry *mount;
105 /* Emulate unmount detection */
107 mount = g_unix_mount_at (local_monitor->dirname, NULL);
109 local_monitor->was_mounted = mount != NULL;
111 if (mount)
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);
117 #endif
120 if (class->start)
121 class->start (local_monitor);
124 static void
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,
136 PROP_DIRNAME,
137 g_param_spec_string ("dirname",
138 P_("Directory name"),
139 P_("Directory to monitor"),
140 NULL,
141 G_PARAM_CONSTRUCT_ONLY|
142 G_PARAM_WRITABLE|
143 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
144 g_object_class_install_property (gobject_class,
145 PROP_FLAGS,
146 g_param_spec_flags ("flags",
147 P_("Monitor flags"),
148 P_("Monitor flags"),
149 G_TYPE_FILE_MONITOR_FLAGS,
151 G_PARAM_CONSTRUCT_ONLY|
152 G_PARAM_WRITABLE|
153 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
155 klass->mount_notify = FALSE;
158 static void
159 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
163 static void
164 mounts_changed (GUnixMountMonitor *mount_monitor,
165 gpointer user_data)
167 GLocalDirectoryMonitor *local_monitor = user_data;
168 #ifdef G_OS_UNIX
169 GUnixMountEntry *mount;
170 #endif
171 gboolean is_mounted;
172 GFile *file;
174 /* Emulate unmount detection */
175 #ifdef G_OS_UNIX
176 mount = g_unix_mount_at (local_monitor->dirname, NULL);
178 is_mounted = mount != NULL;
180 if (mount)
181 g_unix_mount_free (mount);
182 #else
183 /*claim everything was mounted */
184 is_mounted = TRUE;
185 #endif
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),
193 file, NULL,
194 G_FILE_MONITOR_EVENT_UNMOUNTED);
195 g_object_unref (file);
197 local_monitor->was_mounted = is_mounted;
201 GFileMonitor*
202 _g_local_directory_monitor_new (const char *dirname,
203 GFileMonitorFlags flags,
204 GMainContext *context,
205 gboolean is_remote_fs,
206 gboolean do_start,
207 GError **error)
209 GFileMonitor *monitor = NULL;
210 GType type = G_TYPE_INVALID;
212 if (is_remote_fs)
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));
224 else
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));
231 return monitor;
234 static gboolean
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;
246 return TRUE;