Tizen 2.1 base
[platform/upstream/glib2.0.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 "gioenumtypes.h"
26 #include "glocalfilemonitor.h"
27 #include "giomodule-priv.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
30
31 #include <string.h>
32
33
34 enum
35 {
36   PROP_0,
37   PROP_FILENAME,
38   PROP_FLAGS
39 };
40
41 G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)
42
43 static void
44 g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
45 {
46 }
47
48 static void
49 g_local_file_monitor_set_property (GObject      *object,
50                                    guint         property_id,
51                                    const GValue *value,
52                                    GParamSpec   *pspec)
53 {
54   switch (property_id)
55   {
56     case PROP_FILENAME:
57       /* Do nothing */
58       break;
59     case PROP_FLAGS:
60       /* Do nothing as well */
61       break;
62     default:
63       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
64       break;
65   }
66 }
67
68 static GObject *
69 g_local_file_monitor_constructor (GType                  type,
70                                   guint                  n_construct_properties,
71                                   GObjectConstructParam *construct_properties)
72 {
73   GObject *obj;
74   GLocalFileMonitorClass *klass;
75   GObjectClass *parent_class;
76   GLocalFileMonitor *local_monitor;
77   const gchar *filename = NULL;
78   GFileMonitorFlags flags = 0;
79   gint i;
80   
81   klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_FILE_MONITOR));
82   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
83   obj = parent_class->constructor (type,
84                                    n_construct_properties,
85                                    construct_properties);
86
87   local_monitor = G_LOCAL_FILE_MONITOR (obj);
88
89   for (i = 0; i < n_construct_properties; i++)
90     {
91       if (strcmp ("filename", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
92         {
93           g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
94           filename = g_value_get_string (construct_properties[i].value);
95         }
96       else if (strcmp ("flags", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
97         {
98           g_warn_if_fail (G_VALUE_HOLDS_FLAGS (construct_properties[i].value));
99           flags = g_value_get_flags (construct_properties[i].value);
100         }
101     }
102
103   g_warn_if_fail (filename != NULL);
104
105   local_monitor->filename = g_strdup (filename);
106   local_monitor->flags = flags;
107   return obj;
108 }
109
110 static void
111 g_local_file_monitor_finalize (GObject *object)
112 {
113   GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
114   if (local_monitor->filename)
115     {
116       g_free (local_monitor->filename);
117       local_monitor->filename = NULL;
118     }
119
120   G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize (object);
121 }
122
123 static void g_local_file_monitor_class_init (GLocalFileMonitorClass *klass)
124 {
125   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126
127   gobject_class->set_property = g_local_file_monitor_set_property;
128   gobject_class->finalize = g_local_file_monitor_finalize;
129   gobject_class->constructor = g_local_file_monitor_constructor;
130
131   g_object_class_install_property (gobject_class, 
132                                    PROP_FILENAME,
133                                    g_param_spec_string ("filename", 
134                                                         P_("File name"), 
135                                                         P_("File name to monitor"),
136                                                         NULL, 
137                                                         G_PARAM_CONSTRUCT_ONLY|
138                                                         G_PARAM_WRITABLE|
139                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
140
141   g_object_class_install_property (gobject_class,
142                                    PROP_FLAGS,
143                                    g_param_spec_flags ("flags",
144                                                        P_("Monitor flags"),
145                                                        P_("Monitor flags"),
146                                                        G_TYPE_FILE_MONITOR_FLAGS,
147                                                        0,
148                                                        G_PARAM_CONSTRUCT_ONLY|
149                                                        G_PARAM_WRITABLE|
150                                                        G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
151 }
152
153 static gpointer
154 get_default_local_file_monitor (gpointer data)
155 {
156   GLocalFileMonitorClass *chosen_class;
157   GLocalFileMonitorClass **ret = data;
158   GIOExtensionPoint *ep;
159   GList *extensions, *l;
160
161   _g_io_modules_ensure_loaded ();
162
163   ep = g_io_extension_point_lookup (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
164
165   extensions = g_io_extension_point_get_extensions (ep);
166   
167   chosen_class = NULL;
168   for (l = extensions; l != NULL; l = l->next)
169     {
170       GIOExtension *extension = l->data;
171       GLocalFileMonitorClass *klass;
172       
173       klass = G_LOCAL_FILE_MONITOR_CLASS (g_io_extension_ref_class (extension));
174       
175       if (klass->is_supported ())
176         {
177           chosen_class = klass;
178           break;
179         }
180       else
181         g_type_class_unref (klass);
182     }
183   
184   if (chosen_class)
185     {
186       *ret = chosen_class;
187       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
188     }
189   else
190     return (gpointer)G_TYPE_INVALID;
191 }
192
193 GFileMonitor*
194 _g_local_file_monitor_new (const char         *pathname,
195                            GFileMonitorFlags   flags,
196                            GError            **error)
197 {
198   static GOnce once_init = G_ONCE_INIT;
199   GTypeClass *type_class;
200   GFileMonitor *monitor;
201   GType type;
202
203   type_class = NULL;
204   g_once (&once_init, get_default_local_file_monitor, &type_class);
205   type = (GType)once_init.retval;
206
207   monitor = NULL;
208   if (type != G_TYPE_INVALID)
209     monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, NULL));
210   else
211     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
212                          _("Unable to find default local file monitor type"));
213
214   /* This is non-null on first pass here. Unref the class now.
215    * This is to avoid unloading the module and then loading it
216    * again which would happen if we unrefed the class
217    * before creating the monitor.
218    */
219   
220   if (type_class)
221     g_type_class_unref (type_class);
222   
223   return monitor;
224 }