Add Ctrl+space customization.
[platform/upstream/ibus.git] / src / ibusobservedpath.c
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.
6  *
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.
11  *
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.
16  *
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.
21  */
22 #include <glib/gstdio.h>
23 #include <stdlib.h>
24 #include "ibusobservedpath.h"
25
26
27 enum {
28     LAST_SIGNAL,
29 };
30
31
32 /* IBusObservedPathPriv */
33 struct _IBusObservedPathPrivate {
34     gpointer pad;
35 };
36 typedef struct _IBusObservedPathPrivate IBusObservedPathPrivate;
37
38 #define IBUS_OBSERVED_PATH_GET_PRIVATE(o)  \
39    (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_OBSERVED_PATH, IBusObservedPathPrivate))
40
41 // static guint            _signals[LAST_SIGNAL] = { 0 };
42
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,
48                                                      GVariant               *variant);
49 static gboolean  ibus_observed_path_copy            (IBusObservedPath       *dest,
50                                                      const IBusObservedPath *src);
51 static gboolean  ibus_observed_path_parse_xml_node  (IBusObservedPath       *path,
52                                                      XMLNode                *node);
53
54 G_DEFINE_TYPE (IBusObservedPath, ibus_observed_path, IBUS_TYPE_SERIALIZABLE)
55
56 static void
57 ibus_observed_path_class_init (IBusObservedPathClass *class)
58 {
59     IBusObjectClass *object_class = IBUS_OBJECT_CLASS (class);
60     IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (class);
61
62     // g_type_class_add_private (class, sizeof (IBusObservedPathPrivate));
63
64     object_class->destroy = (IBusObjectDestroyFunc) ibus_observed_path_destroy;
65
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;
69 }
70
71
72 static void
73 ibus_observed_path_init (IBusObservedPath *path)
74 {
75 }
76
77 static void
78 ibus_observed_path_destroy (IBusObservedPath *path)
79 {
80     g_free (path->path);
81     IBUS_OBJECT_CLASS (ibus_observed_path_parent_class)->destroy (IBUS_OBJECT (path));
82 }
83
84 static gboolean
85 ibus_observed_path_serialize (IBusObservedPath *path,
86                               GVariantBuilder  *builder)
87 {
88     gboolean retval;
89
90     retval = IBUS_SERIALIZABLE_CLASS (ibus_observed_path_parent_class)->serialize ((IBusSerializable *)path, builder);
91     g_return_val_if_fail (retval, FALSE);
92
93     g_variant_builder_add (builder, "s", path->path);
94     g_variant_builder_add (builder, "x", path->mtime);
95
96     return TRUE;
97 }
98
99 static gint
100 ibus_observed_path_deserialize (IBusObservedPath *path,
101                                 GVariant         *variant)
102 {
103     gint retval;
104
105     retval = IBUS_SERIALIZABLE_CLASS (ibus_observed_path_parent_class)->deserialize ((IBusSerializable *)path, variant);
106     g_return_val_if_fail (retval, 0);
107
108     g_variant_get_child (variant, retval++, "s", &path->path);
109     g_variant_get_child (variant, retval++, "x", &path->mtime);
110
111     return retval;
112 }
113
114 static gboolean
115 ibus_observed_path_copy (IBusObservedPath       *dest,
116                          const IBusObservedPath *src)
117 {
118     gboolean retval;
119
120     retval = IBUS_SERIALIZABLE_CLASS (ibus_observed_path_parent_class)->copy ((IBusSerializable *)dest, (IBusSerializable *)src);
121     g_return_val_if_fail (retval, FALSE);
122
123     dest->path = g_strdup (src->path);
124     dest->mtime = src->mtime;
125
126     return TRUE;
127 }
128
129 #define g_string_append_indent(string, indent)  \
130     {                                           \
131         gint i;                                 \
132         for (i = 0; i < (indent); i++) {        \
133             g_string_append (string, "    ");   \
134         }                                       \
135     }
136
137 void
138 ibus_observed_path_output (IBusObservedPath *path,
139                           GString         *output,
140                           gint             indent)
141 {
142     g_assert (IBUS_IS_OBSERVED_PATH (path));
143     g_assert (output);
144
145     g_string_append_indent (output, indent);
146     g_string_append_printf (output, "<path mtime=\"%ld\" >%s</path>\n",
147                                     path->mtime,
148                                     path->path);
149 }
150
151 gboolean
152 ibus_observed_path_check_modification (IBusObservedPath *path)
153 {
154     g_assert (IBUS_IS_OBSERVED_PATH (path));
155     struct stat buf;
156
157     if (g_stat (path->path, &buf) != 0) {
158         buf.st_mtime = 0;
159     }
160
161     if (path->mtime == buf.st_mtime)
162         return FALSE;
163     return TRUE;
164 }
165
166 static void
167 ibus_observed_path_fill_stat (IBusObservedPath *path)
168 {
169     g_assert (IBUS_IS_OBSERVED_PATH (path));
170
171     struct stat buf;
172
173     if (g_stat (path->path, &buf) == 0) {
174         path->is_exist = 1;
175         if (S_ISDIR (buf.st_mode)) {
176             path->is_dir = 1;
177         }
178         path->mtime = buf.st_mtime;
179     }
180     else {
181         path->is_dir = 0;
182         path->is_exist = 0;
183         path->mtime = 0;
184     }
185 }
186
187 GList *
188 ibus_observed_path_traverse (IBusObservedPath *path,
189                              gboolean          dir_only)
190 {
191     g_assert (IBUS_IS_OBSERVED_PATH (path));
192
193     GDir *dir;
194     const gchar *name;
195     GList *paths = NULL;
196
197     dir = g_dir_open (path->path, 0, NULL);
198
199     if (dir == NULL)
200         return NULL;
201
202     while ((name = g_dir_read_name (dir)) != NULL) {
203         IBusObservedPath *sub;
204
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);
208
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);
216         }
217     }
218     g_dir_close (dir);
219
220     return paths;
221 }
222
223 static gboolean
224 ibus_observed_path_parse_xml_node (IBusObservedPath *path,
225                                    XMLNode          *node)
226 {
227     g_assert (IBUS_IS_OBSERVED_PATH (path));
228     g_assert (node);
229
230     if (G_UNLIKELY (g_strcmp0 (node->name, "path") != 0)) {
231         return FALSE;
232     }
233
234     if (node->text[0] == '~' && node->text[1] != G_DIR_SEPARATOR) {
235         g_warning ("invalide path \"%s\"", node->text);
236         return FALSE;
237     }
238
239     if (node->text[0] == '~') {
240         const gchar *homedir = g_getenv ("HOME");
241         if (homedir == NULL)
242             homedir = g_get_home_dir ();
243         path->path = g_build_filename (homedir, node->text + 2, NULL);
244     }
245     else {
246         path->path = g_strdup (node->text);
247     }
248
249     gchar **attr;
250     for (attr = node->attributes; attr[0]; attr += 2) {
251         if (g_strcmp0 (*attr, "mtime") == 0) {
252             path->mtime = atol (attr[1]);
253             continue;
254         }
255         g_warning ("Unkonwn attribute %s", attr[0]);
256     }
257
258     return TRUE;
259 }
260
261 IBusObservedPath *
262 ibus_observed_path_new_from_xml_node (XMLNode *node,
263                                      gboolean fill_stat)
264 {
265     g_assert (node);
266
267     IBusObservedPath *path;
268
269     path = (IBusObservedPath *) g_object_new (IBUS_TYPE_OBSERVED_PATH, NULL);
270
271     if (!ibus_observed_path_parse_xml_node (path, node)) {
272         g_object_unref (path);
273         path = NULL;
274     }
275     else if (fill_stat) {
276         ibus_observed_path_fill_stat (path);
277     }
278
279     return path;
280 }
281
282 IBusObservedPath *
283 ibus_observed_path_new (const gchar *path,
284                         gboolean     fill_stat)
285 {
286     g_assert (path);
287
288     IBusObservedPath *op;
289
290     op = (IBusObservedPath *) g_object_new (IBUS_TYPE_OBSERVED_PATH, NULL);
291     op->path = g_strdup (path);
292
293     if (fill_stat) {
294         ibus_observed_path_fill_stat (op);
295     }
296
297     return op;
298 }
299