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
36 * @see_also: #GDirectoryMonitor
38 * Monitors a file for changes.
47 G_DEFINE_ABSTRACT_TYPE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT);
49 struct _GFileMonitorPrivate {
53 /* Rate limiting change events */
54 guint32 last_sent_change_time; /* Some monotonic clock in msecs */
55 GFile *last_sent_change_file;
57 guint send_delayed_change_timeout;
59 /* Virtual CHANGES_DONE_HINT emission */
60 GSource *virtual_changes_done_timeout;
61 GFile *virtual_changes_done_file;
71 g_file_monitor_set_property (GObject *object,
76 GFileMonitor *monitor;
78 monitor = G_FILE_MONITOR (object);
83 g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 g_file_monitor_get_property (GObject *object,
98 GFileMonitor *monitor;
99 GFileMonitorPrivate *priv;
101 monitor = G_FILE_MONITOR (object);
102 priv = monitor->priv;
106 case PROP_RATE_LIMIT:
107 g_value_set_int (value, priv->rate_limit_msec);
111 g_value_set_boolean (value, priv->cancelled);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 #define DEFAULT_RATE_LIMIT_MSECS 800
121 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
123 static guint signals[LAST_SIGNAL] = { 0 };
127 g_file_monitor_finalize (GObject *object)
129 GFileMonitor *monitor;
131 monitor = G_FILE_MONITOR (object);
133 if (monitor->priv->last_sent_change_file)
134 g_object_unref (monitor->priv->last_sent_change_file);
136 if (monitor->priv->send_delayed_change_timeout != 0)
137 g_source_remove (monitor->priv->send_delayed_change_timeout);
139 if (monitor->priv->virtual_changes_done_file)
140 g_object_unref (monitor->priv->virtual_changes_done_file);
142 if (monitor->priv->virtual_changes_done_timeout)
143 g_source_destroy (monitor->priv->virtual_changes_done_timeout);
145 if (G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize)
146 (*G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize) (object);
150 g_file_monitor_dispose (GObject *object)
152 GFileMonitor *monitor;
154 monitor = G_FILE_MONITOR (object);
156 /* Make sure we cancel on last unref */
157 if (!monitor->priv->cancelled)
158 g_file_monitor_cancel (monitor);
160 if (G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose)
161 (*G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose) (object);
165 g_file_monitor_class_init (GFileMonitorClass *klass)
167 GObjectClass *object_class;
169 g_type_class_add_private (klass, sizeof (GFileMonitorPrivate));
171 object_class = G_OBJECT_CLASS (klass);
172 object_class->finalize = g_file_monitor_finalize;
173 object_class->dispose = g_file_monitor_dispose;
174 object_class->get_property = g_file_monitor_get_property;
175 object_class->set_property = g_file_monitor_set_property;
178 * GFileMonitor::changed:
179 * @monitor: a #GFileMonitor.
181 * @other_file: a #GFile.
182 * @event_type: a #GFileMonitorEvent.
184 * Emitted when a file has been changed.
187 g_signal_new (I_("changed"),
190 G_STRUCT_OFFSET (GFileMonitorClass, changed),
192 _gio_marshal_VOID__OBJECT_OBJECT_INT,
194 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_INT);
196 g_object_class_install_property (object_class,
198 g_param_spec_int ("rate-limit",
200 P_("The limit of the monitor to watch for changes, in milliseconds"),
202 DEFAULT_RATE_LIMIT_MSECS,
204 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
206 g_object_class_install_property (object_class,
208 g_param_spec_boolean ("cancelled",
210 P_("Whether the monitor has been cancelled"),
213 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
217 g_file_monitor_init (GFileMonitor *monitor)
219 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
221 GFileMonitorPrivate);
222 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
226 * g_file_monitor_is_cancelled:
227 * @monitor: a #GFileMonitor
229 * Returns whether the monitor is canceled.
231 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
234 g_file_monitor_is_cancelled (GFileMonitor *monitor)
236 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
238 return monitor->priv->cancelled;
242 * g_file_monitor_cancel:
243 * @monitor: a #GFileMonitor.
245 * Cancels a file monitor.
247 * Returns: %TRUE if monitor was cancelled.
250 g_file_monitor_cancel (GFileMonitor* monitor)
252 GFileMonitorClass *klass;
254 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
256 if (monitor->priv->cancelled)
259 monitor->priv->cancelled = TRUE;
260 g_object_notify (G_OBJECT (monitor), "cancelled");
262 klass = G_FILE_MONITOR_GET_CLASS (monitor);
263 return (* klass->cancel) (monitor);
267 * g_file_monitor_set_rate_limit:
268 * @monitor: a #GFileMonitor.
269 * @limit_msecs: a integer with the limit in milliseconds to
272 * Sets the rate limit to which the @monitor will poll for changes
277 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
280 GFileMonitorPrivate *priv;
281 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
282 priv = monitor->priv;
283 if (priv->rate_limit_msec != limit_msecs)
285 monitor->priv->rate_limit_msec = limit_msecs;
286 g_object_notify (G_OBJECT (monitor), "rate-limit");
291 get_time_msecs (void)
293 return g_thread_gettime() / (1000 * 1000);
297 time_difference (guint32 from, guint32 to)
304 /* Change event rate limiting support: */
307 update_last_sent_change (GFileMonitor *monitor, GFile *file, guint32 time_now)
309 if (monitor->priv->last_sent_change_file != file)
311 if (monitor->priv->last_sent_change_file)
313 g_object_unref (monitor->priv->last_sent_change_file);
314 monitor->priv->last_sent_change_file = NULL;
317 monitor->priv->last_sent_change_file = g_object_ref (file);
320 monitor->priv->last_sent_change_time = time_now;
324 send_delayed_change_now (GFileMonitor *monitor)
326 if (monitor->priv->send_delayed_change_timeout)
328 g_signal_emit (monitor, signals[CHANGED], 0,
329 monitor->priv->last_sent_change_file, NULL,
330 G_FILE_MONITOR_EVENT_CHANGED);
332 g_source_remove (monitor->priv->send_delayed_change_timeout);
333 monitor->priv->send_delayed_change_timeout = 0;
335 /* Same file, new last_sent time */
336 monitor->priv->last_sent_change_time = get_time_msecs ();
341 delayed_changed_event_timeout (gpointer data)
343 GFileMonitor *monitor = data;
345 send_delayed_change_now (monitor);
351 schedule_delayed_change (GFileMonitor *monitor, GFile *file, guint32 delay_msec)
353 if (monitor->priv->send_delayed_change_timeout == 0) /* Only set the timeout once */
355 monitor->priv->send_delayed_change_timeout =
356 g_timeout_add (delay_msec, delayed_changed_event_timeout, monitor);
361 cancel_delayed_change (GFileMonitor *monitor)
363 if (monitor->priv->send_delayed_change_timeout != 0)
365 g_source_remove (monitor->priv->send_delayed_change_timeout);
366 monitor->priv->send_delayed_change_timeout = 0;
370 /* Virtual changes_done_hint support: */
373 send_virtual_changes_done_now (GFileMonitor *monitor)
375 if (monitor->priv->virtual_changes_done_timeout)
377 g_signal_emit (monitor, signals[CHANGED], 0,
378 monitor->priv->virtual_changes_done_file, NULL,
379 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
381 g_source_destroy (monitor->priv->virtual_changes_done_timeout);
382 monitor->priv->virtual_changes_done_timeout = NULL;
384 g_object_unref (monitor->priv->virtual_changes_done_file);
385 monitor->priv->virtual_changes_done_file = NULL;
390 virtual_changes_done_timeout (gpointer data)
392 GFileMonitor *monitor = data;
394 send_virtual_changes_done_now (monitor);
400 schedule_virtual_change_done (GFileMonitor *monitor, GFile *file)
404 source = g_timeout_source_new_seconds (DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS);
406 g_source_set_callback (source, virtual_changes_done_timeout, monitor, NULL);
407 g_source_attach (source, NULL);
408 monitor->priv->virtual_changes_done_timeout = source;
409 monitor->priv->virtual_changes_done_file = g_object_ref (file);
410 g_source_unref (source);
414 cancel_virtual_changes_done (GFileMonitor *monitor)
416 if (monitor->priv->virtual_changes_done_timeout)
418 g_source_destroy (monitor->priv->virtual_changes_done_timeout);
419 monitor->priv->virtual_changes_done_timeout = NULL;
421 g_object_unref (monitor->priv->virtual_changes_done_file);
422 monitor->priv->virtual_changes_done_file = NULL;
427 * g_file_monitor_emit_event:
428 * @monitor: a #GFileMonitor.
430 * @other_file: a #GFile.
431 * @event_type: a #GFileMonitorEvent
433 * Emits a file monitor event. This is mainly necessary for implementations
438 g_file_monitor_emit_event (GFileMonitor *monitor,
441 GFileMonitorEvent event_type)
443 guint32 time_now, since_last;
446 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
447 g_return_if_fail (G_IS_FILE (file));
449 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
451 send_delayed_change_now (monitor);
452 update_last_sent_change (monitor, NULL, 0);
453 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
454 cancel_virtual_changes_done (monitor);
456 send_virtual_changes_done_now (monitor);
457 g_signal_emit (monitor, signals[CHANGED], 0, file, other_file, event_type);
461 time_now = get_time_msecs ();
464 if (monitor->priv->last_sent_change_file)
466 since_last = time_difference (monitor->priv->last_sent_change_time, time_now);
467 if (since_last < monitor->priv->rate_limit_msec)
469 /* We ignore this change, but arm a timer so that we can fire it later if we
470 don't get any other events (that kill this timeout) */
472 schedule_delayed_change (monitor, file,
473 monitor->priv->rate_limit_msec - since_last);
479 g_signal_emit (monitor, signals[CHANGED], 0, file, other_file, event_type);
481 cancel_delayed_change (monitor);
482 update_last_sent_change (monitor, file, time_now);
485 /* Schedule a virtual change done. This is removed if we get a real one, and
486 postponed if we get more change events. */
487 cancel_virtual_changes_done (monitor);
488 schedule_virtual_change_done (monitor, file);
492 #define __G_FILE_MONITOR_C__
493 #include "gioaliasdef.c"