doc: improve doc of g_file_equal()
[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, 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  */
23
24 #include "config.h"
25
26 #include "ginotifydirectorymonitor.h"
27 #include <gio/giomodule.h>
28
29 #define USE_INOTIFY 1
30 #include "inotify-helper.h"
31
32 struct _GInotifyDirectoryMonitor
33 {
34   GLocalDirectoryMonitor parent_instance;
35   inotify_sub *sub;
36 };
37
38 static gboolean g_inotify_directory_monitor_cancel (GFileMonitor* monitor);
39
40 #define g_inotify_directory_monitor_get_type _g_inotify_directory_monitor_get_type
41 G_DEFINE_TYPE_WITH_CODE (GInotifyDirectoryMonitor, g_inotify_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR,
42                          g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
43                                                          g_define_type_id,
44                                                          "inotify",
45                                                          20))
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   G_OBJECT_CLASS (g_inotify_directory_monitor_parent_class)->finalize (object);
61 }
62
63 static void
64 g_inotify_directory_monitor_start (GLocalDirectoryMonitor *local_monitor)
65 {
66   GInotifyDirectoryMonitor *inotify_monitor = G_INOTIFY_DIRECTORY_MONITOR (local_monitor);
67   const gchar *dirname = NULL;
68   inotify_sub *sub = NULL;
69   gboolean ret_ih_startup; /* return value of _ih_startup, for asserting */
70   gboolean pair_moves;
71
72   dirname = local_monitor->dirname;
73   g_assert (dirname != NULL);
74
75   /* Will never fail as is_supported() should be called before instantiating
76    * anyway */
77   /* assert on return value */
78   ret_ih_startup = _ih_startup();
79   g_assert (ret_ih_startup);
80
81   pair_moves = local_monitor->flags & G_FILE_MONITOR_SEND_MOVED;
82
83   sub = _ih_sub_new (dirname, NULL, pair_moves, FALSE, inotify_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   /* _ih_sub_add allways returns TRUE, see gio/inotify/inotify-helper.c line 109
89    * g_assert (_ih_sub_add (sub)); */
90   _ih_sub_add(sub);
91
92   inotify_monitor->sub = sub;
93 }
94
95 static gboolean
96 g_inotify_directory_monitor_is_supported (void)
97 {
98   return _ih_startup ();
99 }
100
101 static void
102 g_inotify_directory_monitor_class_init (GInotifyDirectoryMonitorClass* klass)
103 {
104   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
105   GFileMonitorClass *directory_monitor_class = G_FILE_MONITOR_CLASS (klass);
106   GLocalDirectoryMonitorClass *local_directory_monitor_class = G_LOCAL_DIRECTORY_MONITOR_CLASS (klass);
107
108   gobject_class->finalize = g_inotify_directory_monitor_finalize;
109   directory_monitor_class->cancel = g_inotify_directory_monitor_cancel;
110
111   local_directory_monitor_class->mount_notify = TRUE;
112   local_directory_monitor_class->is_supported = g_inotify_directory_monitor_is_supported;
113   local_directory_monitor_class->start = g_inotify_directory_monitor_start;
114 }
115
116 static void
117 g_inotify_directory_monitor_init (GInotifyDirectoryMonitor* monitor)
118 {
119 }
120
121 static gboolean
122 g_inotify_directory_monitor_cancel (GFileMonitor* monitor)
123 {
124   GInotifyDirectoryMonitor *inotify_monitor = G_INOTIFY_DIRECTORY_MONITOR (monitor);
125   inotify_sub *sub = inotify_monitor->sub;
126
127   if (sub) 
128     {
129       _ih_sub_cancel (sub);
130       _ih_sub_free (sub);
131       inotify_monitor->sub = NULL;
132     }
133
134   if (G_FILE_MONITOR_CLASS (g_inotify_directory_monitor_parent_class)->cancel)
135     (*G_FILE_MONITOR_CLASS (g_inotify_directory_monitor_parent_class)->cancel) (monitor);
136
137   return TRUE;
138 }