d85d752363cfc62651fa400bedd832802b6df53b
[platform/upstream/glib.git] / gio / glocaldirectorymonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "glocaldirectorymonitor.h"
24 #include "gunixmounts.h"
25 #include "giomodule-priv.h"
26 #include "gfile.h"
27 #include "gioerror.h"
28 #include "glibintl.h"
29
30 #include <string.h>
31
32
33 enum
34 {
35   PROP_0,
36   PROP_DIRNAME,
37   PROP_FLAGS
38 };
39
40 static gboolean g_local_directory_monitor_cancel (GFileMonitor      *monitor);
41 static void     mounts_changed                   (GUnixMountMonitor *mount_monitor, 
42                                                   gpointer           user_data);
43
44 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
45
46 static void
47 g_local_directory_monitor_finalize (GObject *object)
48 {
49   GLocalDirectoryMonitor *local_monitor;
50   local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
51
52   g_free (local_monitor->dirname);
53
54   if (local_monitor->mount_monitor)
55     {
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;
59     }
60
61   G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize (object);
62 }
63
64 static void
65 g_local_directory_monitor_set_property (GObject      *object,
66                                         guint         property_id,
67                                         const GValue *value,
68                                         GParamSpec   *pspec)
69 {
70   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
71
72   switch (property_id)
73     {
74     case PROP_DIRNAME:
75       local_monitor->dirname = g_value_dup_string (value);
76       break;
77
78     case PROP_FLAGS:
79       local_monitor->flags = g_value_get_flags (value);
80       break;
81
82     default:
83       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
84       break;
85     }
86 }
87
88 void
89 g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor)
90 {
91   GLocalDirectoryMonitorClass *class;
92
93   class = G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor);
94
95   if (!class->mount_notify && (local_monitor->flags & G_FILE_MONITOR_WATCH_MOUNTS))
96     {
97 #ifdef G_OS_WIN32
98       /*claim everything was mounted */
99       local_monitor->was_mounted = TRUE;
100 #else
101       GUnixMountEntry *mount;
102
103       /* Emulate unmount detection */
104
105       mount = g_unix_mount_at (local_monitor->dirname, NULL);
106
107       local_monitor->was_mounted = mount != NULL;
108
109       if (mount)
110         g_unix_mount_free (mount);
111
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);
115 #endif
116     }
117
118   if (class->start)
119     class->start (local_monitor);
120 }
121
122 static void
123 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
124 {
125   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
126   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
127
128   gobject_class->finalize = g_local_directory_monitor_finalize;
129   gobject_class->set_property = g_local_directory_monitor_set_property;
130
131   file_monitor_class->cancel = g_local_directory_monitor_cancel;
132
133   g_object_class_install_property (gobject_class, 
134                                    PROP_DIRNAME,
135                                    g_param_spec_string ("dirname", 
136                                                         P_("Directory name"), 
137                                                         P_("Directory to monitor"),
138                                                         NULL, 
139                                                         G_PARAM_CONSTRUCT_ONLY|
140                                                         G_PARAM_WRITABLE|
141                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
142   g_object_class_install_property (gobject_class,
143                                    PROP_FLAGS,
144                                    g_param_spec_flags ("flags",
145                                                        P_("Monitor flags"),
146                                                        P_("Monitor flags"),
147                                                        G_TYPE_FILE_MONITOR_FLAGS,
148                                                        0,
149                                                        G_PARAM_CONSTRUCT_ONLY|
150                                                        G_PARAM_WRITABLE|
151                                                        G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
152
153   klass->mount_notify = FALSE;
154 }
155
156 static void
157 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
158 {
159 }
160
161 static void
162 mounts_changed (GUnixMountMonitor *mount_monitor,
163                 gpointer           user_data)
164 {
165   GLocalDirectoryMonitor *local_monitor = user_data;
166 #ifdef G_OS_UNIX
167   GUnixMountEntry *mount;
168 #endif
169   gboolean is_mounted;
170   GFile *file;
171   
172   /* Emulate unmount detection */
173 #ifdef G_OS_UNIX
174   mount = g_unix_mount_at (local_monitor->dirname, NULL);
175   
176   is_mounted = mount != NULL;
177   
178   if (mount)
179     g_unix_mount_free (mount);
180 #else
181   /*claim everything was mounted */
182   is_mounted = TRUE;
183 #endif
184
185   if (local_monitor->was_mounted != is_mounted)
186     {
187       if (local_monitor->was_mounted && !is_mounted)
188         {
189           file = g_file_new_for_path (local_monitor->dirname);
190           g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
191                                      file, NULL,
192                                      G_FILE_MONITOR_EVENT_UNMOUNTED);
193           g_object_unref (file);
194         }
195       local_monitor->was_mounted = is_mounted;
196     }
197 }
198
199 GFileMonitor*
200 _g_local_directory_monitor_new (const char         *dirname,
201                                 GFileMonitorFlags   flags,
202                                 GMainContext       *context,
203                                 gboolean            is_remote_fs,
204                                 gboolean            do_start,
205                                 GError            **error)
206 {
207   GFileMonitor *monitor = NULL;
208   GType type = G_TYPE_INVALID;
209
210   if (is_remote_fs)
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));
214
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));
219
220   if (type != G_TYPE_INVALID)
221     monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, "context", context, NULL));
222   else
223     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
224                          _("Unable to find default local directory monitor type"));
225
226   if (monitor && do_start)
227     g_local_directory_monitor_start (G_LOCAL_DIRECTORY_MONITOR (monitor));
228
229   return monitor;
230 }
231
232 static gboolean
233 g_local_directory_monitor_cancel (GFileMonitor *monitor)
234 {
235   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
236
237   if (local_monitor->mount_monitor)
238     {
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;
242     }
243
244   return TRUE;
245 }