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.1 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>
24 #include "gfilemonitor.h"
25 #include "gioenumtypes.h"
26 #include "gmarshal-internal.h"
32 * SECTION:gfilemonitor
33 * @short_description: File Monitor
36 * Monitors a file or directory for changes.
38 * To obtain a #GFileMonitor for a file or directory, use
39 * g_file_monitor(), g_file_monitor_file(), or
40 * g_file_monitor_directory().
42 * To get informed about changes to the file or directory you are
43 * monitoring, connect to the #GFileMonitor::changed signal. The
44 * signal will be emitted in the
45 * [thread-default main context][g-main-context-push-thread-default]
46 * of the thread that the monitor was created in
47 * (though if the global default main context is blocked, this may
48 * cause notifications to be blocked even if the thread-default
49 * context is still running).
52 #define DEFAULT_RATE_LIMIT_MSECS 800
54 struct _GFileMonitorPrivate
59 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
68 static guint g_file_monitor_changed_signal;
71 g_file_monitor_set_property (GObject *object,
76 //GFileMonitor *monitor;
78 //monitor = G_FILE_MONITOR (object);
83 /* not supported by default */
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 g_file_monitor_get_property (GObject *object,
100 case PROP_RATE_LIMIT:
101 /* we expect this to be overridden... */
102 g_value_set_int (value, DEFAULT_RATE_LIMIT_MSECS);
106 //g_mutex_lock (&fms->lock);
107 g_value_set_boolean (value, FALSE);//fms->cancelled);
108 //g_mutex_unlock (&fms->lock);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_file_monitor_dispose (GObject *object)
120 GFileMonitor *monitor = G_FILE_MONITOR (object);
122 /* Make sure we cancel on last unref */
123 g_file_monitor_cancel (monitor);
125 G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
129 g_file_monitor_init (GFileMonitor *monitor)
131 monitor->priv = g_file_monitor_get_instance_private (monitor);
135 g_file_monitor_class_init (GFileMonitorClass *klass)
137 GObjectClass *object_class;
139 object_class = G_OBJECT_CLASS (klass);
140 object_class->dispose = g_file_monitor_dispose;
141 object_class->get_property = g_file_monitor_get_property;
142 object_class->set_property = g_file_monitor_set_property;
145 * GFileMonitor::changed:
146 * @monitor: a #GFileMonitor.
148 * @other_file: (nullable): a #GFile or #NULL.
149 * @event_type: a #GFileMonitorEvent.
151 * Emitted when @file has been changed.
153 * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and
154 * the information is available (and if supported by the backend),
155 * @event_type may be %G_FILE_MONITOR_EVENT_RENAMED,
156 * %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT.
158 * In all cases @file will be a child of the monitored directory. For
159 * renames, @file will be the old name and @other_file is the new
160 * name. For "moved in" events, @file is the name of the file that
161 * appeared and @other_file is the old name that it was moved from (in
162 * another directory). For "moved out" events, @file is the name of
163 * the file that used to be in this directory and @other_file is the
164 * name of the file at its new location.
166 * It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as
167 * equivalent to %G_FILE_MONITOR_EVENT_CREATED and
168 * %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to
169 * %G_FILE_MONITOR_EVENT_DELETED, with extra information.
170 * %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create
171 * pair. This is exactly how the events will be reported in the case
172 * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use.
174 * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
175 * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
176 * old path, and @other_file will be set to a #GFile containing the new path.
178 * In all the other cases, @other_file will be set to #NULL.
180 g_file_monitor_changed_signal = g_signal_new (I_("changed"),
183 G_STRUCT_OFFSET (GFileMonitorClass, changed),
185 _g_cclosure_marshal_VOID__OBJECT_OBJECT_ENUM,
187 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
188 g_signal_set_va_marshaller (g_file_monitor_changed_signal,
189 G_TYPE_FROM_CLASS (klass),
190 _g_cclosure_marshal_VOID__OBJECT_OBJECT_ENUMv);
192 g_object_class_install_property (object_class, PROP_RATE_LIMIT,
193 g_param_spec_int ("rate-limit",
195 P_("The limit of the monitor to watch for changes, in milliseconds"),
196 0, G_MAXINT, DEFAULT_RATE_LIMIT_MSECS, G_PARAM_READWRITE |
197 G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (object_class, PROP_CANCELLED,
200 g_param_spec_boolean ("cancelled",
202 P_("Whether the monitor has been cancelled"),
203 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
207 * g_file_monitor_is_cancelled:
208 * @monitor: a #GFileMonitor
210 * Returns whether the monitor is canceled.
212 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
215 g_file_monitor_is_cancelled (GFileMonitor *monitor)
219 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
221 res = monitor->priv->cancelled;
227 * g_file_monitor_cancel:
228 * @monitor: a #GFileMonitor.
230 * Cancels a file monitor.
232 * Returns: always %TRUE
235 g_file_monitor_cancel (GFileMonitor *monitor)
237 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
239 if (!monitor->priv->cancelled)
241 G_FILE_MONITOR_GET_CLASS (monitor)->cancel (monitor);
243 monitor->priv->cancelled = TRUE;
244 g_object_notify (G_OBJECT (monitor), "cancelled");
251 * g_file_monitor_set_rate_limit:
252 * @monitor: a #GFileMonitor.
253 * @limit_msecs: a non-negative integer with the limit in milliseconds
254 * to poll for changes
256 * Sets the rate limit to which the @monitor will report
257 * consecutive change events to the same file.
260 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
263 g_object_set (monitor, "rate-limit", limit_msecs, NULL);
267 * g_file_monitor_emit_event:
268 * @monitor: a #GFileMonitor.
270 * @other_file: a #GFile.
271 * @event_type: a set of #GFileMonitorEvent flags.
273 * Emits the #GFileMonitor::changed signal if a change
274 * has taken place. Should be called from file monitor
275 * implementations only.
277 * Implementations are responsible to call this method from the
278 * [thread-default main context][g-main-context-push-thread-default] of the
279 * thread that the monitor was created in.
282 g_file_monitor_emit_event (GFileMonitor *monitor,
285 GFileMonitorEvent event_type)
287 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
288 g_return_if_fail (G_IS_FILE (child));
289 g_return_if_fail (!other_file || G_IS_FILE (other_file));
291 if (monitor->priv->cancelled)
294 g_signal_emit (monitor, g_file_monitor_changed_signal, 0, child, other_file, event_type);