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.
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 "ibusproplist.h"
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,
30 static gboolean ibus_prop_list_copy (IBusPropList *dest,
31 const IBusPropList *src);
33 G_DEFINE_TYPE (IBusPropList, ibus_prop_list, IBUS_TYPE_SERIALIZABLE)
36 ibus_prop_list_class_init (IBusPropListClass *class)
38 IBusObjectClass *object_class = IBUS_OBJECT_CLASS (class);
39 IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (class);
41 object_class->destroy = (IBusObjectDestroyFunc) ibus_prop_list_destroy;
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;
49 ibus_prop_list_init (IBusPropList *prop_list)
51 prop_list->properties = g_array_new (TRUE, TRUE, sizeof (IBusProperty *));
55 ibus_prop_list_destroy (IBusPropList *prop_list)
60 p = (IBusProperty **) g_array_free (prop_list->properties, FALSE);
62 for (i = 0; p[i] != NULL; i++) {
63 g_object_unref (p[i]);
67 IBUS_OBJECT_CLASS (ibus_prop_list_parent_class)->destroy ((IBusObject *) prop_list);
71 ibus_prop_list_serialize (IBusPropList *prop_list,
72 GVariantBuilder *builder)
77 retval = IBUS_SERIALIZABLE_CLASS (ibus_prop_list_parent_class)->serialize ((IBusSerializable *) prop_list, builder);
78 g_return_val_if_fail (retval, FALSE);
80 GVariantBuilder array;
81 g_variant_builder_init (&array, G_VARIANT_TYPE ("av"));
83 IBusProperty *prop = ibus_prop_list_get (prop_list, i);
86 g_variant_builder_add (&array, "v", ibus_serializable_serialize ((IBusSerializable *)prop));
89 g_variant_builder_add (builder, "av", &array);
95 ibus_prop_list_deserialize (IBusPropList *prop_list,
100 retval = IBUS_SERIALIZABLE_CLASS (ibus_prop_list_parent_class)->deserialize ((IBusSerializable *) prop_list, variant);
101 g_return_val_if_fail (retval, 0);
103 g_return_val_if_fail (IBUS_IS_PROP_LIST (prop_list), 0);
105 GVariantIter *iter = NULL;
106 g_variant_get_child (variant, retval++, "av", &iter);
107 g_return_val_if_fail (iter != NULL, retval);
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);
113 g_variant_iter_free (iter);
121 ibus_prop_list_copy (IBusPropList *dest,
122 const IBusPropList *src)
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);
132 g_return_val_if_fail (IBUS_IS_PROP_LIST (dest), FALSE);
133 g_return_val_if_fail (IBUS_IS_PROP_LIST (src), FALSE);
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);
146 ibus_prop_list_new ()
148 IBusPropList *prop_list;
150 prop_list = g_object_new (IBUS_TYPE_PROP_LIST, NULL);
156 ibus_prop_list_append (IBusPropList *prop_list,
159 g_assert (IBUS_IS_PROP_LIST (prop_list));
160 g_assert (IBUS_IS_PROPERTY (prop));
162 g_object_ref_sink (prop);
164 g_array_append_val (prop_list->properties, prop);
168 ibus_prop_list_get (IBusPropList *prop_list,
171 g_assert (IBUS_IS_PROP_LIST (prop_list));
174 if (index >= prop_list->properties->len)
177 return g_array_index (prop_list->properties, IBusProperty *, index);
183 ibus_prop_list_update_property (IBusPropList *prop_list,
184 IBusProperty *prop_update)
186 g_assert (IBUS_IS_PROP_LIST (prop_list));
187 g_assert (IBUS_IS_PROPERTY (prop_update));
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)) {