Imported Upstream version 2.53.3
[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.1 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  *          Ryan Lortie <desrt@desrt.ca>
23  */
24
25 #include "config.h"
26
27 #include "ginotifyfilemonitor.h"
28 #include <gio/giomodule.h>
29
30 #define USE_INOTIFY 1
31 #include "inotify-helper.h"
32
33 struct _GInotifyFileMonitor
34 {
35   GLocalFileMonitor parent_instance;
36
37   inotify_sub *sub;
38 };
39
40 G_DEFINE_TYPE_WITH_CODE (GInotifyFileMonitor, g_inotify_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
41                          g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
42                                                          g_define_type_id, "inotify", 20))
43
44 static gboolean
45 g_inotify_file_monitor_is_supported (void)
46 {
47   return _ih_startup ();
48 }
49
50 static void
51 g_inotify_file_monitor_start (GLocalFileMonitor  *local_monitor,
52                               const gchar        *dirname,
53                               const gchar        *basename,
54                               const gchar        *filename,
55                               GFileMonitorSource *source)
56 {
57   GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (local_monitor);
58   gboolean success;
59
60   /* should already have been called, from is_supported() */
61   success = _ih_startup ();
62   g_assert (success);
63
64   inotify_monitor->sub = _ih_sub_new (dirname, basename, filename != NULL, source);
65   _ih_sub_add (inotify_monitor->sub);
66 }
67
68 static gboolean
69 g_inotify_file_monitor_cancel (GFileMonitor *monitor)
70 {
71   GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (monitor);
72
73   if (inotify_monitor->sub)
74     {
75       _ih_sub_cancel (inotify_monitor->sub);
76       _ih_sub_free (inotify_monitor->sub);
77       inotify_monitor->sub = NULL;
78     }
79
80   return TRUE;
81 }
82
83 static void
84 g_inotify_file_monitor_finalize (GObject *object)
85 {
86   GInotifyFileMonitor *inotify_monitor = G_INOTIFY_FILE_MONITOR (object);
87
88   /* must surely have been cancelled already */
89   g_assert (!inotify_monitor->sub);
90
91   G_OBJECT_CLASS (g_inotify_file_monitor_parent_class)->finalize (object);
92 }
93
94 static void
95 g_inotify_file_monitor_init (GInotifyFileMonitor* monitor)
96 {
97 }
98
99 static void
100 g_inotify_file_monitor_class_init (GInotifyFileMonitorClass* klass)
101 {
102   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
103   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
104   GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
105
106   local_file_monitor_class->is_supported = g_inotify_file_monitor_is_supported;
107   local_file_monitor_class->start = g_inotify_file_monitor_start;
108   local_file_monitor_class->mount_notify = TRUE;
109   file_monitor_class->cancel = g_inotify_file_monitor_cancel;
110
111   gobject_class->finalize = g_inotify_file_monitor_finalize;
112 }