GSettings: fix check for delaying backend subscription
[platform/upstream/glib.git] / gio / inotify / inotify-missing.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
2
3 /* inotify-helper.c - Gnome VFS Monitor based on inotify.
4
5    Copyright (C) 2005 John McCutchan
6
7    The Gnome Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The Gnome Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the Gnome Library; see the file COPYING.LIB.  If not,
19    see <http://www.gnu.org/licenses/>.
20
21    Authors: 
22                  John McCutchan <john@johnmccutchan.com>
23 */
24
25 #include "config.h"
26 #include <glib.h>
27 #include "inotify-missing.h"
28 #include "inotify-path.h"
29 #include "glib-private.h"
30
31 #define SCAN_MISSING_TIME 4 /* 1/4 Hz */
32
33 static gboolean im_debug_enabled = FALSE;
34 #define IM_W if (im_debug_enabled) g_warning
35
36 /* We put inotify_sub's that are missing on this list */
37 static GList *missing_sub_list = NULL;
38 static gboolean im_scan_missing (gpointer user_data);
39 static gboolean scan_missing_running = FALSE;
40 static void (*missing_cb)(inotify_sub *sub) = NULL;
41
42 G_LOCK_EXTERN (inotify_lock);
43
44 /* inotify_lock must be held before calling */
45 void
46 _im_startup (void (*callback)(inotify_sub *sub))
47 {
48   static gboolean initialized = FALSE;
49   
50   if (!initialized)
51     {
52       missing_cb = callback;
53       initialized = TRUE;
54     }
55 }
56
57 /* inotify_lock must be held before calling */
58 void
59 _im_add (inotify_sub *sub)
60 {
61   if (g_list_find (missing_sub_list, sub))
62     {
63       IM_W ("asked to add %s to missing list but it's already on the list!\n", sub->dirname);
64       return;
65     }
66
67   IM_W ("adding %s to missing list\n", sub->dirname);
68   missing_sub_list = g_list_prepend (missing_sub_list, sub);
69
70   /* If the timeout is turned off, we turn it back on */
71   if (!scan_missing_running)
72     {
73       GSource *source;
74
75       scan_missing_running = TRUE;
76       source = g_timeout_source_new_seconds (SCAN_MISSING_TIME);
77       g_source_set_callback (source, im_scan_missing, NULL, NULL);
78       g_source_attach (source, GLIB_PRIVATE_CALL (g_get_worker_context) ());
79       g_source_unref (source);
80     }
81 }
82
83 /* inotify_lock must be held before calling */
84 void
85 _im_rm (inotify_sub *sub)
86 {
87   GList *link;
88   
89   link = g_list_find (missing_sub_list, sub);
90
91   if (!link)
92     {
93       IM_W ("asked to remove %s from missing list but it isn't on the list!\n", sub->dirname);
94       return;
95     }
96
97   IM_W ("removing %s from missing list\n", sub->dirname);
98
99   missing_sub_list = g_list_remove_link (missing_sub_list, link);
100   g_list_free_1 (link);
101 }
102
103 /* Scans the list of missing subscriptions checking if they
104  * are available yet.
105  */
106 static gboolean
107 im_scan_missing (gpointer user_data)
108 {
109   GList *nolonger_missing = NULL;
110   GList *l;
111   
112   G_LOCK (inotify_lock);
113   
114   IM_W ("scanning missing list with %d items\n", g_list_length (missing_sub_list));
115   for (l = missing_sub_list; l; l = l->next)
116     {
117       inotify_sub *sub = l->data;
118       gboolean not_m = FALSE;
119       
120       IM_W ("checking %p\n", sub);
121       g_assert (sub);
122       g_assert (sub->dirname);
123       not_m = _ip_start_watching (sub);
124
125       if (not_m)
126         {
127           missing_cb (sub);
128           IM_W ("removed %s from missing list\n", sub->dirname);
129           /* We have to build a list of list nodes to remove from the
130            * missing_sub_list. We do the removal outside of this loop.
131            */
132           nolonger_missing = g_list_prepend (nolonger_missing, l);
133         } 
134     }
135
136   for (l = nolonger_missing; l ; l = l->next)
137     {
138       GList *llink = l->data;
139       missing_sub_list = g_list_remove_link (missing_sub_list, llink);
140       g_list_free_1 (llink);
141     }
142
143   g_list_free (nolonger_missing);
144   
145   /* If the missing list is now empty, we disable the timeout */
146   if (missing_sub_list == NULL)
147     {
148       scan_missing_running = FALSE;
149       G_UNLOCK (inotify_lock);
150       return FALSE;
151     }
152   else
153     {
154       G_UNLOCK (inotify_lock);
155       return TRUE;
156     }
157 }