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>
26 #include "gfilemonitor.h"
27 #include "gio-marshal.h"
34 * SECTION:gfilemonitor
35 * @short_description: File Monitor
37 * @see_also: #GDirectoryMonitor
39 * Monitors a file for changes.
48 G_DEFINE_ABSTRACT_TYPE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT);
50 struct _GFileMonitorPrivate {
54 /* Rate limiting change events */
55 guint32 last_sent_change_time; /* Some monotonic clock in msecs */
56 GFile *last_sent_change_file;
58 guint send_delayed_change_timeout;
60 /* Virtual CHANGES_DONE_HINT emission */
61 GSource *virtual_changes_done_timeout;
62 GFile *virtual_changes_done_file;
72 g_file_monitor_set_property (GObject *object,
77 GFileMonitor *monitor;
79 monitor = G_FILE_MONITOR (object);
84 g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 g_file_monitor_get_property (GObject *object,
99 GFileMonitor *monitor;
100 GFileMonitorPrivate *priv;
102 monitor = G_FILE_MONITOR (object);
103 priv = monitor->priv;
107 case PROP_RATE_LIMIT:
108 g_value_set_int (value, priv->rate_limit_msec);
112 g_value_set_boolean (value, priv->cancelled);
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 #define DEFAULT_RATE_LIMIT_MSECS 800
122 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
124 static guint signals[LAST_SIGNAL] = { 0 };
128 g_file_monitor_finalize (GObject *object)
130 GFileMonitor *monitor;
132 monitor = G_FILE_MONITOR (object);
134 if (monitor->priv->last_sent_change_file)
135 g_object_unref (monitor->priv->last_sent_change_file);
137 if (monitor->priv->send_delayed_change_timeout != 0)
138 g_source_remove (monitor->priv->send_delayed_change_timeout);
140 if (monitor->priv->virtual_changes_done_file)
141 g_object_unref (monitor->priv->virtual_changes_done_file);
143 if (monitor->priv->virtual_changes_done_timeout)
144 g_source_destroy (monitor->priv->virtual_changes_done_timeout);
146 if (G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize)
147 (*G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize) (object);
151 g_file_monitor_dispose (GObject *object)
153 GFileMonitor *monitor;
155 monitor = G_FILE_MONITOR (object);
157 /* Make sure we cancel on last unref */
158 if (!monitor->priv->cancelled)
159 g_file_monitor_cancel (monitor);
161 if (G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose)
162 (*G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose) (object);
166 g_file_monitor_class_init (GFileMonitorClass *klass)
168 GObjectClass *object_class;
170 g_type_class_add_private (klass, sizeof (GFileMonitorPrivate));
172 object_class = G_OBJECT_CLASS (klass);
173 object_class->finalize = g_file_monitor_finalize;
174 object_class->dispose = g_file_monitor_dispose;
175 object_class->get_property = g_file_monitor_get_property;
176 object_class->set_property = g_file_monitor_set_property;
179 * GFileMonitor::changed:
180 * @monitor: a #GFileMonitor.
182 * @other_file: a #GFile.
183 * @event_type: a #GFileMonitorEvent.
185 * Emitted when a file has been changed.
188 g_signal_new (I_("changed"),
191 G_STRUCT_OFFSET (GFileMonitorClass, changed),
193 _gio_marshal_VOID__OBJECT_OBJECT_INT,
195 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_INT);
197 g_object_class_install_property (object_class,
199 g_param_spec_int ("rate-limit",
201 P_("The limit of the monitor to watch for changes, in milliseconds"),
203 DEFAULT_RATE_LIMIT_MSECS,
205 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
207 g_object_class_install_property (object_class,
209 g_param_spec_boolean ("cancelled",
211 P_("Whether the monitor has been cancelled"),
214 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
218 g_file_monitor_init (GFileMonitor *monitor)
220 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
222 GFileMonitorPrivate);
223 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
227 * g_file_monitor_is_cancelled:
228 * @monitor: a #GFileMonitor
230 * Returns whether the monitor is canceled.
232 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
235 g_file_monitor_is_cancelled (GFileMonitor *monitor)
237 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
239 return monitor->priv->cancelled;
243 * g_file_monitor_cancel:
244 * @monitor: a #GFileMonitor.
246 * Cancels a file monitor.
248 * Returns: %TRUE if monitor was cancelled.
251 g_file_monitor_cancel (GFileMonitor* monitor)
253 GFileMonitorClass *klass;
255 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
257 if (monitor->priv->cancelled)
260 monitor->priv->cancelled = TRUE;
261 g_object_notify (G_OBJECT (monitor), "cancelled");
263 klass = G_FILE_MONITOR_GET_CLASS (monitor);
264 return (* klass->cancel) (monitor);
268 * g_file_monitor_set_rate_limit:
269 * @monitor: a #GFileMonitor.
270 * @limit_msecs: a integer with the limit in milliseconds to
273 * Sets the rate limit to which the @monitor will report
274 * consecutive change events to the same file.
278 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
281 GFileMonitorPrivate *priv;
282 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
283 priv = monitor->priv;
284 if (priv->rate_limit_msec != limit_msecs)
286 monitor->priv->rate_limit_msec = limit_msecs;
287 g_object_notify (G_OBJECT (monitor), "rate-limit");
292 get_time_msecs (void)
294 return g_thread_gettime() / (1000 * 1000);
298 time_difference (guint32 from, guint32 to)
305 /* Change event rate limiting support: */
308 update_last_sent_change (GFileMonitor *monitor, GFile *file, guint32 time_now)
310 if (monitor->priv->last_sent_change_file != file)
312 if (monitor->priv->last_sent_change_file)
314 g_object_unref (monitor->priv->last_sent_change_file);
315 monitor->priv->last_sent_change_file = NULL;
318 monitor->priv->last_sent_change_file = g_object_ref (file);
321 monitor->priv->last_sent_change_time = time_now;
325 send_delayed_change_now (GFileMonitor *monitor)
327 if (monitor->priv->send_delayed_change_timeout)
329 g_signal_emit (monitor, signals[CHANGED], 0,
330 monitor->priv->last_sent_change_file, NULL,
331 G_FILE_MONITOR_EVENT_CHANGED);
333 g_source_remove (monitor->priv->send_delayed_change_timeout);
334 monitor->priv->send_delayed_change_timeout = 0;
336 /* Same file, new last_sent time */
337 monitor->priv->last_sent_change_time = get_time_msecs ();
342 delayed_changed_event_timeout (gpointer data)
344 GFileMonitor *monitor = data;
346 send_delayed_change_now (monitor);
352 schedule_delayed_change (GFileMonitor *monitor, GFile *file, guint32 delay_msec)
354 if (monitor->priv->send_delayed_change_timeout == 0) /* Only set the timeout once */
356 monitor->priv->send_delayed_change_timeout =
357 g_timeout_add (delay_msec, delayed_changed_event_timeout, monitor);
362 cancel_delayed_change (GFileMonitor *monitor)
364 if (monitor->priv->send_delayed_change_timeout != 0)
366 g_source_remove (monitor->priv->send_delayed_change_timeout);
367 monitor->priv->send_delayed_change_timeout = 0;
371 /* Virtual changes_done_hint support: */
374 send_virtual_changes_done_now (GFileMonitor *monitor)
376 if (monitor->priv->virtual_changes_done_timeout)
378 g_signal_emit (monitor, signals[CHANGED], 0,
379 monitor->priv->virtual_changes_done_file, NULL,
380 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
382 g_source_destroy (monitor->priv->virtual_changes_done_timeout);
383 monitor->priv->virtual_changes_done_timeout = NULL;
385 g_object_unref (monitor->priv->virtual_changes_done_file);
386 monitor->priv->virtual_changes_done_file = NULL;
391 virtual_changes_done_timeout (gpointer data)
393 GFileMonitor *monitor = data;
395 send_virtual_changes_done_now (monitor);
401 schedule_virtual_change_done (GFileMonitor *monitor, GFile *file)
405 source = g_timeout_source_new_seconds (DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS);
407 g_source_set_callback (source, virtual_changes_done_timeout, monitor, NULL);
408 g_source_attach (source, NULL);
409 monitor->priv->virtual_changes_done_timeout = source;
410 monitor->priv->virtual_changes_done_file = g_object_ref (file);
411 g_source_unref (source);
415 cancel_virtual_changes_done (GFileMonitor *monitor)
417 if (monitor->priv->virtual_changes_done_timeout)
419 g_source_destroy (monitor->priv->virtual_changes_done_timeout);
420 monitor->priv->virtual_changes_done_timeout = NULL;
422 g_object_unref (monitor->priv->virtual_changes_done_file);
423 monitor->priv->virtual_changes_done_file = NULL;
428 * g_file_monitor_emit_event:
429 * @monitor: a #GFileMonitor.
431 * @other_file: a #GFile.
432 * @event_type: a #GFileMonitorEvent
434 * Emits a file monitor event. This is mainly necessary for implementations
439 g_file_monitor_emit_event (GFileMonitor *monitor,
442 GFileMonitorEvent event_type)
444 guint32 time_now, since_last;
447 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
448 g_return_if_fail (G_IS_FILE (file));
450 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
452 send_delayed_change_now (monitor);
453 update_last_sent_change (monitor, NULL, 0);
454 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
455 cancel_virtual_changes_done (monitor);
457 send_virtual_changes_done_now (monitor);
458 g_signal_emit (monitor, signals[CHANGED], 0, file, other_file, event_type);
462 time_now = get_time_msecs ();
465 if (monitor->priv->last_sent_change_file)
467 since_last = time_difference (monitor->priv->last_sent_change_time, time_now);
468 if (since_last < monitor->priv->rate_limit_msec)
470 /* We ignore this change, but arm a timer so that we can fire it later if we
471 don't get any other events (that kill this timeout) */
473 schedule_delayed_change (monitor, file,
474 monitor->priv->rate_limit_msec - since_last);
480 g_signal_emit (monitor, signals[CHANGED], 0, file, other_file, event_type);
482 cancel_delayed_change (monitor);
483 update_last_sent_change (monitor, file, time_now);
486 /* Schedule a virtual change done. This is removed if we get a real one, and
487 postponed if we get more change events. */
488 cancel_virtual_changes_done (monitor);
489 schedule_virtual_change_done (monitor, file);
493 #define __G_FILE_MONITOR_C__
494 #include "gioaliasdef.c"