1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "glocalfilemonitor.h"
26 #include "giomodule.h"
38 G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)
41 g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
46 g_local_file_monitor_set_property (GObject *object,
57 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
63 g_local_file_monitor_constructor (GType type,
64 guint n_construct_properties,
65 GObjectConstructParam *construct_properties)
68 GLocalFileMonitorClass *klass;
69 GObjectClass *parent_class;
70 GLocalFileMonitor *local_monitor;
71 const gchar *filename = NULL;
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);
80 local_monitor = G_LOCAL_FILE_MONITOR (obj);
82 for (i = 0; i < n_construct_properties; i++)
84 if (strcmp ("filename", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
86 g_assert (G_VALUE_HOLDS_STRING (construct_properties[i].value));
87 filename = g_value_get_string (construct_properties[i].value);
92 g_assert (filename != NULL);
94 local_monitor->filename = g_strdup (filename);
99 g_local_file_monitor_finalize (GObject *object)
101 GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
102 if (local_monitor->filename)
104 g_free (local_monitor->filename);
105 local_monitor->filename = NULL;
108 if (G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize)
109 (*G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize) (object);
113 g_local_file_monitor_class_init (GLocalFileMonitorClass* klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
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;
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));
127 _compare_monitor_class_by_prio (gconstpointer a,
131 GType *type1 = (GType *) a, *type2 = (GType *) b;
132 GLocalFileMonitorClass *klass1, *klass2;
135 klass1 = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (*type1));
136 klass2 = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (*type2));
138 ret = klass1->prio - klass2->prio;
140 g_type_class_unref (klass1);
141 g_type_class_unref (klass2);
146 extern GType _g_inotify_file_monitor_get_type (void);
149 get_default_local_file_monitor (gpointer data)
151 GType *monitor_impls, chosen_type;
152 guint n_monitor_impls;
154 GType *ret = (GType *) data;
156 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
157 /* Register Inotify monitor */
158 _g_inotify_file_monitor_get_type ();
161 g_io_modules_ensure_loaded (GIO_MODULE_DIR);
163 monitor_impls = g_type_children (G_TYPE_LOCAL_FILE_MONITOR,
166 chosen_type = G_TYPE_INVALID;
168 g_qsort_with_data (monitor_impls,
171 _compare_monitor_class_by_prio,
174 for (i = n_monitor_impls - 1; i >= 0 && chosen_type == G_TYPE_INVALID; i--)
176 GLocalFileMonitorClass *klass;
178 klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
180 if (klass->is_supported())
181 chosen_type = monitor_impls[i];
183 g_type_class_unref (klass);
186 g_free (monitor_impls);
194 * g_local_file_monitor_new:
195 * @pathname: path name to monitor.
196 * @flags: #GFileMonitorFlags.
198 * Returns: a new #GFileMonitor for the given @pathname.
201 _g_local_file_monitor_new (const char *pathname,
202 GFileMonitorFlags flags)
204 static GOnce once_init = G_ONCE_INIT;
205 static GType monitor_type = G_TYPE_INVALID;
207 g_once (&once_init, get_default_local_file_monitor, &monitor_type);
209 if (monitor_type != G_TYPE_INVALID)
210 return G_FILE_MONITOR (g_object_new (monitor_type, "filename", pathname, NULL));
215 #define __G_LOCAL_FILE_MONITOR_C__
216 #include "gioaliasdef.c"