1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Sebastian Dröge.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Authors: Alexander Larsson <alexl@redhat.com>
20 * John McCutchan <john@johnmccutchan.com>
21 * Sebastian Dröge <slomo@circular-chaos.org>
22 * Ryan Lortie <desrt@desrt.ca>
23 */
24
25#include "config.h"
26
27#include "ginotifyfilemonitor.h"
28#include <gio/giomodule.h>
29
30#define USE_INOTIFY 1
31#include "inotify-helper.h"
32
33struct _GInotifyFileMonitor
34{
35 GLocalFileMonitor parent_instance;
36
37 inotify_sub *sub;
38};
39
40G_DEFINE_TYPE_WITH_CODE (GInotifyFileMonitor, g_inotify_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
41 g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
42 g_define_type_id, "inotify", 20))
43
44static gboolean
45g_inotify_file_monitor_is_supported (void)
46{
47 return _ih_startup ();
48}
49
50static void
51g_inotify_file_monitor_start (GLocalFileMonitor *local_monitor,
52 const gchar *dirname,
53 const gchar *basename,
54 const gchar *filename,
55 GFileMonitorSource *source)
56{
57 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (local_monitor);
58 gboolean success G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
59
60 /* should already have been called, from is_supported() */
61 success = _ih_startup ();
62 g_assert (success);
63
64 inotify_monitor->sub = _ih_sub_new (dirname, basename, filename, user_data: source);
65 _ih_sub_add (sub: inotify_monitor->sub);
66}
67
68static gboolean
69g_inotify_file_monitor_cancel (GFileMonitor *monitor)
70{
71 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (monitor);
72
73 if (inotify_monitor->sub)
74 {
75 _ih_sub_cancel (sub: inotify_monitor->sub);
76 _ih_sub_free (sub: inotify_monitor->sub);
77 inotify_monitor->sub = NULL;
78 }
79
80 return TRUE;
81}
82
83static void
84g_inotify_file_monitor_finalize (GObject *object)
85{
86#ifndef G_DISABLE_ASSERT
87 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (object);
88#endif
89
90 /* must surely have been cancelled already */
91 g_assert (!inotify_monitor->sub);
92
93 G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize (object);
94}
95
96static void
97g_inotify_file_monitor_init (GInotifyFileMonitor* monitor)
98{
99}
100
101static void
102g_inotify_file_monitor_class_init (GInotifyFileMonitorClass* klass)
103{
104 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
105 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
106 GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
107
108 local_file_monitor_class->is_supported = g_inotify_file_monitor_is_supported;
109 local_file_monitor_class->start = g_inotify_file_monitor_start;
110 local_file_monitor_class->mount_notify = TRUE;
111 file_monitor_class->cancel = g_inotify_file_monitor_cancel;
112
113 gobject_class->finalize = g_inotify_file_monitor_finalize;
114}
115

source code of gtk/subprojects/glib/gio/inotify/ginotifyfilemonitor.c