1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input IBus
4 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
5 * Copyright (C) 2008-2010 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
22 #include <glib/gstdio.h>
24 #include "ibusobservedpath.h"
32 /* IBusObservedPathPriv */
33 struct _IBusObservedPathPrivate {
36 typedef struct _IBusObservedPathPrivate IBusObservedPathPrivate;
38 #define IBUS_OBSERVED_PATH_GET_PRIVATE(o) \
39 (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_OBSERVED_PATH, IBusObservedPathPrivate))
41 // static guint _signals[LAST_SIGNAL] = { 0 };
43 /* functions prototype */
44 static void ibus_observed_path_destroy (IBusObservedPath *path);
45 static gboolean ibus_observed_path_serialize (IBusObservedPath *path,
46 GVariantBuilder *builder);
47 static gint ibus_observed_path_deserialize (IBusObservedPath *path,
49 static gboolean ibus_observed_path_copy (IBusObservedPath *dest,
50 const IBusObservedPath *src);
51 static gboolean ibus_observed_path_parse_xml_node (IBusObservedPath *path,
54 G_DEFINE_TYPE (IBusObservedPath, ibus_observed_path, IBUS_TYPE_SERIALIZABLE)
57 ibus_observed_path_class_init (IBusObservedPathClass *class)
59 IBusObjectClass *object_class = IBUS_OBJECT_CLASS (class);
60 IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (class);
62 // g_type_class_add_private (class, sizeof (IBusObservedPathPrivate));
64 object_class->destroy = (IBusObjectDestroyFunc) ibus_observed_path_destroy;
66 serializable_class->serialize = (IBusSerializableSerializeFunc) ibus_observed_path_serialize;
67 serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_observed_path_deserialize;
68 serializable_class->copy = (IBusSerializableCopyFunc) ibus_observed_path_copy;
73 ibus_observed_path_init (IBusObservedPath *path)
78 ibus_observed_path_destroy (IBusObservedPath *path)
81 IBUS_OBJECT_CLASS (ibus_observed_path_parent_class)->destroy (IBUS_OBJECT (path));
85 ibus_observed_path_serialize (IBusObservedPath *path,
86 GVariantBuilder *builder)
90 retval = IBUS_SERIALIZABLE_CLASS (ibus_observed_path_parent_class)->serialize ((IBusSerializable *)path, builder);
91 g_return_val_if_fail (retval, FALSE);
93 g_variant_builder_add (builder, "s", path->path);
94 g_variant_builder_add (builder, "x", path->mtime);
100 ibus_observed_path_deserialize (IBusObservedPath *path,
105 retval = IBUS_SERIALIZABLE_CLASS (ibus_observed_path_parent_class)->deserialize ((IBusSerializable *)path, variant);
106 g_return_val_if_fail (retval, 0);
108 g_variant_get_child (variant, retval++, "s", &path->path);
109 g_variant_get_child (variant, retval++, "x", &path->mtime);
115 ibus_observed_path_copy (IBusObservedPath *dest,
116 const IBusObservedPath *src)
120 retval = IBUS_SERIALIZABLE_CLASS (ibus_observed_path_parent_class)->copy ((IBusSerializable *)dest, (IBusSerializable *)src);
121 g_return_val_if_fail (retval, FALSE);
123 dest->path = g_strdup (src->path);
124 dest->mtime = src->mtime;
129 #define g_string_append_indent(string, indent) \
132 for (i = 0; i < (indent); i++) { \
133 g_string_append (string, " "); \
138 ibus_observed_path_output (IBusObservedPath *path,
142 g_assert (IBUS_IS_OBSERVED_PATH (path));
145 g_string_append_indent (output, indent);
146 g_string_append_printf (output, "<path mtime=\"%ld\" >%s</path>\n",
152 ibus_observed_path_check_modification (IBusObservedPath *path)
154 g_assert (IBUS_IS_OBSERVED_PATH (path));
157 if (g_stat (path->path, &buf) != 0) {
161 if (path->mtime == buf.st_mtime)
167 ibus_observed_path_fill_stat (IBusObservedPath *path)
169 g_assert (IBUS_IS_OBSERVED_PATH (path));
173 if (g_stat (path->path, &buf) == 0) {
175 if (S_ISDIR (buf.st_mode)) {
178 path->mtime = buf.st_mtime;
188 ibus_observed_path_traverse (IBusObservedPath *path,
191 g_assert (IBUS_IS_OBSERVED_PATH (path));
197 dir = g_dir_open (path->path, 0, NULL);
202 while ((name = g_dir_read_name (dir)) != NULL) {
203 IBusObservedPath *sub;
205 sub = g_object_new (IBUS_TYPE_OBSERVED_PATH, NULL);
206 g_object_ref_sink (sub);
207 sub->path = g_build_filename (path->path, name, NULL);
209 ibus_observed_path_fill_stat (sub);
210 if (sub->is_exist && sub->is_dir) {
211 paths = g_list_append (paths, sub);
212 paths = g_list_concat (paths,
213 ibus_observed_path_traverse (sub, dir_only));
214 } else if (!dir_only) {
215 paths = g_list_append (paths, sub);
224 ibus_observed_path_parse_xml_node (IBusObservedPath *path,
227 g_assert (IBUS_IS_OBSERVED_PATH (path));
230 if (G_UNLIKELY (g_strcmp0 (node->name, "path") != 0)) {
234 if (node->text[0] == '~' && node->text[1] != G_DIR_SEPARATOR) {
235 g_warning ("invalide path \"%s\"", node->text);
239 if (node->text[0] == '~') {
240 const gchar *homedir = g_getenv ("HOME");
242 homedir = g_get_home_dir ();
243 path->path = g_build_filename (homedir, node->text + 2, NULL);
246 path->path = g_strdup (node->text);
250 for (attr = node->attributes; attr[0]; attr += 2) {
251 if (g_strcmp0 (*attr, "mtime") == 0) {
252 path->mtime = atol (attr[1]);
255 g_warning ("Unkonwn attribute %s", attr[0]);
262 ibus_observed_path_new_from_xml_node (XMLNode *node,
267 IBusObservedPath *path;
269 path = (IBusObservedPath *) g_object_new (IBUS_TYPE_OBSERVED_PATH, NULL);
271 if (!ibus_observed_path_parse_xml_node (path, node)) {
272 g_object_unref (path);
275 else if (fill_stat) {
276 ibus_observed_path_fill_stat (path);
283 ibus_observed_path_new (const gchar *path,
288 IBusObservedPath *op;
290 op = (IBusObservedPath *) g_object_new (IBUS_TYPE_OBSERVED_PATH, NULL);
291 op->path = g_strdup (path);
294 ibus_observed_path_fill_stat (op);