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"
28 #include "gioenumtypes.h"
35 * SECTION:gfilemonitor
36 * @short_description: File Monitor
39 * Monitors a file or directory for changes.
48 G_DEFINE_ABSTRACT_TYPE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT);
52 guint32 last_sent_change_time; /* 0 == not sent */
53 guint32 send_delayed_change_at; /* 0 == never */
54 guint32 send_virtual_changes_done_at; /* 0 == never */
57 struct _GFileMonitorPrivate {
61 /* Rate limiting change events */
62 GHashTable *rate_limiter;
65 guint32 timeout_fires_at;
75 g_file_monitor_set_property (GObject *object,
80 GFileMonitor *monitor;
82 monitor = G_FILE_MONITOR (object);
87 g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 g_file_monitor_get_property (GObject *object,
102 GFileMonitor *monitor;
103 GFileMonitorPrivate *priv;
105 monitor = G_FILE_MONITOR (object);
106 priv = monitor->priv;
110 case PROP_RATE_LIMIT:
111 g_value_set_int (value, priv->rate_limit_msec);
115 g_value_set_boolean (value, priv->cancelled);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 #define DEFAULT_RATE_LIMIT_MSECS 800
125 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
127 static guint signals[LAST_SIGNAL] = { 0 };
130 rate_limiter_free (RateLimiter *limiter)
132 g_object_unref (limiter->file);
133 g_slice_free (RateLimiter, limiter);
137 g_file_monitor_finalize (GObject *object)
139 GFileMonitor *monitor;
141 monitor = G_FILE_MONITOR (object);
143 if (monitor->priv->timeout)
145 g_source_destroy (monitor->priv->timeout);
146 g_source_unref (monitor->priv->timeout);
149 g_hash_table_destroy (monitor->priv->rate_limiter);
151 if (G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize)
152 (*G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize) (object);
156 g_file_monitor_dispose (GObject *object)
158 GFileMonitor *monitor;
160 monitor = G_FILE_MONITOR (object);
162 /* Make sure we cancel on last unref */
163 if (!monitor->priv->cancelled)
164 g_file_monitor_cancel (monitor);
166 if (G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose)
167 (*G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose) (object);
171 g_file_monitor_class_init (GFileMonitorClass *klass)
173 GObjectClass *object_class;
175 g_type_class_add_private (klass, sizeof (GFileMonitorPrivate));
177 object_class = G_OBJECT_CLASS (klass);
178 object_class->finalize = g_file_monitor_finalize;
179 object_class->dispose = g_file_monitor_dispose;
180 object_class->get_property = g_file_monitor_get_property;
181 object_class->set_property = g_file_monitor_set_property;
184 * GFileMonitor::changed:
185 * @monitor: a #GFileMonitor.
187 * @other_file: a #GFile.
188 * @event_type: a #GFileMonitorEvent.
190 * Emitted when a file has been changed.
193 g_signal_new (I_("changed"),
196 G_STRUCT_OFFSET (GFileMonitorClass, changed),
198 _gio_marshal_VOID__OBJECT_OBJECT_ENUM,
200 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
202 g_object_class_install_property (object_class,
204 g_param_spec_int ("rate-limit",
206 P_("The limit of the monitor to watch for changes, in milliseconds"),
208 DEFAULT_RATE_LIMIT_MSECS,
210 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
212 g_object_class_install_property (object_class,
214 g_param_spec_boolean ("cancelled",
216 P_("Whether the monitor has been cancelled"),
219 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
223 g_file_monitor_init (GFileMonitor *monitor)
225 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
227 GFileMonitorPrivate);
228 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
229 monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
230 NULL, (GDestroyNotify) rate_limiter_free);
234 * g_file_monitor_is_cancelled:
235 * @monitor: a #GFileMonitor
237 * Returns whether the monitor is canceled.
239 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
242 g_file_monitor_is_cancelled (GFileMonitor *monitor)
244 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
246 return monitor->priv->cancelled;
250 * g_file_monitor_cancel:
251 * @monitor: a #GFileMonitor.
253 * Cancels a file monitor.
255 * Returns: %TRUE if monitor was cancelled.
258 g_file_monitor_cancel (GFileMonitor* monitor)
260 GFileMonitorClass *klass;
262 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
264 if (monitor->priv->cancelled)
267 monitor->priv->cancelled = TRUE;
268 g_object_notify (G_OBJECT (monitor), "cancelled");
270 klass = G_FILE_MONITOR_GET_CLASS (monitor);
271 return (* klass->cancel) (monitor);
275 * g_file_monitor_set_rate_limit:
276 * @monitor: a #GFileMonitor.
277 * @limit_msecs: a integer with the limit in milliseconds to
280 * Sets the rate limit to which the @monitor will report
281 * consecutive change events to the same file.
285 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
288 GFileMonitorPrivate *priv;
290 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
292 priv = monitor->priv;
293 if (priv->rate_limit_msec != limit_msecs)
295 monitor->priv->rate_limit_msec = limit_msecs;
296 g_object_notify (G_OBJECT (monitor), "rate-limit");
301 get_time_msecs (void)
303 return g_thread_gettime() / (1000 * 1000);
307 time_difference (guint32 from, guint32 to)
314 /* Change event rate limiting support: */
317 new_limiter (GFileMonitor *monitor,
320 RateLimiter *limiter;
322 limiter = g_slice_new0 (RateLimiter);
323 limiter->file = g_object_ref (file);
324 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
330 rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor,
331 RateLimiter *limiter)
333 if (limiter->send_virtual_changes_done_at != 0)
335 g_signal_emit (monitor, signals[CHANGED], 0,
337 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
338 limiter->send_virtual_changes_done_at = 0;
343 rate_limiter_send_delayed_change_now (GFileMonitor *monitor,
344 RateLimiter *limiter,
347 if (limiter->send_delayed_change_at != 0)
349 g_signal_emit (monitor, signals[CHANGED], 0,
351 G_FILE_MONITOR_EVENT_CHANGED);
352 limiter->send_delayed_change_at = 0;
353 limiter->last_sent_change_time = time_now;
360 GFileMonitor *monitor;
364 calc_min_time (GFileMonitor *monitor,
365 RateLimiter *limiter,
374 if (limiter->last_sent_change_time != 0)
376 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventualy */
377 expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
379 if (time_difference (time_now, expire_at) > 0)
382 *min_time = MIN (*min_time,
383 time_difference (time_now, expire_at));
387 if (limiter->send_delayed_change_at != 0)
390 *min_time = MIN (*min_time,
391 time_difference (time_now, limiter->send_delayed_change_at));
394 if (limiter->send_virtual_changes_done_at != 0)
397 *min_time = MIN (*min_time,
398 time_difference (time_now, limiter->send_virtual_changes_done_at));
405 foreach_rate_limiter_fire (gpointer key,
409 RateLimiter *limiter = value;
410 ForEachData *data = user_data;
412 if (limiter->send_delayed_change_at != 0 &&
413 time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
414 rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
416 if (limiter->send_virtual_changes_done_at != 0 &&
417 time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
418 rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
420 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
424 rate_limiter_timeout (gpointer timeout_data)
426 GFileMonitor *monitor = timeout_data;
430 data.min_time = G_MAXUINT32;
431 data.monitor = monitor;
432 data.time_now = get_time_msecs ();
433 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
434 foreach_rate_limiter_fire,
437 /* Remove old timeout */
438 if (monitor->priv->timeout)
440 g_source_destroy (monitor->priv->timeout);
441 g_source_unref (monitor->priv->timeout);
442 monitor->priv->timeout = NULL;
443 monitor->priv->timeout_fires_at = 0;
446 /* Set up new timeout */
447 if (data.min_time != G_MAXUINT32)
449 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
450 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
451 g_source_attach (source, NULL);
453 monitor->priv->timeout = source;
454 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
461 foreach_rate_limiter_update (gpointer key,
465 RateLimiter *limiter = value;
466 ForEachData *data = user_data;
468 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
472 update_rate_limiter_timeout (GFileMonitor *monitor,
478 if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
479 time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
480 return; /* Nothing to do, we already fire earlier than that */
482 data.min_time = G_MAXUINT32;
483 data.monitor = monitor;
484 data.time_now = get_time_msecs ();
485 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
486 foreach_rate_limiter_update,
489 /* Remove old timeout */
490 if (monitor->priv->timeout)
492 g_source_destroy (monitor->priv->timeout);
493 g_source_unref (monitor->priv->timeout);
494 monitor->priv->timeout_fires_at = 0;
495 monitor->priv->timeout = NULL;
498 /* Set up new timeout */
499 if (data.min_time != G_MAXUINT32)
501 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
502 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
503 g_source_attach (source, NULL);
505 monitor->priv->timeout = source;
506 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
511 * g_file_monitor_emit_event:
512 * @monitor: a #GFileMonitor.
514 * @other_file: a #GFile.
515 * @event_type: a set of #GFileMonitorEvent flags.
517 * Emits the #GFileMonitor::changed signal if a change
518 * has taken place. Should be called from file monitor
519 * implementations only.
522 g_file_monitor_emit_event (GFileMonitor *monitor,
525 GFileMonitorEvent event_type)
527 guint32 time_now, since_last;
529 RateLimiter *limiter;
531 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
532 g_return_if_fail (G_IS_FILE (child));
534 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
536 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
540 rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
541 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
542 limiter->send_virtual_changes_done_at = 0;
544 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
545 update_rate_limiter_timeout (monitor, 0);
547 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
551 /* Changed event, rate limit */
552 time_now = get_time_msecs ();
557 since_last = time_difference (limiter->last_sent_change_time, time_now);
558 if (since_last < monitor->priv->rate_limit_msec)
560 /* We ignore this change, but arm a timer so that we can fire it later if we
561 don't get any other events (that kill this timeout) */
563 if (limiter->send_delayed_change_at == 0)
565 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
566 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
572 limiter = new_limiter (monitor, child);
576 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
578 limiter->last_sent_change_time = time_now;
579 limiter->send_delayed_change_at = 0;
580 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventualy */
581 update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
584 /* Schedule a virtual change done. This is removed if we get a real one, and
585 postponed if we get more change events. */
587 limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
588 update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
592 #define __G_FILE_MONITOR_C__
593 #include "gioaliasdef.c"