Add Ctrl+space customization.
[platform/upstream/ibus.git] / src / ibusproplist.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* IBus - The Input Bus
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 "ibusproplist.h"
23
24 /* functions prototype */
25 static void         ibus_prop_list_destroy      (IBusPropList       *prop_list);
26 static gboolean     ibus_prop_list_serialize    (IBusPropList       *prop_list,
27                                                  GVariantBuilder    *builder);
28 static gint         ibus_prop_list_deserialize  (IBusPropList       *prop_list,
29                                                  GVariant           *variant);
30 static gboolean     ibus_prop_list_copy         (IBusPropList       *dest,
31                                                  const IBusPropList *src);
32
33 G_DEFINE_TYPE (IBusPropList, ibus_prop_list, IBUS_TYPE_SERIALIZABLE)
34
35 static void
36 ibus_prop_list_class_init (IBusPropListClass *class)
37 {
38     IBusObjectClass *object_class = IBUS_OBJECT_CLASS (class);
39     IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (class);
40
41     object_class->destroy = (IBusObjectDestroyFunc) ibus_prop_list_destroy;
42
43     serializable_class->serialize   = (IBusSerializableSerializeFunc) ibus_prop_list_serialize;
44     serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_prop_list_deserialize;
45     serializable_class->copy        = (IBusSerializableCopyFunc) ibus_prop_list_copy;
46 }
47
48 static void
49 ibus_prop_list_init (IBusPropList *prop_list)
50 {
51     prop_list->properties = g_array_new (TRUE, TRUE, sizeof (IBusProperty *));
52 }
53
54 static void
55 ibus_prop_list_destroy (IBusPropList *prop_list)
56 {
57     IBusProperty **p;
58     gint i;
59
60     p = (IBusProperty **) g_array_free (prop_list->properties, FALSE);
61
62     for (i = 0; p[i] != NULL; i++) {
63         g_object_unref (p[i]);
64     }
65     g_free (p);
66
67     IBUS_OBJECT_CLASS (ibus_prop_list_parent_class)->destroy ((IBusObject *) prop_list);
68 }
69
70 static gboolean
71 ibus_prop_list_serialize (IBusPropList    *prop_list,
72                           GVariantBuilder *builder)
73 {
74     gboolean retval;
75     guint i;
76
77     retval = IBUS_SERIALIZABLE_CLASS (ibus_prop_list_parent_class)->serialize ((IBusSerializable *) prop_list, builder);
78     g_return_val_if_fail (retval, FALSE);
79
80     GVariantBuilder array;
81     g_variant_builder_init (&array, G_VARIANT_TYPE ("av"));
82     for (i = 0;; i++) {
83         IBusProperty *prop = ibus_prop_list_get (prop_list, i);
84         if (prop == NULL)
85             break;
86         g_variant_builder_add (&array, "v", ibus_serializable_serialize ((IBusSerializable *)prop));
87     }
88
89     g_variant_builder_add (builder, "av", &array);
90
91     return TRUE;
92 }
93
94 gint
95 ibus_prop_list_deserialize (IBusPropList    *prop_list,
96                             GVariant        *variant)
97 {
98     gint retval;
99
100     retval = IBUS_SERIALIZABLE_CLASS (ibus_prop_list_parent_class)->deserialize ((IBusSerializable *) prop_list, variant);
101     g_return_val_if_fail (retval, 0);
102
103     g_return_val_if_fail (IBUS_IS_PROP_LIST (prop_list), 0);
104
105     GVariantIter *iter = NULL;
106     g_variant_get_child (variant, retval++, "av", &iter);
107     g_return_val_if_fail (iter != NULL, retval);
108     GVariant *var;
109     while (g_variant_iter_loop (iter, "v", &var)) {
110         IBusProperty *prop = IBUS_PROPERTY (ibus_serializable_deserialize (var));
111         ibus_prop_list_append (prop_list, prop);
112     }
113     g_variant_iter_free (iter);
114
115     return retval;
116 }
117
118
119
120 static gboolean
121 ibus_prop_list_copy (IBusPropList       *dest,
122                      const IBusPropList *src)
123 {
124     gboolean retval;
125     IBusProperty *prop;
126     guint i;
127
128     retval = IBUS_SERIALIZABLE_CLASS (ibus_prop_list_parent_class)->copy ((IBusSerializable *) dest,
129                                  (const IBusSerializable *) src);
130     g_return_val_if_fail (retval, FALSE);
131
132     g_return_val_if_fail (IBUS_IS_PROP_LIST (dest), FALSE);
133     g_return_val_if_fail (IBUS_IS_PROP_LIST (src), FALSE);
134
135     i = 0;
136     while ((prop = ibus_prop_list_get ((IBusPropList *)src, i)) != NULL) {
137         prop = (IBusProperty *) ibus_serializable_copy ((IBusSerializable *) prop);
138         ibus_prop_list_append (dest, prop);
139         i ++;
140     }
141     return TRUE;
142 }
143
144
145 IBusPropList *
146 ibus_prop_list_new ()
147 {
148     IBusPropList *prop_list;
149
150     prop_list = g_object_new (IBUS_TYPE_PROP_LIST, NULL);
151
152     return prop_list;
153 }
154
155 void
156 ibus_prop_list_append (IBusPropList *prop_list,
157                        IBusProperty *prop)
158 {
159     g_assert (IBUS_IS_PROP_LIST (prop_list));
160     g_assert (IBUS_IS_PROPERTY (prop));
161
162     g_object_ref_sink (prop);
163
164     g_array_append_val (prop_list->properties, prop);
165 }
166
167 IBusProperty *
168 ibus_prop_list_get (IBusPropList *prop_list,
169                     guint         index)
170 {
171     g_assert (IBUS_IS_PROP_LIST (prop_list));
172
173
174     if (index >= prop_list->properties->len)
175         return NULL;
176
177     return g_array_index (prop_list->properties, IBusProperty *, index);
178 }
179
180
181
182 gboolean
183 ibus_prop_list_update_property (IBusPropList *prop_list,
184                                 IBusProperty *prop_update)
185 {
186     g_assert (IBUS_IS_PROP_LIST (prop_list));
187     g_assert (IBUS_IS_PROPERTY (prop_update));
188
189     gint i;
190
191     for (i = 0; i < prop_list->properties->len; i ++) {
192         IBusProperty *prop = g_array_index (prop_list->properties, IBusProperty *, i);
193         if (ibus_property_update (prop, prop_update)) {
194             return TRUE;
195         }
196     }
197
198     return FALSE;
199 }