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

« back to all changes in this revision

Viewing changes to maliit-glib/maliitattributeextension.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 "maliitattributeextension.h"
24
 
#include "maliitattributeextensionprivate.h"
25
 
#include "maliitattributeextensionregistry.h"
26
 
#include "maliitmarshallers.h"
27
 
 
28
 
/**
29
 
 * SECTION:maliitattributeextension
30
 
 * @short_description: attribute extensions
31
 
 * @title: MaliitAttributeExtension
32
 
 * @stability: Stable
33
 
 * @include: maliit/maliitattributeextension.h
34
 
 *
35
 
 * #MaliitAttributeExtension class can be used by application to
36
 
 * override some aspect of IM plugin currently used, like the looks of
37
 
 * action key.
38
 
 */
39
 
 
40
 
struct _MaliitAttributeExtensionPrivate
41
 
{
42
 
    int id;
43
 
    gchar *filename;
44
 
    GHashTable *attributes;
45
 
    MaliitAttributeExtensionRegistry *registry;
46
 
};
47
 
 
48
 
G_DEFINE_TYPE (MaliitAttributeExtension, maliit_attribute_extension, G_TYPE_OBJECT)
49
 
 
50
 
enum
51
 
{
52
 
    EXTENDED_ATTRIBUTE_CHANGED,
53
 
 
54
 
    LAST_SIGNAL
55
 
};
56
 
 
57
 
enum
58
 
{
59
 
    PROP_0,
60
 
 
61
 
    PROP_ID,
62
 
    PROP_FILENAME,
63
 
    PROP_ATTRIBUTES
64
 
};
65
 
 
66
 
static guint signals[LAST_SIGNAL] = { 0 };
67
 
 
68
 
static void
69
 
maliit_attribute_extension_finalize (GObject *object)
70
 
{
71
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
72
 
    MaliitAttributeExtensionPrivate *priv = extension->priv;
73
 
 
74
 
    g_free (priv->filename);
75
 
 
76
 
    G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->finalize (object);
77
 
}
78
 
 
79
 
static void
80
 
maliit_attribute_extension_dispose (GObject *object)
81
 
{
82
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
83
 
    MaliitAttributeExtensionPrivate *priv = extension->priv;
84
 
 
85
 
    if (priv->registry) {
86
 
        MaliitAttributeExtensionRegistry *registry = priv->registry;
87
 
 
88
 
        priv->registry = NULL;
89
 
        maliit_attribute_extension_registry_remove_extension (registry,
90
 
                                                              extension);
91
 
        g_object_unref (registry);
92
 
    }
93
 
 
94
 
    if (priv->attributes) {
95
 
        GHashTable *attributes = priv->attributes;
96
 
 
97
 
        priv->attributes = NULL;
98
 
        g_hash_table_unref (attributes);
99
 
    }
100
 
 
101
 
    G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->dispose (object);
102
 
}
103
 
 
104
 
static void
105
 
maliit_attribute_extension_set_property (GObject *object,
106
 
                                         guint prop_id,
107
 
                                         const GValue *value,
108
 
                                         GParamSpec *pspec)
109
 
{
110
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
111
 
    MaliitAttributeExtensionPrivate *priv = extension->priv;
112
 
 
113
 
    switch (prop_id) {
114
 
    case PROP_ID:
115
 
        priv->id = g_value_get_int (value);
116
 
        break;
117
 
    case PROP_FILENAME:
118
 
        g_free (extension->priv->filename);
119
 
        priv->filename = g_value_dup_string (value);
120
 
        break;
121
 
        /* PROP_ATTRIBUTES is read only. */
122
 
    default:
123
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124
 
        break;
125
 
    }
126
 
}
127
 
 
128
 
static void
129
 
maliit_attribute_extension_get_property (GObject *object,
130
 
                                         guint prop_id,
131
 
                                         GValue *value,
132
 
                                         GParamSpec *pspec)
133
 
{
134
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
135
 
    MaliitAttributeExtensionPrivate *priv = extension->priv;
136
 
 
137
 
    switch (prop_id) {
138
 
    case PROP_ID:
139
 
        g_value_set_int (value, priv->id);
140
 
        break;
141
 
    case PROP_FILENAME:
142
 
        g_value_set_string (value, priv->filename);
143
 
        break;
144
 
    case PROP_ATTRIBUTES:
145
 
        g_value_set_boxed (value, priv->attributes);
146
 
    default:
147
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148
 
        break;
149
 
    }
150
 
}
151
 
 
152
 
static void
153
 
maliit_attribute_extension_constructed (GObject *object)
154
 
{
155
 
    static int id_counter = 0;
156
 
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
157
 
    MaliitAttributeExtensionPrivate *priv = extension->priv;
158
 
 
159
 
    if (priv->id == 0) {
160
 
        priv->id = id_counter++;
161
 
    }
162
 
 
163
 
    maliit_attribute_extension_registry_add_extension (priv->registry,
164
 
                                                       extension);
165
 
 
166
 
    G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->constructed (object);
167
 
}
168
 
 
169
 
static void
170
 
maliit_attribute_extension_class_init (MaliitAttributeExtensionClass *extension_class)
171
 
{
172
 
    GObjectClass *g_object_class = G_OBJECT_CLASS (extension_class);
173
 
 
174
 
    g_object_class->finalize = maliit_attribute_extension_finalize;
175
 
    g_object_class->dispose = maliit_attribute_extension_dispose;
176
 
    g_object_class->set_property = maliit_attribute_extension_set_property;
177
 
    g_object_class->get_property = maliit_attribute_extension_get_property;
178
 
    g_object_class->constructed = maliit_attribute_extension_constructed;
179
 
 
180
 
    /**
181
 
     * MaliitAttributeExtension:id:
182
 
     *
183
 
     * ID of the extension.
184
 
     */
185
 
    g_object_class_install_property (g_object_class,
186
 
                                     PROP_ID,
187
 
                                     g_param_spec_int ("id",
188
 
                                                       "ID", /* TODO: mark as translatable? */
189
 
                                                       "ID of the extension", /* TODO: mark as translatable? */
190
 
                                                       G_MININT,
191
 
                                                       G_MAXINT,
192
 
                                                       0,
193
 
                                                       G_PARAM_READABLE |
194
 
                                                       G_PARAM_WRITABLE |
195
 
                                                       G_PARAM_CONSTRUCT_ONLY |
196
 
                                                       G_PARAM_STATIC_NAME |
197
 
                                                       G_PARAM_STATIC_BLURB |
198
 
                                                       G_PARAM_STATIC_NICK));
199
 
 
200
 
    /**
201
 
     * MaliitAttributeExtension:id:
202
 
     *
203
 
     * Path to file where definitions of overrides are defined.
204
 
     */
205
 
    g_object_class_install_property (g_object_class,
206
 
                                     PROP_FILENAME,
207
 
                                     g_param_spec_string ("filename",
208
 
                                                          "Filename", /* TODO: mark as translatable? */
209
 
                                                          "Filename of the extension", /* TODO: mark as translatable? */
210
 
                                                          NULL,
211
 
                                                          G_PARAM_READABLE |
212
 
                                                          G_PARAM_WRITABLE |
213
 
                                                          G_PARAM_CONSTRUCT_ONLY |
214
 
                                                          G_PARAM_STATIC_NAME |
215
 
                                                          G_PARAM_STATIC_BLURB |
216
 
                                                          G_PARAM_STATIC_NICK));
217
 
 
218
 
    g_object_class_install_property (g_object_class,
219
 
                                     PROP_ATTRIBUTES,
220
 
                                     g_param_spec_boxed ("attributes",
221
 
                                                         "Attributes", /* TODO: mark as translatable? */
222
 
                                                         "Attributes overrides", /* TODO: mark as translatable? */
223
 
                                                         G_TYPE_HASH_TABLE,
224
 
                                                         G_PARAM_READABLE |
225
 
                                                         G_PARAM_STATIC_NAME |
226
 
                                                         G_PARAM_STATIC_BLURB |
227
 
                                                         G_PARAM_STATIC_NICK));
228
 
 
229
 
    /**
230
 
     * MaliitAttributeExtension::extended-attribute-changed:
231
 
     * @extension: The #MaliitAttributeExtension emitting the signal.
232
 
     * @key: A string specifying the target for the attribute.
233
 
     * @value: A new value.
234
 
     *
235
 
     * Informs application that input method server has changed the
236
 
     * extended attribute.
237
 
     */
238
 
    signals[EXTENDED_ATTRIBUTE_CHANGED] =
239
 
        g_signal_new ("extended-attribute-changed",
240
 
                      MALIIT_TYPE_ATTRIBUTE_EXTENSION,
241
 
                      G_SIGNAL_RUN_FIRST,
242
 
                      0,
243
 
                      NULL,
244
 
                      NULL,
245
 
                      maliit_marshal_VOID__STRING_VARIANT,
246
 
                      G_TYPE_NONE,
247
 
                      2,
248
 
                      G_TYPE_STRING,
249
 
                      G_TYPE_VARIANT);
250
 
 
251
 
    g_type_class_add_private (extension_class, sizeof (MaliitAttributeExtensionPrivate));
252
 
}
253
 
 
254
 
static void
255
 
maliit_attribute_extension_init (MaliitAttributeExtension *extension)
256
 
{
257
 
    MaliitAttributeExtensionPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (extension,
258
 
                                                                         MALIIT_TYPE_ATTRIBUTE_EXTENSION,
259
 
                                                                         MaliitAttributeExtensionPrivate);
260
 
 
261
 
    priv->id = 0;
262
 
    priv->filename = NULL;
263
 
    priv->attributes = g_hash_table_new_full (g_str_hash,
264
 
                                              g_str_equal,
265
 
                                              g_free,
266
 
                                              (GDestroyNotify) g_variant_unref);
267
 
    priv->registry = maliit_attribute_extension_registry_get_instance ();
268
 
 
269
 
    extension->priv = priv;
270
 
}
271
 
 
272
 
/**
273
 
 * maliit_attribute_extension_new:
274
 
 *
275
 
 * Creates new attribute extension, which is not associated with any file.
276
 
 *
277
 
 * Returns: (transfer full): The newly created
278
 
 * #MaliitAttributeExtension.
279
 
 */
280
 
MaliitAttributeExtension *
281
 
maliit_attribute_extension_new (void)
282
 
{
283
 
    return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
284
 
                                                     NULL));
285
 
}
286
 
 
287
 
/**
288
 
 * maliit_attribute_extension_new_with_id: (skip)
289
 
 * @id: An overriden id.
290
 
 *
291
 
 * Creates a new attribute extension with already existing id. Used
292
 
 * internally by #MaliitSettingsManager.
293
 
 *
294
 
 * Returns: (transfer full): The newly created
295
 
 * #MaliitAttributeExtension.
296
 
 */
297
 
MaliitAttributeExtension *
298
 
maliit_attribute_extension_new_with_id (int id)
299
 
{
300
 
    return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
301
 
                                                     "id", id,
302
 
                                                     NULL));
303
 
}
304
 
 
305
 
/**
306
 
 * maliit_attribute_extension_new_with_filename:
307
 
 * @filename: (transfer none) (type filename): Filename where overrides are stored.
308
 
 *
309
 
 * Creates new attribute extension, which is associated with file
310
 
 * given as @filename.
311
 
 *
312
 
 * Returns: (transfer full): The newly created
313
 
 * #MaliitAttributeExtension.
314
 
 */
315
 
MaliitAttributeExtension *
316
 
maliit_attribute_extension_new_with_filename (const gchar *filename)
317
 
{
318
 
    return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
319
 
                                                     "filename", filename,
320
 
                                                     NULL));
321
 
}
322
 
 
323
 
/**
324
 
 * maliit_attribute_extension_get_attributes:
325
 
 * @extension: (transfer none): The #MaliitAttributeExtension which attributes you want to get.
326
 
 *
327
 
 * Gets all attributes of this extension that were set previously with
328
 
 * maliit_attribute_extension_set_attribute().
329
 
 *
330
 
 * Returns: (transfer none) (element-type utf8 GLib.Variant): The #GHashTable
331
 
 * containing strings as keys and #GVariant<!-- -->s as values. Should not be
332
 
 * freed nor modified.
333
 
 */
334
 
GHashTable *
335
 
maliit_attribute_extension_get_attributes (MaliitAttributeExtension *extension)
336
 
{
337
 
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), NULL);
338
 
 
339
 
    return extension->priv->attributes;
340
 
}
341
 
 
342
 
/**
343
 
 * maliit_attribute_extension_get_filename:
344
 
 * @extension: (transfer none): The #MaliitAttributeExtension which filename you want to get.
345
 
 *
346
 
 * Gets filename of this extension that were set previously with
347
 
 * maliit_attribute_extension_new_with_filename().
348
 
 *
349
 
 * Returns: (transfer none) (type filename): The string being a
350
 
 * filename of this extension or %NULL. Returned string should not be
351
 
 * freed nor modified.
352
 
 */
353
 
const gchar *
354
 
maliit_attribute_extension_get_filename (MaliitAttributeExtension *extension)
355
 
{
356
 
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), NULL);
357
 
 
358
 
    return extension->priv->filename;
359
 
}
360
 
 
361
 
/**
362
 
 * maliit_attribute_extension_get_id:
363
 
 * @extension: (transfer none): The #MaliitAttributeExtension which ID you want to get.
364
 
 *
365
 
 * Gets ID of this extension.
366
 
 *
367
 
 * Returns: The ID of this extension.
368
 
 */
369
 
int
370
 
maliit_attribute_extension_get_id (MaliitAttributeExtension *extension)
371
 
{
372
 
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), -1);
373
 
 
374
 
    return extension->priv->id;
375
 
}
376
 
 
377
 
/**
378
 
 * maliit_attribute_extension_update_attribute:
379
 
 * @extension: (transfer none): The #MaliitAttributeExtension which attribute you want to update.
380
 
 * @key: (transfer none): Attribute name to update.
381
 
 * @value: (transfer none): Attribute value to update.
382
 
 *
383
 
 * Updates the @extension's attribute described by @key with
384
 
 * @value. This function always emits a
385
 
 * #MaliitAttributeExtension::extended-attribute-changed signal.
386
 
 */
387
 
void
388
 
maliit_attribute_extension_update_attribute (MaliitAttributeExtension *extension,
389
 
                                             const gchar *key,
390
 
                                             GVariant *value)
391
 
{
392
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
393
 
    g_return_if_fail (key != NULL);
394
 
    g_return_if_fail (value != NULL);
395
 
 
396
 
    g_hash_table_replace (extension->priv->attributes,
397
 
                          g_strdup (key),
398
 
                          g_variant_ref (value));
399
 
 
400
 
    g_signal_emit (extension,
401
 
                   signals[EXTENDED_ATTRIBUTE_CHANGED],
402
 
                   0,
403
 
                   key,
404
 
                   value);
405
 
}
406
 
 
407
 
/**
408
 
 * maliit_attribute_extension_set_attribute:
409
 
 * @extension: (transfer none): The #MaliitAttributeExtension which attribute you want to set.
410
 
 * @key: (transfer none): Attribute name to update.
411
 
 * @value: (transfer none): Attribute value to update.
412
 
 *
413
 
 * Sets an attribute in @extension described by @key to value in @value.
414
 
 */
415
 
void maliit_attribute_extension_set_attribute (MaliitAttributeExtension *extension,
416
 
                                               const gchar *key,
417
 
                                               GVariant *value)
418
 
{
419
 
    MaliitAttributeExtensionPrivate *priv;
420
 
    GHashTable *attributes;
421
 
    GVariant *orig_value;
422
 
 
423
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
424
 
    g_return_if_fail (key != NULL);
425
 
    g_return_if_fail (value != NULL);
426
 
 
427
 
    priv = extension->priv;
428
 
    attributes = priv->attributes;
429
 
 
430
 
    if (!g_hash_table_lookup_extended (attributes, key, NULL, (gpointer *)&orig_value) ||
431
 
        !g_variant_equal (orig_value, value)) {
432
 
 
433
 
        g_hash_table_replace (attributes,
434
 
                              g_strdup (key),
435
 
                              g_variant_ref (value));
436
 
 
437
 
        maliit_attribute_extension_registry_extension_changed (priv->registry,
438
 
                                                               extension,
439
 
                                                               key,
440
 
                                                               value);
441
 
    }
442
 
}
443
 
 
444
 
/**
445
 
 * maliit_attribute_extension_attach_to_object:
446
 
 * @extension: (transfer none): The #MaliitAttributeExtension which you want to be attached.
447
 
 * @object: (transfer none): The #GObject to which @extension will be attached.
448
 
 *
449
 
 * Attaches @extension to @object, so input context can retrieve it
450
 
 * from @object. Note that attaching extensions to non-input
451
 
 * #GObject<!-- -->s does not have much sense.
452
 
 */
453
 
void
454
 
maliit_attribute_extension_attach_to_object (MaliitAttributeExtension *extension,
455
 
                                             GObject *object)
456
 
{
457
 
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
458
 
    g_return_if_fail (G_IS_OBJECT (object));
459
 
 
460
 
    g_object_set_qdata_full (object, MALIIT_ATTRIBUTE_EXTENSION_DATA_QUARK,
461
 
                             extension, g_object_unref);
462
 
}