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"
36 G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)
39 g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
44 g_local_file_monitor_set_property (GObject *object,
55 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
61 g_local_file_monitor_constructor (GType type,
62 guint n_construct_properties,
63 GObjectConstructParam *construct_properties)
66 GLocalFileMonitorClass *klass;
67 GObjectClass *parent_class;
68 GLocalFileMonitor *local_monitor;
69 const gchar *filename = NULL;
72 klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_FILE_MONITOR));
73 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
74 obj = parent_class->constructor (type,
75 n_construct_properties,
76 construct_properties);
78 local_monitor = G_LOCAL_FILE_MONITOR (obj);
80 for (i = 0; i < n_construct_properties; i++)
82 if (strcmp ("filename", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
84 g_assert (G_VALUE_HOLDS_STRING (construct_properties[i].value));
85 filename = g_value_get_string (construct_properties[i].value);
90 g_assert (filename != NULL);
92 local_monitor->filename = g_strdup (filename);
97 g_local_file_monitor_finalize (GObject *object)
99 GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
100 if (local_monitor->filename)
102 g_free (local_monitor->filename);
103 local_monitor->filename = NULL;
106 if (G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize)
107 (*G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize) (object);
111 g_local_file_monitor_class_init (GLocalFileMonitorClass* klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 gobject_class->set_property = g_local_file_monitor_set_property;
116 gobject_class->finalize = g_local_file_monitor_finalize;
117 gobject_class->constructor = g_local_file_monitor_constructor;
119 g_object_class_install_property (gobject_class, PROP_FILENAME,
120 g_param_spec_string ("filename", "File name", "File name to monitor",
121 NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
125 _compare_monitor_class_by_prio (gconstpointer a,
129 GType *type1 = (GType *) a, *type2 = (GType *) b;
130 GLocalFileMonitorClass *klass1, *klass2;
133 klass1 = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (*type1));
134 klass2 = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (*type2));
136 ret = klass1->prio - klass2->prio;
138 g_type_class_unref (klass1);
139 g_type_class_unref (klass2);
144 extern GType g_inotify_file_monitor_get_type (void);
147 get_default_local_file_monitor (gpointer data)
149 GType *monitor_impls, chosen_type;
150 guint n_monitor_impls;
152 GType *ret = (GType *) data;
154 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
155 /* Register Inotify monitor */
156 g_inotify_file_monitor_get_type ();
159 g_io_modules_ensure_loaded (GIO_MODULE_DIR);
161 monitor_impls = g_type_children (G_TYPE_LOCAL_FILE_MONITOR,
164 chosen_type = G_TYPE_INVALID;
166 g_qsort_with_data (monitor_impls,
169 _compare_monitor_class_by_prio,
172 for (i = n_monitor_impls - 1; i >= 0 && chosen_type == G_TYPE_INVALID; i--)
174 GLocalFileMonitorClass *klass;
176 klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
178 if (klass->is_supported())
179 chosen_type = monitor_impls[i];
181 g_type_class_unref (klass);
184 g_free (monitor_impls);
192 * g_local_file_monitor_new:
193 * @pathname: path name to monitor.
194 * @flags: #GFileMonitorFlags.
196 * Returns: a new #GFileMonitor for the given @pathname.
199 g_local_file_monitor_new (const char* pathname,
200 GFileMonitorFlags flags)
202 static GOnce once_init = G_ONCE_INIT;
203 static GType monitor_type = G_TYPE_INVALID;
205 g_once (&once_init, get_default_local_file_monitor, &monitor_type);
207 if (monitor_type != G_TYPE_INVALID)
208 return G_FILE_MONITOR (g_object_new (monitor_type, "filename", pathname, NULL));