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