eb9053438548004f38989596bde76a114cbfa653
[platform/upstream/glib.git] / gio / inotify / ginotifydirectorymonitor.c
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 "ginotifydirectorymonitor.h"
29 #include "giomodule.h"
30
31 #define USE_INOTIFY 1
32 #include "inotify-helper.h"
33
34 #include "gioalias.h"
35
36 struct _GInotifyDirectoryMonitor
37 {
38   GLocalDirectoryMonitor parent_instance;
39   inotify_sub *sub;
40 };
41
42 static gboolean g_inotify_directory_monitor_cancel (GFileMonitor* monitor);
43
44 #define g_inotify_directory_monitor_get_type _g_inotify_directory_monitor_get_type
45 G_DEFINE_TYPE (GInotifyDirectoryMonitor, g_inotify_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR)
46
47 static void
48 g_inotify_directory_monitor_finalize (GObject *object)
49 {
50   GInotifyDirectoryMonitor *inotify_monitor = G_INOTIFY_DIRECTORY_MONITOR (object);
51   inotify_sub *sub = inotify_monitor->sub;
52
53   if (sub)
54     {
55       _ih_sub_cancel (sub);
56       _ih_sub_free (sub);
57       inotify_monitor->sub = NULL;
58     }
59
60   if (G_OBJECT_CLASS (g_inotify_directory_monitor_parent_class)->finalize)
61     (*G_OBJECT_CLASS (g_inotify_directory_monitor_parent_class)->finalize) (object);
62 }
63
64 static GObject *
65 g_inotify_directory_monitor_constructor (GType type,
66                                     guint n_construct_properties,
67                                     GObjectConstructParam *construct_properties)
68 {
69   GObject *obj;
70   GInotifyDirectoryMonitorClass *klass;
71   GObjectClass *parent_class;
72   GInotifyDirectoryMonitor *inotify_monitor;
73   const gchar *dirname = NULL;
74   inotify_sub *sub = NULL;
75   
76   klass = G_INOTIFY_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_INOTIFY_DIRECTORY_MONITOR));
77   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
78   obj = parent_class->constructor (type,
79                                    n_construct_properties,
80                                    construct_properties);
81
82   inotify_monitor = G_INOTIFY_DIRECTORY_MONITOR (obj);
83
84   dirname = G_LOCAL_DIRECTORY_MONITOR (obj)->dirname;
85   g_assert (dirname != NULL);
86
87   /* Will never fail as is_supported() should be called before instanciating
88    * anyway */
89   g_assert (_ih_startup ());
90
91   sub = _ih_sub_new (dirname, NULL, inotify_monitor);
92   /* FIXME: what to do about errors here? we can't return NULL or another
93    * kind of error and an assertion is probably too hard */
94   g_assert (sub != NULL);
95   g_assert (_ih_sub_add (sub));
96
97   inotify_monitor->sub = sub;
98
99   return obj;
100 }
101
102 static gboolean
103 g_inotify_directory_monitor_is_supported (void)
104 {
105   return _ih_startup ();
106 }
107
108 static void
109 g_inotify_directory_monitor_class_init (GInotifyDirectoryMonitorClass* klass)
110 {
111   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
112   GFileMonitorClass *directory_monitor_class = G_FILE_MONITOR_CLASS (klass);
113   GLocalDirectoryMonitorClass *local_directory_monitor_class = G_LOCAL_DIRECTORY_MONITOR_CLASS (klass);
114   
115   gobject_class->finalize = g_inotify_directory_monitor_finalize;
116   gobject_class->constructor = g_inotify_directory_monitor_constructor;
117   directory_monitor_class->cancel = g_inotify_directory_monitor_cancel;
118
119   local_directory_monitor_class->prio = 20;
120   local_directory_monitor_class->mount_notify = TRUE;
121   local_directory_monitor_class->is_supported = g_inotify_directory_monitor_is_supported;
122 }
123
124 static void
125 g_inotify_directory_monitor_init (GInotifyDirectoryMonitor* monitor)
126 {
127 }
128
129 static gboolean
130 g_inotify_directory_monitor_cancel (GFileMonitor* monitor)
131 {
132   GInotifyDirectoryMonitor *inotify_monitor = G_INOTIFY_DIRECTORY_MONITOR (monitor);
133   inotify_sub *sub = inotify_monitor->sub;
134
135   if (sub) 
136     {
137       _ih_sub_cancel (sub);
138       _ih_sub_free (sub);
139       inotify_monitor->sub = NULL;
140     }
141
142   if (G_FILE_MONITOR_CLASS (g_inotify_directory_monitor_parent_class)->cancel)
143     (*G_FILE_MONITOR_CLASS (g_inotify_directory_monitor_parent_class)->cancel) (monitor);
144
145   return TRUE;
146 }
147