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
,
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
87 g_local_directory_monitor_constructor (GType type
,
88 guint n_construct_properties
,
89 GObjectConstructParam
*construct_properties
)
92 GLocalDirectoryMonitorClass
*klass
;
93 GObjectClass
*parent_class
;
94 GLocalDirectoryMonitor
*local_monitor
;
95 GFileMonitorFlags flags
= 0;
96 const gchar
*dirname
= NULL
;
99 klass
= G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR
));
100 parent_class
= G_OBJECT_CLASS (g_type_class_peek_parent (klass
));
101 obj
= parent_class
->constructor (type
,
102 n_construct_properties
,
103 construct_properties
);
105 local_monitor
= G_LOCAL_DIRECTORY_MONITOR (obj
);
107 for (i
= 0; i
< n_construct_properties
; i
++)
109 if (strcmp ("dirname", g_param_spec_get_name (construct_properties
[i
].pspec
)) == 0)
111 g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties
[i
].value
));
112 dirname
= g_value_get_string (construct_properties
[i
].value
);
114 if (strcmp ("flags", g_param_spec_get_name (construct_properties
[i
].pspec
)) == 0)
116 g_warn_if_fail (G_VALUE_HOLDS_FLAGS (construct_properties
[i
].value
));
117 flags
= g_value_get_flags (construct_properties
[i
].value
);
121 local_monitor
->dirname
= g_strdup (dirname
);
122 local_monitor
->flags
= flags
;
124 if (!klass
->mount_notify
&&
125 (flags
& G_FILE_MONITOR_WATCH_MOUNTS
))
128 /*claim everything was mounted */
129 local_monitor
->was_mounted
= TRUE
;
131 GUnixMountEntry
*mount
;
133 /* Emulate unmount detection */
135 mount
= g_unix_mount_at (local_monitor
->dirname
, NULL
);
137 local_monitor
->was_mounted
= mount
!= NULL
;
140 g_unix_mount_free (mount
);
142 local_monitor
->mount_monitor
= g_unix_mount_monitor_new ();
143 g_signal_connect_object (local_monitor
->mount_monitor
, "mounts-changed",
144 G_CALLBACK (mounts_changed
), local_monitor
, 0);
152 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass
* klass
)
154 GObjectClass
* gobject_class
= G_OBJECT_CLASS (klass
);
155 GFileMonitorClass
*file_monitor_class
= G_FILE_MONITOR_CLASS (klass
);
157 gobject_class
->finalize
= g_local_directory_monitor_finalize
;
158 gobject_class
->set_property
= g_local_directory_monitor_set_property
;
159 gobject_class
->constructor
= g_local_directory_monitor_constructor
;
161 file_monitor_class
->cancel
= g_local_directory_monitor_cancel
;
163 g_object_class_install_property (gobject_class
,
165 g_param_spec_string ("dirname",
166 P_("Directory name"),
167 P_("Directory to monitor"),
169 G_PARAM_CONSTRUCT_ONLY
|
171 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
172 g_object_class_install_property (gobject_class
,
174 g_param_spec_flags ("flags",
177 G_TYPE_FILE_MONITOR_FLAGS
,
179 G_PARAM_CONSTRUCT_ONLY
|
181 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
183 klass
->mount_notify
= FALSE
;
187 g_local_directory_monitor_init (GLocalDirectoryMonitor
*local_monitor
)
192 mounts_changed (GUnixMountMonitor
*mount_monitor
,
195 GLocalDirectoryMonitor
*local_monitor
= user_data
;
197 GUnixMountEntry
*mount
;
202 /* Emulate unmount detection */
204 mount
= g_unix_mount_at (local_monitor
->dirname
, NULL
);
206 is_mounted
= mount
!= NULL
;
209 g_unix_mount_free (mount
);
211 /*claim everything was mounted */
215 if (local_monitor
->was_mounted
!= is_mounted
)
217 if (local_monitor
->was_mounted
&& !is_mounted
)
219 file
= g_file_new_for_path (local_monitor
->dirname
);
220 g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor
),
222 G_FILE_MONITOR_EVENT_UNMOUNTED
);
223 g_object_unref (file
);
225 local_monitor
->was_mounted
= is_mounted
;
230 _g_local_directory_monitor_new (const char *dirname
,
231 GFileMonitorFlags flags
,
232 gboolean is_remote_fs
,
235 GFileMonitor
*monitor
= NULL
;
236 GType type
= G_TYPE_INVALID
;
239 type
= _g_io_module_get_default_type (G_NFS_DIRECTORY_MONITOR_EXTENSION_POINT_NAME
,
240 "GIO_USE_FILE_MONITOR",
241 G_STRUCT_OFFSET (GLocalDirectoryMonitorClass
, is_supported
));
243 if (type
== G_TYPE_INVALID
)
244 type
= _g_io_module_get_default_type (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME
,
245 "GIO_USE_FILE_MONITOR",
246 G_STRUCT_OFFSET (GLocalDirectoryMonitorClass
, is_supported
));
248 if (type
!= G_TYPE_INVALID
)
249 monitor
= G_FILE_MONITOR (g_object_new (type
, "dirname", dirname
, "flags", flags
, NULL
));
251 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
252 _("Unable to find default local directory monitor type"));
258 g_local_directory_monitor_cancel (GFileMonitor
*monitor
)
260 GLocalDirectoryMonitor
*local_monitor
= G_LOCAL_DIRECTORY_MONITOR (monitor
);
262 if (local_monitor
->mount_monitor
)
264 g_signal_handlers_disconnect_by_func (local_monitor
->mount_monitor
, mounts_changed
, local_monitor
);
265 g_object_unref (local_monitor
->mount_monitor
);
266 local_monitor
->mount_monitor
= NULL
;