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