summaryrefslogtreecommitdiff
path: root/libmenu/menu-monitor.c
blob: ca52403b682ecf292aedeb5d14cd04bd798899ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright (C) 2005 Red Hat, Inc.
 * Copyright (C) 2006 Mark McLoughlin
 * Copyright (C) 2007 Sebastian Dröge
 * Copyright (C) 2008 Vincent Untz
 * Copyright (C) 2012-2021 MATE Developers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include "menu-monitor.h"

#include <gio/gio.h>

#include "menu-util.h"

struct MenuMonitor {
	char* path;
	guint refcount;

	GSList* notifies;

	GFileMonitor* monitor;

	guint is_directory: 1;
};

typedef struct {
	MenuMonitor* monitor;
	MenuMonitorEvent event;
	char* path;
} MenuMonitorEventInfo;

typedef struct {
	MenuMonitorNotifyFunc notify_func;
	gpointer user_data;
	guint refcount;
} MenuMonitorNotify;

static MenuMonitorNotify* mate_menu_monitor_notify_ref(MenuMonitorNotify* notify);
static void mate_menu_monitor_notify_unref(MenuMonitorNotify* notify);

static GHashTable* monitors_registry = NULL;
static guint events_idle_handler = 0;
static GSList* pending_events = NULL;

static void invoke_notifies(MenuMonitor* monitor, MenuMonitorEvent  event, const char* path)
{
  GSList *copy;
  GSList *tmp;

  copy = g_slist_copy (monitor->notifies);
  g_slist_foreach (copy,
		   (GFunc) mate_menu_monitor_notify_ref,
		   NULL);

  tmp = copy;
  while (tmp != NULL)
    {
      MenuMonitorNotify *notify = tmp->data;
      GSList            *next   = tmp->next;

      if (notify->notify_func)
	{
	  notify->notify_func (monitor, event, path, notify->user_data);
	}

      mate_menu_monitor_notify_unref(notify);

      tmp = next;
    }

  g_slist_free (copy);
}

static gboolean emit_events_in_idle(void)
{
  GSList *events_to_emit;
  GSList *tmp;

  events_to_emit = pending_events;

  pending_events = NULL;
  events_idle_handler = 0;

  tmp = events_to_emit;
  while (tmp != NULL)
    {
      MenuMonitorEventInfo *event_info = tmp->data;

      mate_menu_monitor_ref(event_info->monitor);

      tmp = tmp->next;
    }

  tmp = events_to_emit;
  while (tmp != NULL)
    {
      MenuMonitorEventInfo *event_info = tmp->data;

      invoke_notifies (event_info->monitor,
		       event_info->event,
		       event_info->path);

      menu_monitor_unref (event_info->monitor);
      event_info->monitor = NULL;

      g_free (event_info->path);
      event_info->path = NULL;

      event_info->event = MENU_MONITOR_EVENT_INVALID;

      g_free (event_info);

      tmp = tmp->next;
    }

  g_slist_free (events_to_emit);

  return FALSE;
}

static void menu_monitor_queue_event(MenuMonitorEventInfo* event_info)
{
  pending_events = g_slist_append (pending_events, event_info);

  if (events_idle_handler == 0)
    {
      events_idle_handler = g_idle_add ((GSourceFunc) emit_events_in_idle, NULL);
    }
}

static inline char* get_registry_key(const char* path, gboolean is_directory)
{
  return g_strdup_printf ("%s:%s",
			  path,
			  is_directory ? "<dir>" : "<file>");
}

static gboolean monitor_callback (GFileMonitor* monitor, GFile* child, GFile* other_file, GFileMonitorEvent eflags, gpointer user_data)
{
  MenuMonitorEventInfo *event_info;
  MenuMonitorEvent      event;
  MenuMonitor          *menu_monitor = (MenuMonitor *) user_data;

  event = MENU_MONITOR_EVENT_INVALID;
  switch (eflags)
    {
    case G_FILE_MONITOR_EVENT_CHANGED:
      event = MENU_MONITOR_EVENT_CHANGED;
      break;
    case G_FILE_MONITOR_EVENT_CREATED:
      event = MENU_MONITOR_EVENT_CREATED;
      break;
    case G_FILE_MONITOR_EVENT_DELETED:
      event = MENU_MONITOR_EVENT_DELETED;
      break;
    default:
      return TRUE;
    }

  event_info = g_new0 (MenuMonitorEventInfo, 1);

  event_info->path    = g_file_get_path (child);
  event_info->event   = event;
  event_info->monitor = menu_monitor;

  menu_monitor_queue_event (event_info);

  return TRUE;
}

static MenuMonitor* register_monitor(const char* path, gboolean is_directory)
{
  MenuMonitor     *retval;
  GFile           *file;

  retval = g_new0 (MenuMonitor, 1);

  retval->path         = g_strdup (path);
  retval->refcount     = 1;
  retval->is_directory = is_directory != FALSE;

  file = g_file_new_for_path (retval->path);

  if (file == NULL)
    {
      menu_verbose ("Not adding monitor on '%s', failed to create GFile\n",
                    retval->path);
      return retval;
    }

  if (retval->is_directory)
      retval->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE,
                                                  NULL, NULL);
  else
      retval->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE,
                                             NULL, NULL);

  g_object_unref (G_OBJECT (file));

  if (retval->monitor == NULL)
    {
      menu_verbose ("Not adding monitor on '%s', failed to create monitor\n",
                    retval->path);
      return retval;
    }

  g_signal_connect (retval->monitor, "changed",
                    G_CALLBACK (monitor_callback), retval);

  return retval;
}

static MenuMonitor* lookup_monitor(const char* path, gboolean is_directory)
{
  MenuMonitor *retval;
  char        *registry_key;

  retval = NULL;

  registry_key = get_registry_key (path, is_directory);

  if (monitors_registry == NULL)
    {
      monitors_registry = g_hash_table_new_full (g_str_hash,
						 g_str_equal,
						 g_free,
						 NULL);
    }
  else
    {
      retval = g_hash_table_lookup (monitors_registry, registry_key);
    }

  if (retval == NULL)
    {
      retval = register_monitor (path, is_directory);
      g_hash_table_insert (monitors_registry, registry_key, retval);

      return retval;
    }
  else
    {
      g_free (registry_key);

      return mate_menu_monitor_ref(retval);
    }
}

MenuMonitor* mate_menu_monitor_file_get(const char* path)
{
	g_return_val_if_fail(path != NULL, NULL);

	return lookup_monitor(path, FALSE);
}

MenuMonitor* menu_get_directory_monitor(const char* path)
{
  g_return_val_if_fail (path != NULL, NULL);

  return lookup_monitor (path, TRUE);
}

MenuMonitor* mate_menu_monitor_ref(MenuMonitor* monitor)
{
	g_return_val_if_fail(monitor != NULL, NULL);
	g_return_val_if_fail(monitor->refcount > 0, NULL);

	monitor->refcount++;

	return monitor;
}

static void menu_monitor_clear_pending_events(MenuMonitor* monitor)
{
  GSList *tmp;

  tmp = pending_events;
  while (tmp != NULL)
    {
      MenuMonitorEventInfo *event_info = tmp->data;
      GSList               *next = tmp->next;

      if (event_info->monitor == monitor)
	{
	  pending_events = g_slist_delete_link (pending_events, tmp);

	  g_free (event_info->path);
	  event_info->path = NULL;

	  event_info->monitor = NULL;
	  event_info->event   = MENU_MONITOR_EVENT_INVALID;

	  g_free (event_info);
	}

      tmp = next;
    }
}

void menu_monitor_unref(MenuMonitor* monitor)
{
  char *registry_key;

  g_return_if_fail (monitor != NULL);
  g_return_if_fail (monitor->refcount > 0);

  if (--monitor->refcount > 0)
    return;

  registry_key = get_registry_key (monitor->path, monitor->is_directory);
  g_hash_table_remove (monitors_registry, registry_key);
  g_free (registry_key);

  if (g_hash_table_size (monitors_registry) == 0)
    {
      g_hash_table_destroy (monitors_registry);
      monitors_registry = NULL;
    }

  if (monitor->monitor)
    {
      g_file_monitor_cancel (monitor->monitor);
      g_object_unref (monitor->monitor);
      monitor->monitor = NULL;
    }

  g_slist_foreach (monitor->notifies, (GFunc) mate_menu_monitor_notify_unref, NULL);
  g_slist_free (monitor->notifies);
  monitor->notifies = NULL;

  menu_monitor_clear_pending_events (monitor);

  g_free (monitor->path);
  monitor->path = NULL;

  g_free (monitor);
}

static MenuMonitorNotify* mate_menu_monitor_notify_ref(MenuMonitorNotify* notify)
{
	g_return_val_if_fail(notify != NULL, NULL);
	g_return_val_if_fail(notify->refcount > 0, NULL);

	notify->refcount++;

	return notify;
}

static void mate_menu_monitor_notify_unref(MenuMonitorNotify* notify)
{
	g_return_if_fail(notify != NULL);
	g_return_if_fail(notify->refcount > 0);

	if (--notify->refcount > 0)
	{
		return;
	}

	g_free(notify);
}

void menu_monitor_add_notify(MenuMonitor* monitor, MenuMonitorNotifyFunc notify_func, gpointer user_data)
{
	GSList* tmp;
	MenuMonitorNotify* notify;

	g_return_if_fail(monitor != NULL);
	g_return_if_fail(notify_func != NULL);

	tmp = monitor->notifies;

	while (tmp != NULL)
	{
		notify = tmp->data;

		if (notify->notify_func == notify_func && notify->user_data == user_data)
		{
			break;
		}

		tmp = tmp->next;
	}

	if (tmp == NULL)
	{
		notify = g_new0(MenuMonitorNotify, 1);
		notify->notify_func = notify_func;
		notify->user_data = user_data;
		notify->refcount = 1;

		monitor->notifies = g_slist_append(monitor->notifies, notify);
	}
}

void mate_menu_monitor_notify_remove(MenuMonitor* monitor, MenuMonitorNotifyFunc notify_func, gpointer user_data)
{
	GSList* tmp = monitor->notifies;

	while (tmp != NULL)
	{
		MenuMonitorNotify* notify = tmp->data;
		GSList* next = tmp->next;

		if (notify->notify_func == notify_func && notify->user_data == user_data)
		{
			notify->notify_func = NULL;
			notify->user_data = NULL;

			mate_menu_monitor_notify_unref(notify);

			monitor->notifies = g_slist_delete_link(monitor->notifies, tmp);
		}

		tmp = next;
	}
}