File: | menu-monitor.c |
Warning: | line 169, column 3 Value stored to 'event' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright (C) 2005 Red Hat, Inc. |
3 | * Copyright (C) 2006 Mark McLoughlin |
4 | * Copyright (C) 2007 Sebastian Dröge |
5 | * Copyright (C) 2008 Vincent Untz |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Lesser General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * This 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 | * Lesser General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Lesser General Public |
18 | * License along with this library; if not, write to the |
19 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include <config.h> |
24 | |
25 | #include "menu-monitor.h" |
26 | |
27 | #include <gio/gio.h> |
28 | |
29 | #include "menu-util.h" |
30 | |
31 | struct MenuMonitor { |
32 | char* path; |
33 | guint refcount; |
34 | |
35 | GSList* notifies; |
36 | |
37 | GFileMonitor* monitor; |
38 | |
39 | guint is_directory: 1; |
40 | }; |
41 | |
42 | typedef struct { |
43 | MenuMonitor* monitor; |
44 | MenuMonitorEvent event; |
45 | char* path; |
46 | } MenuMonitorEventInfo; |
47 | |
48 | typedef struct { |
49 | MenuMonitorNotifyFunc notify_func; |
50 | gpointer user_data; |
51 | guint refcount; |
52 | } MenuMonitorNotify; |
53 | |
54 | static MenuMonitorNotify* cafe_menu_monitor_notify_refmenu_monitor_notify_ref(MenuMonitorNotify* notify); |
55 | static void cafe_menu_monitor_notify_unrefmenu_monitor_notify_unref(MenuMonitorNotify* notify); |
56 | |
57 | static GHashTable* monitors_registry = NULL((void*)0); |
58 | static guint events_idle_handler = 0; |
59 | static GSList* pending_events = NULL((void*)0); |
60 | |
61 | static void cafe_menu_monitor_notify_ref_wrapper (gpointer data, gpointer user_data) |
62 | { |
63 | cafe_menu_monitor_notify_refmenu_monitor_notify_ref ((MenuMonitorNotify *) data); |
64 | } |
65 | |
66 | static void invoke_notifies(MenuMonitor* monitor, MenuMonitorEvent event, const char* path) |
67 | { |
68 | GSList *copy; |
69 | GSList *tmp; |
70 | |
71 | copy = g_slist_copy (monitor->notifies); |
72 | g_slist_foreach (copy, |
73 | (GFunc) cafe_menu_monitor_notify_ref_wrapper, |
74 | NULL((void*)0)); |
75 | |
76 | tmp = copy; |
77 | while (tmp != NULL((void*)0)) |
78 | { |
79 | MenuMonitorNotify *notify = tmp->data; |
80 | GSList *next = tmp->next; |
81 | |
82 | if (notify->notify_func) |
83 | { |
84 | notify->notify_func (monitor, event, path, notify->user_data); |
85 | } |
86 | |
87 | cafe_menu_monitor_notify_unrefmenu_monitor_notify_unref(notify); |
88 | |
89 | tmp = next; |
90 | } |
91 | |
92 | g_slist_free (copy); |
93 | } |
94 | |
95 | static gboolean emit_events_in_idle(void) |
96 | { |
97 | GSList *events_to_emit; |
98 | GSList *tmp; |
99 | |
100 | events_to_emit = pending_events; |
101 | |
102 | pending_events = NULL((void*)0); |
103 | events_idle_handler = 0; |
104 | |
105 | tmp = events_to_emit; |
106 | while (tmp != NULL((void*)0)) |
107 | { |
108 | MenuMonitorEventInfo *event_info = tmp->data; |
109 | |
110 | cafe_menu_monitor_refmenu_monitor_ref(event_info->monitor); |
111 | |
112 | tmp = tmp->next; |
113 | } |
114 | |
115 | tmp = events_to_emit; |
116 | while (tmp != NULL((void*)0)) |
117 | { |
118 | MenuMonitorEventInfo *event_info = tmp->data; |
119 | |
120 | invoke_notifies (event_info->monitor, |
121 | event_info->event, |
122 | event_info->path); |
123 | |
124 | menu_monitor_unref (event_info->monitor); |
125 | event_info->monitor = NULL((void*)0); |
126 | |
127 | g_free (event_info->path); |
128 | event_info->path = NULL((void*)0); |
129 | |
130 | event_info->event = MENU_MONITOR_EVENT_INVALID; |
131 | |
132 | g_free (event_info); |
133 | |
134 | tmp = tmp->next; |
135 | } |
136 | |
137 | g_slist_free (events_to_emit); |
138 | |
139 | return FALSE(0); |
140 | } |
141 | |
142 | static void menu_monitor_queue_event(MenuMonitorEventInfo* event_info) |
143 | { |
144 | pending_events = g_slist_append (pending_events, event_info); |
145 | |
146 | if (events_idle_handler == 0) |
147 | { |
148 | events_idle_handler = g_idle_add ((GSourceFunc) emit_events_in_idle, NULL((void*)0)); |
149 | } |
150 | } |
151 | |
152 | static inline char* get_registry_key(const char* path, gboolean is_directory) |
153 | { |
154 | return g_strdup_printf ("%s:%s", |
155 | path, |
156 | is_directory ? "<dir>" : "<file>"); |
157 | } |
158 | |
159 | static gboolean monitor_callback (GFileMonitor *monitor G_GNUC_UNUSED__attribute__ ((__unused__)), |
160 | GFile *child, |
161 | GFile *other_file G_GNUC_UNUSED__attribute__ ((__unused__)), |
162 | GFileMonitorEvent eflags, |
163 | gpointer user_data) |
164 | { |
165 | MenuMonitorEventInfo *event_info; |
166 | MenuMonitorEvent event; |
167 | MenuMonitor *menu_monitor = (MenuMonitor *) user_data; |
168 | |
169 | event = MENU_MONITOR_EVENT_INVALID; |
Value stored to 'event' is never read | |
170 | switch (eflags) |
171 | { |
172 | case G_FILE_MONITOR_EVENT_CHANGED: |
173 | event = MENU_MONITOR_EVENT_CHANGED; |
174 | break; |
175 | case G_FILE_MONITOR_EVENT_CREATED: |
176 | event = MENU_MONITOR_EVENT_CREATED; |
177 | break; |
178 | case G_FILE_MONITOR_EVENT_DELETED: |
179 | event = MENU_MONITOR_EVENT_DELETED; |
180 | break; |
181 | default: |
182 | return TRUE(!(0)); |
183 | } |
184 | |
185 | event_info = g_new0 (MenuMonitorEventInfo, 1)((MenuMonitorEventInfo *) g_malloc0_n ((1), sizeof (MenuMonitorEventInfo ))); |
186 | |
187 | event_info->path = g_file_get_path (child); |
188 | event_info->event = event; |
189 | event_info->monitor = menu_monitor; |
190 | |
191 | menu_monitor_queue_event (event_info); |
192 | |
193 | return TRUE(!(0)); |
194 | } |
195 | |
196 | static MenuMonitor* register_monitor(const char* path, gboolean is_directory) |
197 | { |
198 | MenuMonitor *retval; |
199 | GFile *file; |
200 | |
201 | retval = g_new0 (MenuMonitor, 1)((MenuMonitor *) g_malloc0_n ((1), sizeof (MenuMonitor))); |
202 | |
203 | retval->path = g_strdup (path)g_strdup_inline (path); |
204 | retval->refcount = 1; |
205 | retval->is_directory = is_directory != FALSE(0); |
206 | |
207 | file = g_file_new_for_path (retval->path); |
208 | |
209 | if (file == NULL((void*)0)) |
210 | { |
211 | menu_verbose ("Not adding monitor on '%s', failed to create GFile\n", |
212 | retval->path); |
213 | return retval; |
214 | } |
215 | |
216 | if (retval->is_directory) |
217 | retval->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, |
218 | NULL((void*)0), NULL((void*)0)); |
219 | else |
220 | retval->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, |
221 | NULL((void*)0), NULL((void*)0)); |
222 | |
223 | g_object_unref (G_OBJECT (file)((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((file)), (((GType) ((20) << (2))))))))); |
224 | |
225 | if (retval->monitor == NULL((void*)0)) |
226 | { |
227 | menu_verbose ("Not adding monitor on '%s', failed to create monitor\n", |
228 | retval->path); |
229 | return retval; |
230 | } |
231 | |
232 | g_signal_connect (retval->monitor, "changed",g_signal_connect_data ((retval->monitor), ("changed"), ((( GCallback) (monitor_callback))), (retval), ((void*)0), (GConnectFlags ) 0) |
233 | G_CALLBACK (monitor_callback), retval)g_signal_connect_data ((retval->monitor), ("changed"), ((( GCallback) (monitor_callback))), (retval), ((void*)0), (GConnectFlags ) 0); |
234 | |
235 | return retval; |
236 | } |
237 | |
238 | static MenuMonitor* lookup_monitor(const char* path, gboolean is_directory) |
239 | { |
240 | MenuMonitor *retval; |
241 | char *registry_key; |
242 | |
243 | retval = NULL((void*)0); |
244 | |
245 | registry_key = get_registry_key (path, is_directory); |
246 | |
247 | if (monitors_registry == NULL((void*)0)) |
248 | { |
249 | monitors_registry = g_hash_table_new_full (g_str_hash, |
250 | g_str_equal, |
251 | g_free, |
252 | NULL((void*)0)); |
253 | } |
254 | else |
255 | { |
256 | retval = g_hash_table_lookup (monitors_registry, registry_key); |
257 | } |
258 | |
259 | if (retval == NULL((void*)0)) |
260 | { |
261 | retval = register_monitor (path, is_directory); |
262 | g_hash_table_insert (monitors_registry, registry_key, retval); |
263 | |
264 | return retval; |
265 | } |
266 | else |
267 | { |
268 | g_free (registry_key); |
269 | |
270 | return cafe_menu_monitor_refmenu_monitor_ref(retval); |
271 | } |
272 | } |
273 | |
274 | MenuMonitor* cafe_menu_monitor_file_getmenu_get_file_monitor(const char* path) |
275 | { |
276 | g_return_val_if_fail(path != NULL, NULL)do { if ((path != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "path != NULL"); return (((void*)0)); } } while (0); |
277 | |
278 | return lookup_monitor(path, FALSE(0)); |
279 | } |
280 | |
281 | MenuMonitor* menu_get_directory_monitor(const char* path) |
282 | { |
283 | g_return_val_if_fail (path != NULL, NULL)do { if ((path != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "path != NULL"); return (((void*)0)); } } while (0); |
284 | |
285 | return lookup_monitor (path, TRUE(!(0))); |
286 | } |
287 | |
288 | MenuMonitor* cafe_menu_monitor_refmenu_monitor_ref(MenuMonitor* monitor) |
289 | { |
290 | g_return_val_if_fail(monitor != NULL, NULL)do { if ((monitor != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "monitor != NULL" ); return (((void*)0)); } } while (0); |
291 | g_return_val_if_fail(monitor->refcount > 0, NULL)do { if ((monitor->refcount > 0)) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "monitor->refcount > 0" ); return (((void*)0)); } } while (0); |
292 | |
293 | monitor->refcount++; |
294 | |
295 | return monitor; |
296 | } |
297 | |
298 | static void menu_monitor_clear_pending_events(MenuMonitor* monitor) |
299 | { |
300 | GSList *tmp; |
301 | |
302 | tmp = pending_events; |
303 | while (tmp != NULL((void*)0)) |
304 | { |
305 | MenuMonitorEventInfo *event_info = tmp->data; |
306 | GSList *next = tmp->next; |
307 | |
308 | if (event_info->monitor == monitor) |
309 | { |
310 | pending_events = g_slist_delete_link (pending_events, tmp); |
311 | |
312 | g_free (event_info->path); |
313 | event_info->path = NULL((void*)0); |
314 | |
315 | event_info->monitor = NULL((void*)0); |
316 | event_info->event = MENU_MONITOR_EVENT_INVALID; |
317 | |
318 | g_free (event_info); |
319 | } |
320 | |
321 | tmp = next; |
322 | } |
323 | } |
324 | |
325 | void menu_monitor_unref(MenuMonitor* monitor) |
326 | { |
327 | char *registry_key; |
328 | |
329 | g_return_if_fail (monitor != NULL)do { if ((monitor != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "monitor != NULL" ); return; } } while (0); |
330 | g_return_if_fail (monitor->refcount > 0)do { if ((monitor->refcount > 0)) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "monitor->refcount > 0" ); return; } } while (0); |
331 | |
332 | if (--monitor->refcount > 0) |
333 | return; |
334 | |
335 | registry_key = get_registry_key (monitor->path, monitor->is_directory); |
336 | g_hash_table_remove (monitors_registry, registry_key); |
337 | g_free (registry_key); |
338 | |
339 | if (g_hash_table_size (monitors_registry) == 0) |
340 | { |
341 | g_hash_table_destroy (monitors_registry); |
342 | monitors_registry = NULL((void*)0); |
343 | } |
344 | |
345 | if (monitor->monitor) |
346 | { |
347 | g_file_monitor_cancel (monitor->monitor); |
348 | g_object_unref (monitor->monitor); |
349 | monitor->monitor = NULL((void*)0); |
350 | } |
351 | |
352 | g_slist_foreach (monitor->notifies, (GFunc) cafe_menu_monitor_notify_unrefmenu_monitor_notify_unref, NULL((void*)0)); |
353 | g_slist_free (monitor->notifies); |
354 | monitor->notifies = NULL((void*)0); |
355 | |
356 | menu_monitor_clear_pending_events (monitor); |
357 | |
358 | g_free (monitor->path); |
359 | monitor->path = NULL((void*)0); |
360 | |
361 | g_free (monitor); |
362 | } |
363 | |
364 | static MenuMonitorNotify* cafe_menu_monitor_notify_refmenu_monitor_notify_ref(MenuMonitorNotify* notify) |
365 | { |
366 | g_return_val_if_fail(notify != NULL, NULL)do { if ((notify != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "notify != NULL") ; return (((void*)0)); } } while (0); |
367 | g_return_val_if_fail(notify->refcount > 0, NULL)do { if ((notify->refcount > 0)) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "notify->refcount > 0" ); return (((void*)0)); } } while (0); |
368 | |
369 | notify->refcount++; |
370 | |
371 | return notify; |
372 | } |
373 | |
374 | static void cafe_menu_monitor_notify_unrefmenu_monitor_notify_unref(MenuMonitorNotify* notify) |
375 | { |
376 | g_return_if_fail(notify != NULL)do { if ((notify != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "notify != NULL") ; return; } } while (0); |
377 | g_return_if_fail(notify->refcount > 0)do { if ((notify->refcount > 0)) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "notify->refcount > 0" ); return; } } while (0); |
378 | |
379 | if (--notify->refcount > 0) |
380 | { |
381 | return; |
382 | } |
383 | |
384 | g_free(notify); |
385 | } |
386 | |
387 | void menu_monitor_add_notify(MenuMonitor* monitor, MenuMonitorNotifyFunc notify_func, gpointer user_data) |
388 | { |
389 | MenuMonitorNotify* notify; |
390 | |
391 | g_return_if_fail(monitor != NULL)do { if ((monitor != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "monitor != NULL" ); return; } } while (0); |
392 | g_return_if_fail(notify_func != NULL)do { if ((notify_func != ((void*)0))) { } else { g_return_if_fail_warning (((gchar*) 0), ((const char*) (__func__)), "notify_func != NULL" ); return; } } while (0); |
393 | |
394 | GSList* tmp = monitor->notifies; |
395 | |
396 | while (tmp != NULL((void*)0)) |
397 | { |
398 | notify = tmp->data; |
399 | |
400 | if (notify->notify_func == notify_func && notify->user_data == user_data) |
401 | { |
402 | break; |
403 | } |
404 | |
405 | tmp = tmp->next; |
406 | } |
407 | |
408 | if (tmp == NULL((void*)0)) |
409 | { |
410 | notify = g_new0(MenuMonitorNotify, 1)((MenuMonitorNotify *) g_malloc0_n ((1), sizeof (MenuMonitorNotify ))); |
411 | notify->notify_func = notify_func; |
412 | notify->user_data = user_data; |
413 | notify->refcount = 1; |
414 | |
415 | monitor->notifies = g_slist_append(monitor->notifies, notify); |
416 | } |
417 | } |
418 | |
419 | void cafe_menu_monitor_notify_removemenu_monitor_remove_notify(MenuMonitor* monitor, MenuMonitorNotifyFunc notify_func, gpointer user_data) |
420 | { |
421 | GSList* tmp = monitor->notifies; |
422 | |
423 | while (tmp != NULL((void*)0)) |
424 | { |
425 | MenuMonitorNotify* notify = tmp->data; |
426 | GSList* next = tmp->next; |
427 | |
428 | if (notify->notify_func == notify_func && notify->user_data == user_data) |
429 | { |
430 | notify->notify_func = NULL((void*)0); |
431 | notify->user_data = NULL((void*)0); |
432 | |
433 | cafe_menu_monitor_notify_unrefmenu_monitor_notify_unref(notify); |
434 | |
435 | monitor->notifies = g_slist_delete_link(monitor->notifies, tmp); |
436 | } |
437 | |
438 | tmp = next; |
439 | } |
440 | } |