GMenuModel exporter: remove workaround
[glib.git] / gio / inotify / ginotifyfilemonitor.c
blobbdfea194e0a55189e64ad54806feefd7181d7a1b
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Sebastian Dröge.
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 of the License, or (at your option) any later version.
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.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Alexander Larsson <alexl@redhat.com>
22 * John McCutchan <john@johnmccutchan.com>
23 * Sebastian Dröge <slomo@circular-chaos.org>
26 #include "config.h"
28 #include "ginotifyfilemonitor.h"
29 #include <gio/giomodule.h>
31 #define USE_INOTIFY 1
32 #include "inotify-helper.h"
34 struct _GInotifyFileMonitor
36 GLocalFileMonitor parent_instance;
37 gchar *filename;
38 gchar *dirname;
39 inotify_sub *sub;
40 gboolean pair_moves;
43 static gboolean g_inotify_file_monitor_cancel (GFileMonitor* monitor);
45 #define g_inotify_file_monitor_get_type _g_inotify_file_monitor_get_type
46 G_DEFINE_TYPE_WITH_CODE (GInotifyFileMonitor, g_inotify_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
47 g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
48 g_define_type_id,
49 "inotify",
50 20))
52 static void
53 g_inotify_file_monitor_finalize (GObject *object)
55 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (object);
56 inotify_sub *sub = inotify_monitor->sub;
58 if (sub)
60 _ih_sub_cancel (sub);
61 _ih_sub_free (sub);
62 inotify_monitor->sub = NULL;
65 if (inotify_monitor->filename)
67 g_free (inotify_monitor->filename);
68 inotify_monitor->filename = NULL;
71 if (inotify_monitor->dirname)
73 g_free (inotify_monitor->dirname);
74 inotify_monitor->dirname = NULL;
77 if (G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize)
78 (*G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize) (object);
81 static GObject *
82 g_inotify_file_monitor_constructor (GType type,
83 guint n_construct_properties,
84 GObjectConstructParam *construct_properties)
86 GObject *obj;
87 GInotifyFileMonitorClass *klass;
88 GObjectClass *parent_class;
89 GInotifyFileMonitor *inotify_monitor;
90 const gchar *filename = NULL;
91 inotify_sub *sub = NULL;
92 gboolean pair_moves;
93 gboolean ret_ih_startup; /* return value of _ih_startup, for asserting */
95 klass = G_INOTIFY_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_INOTIFY_FILE_MONITOR));
96 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
97 obj = parent_class->constructor (type,
98 n_construct_properties,
99 construct_properties);
101 inotify_monitor = G_INOTIFY_FILE_MONITOR (obj);
103 filename = G_LOCAL_FILE_MONITOR (obj)->filename;
105 g_assert (filename != NULL);
107 inotify_monitor->filename = g_path_get_basename (filename);
108 inotify_monitor->dirname = g_path_get_dirname (filename);
110 /* Will never fail as is_supported() should be called before instanciating
111 * anyway */
112 /* assert on return value */
113 ret_ih_startup = _ih_startup();
114 g_assert (ret_ih_startup);
116 pair_moves = G_LOCAL_FILE_MONITOR (obj)->flags & G_FILE_MONITOR_SEND_MOVED;
118 sub = _ih_sub_new (inotify_monitor->dirname,
119 inotify_monitor->filename,
120 pair_moves,
121 inotify_monitor);
123 /* FIXME: what to do about errors here? we can't return NULL or another
124 * kind of error and an assertion is probably too hard */
125 g_assert (sub != NULL);
127 /* _ih_sub_add allways returns TRUE, see gio/inotify/inotify-helper.c line 109
128 * g_assert (_ih_sub_add (sub)); */
129 _ih_sub_add (sub);
131 inotify_monitor->sub = sub;
133 return obj;
136 static gboolean
137 g_inotify_file_monitor_is_supported (void)
139 return _ih_startup ();
142 static void
143 g_inotify_file_monitor_class_init (GInotifyFileMonitorClass* klass)
145 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
146 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
147 GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
149 gobject_class->finalize = g_inotify_file_monitor_finalize;
150 gobject_class->constructor = g_inotify_file_monitor_constructor;
151 file_monitor_class->cancel = g_inotify_file_monitor_cancel;
153 local_file_monitor_class->is_supported = g_inotify_file_monitor_is_supported;
156 static void
157 g_inotify_file_monitor_init (GInotifyFileMonitor* monitor)
161 static gboolean
162 g_inotify_file_monitor_cancel (GFileMonitor* monitor)
164 GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (monitor);
165 inotify_sub *sub = inotify_monitor->sub;
167 if (sub)
169 _ih_sub_cancel (sub);
170 _ih_sub_free (sub);
171 inotify_monitor->sub = NULL;
174 if (G_FILE_MONITOR_CLASS (g_inotify_file_monitor_parent_class)->cancel)
175 (*G_FILE_MONITOR_CLASS (g_inotify_file_monitor_parent_class)->cancel) (monitor);
177 return TRUE;