GSettings: properly support 'extends'
[glib.git] / gio / kqueue / gkqueuedirectorymonitor.c
blobee0cf3a80d71f442217476ccc0e218da57049ed4
1 /*******************************************************************************
2 Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 *******************************************************************************/
23 #include "config.h"
25 #include "gkqueuedirectorymonitor.h"
26 #include "kqueue-helper.h"
27 #include "kqueue-exclusions.h"
28 #include <gio/gpollfilemonitor.h>
29 #include <gio/gfile.h>
30 #include <gio/giomodule.h>
33 struct _GKqueueDirectoryMonitor
35 GLocalDirectoryMonitor parent_instance;
36 kqueue_sub *sub;
38 GFileMonitor *fallback;
39 GFile *fbfile;
41 gboolean pair_moves;
44 static gboolean g_kqueue_directory_monitor_cancel (GFileMonitor *monitor);
46 #define g_kqueue_directory_monitor_get_type _g_kqueue_directory_monitor_get_type
47 G_DEFINE_TYPE_WITH_CODE (GKqueueDirectoryMonitor, g_kqueue_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR,
48 g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
49 g_define_type_id,
50 "kqueue",
51 20))
54 static void
55 _fallback_callback (GFileMonitor *unused,
56 GFile *first,
57 GFile *second,
58 GFileMonitorEvent event,
59 gpointer udata)
61 GKqueueDirectoryMonitor *kq_mon = G_KQUEUE_DIRECTORY_MONITOR (udata);
62 GFileMonitor *mon = G_FILE_MONITOR (kq_mon);
63 g_assert (kq_mon != NULL);
64 g_assert (mon != NULL);
65 (void) unused;
67 if (event == G_FILE_MONITOR_EVENT_CHANGED)
69 _kh_dir_diff (kq_mon->sub, mon);
71 else
72 g_file_monitor_emit_event (mon, first, second, event);
76 static void
77 g_kqueue_directory_monitor_finalize (GObject *object)
79 GKqueueDirectoryMonitor *kqueue_monitor = G_KQUEUE_DIRECTORY_MONITOR (object);
81 if (kqueue_monitor->sub)
83 _kh_cancel_sub (kqueue_monitor->sub);
84 _kh_sub_free (kqueue_monitor->sub);
85 kqueue_monitor->sub = NULL;
88 if (kqueue_monitor->fallback)
89 g_object_unref (kqueue_monitor->fallback);
91 if (kqueue_monitor->fbfile)
92 g_object_unref (kqueue_monitor->fbfile);
94 if (G_OBJECT_CLASS (g_kqueue_directory_monitor_parent_class)->finalize)
95 (*G_OBJECT_CLASS (g_kqueue_directory_monitor_parent_class)->finalize) (object);
98 static GObject*
99 g_kqueue_directory_monitor_constructor (GType type,
100 guint n_construct_properties,
101 GObjectConstructParam *construct_properties)
103 GObject *obj;
104 GKqueueDirectoryMonitorClass *klass;
105 GObjectClass *parent_class;
106 GKqueueDirectoryMonitor *kqueue_monitor;
107 kqueue_sub *sub = NULL;
108 gboolean ret_kh_startup;
109 const gchar *path = NULL;
111 klass = G_KQUEUE_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_KQUEUE_DIRECTORY_MONITOR));
112 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
113 obj = parent_class->constructor (type,
114 n_construct_properties,
115 construct_properties);
117 kqueue_monitor = G_KQUEUE_DIRECTORY_MONITOR (obj);
119 ret_kh_startup = _kh_startup ();
120 g_assert (ret_kh_startup);
122 kqueue_monitor->pair_moves = (G_LOCAL_DIRECTORY_MONITOR (obj)->flags & G_FILE_MONITOR_SEND_MOVED)
123 ? TRUE : FALSE;
125 kqueue_monitor->sub = NULL;
126 kqueue_monitor->fallback = NULL;
127 kqueue_monitor->fbfile = NULL;
129 path = G_LOCAL_DIRECTORY_MONITOR (obj)->dirname;
131 /* For a directory monitor, create a subscription object anyway.
132 * It will be used for directory diff calculation routines. */
134 sub = _kh_sub_new (path,
135 kqueue_monitor->pair_moves,
136 kqueue_monitor);
138 /* FIXME: what to do about errors here? we can't return NULL or another
139 * kind of error and an assertion is probably too hard (same issue as in
140 * the inotify backend) */
141 g_assert (sub != NULL);
142 kqueue_monitor->sub = sub;
144 if (!_ke_is_excluded (path))
145 _kh_add_sub (sub);
146 else
148 GFile *file = g_file_new_for_path (path);
149 kqueue_monitor->fbfile = file;
150 kqueue_monitor->fallback = _g_poll_file_monitor_new (file);
151 g_signal_connect (kqueue_monitor->fallback,
152 "changed",
153 G_CALLBACK (_fallback_callback),
154 kqueue_monitor);
157 return obj;
160 static gboolean
161 g_kqueue_directory_monitor_is_supported (void)
163 return _kh_startup ();
166 static void
167 g_kqueue_directory_monitor_class_init (GKqueueDirectoryMonitorClass *klass)
169 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170 GFileMonitorClass *directory_monitor_class = G_FILE_MONITOR_CLASS (klass);
171 GLocalDirectoryMonitorClass *local_directory_monitor_class = G_LOCAL_DIRECTORY_MONITOR_CLASS (klass);
173 gobject_class->finalize = g_kqueue_directory_monitor_finalize;
174 gobject_class->constructor = g_kqueue_directory_monitor_constructor;
175 directory_monitor_class->cancel = g_kqueue_directory_monitor_cancel;
177 local_directory_monitor_class->mount_notify = TRUE; /* TODO: ??? */
178 local_directory_monitor_class->is_supported = g_kqueue_directory_monitor_is_supported;
181 static void
182 g_kqueue_directory_monitor_init (GKqueueDirectoryMonitor *monitor)
186 static gboolean
187 g_kqueue_directory_monitor_cancel (GFileMonitor *monitor)
189 GKqueueDirectoryMonitor *kqueue_monitor = G_KQUEUE_DIRECTORY_MONITOR (monitor);
191 if (kqueue_monitor->sub)
193 _kh_cancel_sub (kqueue_monitor->sub);
194 _kh_sub_free (kqueue_monitor->sub);
195 kqueue_monitor->sub = NULL;
197 else if (kqueue_monitor->fallback)
198 g_file_monitor_cancel (kqueue_monitor->fallback);
201 if (G_FILE_MONITOR_CLASS (g_kqueue_directory_monitor_parent_class)->cancel)
202 (*G_FILE_MONITOR_CLASS (g_kqueue_directory_monitor_parent_class)->cancel) (monitor);
204 return TRUE;