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