gio/ docs/reference/gio Merged gio-standalone into glib.
[platform/upstream/glib.git] / gio / glocalfilemonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include "glocalfilemonitor.h"
26 #include "giomodule.h"
27
28 #include <string.h>
29
30 enum
31 {
32   PROP_0,
33   PROP_FILENAME
34 };
35
36 G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)
37
38 static void
39 g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
40 {
41 }
42
43 static void
44 g_local_file_monitor_set_property (GObject *object,
45                                    guint property_id,
46                                    const GValue *value,
47                                    GParamSpec *pspec)
48 {
49   switch (property_id)
50   {
51     case PROP_FILENAME:
52       /* Do nothing */
53       break;
54     default:
55       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
56       break;
57   }
58 }
59
60 static GObject *
61 g_local_file_monitor_constructor (GType type,
62                                   guint n_construct_properties,
63                                   GObjectConstructParam *construct_properties)
64 {
65   GObject *obj;
66   GLocalFileMonitorClass *klass;
67   GObjectClass *parent_class;
68   GLocalFileMonitor *local_monitor;
69   const gchar *filename = NULL;
70   gint i;
71   
72   klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_FILE_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   local_monitor = G_LOCAL_FILE_MONITOR (obj);
79
80   for (i = 0; i < n_construct_properties; i++)
81     {
82       if (strcmp ("filename", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
83         {
84           g_assert (G_VALUE_HOLDS_STRING (construct_properties[i].value));
85           filename = g_value_get_string (construct_properties[i].value);
86           break;
87         }
88     }
89
90   g_assert (filename != NULL);
91
92   local_monitor->filename = g_strdup (filename);
93   return obj;
94 }
95
96 static void
97 g_local_file_monitor_finalize (GObject *object)
98 {
99   GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
100   if (local_monitor->filename)
101     {
102       g_free (local_monitor->filename);
103       local_monitor->filename = NULL;
104     }
105
106   if (G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize)
107     (*G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize) (object);
108 }
109
110 static void
111 g_local_file_monitor_class_init (GLocalFileMonitorClass* klass)
112 {
113   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114
115   gobject_class->set_property = g_local_file_monitor_set_property;
116   gobject_class->finalize = g_local_file_monitor_finalize;
117   gobject_class->constructor = g_local_file_monitor_constructor;
118
119   g_object_class_install_property (gobject_class, PROP_FILENAME,
120       g_param_spec_string ("filename", "File name", "File name to monitor",
121           NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
122 }
123
124 static gint
125 _compare_monitor_class_by_prio (gconstpointer a,
126                                 gconstpointer b,
127                                 gpointer user_data)
128 {
129   GType *type1 = (GType *) a, *type2 = (GType *) b;
130   GLocalFileMonitorClass *klass1, *klass2;
131   gint ret;
132
133   klass1 = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (*type1));
134   klass2 = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (*type2));
135
136   ret = klass1->prio - klass2->prio;
137
138   g_type_class_unref (klass1);
139   g_type_class_unref (klass2);
140
141   return ret;
142 }
143
144 extern GType g_inotify_file_monitor_get_type (void);
145
146 static gpointer
147 get_default_local_file_monitor (gpointer data)
148 {
149   GType *monitor_impls, chosen_type;
150   guint n_monitor_impls;
151   gint i;
152   GType *ret = (GType *) data;
153
154 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
155   /* Register Inotify monitor */
156   g_inotify_file_monitor_get_type ();
157 #endif
158
159   g_io_modules_ensure_loaded (GIO_MODULE_DIR);
160   
161   monitor_impls = g_type_children (G_TYPE_LOCAL_FILE_MONITOR,
162                                    &n_monitor_impls);
163
164   chosen_type = G_TYPE_INVALID;
165
166   g_qsort_with_data (monitor_impls,
167                      n_monitor_impls,
168                      sizeof (GType),
169                      _compare_monitor_class_by_prio,
170                      NULL);
171
172   for (i = n_monitor_impls - 1; i >= 0 && chosen_type == G_TYPE_INVALID; i--)
173     {    
174       GLocalFileMonitorClass *klass;
175
176       klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
177
178       if (klass->is_supported())
179         chosen_type = monitor_impls[i];
180
181       g_type_class_unref (klass);
182     }
183
184   g_free (monitor_impls);
185
186   *ret = chosen_type;
187
188   return NULL;
189 }
190
191 /**
192  * g_local_file_monitor_new:
193  * @pathname: path name to monitor.
194  * @flags: #GFileMonitorFlags.
195  * 
196  * Returns: a new #GFileMonitor for the given @pathname. 
197  **/
198 GFileMonitor*
199 g_local_file_monitor_new (const char* pathname,
200                           GFileMonitorFlags flags)
201 {
202   static GOnce once_init = G_ONCE_INIT;
203   static GType monitor_type = G_TYPE_INVALID;
204
205   g_once (&once_init, get_default_local_file_monitor, &monitor_type);
206
207   if (monitor_type != G_TYPE_INVALID)
208     return G_FILE_MONITOR (g_object_new (monitor_type, "filename", pathname, NULL));
209
210   return NULL;
211 }