Use the new g_strv_contains
[platform/upstream/glib.git] / gio / inotify / ginotifyfilemonitor.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 "ginotifyfilemonitor.h"
27 #include <gio/giomodule.h>
28
29 #define USE_INOTIFY 1
30 #include "inotify-helper.h"
31
32 struct _GInotifyFileMonitor
33 {
34   GLocalFileMonitor parent_instance;
35   gchar *filename;
36   gchar *dirname;
37   inotify_sub *sub;
38   gboolean pair_moves;
39 };
40
41 static gboolean g_inotify_file_monitor_cancel (GFileMonitor* monitor);
42
43 #define g_inotify_file_monitor_get_type _g_inotify_file_monitor_get_type
44 G_DEFINE_TYPE_WITH_CODE (GInotifyFileMonitor, g_inotify_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
45                          g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
46                                                          g_define_type_id,
47                                                          "inotify",
48                                                          20))
49
50 static void
51 g_inotify_file_monitor_finalize (GObject *object)
52 {
53   GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (object);
54   inotify_sub *sub = inotify_monitor->sub;
55
56   if (sub)
57     {
58       _ih_sub_cancel (sub);
59       _ih_sub_free (sub);
60       inotify_monitor->sub = NULL;
61     }
62
63   g_free (inotify_monitor->filename);
64   g_free (inotify_monitor->dirname);
65
66   G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize (object);
67 }
68
69 static void
70 g_inotify_file_monitor_start (GLocalFileMonitor *local_monitor)
71 {
72   GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (local_monitor);
73   const gchar *filename = NULL;
74   gboolean watch_hardlinks;
75   inotify_sub *sub = NULL;
76   gboolean pair_moves;
77   gboolean ret_ih_startup; /* return value of _ih_startup, for asserting */
78
79   filename = local_monitor->filename;
80   g_assert (filename != NULL);
81
82   inotify_monitor->filename = g_path_get_basename (filename);
83   inotify_monitor->dirname = g_path_get_dirname (filename);
84
85   /* Will never fail as is_supported() should be called before instantiating
86    * anyway */
87   /* assert on return value */
88   ret_ih_startup = _ih_startup();
89   g_assert (ret_ih_startup);
90
91   pair_moves = local_monitor->flags & G_FILE_MONITOR_SEND_MOVED;
92   watch_hardlinks = local_monitor->flags & G_FILE_MONITOR_WATCH_HARD_LINKS;
93
94   sub = _ih_sub_new (inotify_monitor->dirname,
95                      inotify_monitor->filename,
96                      pair_moves,
97                      watch_hardlinks,
98                      inotify_monitor);
99
100   /* FIXME: what to do about errors here? we can't return NULL or another
101    * kind of error and an assertion is probably too hard */
102   g_assert (sub != NULL);
103
104   /* _ih_sub_add allways returns TRUE, see gio/inotify/inotify-helper.c line 109
105    * g_assert (_ih_sub_add (sub)); */
106   _ih_sub_add (sub);
107
108   inotify_monitor->sub = sub;
109 }
110
111 static gboolean
112 g_inotify_file_monitor_is_supported (void)
113 {
114   return _ih_startup ();
115 }
116
117 static void
118 g_inotify_file_monitor_class_init (GInotifyFileMonitorClass* klass)
119 {
120   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
121   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
122   GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
123
124   gobject_class->finalize = g_inotify_file_monitor_finalize;
125   file_monitor_class->cancel = g_inotify_file_monitor_cancel;
126
127   local_file_monitor_class->is_supported = g_inotify_file_monitor_is_supported;
128   local_file_monitor_class->start = g_inotify_file_monitor_start;
129 }
130
131 static void
132 g_inotify_file_monitor_init (GInotifyFileMonitor* monitor)
133 {
134 }
135
136 static gboolean
137 g_inotify_file_monitor_cancel (GFileMonitor* monitor)
138 {
139   GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (monitor);
140   inotify_sub *sub = inotify_monitor->sub;
141
142   if (sub) 
143     {
144       _ih_sub_cancel (sub);
145       _ih_sub_free (sub);
146       inotify_monitor->sub = NULL;
147     }
148
149   if (G_FILE_MONITOR_CLASS (g_inotify_file_monitor_parent_class)->cancel)
150     (*G_FILE_MONITOR_CLASS (g_inotify_file_monitor_parent_class)->cancel) (monitor);
151
152   return TRUE;
153 }