docs: Add GIOModuleScope and GIOModuleScopeFlags
[glib.git] / gio / gfilemonitor.c
blobb90e5d1421e8987a417507f267718aab9a7cb3f4
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
24 #include <string.h>
26 #include "gfilemonitor.h"
27 #include "gioenumtypes.h"
28 #include "gfile.h"
29 #include "gvfs.h"
30 #include "glibintl.h"
33 struct _FileChange;
34 typedef struct _FileChange FileChange;
35 static void file_change_free (FileChange *change);
37 /**
38 * SECTION:gfilemonitor
39 * @short_description: File Monitor
40 * @include: gio/gio.h
42 * Monitors a file or directory for changes.
44 * To obtain a #GFileMonitor for a file or directory, use
45 * g_file_monitor(), g_file_monitor_file(), or
46 * g_file_monitor_directory().
48 * To get informed about changes to the file or directory you are
49 * monitoring, connect to the #GFileMonitor::changed signal. The
50 * signal will be emitted in the <link
51 * linkend="g-main-context-push-thread-default">thread-default main
52 * context</link> of the thread that the monitor was created in
53 * (though if the global default main context is blocked, this may
54 * cause notifications to be blocked even if the thread-default
55 * context is still running).
56 **/
58 G_LOCK_DEFINE_STATIC(cancelled);
60 enum {
61 CHANGED,
62 LAST_SIGNAL
65 /* work around a limitation of the aliasing foo */
66 #undef g_file_monitor
68 G_DEFINE_ABSTRACT_TYPE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT);
70 typedef struct {
71 GFile *file;
72 guint32 last_sent_change_time; /* 0 == not sent */
73 guint32 send_delayed_change_at; /* 0 == never */
74 guint32 send_virtual_changes_done_at; /* 0 == never */
75 } RateLimiter;
77 struct _GFileMonitorPrivate {
78 gboolean cancelled;
79 int rate_limit_msec;
81 /* Rate limiting change events */
82 GHashTable *rate_limiter;
84 GSource *pending_file_change_source;
85 GSList *pending_file_changes; /* FileChange */
87 GSource *timeout;
88 guint32 timeout_fires_at;
90 GMainContext *context;
93 enum {
94 PROP_0,
95 PROP_RATE_LIMIT,
96 PROP_CANCELLED
99 static void
100 g_file_monitor_set_property (GObject *object,
101 guint prop_id,
102 const GValue *value,
103 GParamSpec *pspec)
105 GFileMonitor *monitor;
107 monitor = G_FILE_MONITOR (object);
109 switch (prop_id)
111 case PROP_RATE_LIMIT:
112 g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
113 break;
115 default:
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 break;
121 static void
122 g_file_monitor_get_property (GObject *object,
123 guint prop_id,
124 GValue *value,
125 GParamSpec *pspec)
127 GFileMonitor *monitor;
128 GFileMonitorPrivate *priv;
130 monitor = G_FILE_MONITOR (object);
131 priv = monitor->priv;
133 switch (prop_id)
135 case PROP_RATE_LIMIT:
136 g_value_set_int (value, priv->rate_limit_msec);
137 break;
139 case PROP_CANCELLED:
140 G_LOCK (cancelled);
141 g_value_set_boolean (value, priv->cancelled);
142 G_UNLOCK (cancelled);
143 break;
145 default:
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 break;
151 #define DEFAULT_RATE_LIMIT_MSECS 800
152 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
154 static guint signals[LAST_SIGNAL] = { 0 };
156 static void
157 rate_limiter_free (RateLimiter *limiter)
159 g_object_unref (limiter->file);
160 g_slice_free (RateLimiter, limiter);
163 static void
164 g_file_monitor_finalize (GObject *object)
166 GFileMonitor *monitor;
168 monitor = G_FILE_MONITOR (object);
170 if (monitor->priv->timeout)
172 g_source_destroy (monitor->priv->timeout);
173 g_source_unref (monitor->priv->timeout);
176 g_hash_table_destroy (monitor->priv->rate_limiter);
178 g_main_context_unref (monitor->priv->context);
180 G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
183 static void
184 g_file_monitor_dispose (GObject *object)
186 GFileMonitor *monitor;
187 GFileMonitorPrivate *priv;
189 monitor = G_FILE_MONITOR (object);
190 priv = monitor->priv;
192 if (priv->pending_file_change_source)
194 g_source_destroy (priv->pending_file_change_source);
195 g_source_unref (priv->pending_file_change_source);
196 priv->pending_file_change_source = NULL;
198 g_slist_free_full (priv->pending_file_changes, (GDestroyNotify) file_change_free);
199 priv->pending_file_changes = NULL;
201 /* Make sure we cancel on last unref */
202 g_file_monitor_cancel (monitor);
204 G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
207 static void
208 g_file_monitor_class_init (GFileMonitorClass *klass)
210 GObjectClass *object_class;
212 g_type_class_add_private (klass, sizeof (GFileMonitorPrivate));
214 object_class = G_OBJECT_CLASS (klass);
215 object_class->finalize = g_file_monitor_finalize;
216 object_class->dispose = g_file_monitor_dispose;
217 object_class->get_property = g_file_monitor_get_property;
218 object_class->set_property = g_file_monitor_set_property;
221 * GFileMonitor::changed:
222 * @monitor: a #GFileMonitor.
223 * @file: a #GFile.
224 * @other_file: (allow-none): a #GFile or #NULL.
225 * @event_type: a #GFileMonitorEvent.
227 * Emitted when @file has been changed.
229 * If using #G_FILE_MONITOR_SEND_MOVED flag and @event_type is
230 * #G_FILE_MONITOR_SEND_MOVED, @file will be set to a #GFile containing the
231 * old path, and @other_file will be set to a #GFile containing the new path.
233 * In all the other cases, @other_file will be set to #NULL.
235 signals[CHANGED] =
236 g_signal_new (I_("changed"),
237 G_TYPE_FILE_MONITOR,
238 G_SIGNAL_RUN_LAST,
239 G_STRUCT_OFFSET (GFileMonitorClass, changed),
240 NULL, NULL,
241 NULL,
242 G_TYPE_NONE, 3,
243 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
245 g_object_class_install_property (object_class,
246 PROP_RATE_LIMIT,
247 g_param_spec_int ("rate-limit",
248 P_("Rate limit"),
249 P_("The limit of the monitor to watch for changes, in milliseconds"),
250 0, G_MAXINT,
251 DEFAULT_RATE_LIMIT_MSECS,
252 G_PARAM_READWRITE|
253 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
255 g_object_class_install_property (object_class,
256 PROP_CANCELLED,
257 g_param_spec_boolean ("cancelled",
258 P_("Cancelled"),
259 P_("Whether the monitor has been cancelled"),
260 FALSE,
261 G_PARAM_READABLE|
262 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
265 static void
266 g_file_monitor_init (GFileMonitor *monitor)
268 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
269 G_TYPE_FILE_MONITOR,
270 GFileMonitorPrivate);
271 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
272 monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
273 NULL, (GDestroyNotify) rate_limiter_free);
274 monitor->priv->context = g_main_context_ref_thread_default ();
278 * g_file_monitor_is_cancelled:
279 * @monitor: a #GFileMonitor
281 * Returns whether the monitor is canceled.
283 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
285 gboolean
286 g_file_monitor_is_cancelled (GFileMonitor *monitor)
288 gboolean res;
290 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
292 G_LOCK (cancelled);
293 res = monitor->priv->cancelled;
294 G_UNLOCK (cancelled);
296 return res;
300 * g_file_monitor_cancel:
301 * @monitor: a #GFileMonitor.
303 * Cancels a file monitor.
305 * Returns: %TRUE if monitor was cancelled.
307 gboolean
308 g_file_monitor_cancel (GFileMonitor* monitor)
310 GFileMonitorClass *klass;
312 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
314 G_LOCK (cancelled);
315 if (monitor->priv->cancelled)
317 G_UNLOCK (cancelled);
318 return TRUE;
321 monitor->priv->cancelled = TRUE;
322 G_UNLOCK (cancelled);
324 g_object_notify (G_OBJECT (monitor), "cancelled");
326 klass = G_FILE_MONITOR_GET_CLASS (monitor);
327 return (* klass->cancel) (monitor);
331 * g_file_monitor_set_rate_limit:
332 * @monitor: a #GFileMonitor.
333 * @limit_msecs: a non-negative integer with the limit in milliseconds
334 * to poll for changes
336 * Sets the rate limit to which the @monitor will report
337 * consecutive change events to the same file.
339 void
340 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
341 gint limit_msecs)
343 GFileMonitorPrivate *priv;
345 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
346 g_return_if_fail (limit_msecs >= 0);
348 priv = monitor->priv;
349 if (priv->rate_limit_msec != limit_msecs)
351 monitor->priv->rate_limit_msec = limit_msecs;
352 g_object_notify (G_OBJECT (monitor), "rate-limit");
356 struct _FileChange {
357 GFile *child;
358 GFile *other_file;
359 GFileMonitorEvent event_type;
362 static void
363 file_change_free (FileChange *change)
365 g_object_unref (change->child);
366 if (change->other_file)
367 g_object_unref (change->other_file);
369 g_slice_free (FileChange, change);
372 static gboolean
373 emit_cb (gpointer data)
375 GFileMonitor *monitor = G_FILE_MONITOR (data);
376 GSList *pending, *iter;
378 pending = g_slist_reverse (monitor->priv->pending_file_changes);
379 monitor->priv->pending_file_changes = NULL;
380 if (monitor->priv->pending_file_change_source)
382 g_source_unref (monitor->priv->pending_file_change_source);
383 monitor->priv->pending_file_change_source = NULL;
386 g_object_ref (monitor);
387 for (iter = pending; iter; iter = iter->next)
389 FileChange *change = iter->data;
390 g_signal_emit (monitor, signals[CHANGED], 0,
391 change->child, change->other_file, change->event_type);
392 file_change_free (change);
394 g_slist_free (pending);
395 g_object_unref (monitor);
397 return FALSE;
400 static void
401 emit_in_idle (GFileMonitor *monitor,
402 GFile *child,
403 GFile *other_file,
404 GFileMonitorEvent event_type)
406 GSource *source;
407 FileChange *change;
408 GFileMonitorPrivate *priv;
410 priv = monitor->priv;
412 change = g_slice_new (FileChange);
414 change->child = g_object_ref (child);
415 if (other_file)
416 change->other_file = g_object_ref (other_file);
417 else
418 change->other_file = NULL;
419 change->event_type = event_type;
421 if (!priv->pending_file_change_source)
423 source = g_idle_source_new ();
424 priv->pending_file_change_source = source;
425 g_source_set_priority (source, 0);
427 /* We don't ref monitor here - instead dispose will free any
428 * pending idles.
430 g_source_set_callback (source, emit_cb, monitor, NULL);
431 g_source_attach (source, monitor->priv->context);
433 /* We reverse this in the processor */
434 priv->pending_file_changes = g_slist_prepend (priv->pending_file_changes, change);
437 static guint32
438 get_time_msecs (void)
440 return g_get_monotonic_time () / G_TIME_SPAN_MILLISECOND;
443 static guint32
444 time_difference (guint32 from, guint32 to)
446 if (from > to)
447 return 0;
448 return to - from;
451 /* Change event rate limiting support: */
453 static RateLimiter *
454 new_limiter (GFileMonitor *monitor,
455 GFile *file)
457 RateLimiter *limiter;
459 limiter = g_slice_new0 (RateLimiter);
460 limiter->file = g_object_ref (file);
461 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
463 return limiter;
466 static void
467 rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor,
468 RateLimiter *limiter)
470 if (limiter->send_virtual_changes_done_at != 0)
472 emit_in_idle (monitor, limiter->file, NULL,
473 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
474 limiter->send_virtual_changes_done_at = 0;
478 static void
479 rate_limiter_send_delayed_change_now (GFileMonitor *monitor,
480 RateLimiter *limiter,
481 guint32 time_now)
483 if (limiter->send_delayed_change_at != 0)
485 emit_in_idle (monitor,
486 limiter->file, NULL,
487 G_FILE_MONITOR_EVENT_CHANGED);
488 limiter->send_delayed_change_at = 0;
489 limiter->last_sent_change_time = time_now;
493 typedef struct {
494 guint32 min_time;
495 guint32 time_now;
496 GFileMonitor *monitor;
497 } ForEachData;
499 static gboolean
500 calc_min_time (GFileMonitor *monitor,
501 RateLimiter *limiter,
502 guint32 time_now,
503 guint32 *min_time)
505 gboolean delete_me;
506 guint32 expire_at;
508 delete_me = TRUE;
510 if (limiter->last_sent_change_time != 0)
512 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventually */
513 expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
515 if (time_difference (time_now, expire_at) > 0)
517 delete_me = FALSE;
518 *min_time = MIN (*min_time,
519 time_difference (time_now, expire_at));
523 if (limiter->send_delayed_change_at != 0)
525 delete_me = FALSE;
526 *min_time = MIN (*min_time,
527 time_difference (time_now, limiter->send_delayed_change_at));
530 if (limiter->send_virtual_changes_done_at != 0)
532 delete_me = FALSE;
533 *min_time = MIN (*min_time,
534 time_difference (time_now, limiter->send_virtual_changes_done_at));
537 return delete_me;
540 static gboolean
541 foreach_rate_limiter_fire (gpointer key,
542 gpointer value,
543 gpointer user_data)
545 RateLimiter *limiter = value;
546 ForEachData *data = user_data;
548 if (limiter->send_delayed_change_at != 0 &&
549 time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
550 rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
552 if (limiter->send_virtual_changes_done_at != 0 &&
553 time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
554 rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
556 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
559 static gboolean
560 rate_limiter_timeout (gpointer timeout_data)
562 GFileMonitor *monitor = timeout_data;
563 ForEachData data;
564 GSource *source;
566 data.min_time = G_MAXUINT32;
567 data.monitor = monitor;
568 data.time_now = get_time_msecs ();
569 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
570 foreach_rate_limiter_fire,
571 &data);
573 /* Remove old timeout */
574 if (monitor->priv->timeout)
576 g_source_destroy (monitor->priv->timeout);
577 g_source_unref (monitor->priv->timeout);
578 monitor->priv->timeout = NULL;
579 monitor->priv->timeout_fires_at = 0;
582 /* Set up new timeout */
583 if (data.min_time != G_MAXUINT32)
585 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
586 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
587 g_source_attach (source, monitor->priv->context);
589 monitor->priv->timeout = source;
590 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
593 return FALSE;
596 static gboolean
597 foreach_rate_limiter_update (gpointer key,
598 gpointer value,
599 gpointer user_data)
601 RateLimiter *limiter = value;
602 ForEachData *data = user_data;
604 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
607 static void
608 update_rate_limiter_timeout (GFileMonitor *monitor,
609 guint new_time)
611 ForEachData data;
612 GSource *source;
614 if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
615 time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
616 return; /* Nothing to do, we already fire earlier than that */
618 data.min_time = G_MAXUINT32;
619 data.monitor = monitor;
620 data.time_now = get_time_msecs ();
621 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
622 foreach_rate_limiter_update,
623 &data);
625 /* Remove old timeout */
626 if (monitor->priv->timeout)
628 g_source_destroy (monitor->priv->timeout);
629 g_source_unref (monitor->priv->timeout);
630 monitor->priv->timeout_fires_at = 0;
631 monitor->priv->timeout = NULL;
634 /* Set up new timeout */
635 if (data.min_time != G_MAXUINT32)
637 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
638 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
639 g_source_attach (source, monitor->priv->context);
641 monitor->priv->timeout = source;
642 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
647 * g_file_monitor_emit_event:
648 * @monitor: a #GFileMonitor.
649 * @child: a #GFile.
650 * @other_file: a #GFile.
651 * @event_type: a set of #GFileMonitorEvent flags.
653 * Emits the #GFileMonitor::changed signal if a change
654 * has taken place. Should be called from file monitor
655 * implementations only.
657 * The signal will be emitted from an idle handler (in the <link
658 * linkend="g-main-context-push-thread-default">thread-default main
659 * context</link>).
661 void
662 g_file_monitor_emit_event (GFileMonitor *monitor,
663 GFile *child,
664 GFile *other_file,
665 GFileMonitorEvent event_type)
667 guint32 time_now, since_last;
668 gboolean emit_now;
669 RateLimiter *limiter;
671 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
672 g_return_if_fail (G_IS_FILE (child));
674 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
676 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
678 if (limiter)
680 rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
681 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
682 limiter->send_virtual_changes_done_at = 0;
683 else
684 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
685 update_rate_limiter_timeout (monitor, 0);
687 emit_in_idle (monitor, child, other_file, event_type);
689 else
691 /* Changed event, rate limit */
692 time_now = get_time_msecs ();
693 emit_now = TRUE;
695 if (limiter)
697 since_last = time_difference (limiter->last_sent_change_time, time_now);
698 if (since_last < monitor->priv->rate_limit_msec)
700 /* We ignore this change, but arm a timer so that we can fire it later if we
701 don't get any other events (that kill this timeout) */
702 emit_now = FALSE;
703 if (limiter->send_delayed_change_at == 0)
705 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
706 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
711 if (limiter == NULL)
712 limiter = new_limiter (monitor, child);
714 if (emit_now)
716 emit_in_idle (monitor, child, other_file, event_type);
718 limiter->last_sent_change_time = time_now;
719 limiter->send_delayed_change_at = 0;
720 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventually */
721 update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
724 /* Schedule a virtual change done. This is removed if we get a real one, and
725 postponed if we get more change events. */
727 limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
728 update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);