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 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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 * Authors: Alexander Larsson <alexl@redhat.com>
22 * John McCutchan <john@johnmccutchan.com>
23 * Sebastian Dröge <slomo@circular-chaos.org>
24 */
25
26 #include "config.h"
27
28 #include "gfamdirectorymonitor.h"
29 #include "giomodule.h"
30
31 #include "fam-helper.h"
32
33 struct _GFamDirectoryMonitor
34 {
35 GLocalDirectoryMonitor parent_instance;
36 fam_sub *sub;
37 };
38
39 static gboolean g_fam_directory_monitor_cancel (GFileMonitor* monitor);
40
G_DEFINE_DYNAMIC_TYPE(GFamDirectoryMonitor,g_fam_directory_monitor,G_TYPE_LOCAL_DIRECTORY_MONITOR)41 G_DEFINE_DYNAMIC_TYPE (GFamDirectoryMonitor, g_fam_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR)
42
43 static void
44 g_fam_directory_monitor_finalize (GObject *object)
45 {
46 GFamDirectoryMonitor *fam_monitor = G_FAM_DIRECTORY_MONITOR (object);
47 fam_sub *sub = fam_monitor->sub;
48
49 if (sub) {
50 if (!_fam_sub_cancel (sub))
51 g_warning ("Unexpected error cancelling fam monitor");
52
53 fam_monitor->sub = NULL;
54 }
55
56 if (G_OBJECT_CLASS (g_fam_directory_monitor_parent_class)->finalize)
57 (*G_OBJECT_CLASS (g_fam_directory_monitor_parent_class)->finalize) (object);
58 }
59
60 static GObject *
g_fam_directory_monitor_constructor(GType type,guint n_construct_properties,GObjectConstructParam * construct_properties)61 g_fam_directory_monitor_constructor (GType type,
62 guint n_construct_properties,
63 GObjectConstructParam *construct_properties)
64 {
65 GObject *obj;
66 GFamDirectoryMonitorClass *klass;
67 GObjectClass *parent_class;
68 GFamDirectoryMonitor *fam_monitor;
69 const gchar *dirname = NULL;
70 fam_sub *sub = NULL;
71
72 klass = G_FAM_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_FAM_DIRECTORY_MONITOR));
73 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
74 obj = parent_class->constructor (type,
75 n_construct_properties,
76 construct_properties);
77
78 fam_monitor = G_FAM_DIRECTORY_MONITOR (obj);
79
80 dirname = G_LOCAL_DIRECTORY_MONITOR (obj)->dirname;
81 g_assert (dirname != NULL);
82
83 sub = _fam_sub_add (dirname, TRUE, fam_monitor);
84 /* FIXME: what to do about errors here? we can't return NULL or another
85 * kind of error and an assertion is probably too hard */
86 g_assert (sub != NULL);
87
88 fam_monitor->sub = sub;
89
90 return obj;
91 }
92
93 static void
g_fam_directory_monitor_class_finalize(GFamDirectoryMonitorClass * klass)94 g_fam_directory_monitor_class_finalize (GFamDirectoryMonitorClass *klass)
95 {
96 }
97
98 static gboolean
g_fam_directory_monitor_is_supported(void)99 g_fam_directory_monitor_is_supported (void)
100 {
101 return _fam_sub_startup ();
102 }
103
104 static void
g_fam_directory_monitor_class_init(GFamDirectoryMonitorClass * klass)105 g_fam_directory_monitor_class_init (GFamDirectoryMonitorClass* klass)
106 {
107 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
108 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
109 GLocalDirectoryMonitorClass *local_directory_monitor_class = G_LOCAL_DIRECTORY_MONITOR_CLASS (klass);
110
111 gobject_class->finalize = g_fam_directory_monitor_finalize;
112 gobject_class->constructor = g_fam_directory_monitor_constructor;
113 file_monitor_class->cancel = g_fam_directory_monitor_cancel;
114
115 local_directory_monitor_class->mount_notify = FALSE;
116 local_directory_monitor_class->is_supported = g_fam_directory_monitor_is_supported;
117 }
118
119 static void
g_fam_directory_monitor_init(GFamDirectoryMonitor * monitor)120 g_fam_directory_monitor_init (GFamDirectoryMonitor* monitor)
121 {
122
123 }
124
125 static gboolean
g_fam_directory_monitor_cancel(GFileMonitor * monitor)126 g_fam_directory_monitor_cancel (GFileMonitor* monitor)
127 {
128 GFamDirectoryMonitor *fam_monitor = G_FAM_DIRECTORY_MONITOR (monitor);
129 fam_sub *sub = fam_monitor->sub;
130
131 if (sub) {
132 if (!_fam_sub_cancel (sub))
133 g_warning ("Unexpected error cancelling fam monitor");
134
135 fam_monitor->sub = NULL;
136 }
137
138 if (G_FILE_MONITOR_CLASS (g_fam_directory_monitor_parent_class)->cancel)
139 (*G_FILE_MONITOR_CLASS (g_fam_directory_monitor_parent_class)->cancel) (monitor);
140
141 return TRUE;
142 }
143
144 void
g_fam_directory_monitor_register(GIOModule * module)145 g_fam_directory_monitor_register (GIOModule *module)
146 {
147 g_fam_directory_monitor_register_type (G_TYPE_MODULE (module));
148 g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
149 G_TYPE_FAM_DIRECTORY_MONITOR,
150 "fam",
151 10);
152 }
153
154