~timo-jyrinki/ubuntu/trusty/maliit-framework/fix_qt52

« back to all changes in this revision

Viewing changes to maliit-glib/maliitattributeextensionregistry.c

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo, Sergio Schvezov, Ricardo Salveti de Araujo
  • Date: 2013-07-23 19:47:04 UTC
  • mfrom: (1.1.2) (1.2.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130723194704-1lsy1kmlda069cea
Tags: 0.99.0+git20130615+97e8335-0ubuntu1
[ Sergio Schvezov ]
* New build from HEAD 97e8335.
* Packaging import from lp:phablet-extras/maliit-framework.

[ Ricardo Salveti de Araujo ]
* debian/control: adding vcs and fixing dependencies
* General package cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Maliit framework
2
 
 *
3
 
 * Copyright (C) 2012 One Laptop per Child Association
4
 
 *
5
 
 * Contact: maliit-discuss@lists.maliit.org
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.1 of the licence, 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
 
 
23
 
#include <dbus/dbus-glib.h>
24
 
 
25
 
#include "maliitattributeextensionregistry.h"
26
 
#include "maliitmarshallers.h"
27
 
#include "meego-im-connector.h"
28
 
 
29
 
struct _MaliitAttributeExtensionRegistryPrivate
30
 
{
31
 
    GHashTable *extensions;
32
 
};
33
 
 
34
 
static MaliitAttributeExtensionRegistry *global_singleton;
35
 
 
36
 
G_DEFINE_TYPE (MaliitAttributeExtensionRegistry, maliit_attribute_extension_registry, G_TYPE_OBJECT)
37
 
 
38
 
static void
39
 
maliit_attribute_extension_registry_finalize (GObject *object)
40
 
{
41
 
    global_singleton = NULL;
42
 
 
43
 
    G_OBJECT_CLASS (maliit_attribute_extension_registry_parent_class)->finalize (object);
44
 
}
45
 
 
46
 
static void
47
 
extension_notify (gpointer data,
48
 
                  GObject *where_the_object_was)
49
 
{
50
 
    MaliitAttributeExtensionRegistry *registry = MALIIT_ATTRIBUTE_EXTENSION_REGISTRY (data);
51
 
    MaliitAttributeExtensionRegistryPrivate *priv = registry->priv;
52
 
    GHashTableIter iter;
53
 
    MaliitAttributeExtension *extension;
54
 
 
55
 
    g_hash_table_iter_init (&iter, priv->extensions);
56
 
    while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&extension)) {
57
 
        if ((gpointer)extension == (gpointer)where_the_object_was) {
58
 
            g_hash_table_iter_steal (&iter);
59
 
            break;
60
 
        }
61
 
    }
62
 
}
63
 
 
64
 
static void
65
 
maliit_attribute_extension_registry_dispose (GObject *object)
66
 
{
67
 
    MaliitAttributeExtensionRegistry *registry = MALIIT_ATTRIBUTE_EXTENSION_REGISTRY (object);
68
 
    MaliitAttributeExtensionRegistryPrivate *priv = registry->priv;
69
 
 
70
 
    if (priv->extensions) {
71
 
        GHashTable *extensions = priv->extensions;
72
 
 
73
 
        priv->extensions = NULL;
74
 
        g_hash_table_unref (extensions);
75
 
    }
76
 
 
77
 
    G_OBJECT_CLASS (maliit_attribute_extension_registry_parent_class)->dispose (object);
78
 
}
79
 
 
80
 
static GObject*
81
 
maliit_attribute_extension_registry_constructor (GType type,
82
 
                                                 guint n_params,
83
 
                                                 GObjectConstructParam *params)
84
 
{
85
 
    GObject *object;
86
 
 
87
 
    if (global_singleton) {
88
 
        object = g_object_ref (G_OBJECT (global_singleton));
89
 
    } else {
90
 
        object = G_OBJECT_CLASS (maliit_attribute_extension_registry_parent_class)->constructor (type,
91
 
                                                                                                 n_params,
92
 
                                                                                                 params);
93
 
        /* We are doing an additional reference here, so object will not
94
 
         * be destroyed when last owner removes its reference. This is a
95
 
         * leak, but for now it ensures that singleton have a lifetime of
96
 
         * application.  This needs to be fixed, when object lifetimes are
97
 
         * fixed in gtk-input-context. */
98
 
        global_singleton = MALIIT_ATTRIBUTE_EXTENSION_REGISTRY (g_object_ref (object));
99
 
    }
100
 
 
101
 
    return object;
102
 
}
103
 
 
104
 
static void
105
 
maliit_attribute_extension_registry_class_init (MaliitAttributeExtensionRegistryClass *registry_class)
106
 
{
107
 
    GObjectClass *g_object_class = G_OBJECT_CLASS (registry_class);
108
 
 
109
 
    g_object_class->finalize = maliit_attribute_extension_registry_finalize;
110
 
    g_object_class->dispose = maliit_attribute_extension_registry_dispose;
111
 
    g_object_class->constructor = maliit_attribute_extension_registry_constructor;
112
 
 
113
 
    g_type_class_add_private (registry_class, sizeof (MaliitAttributeExtensionRegistryPrivate));
114
 
}
115
 
 
116
 
static void
117
 
extension_weak_unref (MaliitAttributeExtension *extension,
118
 
                      MaliitAttributeExtensionRegistry *registry)
119
 
{
120
 
    g_object_weak_unref (G_OBJECT (extension),
121
 
                         extension_notify,
122
 
                         registry);
123
 
}
124
 
 
125
 
static void
126
 
extension_weak_unref_global (gpointer data)
127
 
{
128
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (data);
129
 
 
130
 
    extension_weak_unref (extension,
131
 
                          global_singleton);
132
 
}
133
 
 
134
 
static void
135
 
register_all_extensions (MeegoIMProxy *proxy, gpointer user_data)
136
 
{
137
 
    MaliitAttributeExtensionRegistry *registry = user_data;
138
 
    GList *extensions = maliit_attribute_extension_registry_get_extensions (registry);
139
 
    GList *iter;
140
 
 
141
 
    for (iter = extensions; iter; iter = iter->next) {
142
 
        MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (iter->data);
143
 
 
144
 
        if (!meego_im_proxy_register_extension (proxy,
145
 
                                                maliit_attribute_extension_get_id (extension),
146
 
                                                maliit_attribute_extension_get_filename (extension))) {
147
 
            g_warning ("Could not register an extension in mass registerer - no proxy");
148
 
        } else {
149
 
            GHashTable *attributes = maliit_attribute_extension_get_attributes (extension);
150
 
            GHashTableIter attributes_iter;
151
 
            gpointer key;
152
 
            gpointer value;
153
 
 
154
 
            g_hash_table_iter_init (&attributes_iter, attributes);
155
 
 
156
 
            while (g_hash_table_iter_next (&attributes_iter, &key, &value)) {
157
 
                maliit_attribute_extension_registry_extension_changed(registry, extension, key, value);
158
 
            }
159
 
        }
160
 
    }
161
 
 
162
 
    g_list_free (extensions);
163
 
}
164
 
 
165
 
static void
166
 
maliit_attribute_extension_registry_init (MaliitAttributeExtensionRegistry *registry)
167
 
{
168
 
    MeegoImConnector *connector = meego_im_connector_get_singleton();
169
 
 
170
 
    MaliitAttributeExtensionRegistryPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (registry,
171
 
                                                                                 MALIIT_TYPE_ATTRIBUTE_EXTENSION_REGISTRY,
172
 
                                                                                 MaliitAttributeExtensionRegistryPrivate);
173
 
 
174
 
    priv->extensions = g_hash_table_new_full (g_direct_hash,
175
 
                                              g_direct_equal,
176
 
                                              NULL,
177
 
                                              extension_weak_unref_global);
178
 
    registry->priv = priv;
179
 
 
180
 
    g_signal_connect(connector->proxy, "connection-established",
181
 
                     G_CALLBACK(register_all_extensions), registry);
182
 
}
183
 
 
184
 
MaliitAttributeExtensionRegistry *
185
 
maliit_attribute_extension_registry_get_instance (void)
186
 
{
187
 
    return MALIIT_ATTRIBUTE_EXTENSION_REGISTRY (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION_REGISTRY,
188
 
                                                              NULL));
189
 
}
190
 
 
191
 
void
192
 
maliit_attribute_extension_registry_add_extension (MaliitAttributeExtensionRegistry *registry,
193
 
                                                   MaliitAttributeExtension *extension)
194
 
{
195
 
    GHashTable *extensions;
196
 
    gint id;
197
 
 
198
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION_REGISTRY (registry));
199
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
200
 
 
201
 
    extensions = registry->priv->extensions;
202
 
    id = maliit_attribute_extension_get_id (extension);
203
 
 
204
 
    if (!g_hash_table_lookup_extended (extensions, GINT_TO_POINTER (id), NULL, NULL)) {
205
 
        MeegoImConnector *connector = meego_im_connector_get_singleton();
206
 
 
207
 
        g_object_weak_ref (G_OBJECT (extension),
208
 
                           extension_notify,
209
 
                           registry);
210
 
 
211
 
        g_hash_table_insert (extensions,
212
 
                             GINT_TO_POINTER (id),
213
 
                             extension);
214
 
        meego_im_proxy_register_extension(connector->proxy,
215
 
                                          id,
216
 
                                          maliit_attribute_extension_get_filename (extension));
217
 
    }
218
 
}
219
 
 
220
 
void
221
 
maliit_attribute_extension_registry_remove_extension (MaliitAttributeExtensionRegistry *registry,
222
 
                                                      MaliitAttributeExtension *extension)
223
 
{
224
 
    GHashTable *extensions;
225
 
    gint id;
226
 
 
227
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION_REGISTRY (registry));
228
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
229
 
 
230
 
    extensions = registry->priv->extensions;
231
 
    id = maliit_attribute_extension_get_id (extension);
232
 
 
233
 
    if (g_hash_table_lookup_extended (extensions, GINT_TO_POINTER (id), NULL, NULL)) {
234
 
        MeegoImConnector *connector = meego_im_connector_get_singleton();
235
 
 
236
 
        g_hash_table_remove (extensions,
237
 
                             GINT_TO_POINTER (id));
238
 
        meego_im_proxy_unregister_extension(connector->proxy,
239
 
                                            id);
240
 
    }
241
 
}
242
 
 
243
 
/* For glib < 2.30 */
244
 
#ifndef G_VALUE_INIT
245
 
#define G_VALUE_INIT { 0, { { 0 } } }
246
 
#endif
247
 
 
248
 
void
249
 
maliit_attribute_extension_registry_extension_changed (MaliitAttributeExtensionRegistry *registry,
250
 
                                                       MaliitAttributeExtension *extension,
251
 
                                                       const gchar *key,
252
 
                                                       GVariant *value)
253
 
{
254
 
    gchar **parts;
255
 
 
256
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION_REGISTRY (registry));
257
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
258
 
    g_return_if_fail (key != NULL);
259
 
    g_return_if_fail (value != NULL);
260
 
 
261
 
    parts = g_strsplit (key + 1, "/", 3);
262
 
 
263
 
    if (!parts)
264
 
        return;
265
 
 
266
 
    if (g_strv_length (parts) == 3) {
267
 
        GValue g_value = G_VALUE_INIT;
268
 
 
269
 
        dbus_g_value_parse_g_variant (value, &g_value);
270
 
        if (G_VALUE_TYPE (&g_value) != G_TYPE_INVALID) {
271
 
            MeegoImConnector *connector = meego_im_connector_get_singleton();
272
 
            gchar *target = g_strdup_printf ("/%s", parts[0]);
273
 
 
274
 
            meego_im_proxy_set_extended_attribute (connector->proxy,
275
 
                                                   maliit_attribute_extension_get_id(extension),
276
 
                                                   target,
277
 
                                                   parts[1],
278
 
                                                   parts[2],
279
 
                                                   &g_value);
280
 
            g_free (target);
281
 
            g_value_unset (&g_value);
282
 
        } else {
283
 
            g_warning ("Could not convert variant into value");
284
 
        }
285
 
    } else {
286
 
        g_warning("Key `%s' is not valid. It needs to be `/target/item/key'", key);
287
 
    }
288
 
    g_strfreev (parts);
289
 
}
290
 
 
291
 
static void
292
 
fill_list_with_extensions (gpointer key G_GNUC_UNUSED,
293
 
                           gpointer value,
294
 
                           gpointer user_data)
295
 
{
296
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (value);
297
 
    GList **list = (GList **)user_data;
298
 
 
299
 
    *list = g_list_prepend (*list, extension);
300
 
}
301
 
 
302
 
GList *
303
 
maliit_attribute_extension_registry_get_extensions (MaliitAttributeExtensionRegistry *registry)
304
 
{
305
 
    GList *list;
306
 
 
307
 
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION_REGISTRY (registry), NULL);
308
 
 
309
 
    list = NULL;
310
 
    g_hash_table_foreach (registry->priv->extensions,
311
 
                          fill_list_with_extensions,
312
 
                          &list);
313
 
 
314
 
    return list;
315
 
}
316
 
 
317
 
void
318
 
maliit_attribute_extension_registry_update_attribute (MaliitAttributeExtensionRegistry *registry,
319
 
                                                      gint id,
320
 
                                                      const gchar *target,
321
 
                                                      const gchar *target_item,
322
 
                                                      const gchar *attribute,
323
 
                                                      GVariant *value)
324
 
{
325
 
    MaliitAttributeExtension *extension;
326
 
 
327
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION_REGISTRY (registry));
328
 
    g_return_if_fail (id >= 0);
329
 
    g_return_if_fail (target != NULL);
330
 
    g_return_if_fail (target_item != NULL);
331
 
    g_return_if_fail (attribute != NULL);
332
 
    g_return_if_fail (value != NULL);
333
 
 
334
 
    if (g_hash_table_lookup_extended (registry->priv->extensions,
335
 
                                      GINT_TO_POINTER (id),
336
 
                                      NULL,
337
 
                                      (gpointer *)&extension)) {
338
 
        gchar *key = g_strdup_printf ("%s/%s/%s", target, target_item, attribute);
339
 
 
340
 
        maliit_attribute_extension_update_attribute (extension,
341
 
                                                     key,
342
 
                                                     value);
343
 
        g_free (key);
344
 
    } else {
345
 
        g_warning ("Extension %d was not found.", id);
346
 
    }
347
 
}