Trivial formatting fixes
[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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
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"
31
32 #include <string.h>
33
34
35 enum
36 {
37   PROP_0,
38   PROP_DIRNAME,
39   PROP_FLAGS
40 };
41
42 static gboolean g_local_directory_monitor_cancel (GFileMonitor      *monitor);
43 static void     mounts_changed                   (GUnixMountMonitor *mount_monitor, 
44                                                   gpointer           user_data);
45
46 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
47
48 static void
49 g_local_directory_monitor_finalize (GObject *object)
50 {
51   GLocalDirectoryMonitor *local_monitor;
52   local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
53
54   g_free (local_monitor->dirname);
55
56   if (local_monitor->mount_monitor)
57     {
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;
61     }
62
63   G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize (object);
64 }
65
66 static void
67 g_local_directory_monitor_set_property (GObject      *object,
68                                         guint         property_id,
69                                         const GValue *value,
70                                         GParamSpec   *pspec)
71 {
72   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
73
74   switch (property_id)
75     {
76     case PROP_DIRNAME:
77       local_monitor->dirname = g_value_dup_string (value);
78       break;
79
80     case PROP_FLAGS:
81       local_monitor->flags = g_value_get_flags (value);
82       break;
83
84     default:
85       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86       break;
87     }
88 }
89
90 void
91 g_local_directory_monitor_start (GLocalDirectoryMonitor *local_monitor)
92 {
93   GLocalDirectoryMonitorClass *class;
94
95   class = G_LOCAL_DIRECTORY_MONITOR_GET_CLASS (local_monitor);
96
97   if (!class->mount_notify && (local_monitor->flags & G_FILE_MONITOR_WATCH_MOUNTS))
98     {
99 #ifdef G_OS_WIN32
100       /*claim everything was mounted */
101       local_monitor->was_mounted = TRUE;
102 #else
103       GUnixMountEntry *mount;
104
105       /* Emulate unmount detection */
106
107       mount = g_unix_mount_at (local_monitor->dirname, NULL);
108
109       local_monitor->was_mounted = mount != NULL;
110
111       if (mount)
112         g_unix_mount_free (mount);
113
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
118     }
119
120   if (class->start)
121     class->start (local_monitor);
122 }
123
124 static void
125 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
126 {
127   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
128   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
129
130   gobject_class->finalize = g_local_directory_monitor_finalize;
131   gobject_class->set_property = g_local_directory_monitor_set_property;
132
133   file_monitor_class->cancel = g_local_directory_monitor_cancel;
134
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,
150                                                        0,
151                                                        G_PARAM_CONSTRUCT_ONLY|
152                                                        G_PARAM_WRITABLE|
153                                                        G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
154
155   klass->mount_notify = FALSE;
156 }
157
158 static void
159 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
160 {
161 }
162
163 static void
164 mounts_changed (GUnixMountMonitor *mount_monitor,
165                 gpointer           user_data)
166 {
167   GLocalDirectoryMonitor *local_monitor = user_data;
168 #ifdef G_OS_UNIX
169   GUnixMountEntry *mount;
170 #endif
171   gboolean is_mounted;
172   GFile *file;
173   
174   /* Emulate unmount detection */
175 #ifdef G_OS_UNIX
176   mount = g_unix_mount_at (local_monitor->dirname, NULL);
177   
178   is_mounted = mount != NULL;
179   
180   if (mount)
181     g_unix_mount_free (mount);
182 #else
183   /*claim everything was mounted */
184   is_mounted = TRUE;
185 #endif
186
187   if (local_monitor->was_mounted != is_mounted)
188     {
189       if (local_monitor->was_mounted && !is_mounted)
190         {
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);
196         }
197       local_monitor->was_mounted = is_mounted;
198     }
199 }
200
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)
208 {
209   GFileMonitor *monitor = NULL;
210   GType type = G_TYPE_INVALID;
211
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));
216
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));
221
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"));
227
228   if (monitor && do_start)
229     g_local_directory_monitor_start (G_LOCAL_DIRECTORY_MONITOR (monitor));
230
231   return monitor;
232 }
233
234 static gboolean
235 g_local_directory_monitor_cancel (GFileMonitor *monitor)
236 {
237   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
238
239   if (local_monitor->mount_monitor)
240     {
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;
244     }
245
246   return TRUE;
247 }