File: maliitattributeextension.c

package info (click to toggle)
maliit-framework 2.3.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,488 kB
  • sloc: cpp: 13,098; ansic: 2,506; xml: 299; sh: 34; makefile: 23; sed: 4
file content (458 lines) | stat: -rw-r--r-- 16,564 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* This file is part of Maliit framework
 *
 * Copyright (C) 2012 One Laptop per Child Association
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "maliitattributeextension.h"
#include "maliitattributeextensionprivate.h"
#include "maliitattributeextensionregistry.h"
#include "maliitmarshallers.h"

/**
 * SECTION:maliitattributeextension
 * @short_description: attribute extensions
 * @title: MaliitAttributeExtension
 * @stability: Stable
 * @include: maliit/maliitattributeextension.h
 *
 * #MaliitAttributeExtension class can be used by application to
 * override some aspect of IM plugin currently used, like the looks of
 * action key.
 */

struct _MaliitAttributeExtensionPrivate
{
    int id;
    gchar *filename;
    GHashTable *attributes;
    MaliitAttributeExtensionRegistry *registry;
};

G_DEFINE_TYPE_WITH_CODE (MaliitAttributeExtension, maliit_attribute_extension,
                         G_TYPE_OBJECT, G_ADD_PRIVATE (MaliitAttributeExtension))

enum
{
    EXTENDED_ATTRIBUTE_CHANGED,

    LAST_SIGNAL
};

enum
{
    PROP_0,

    PROP_ID,
    PROP_FILENAME,
    PROP_ATTRIBUTES
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
maliit_attribute_extension_finalize (GObject *object)
{
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
    MaliitAttributeExtensionPrivate *priv = extension->priv;

    g_free (priv->filename);

    G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->finalize (object);
}

static void
maliit_attribute_extension_dispose (GObject *object)
{
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
    MaliitAttributeExtensionPrivate *priv = extension->priv;

    if (priv->registry) {
        MaliitAttributeExtensionRegistry *registry = priv->registry;

        priv->registry = NULL;
        maliit_attribute_extension_registry_remove_extension (registry,
                                                              extension);
        g_object_unref (registry);
    }

    if (priv->attributes) {
        GHashTable *attributes = priv->attributes;

        priv->attributes = NULL;
        g_hash_table_unref (attributes);
    }

    G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->dispose (object);
}

static void
maliit_attribute_extension_set_property (GObject *object,
                                         guint prop_id,
                                         const GValue *value,
                                         GParamSpec *pspec)
{
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
    MaliitAttributeExtensionPrivate *priv = extension->priv;

    switch (prop_id) {
    case PROP_ID:
        priv->id = g_value_get_int (value);
        break;
    case PROP_FILENAME:
        g_free (extension->priv->filename);
        priv->filename = g_value_dup_string (value);
        break;
        /* PROP_ATTRIBUTES is read only. */
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
maliit_attribute_extension_get_property (GObject *object,
                                         guint prop_id,
                                         GValue *value,
                                         GParamSpec *pspec)
{
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
    MaliitAttributeExtensionPrivate *priv = extension->priv;

    switch (prop_id) {
    case PROP_ID:
        g_value_set_int (value, priv->id);
        break;
    case PROP_FILENAME:
        g_value_set_string (value, priv->filename);
        break;
    case PROP_ATTRIBUTES:
        g_value_set_boxed (value, priv->attributes);
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
maliit_attribute_extension_constructed (GObject *object)
{
    static int id_counter = 0;
    MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
    MaliitAttributeExtensionPrivate *priv = extension->priv;

    if (priv->id == 0) {
        priv->id = id_counter++;
    }

    maliit_attribute_extension_registry_add_extension (priv->registry,
                                                       extension);

    G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->constructed (object);
}

static void
maliit_attribute_extension_class_init (MaliitAttributeExtensionClass *extension_class)
{
    GObjectClass *g_object_class = G_OBJECT_CLASS (extension_class);

    g_object_class->finalize = maliit_attribute_extension_finalize;
    g_object_class->dispose = maliit_attribute_extension_dispose;
    g_object_class->set_property = maliit_attribute_extension_set_property;
    g_object_class->get_property = maliit_attribute_extension_get_property;
    g_object_class->constructed = maliit_attribute_extension_constructed;

    /**
     * MaliitAttributeExtension:id:
     *
     * ID of the extension.
     */
    g_object_class_install_property (g_object_class,
                                     PROP_ID,
                                     g_param_spec_int ("id",
                                                       "ID", /* TODO: mark as translatable? */
                                                       "ID of the extension", /* TODO: mark as translatable? */
                                                       G_MININT,
                                                       G_MAXINT,
                                                       0,
                                                       G_PARAM_READABLE |
                                                       G_PARAM_WRITABLE |
                                                       G_PARAM_CONSTRUCT_ONLY |
                                                       G_PARAM_STATIC_NAME |
                                                       G_PARAM_STATIC_BLURB |
                                                       G_PARAM_STATIC_NICK));

    /**
     * MaliitAttributeExtension:id:
     *
     * Path to file where definitions of overrides are defined.
     */
    g_object_class_install_property (g_object_class,
                                     PROP_FILENAME,
                                     g_param_spec_string ("filename",
                                                          "Filename", /* TODO: mark as translatable? */
                                                          "Filename of the extension", /* TODO: mark as translatable? */
                                                          NULL,
                                                          G_PARAM_READABLE |
                                                          G_PARAM_WRITABLE |
                                                          G_PARAM_CONSTRUCT_ONLY |
                                                          G_PARAM_STATIC_NAME |
                                                          G_PARAM_STATIC_BLURB |
                                                          G_PARAM_STATIC_NICK));

    g_object_class_install_property (g_object_class,
                                     PROP_ATTRIBUTES,
                                     g_param_spec_boxed ("attributes",
                                                         "Attributes", /* TODO: mark as translatable? */
                                                         "Attributes overrides", /* TODO: mark as translatable? */
                                                         G_TYPE_HASH_TABLE,
                                                         G_PARAM_READABLE |
                                                         G_PARAM_STATIC_NAME |
                                                         G_PARAM_STATIC_BLURB |
                                                         G_PARAM_STATIC_NICK));

    /**
     * MaliitAttributeExtension::extended-attribute-changed:
     * @extension: The #MaliitAttributeExtension emitting the signal.
     * @key: A string specifying the target for the attribute.
     * @value: A new value.
     *
     * Informs application that input method server has changed the
     * extended attribute.
     */
    signals[EXTENDED_ATTRIBUTE_CHANGED] =
        g_signal_new ("extended-attribute-changed",
                      MALIIT_TYPE_ATTRIBUTE_EXTENSION,
                      G_SIGNAL_RUN_FIRST,
                      0,
                      NULL,
                      NULL,
                      maliit_marshal_VOID__STRING_VARIANT,
                      G_TYPE_NONE,
                      2,
                      G_TYPE_STRING,
                      G_TYPE_VARIANT);
}

static void
maliit_attribute_extension_init (MaliitAttributeExtension *extension)
{
    MaliitAttributeExtensionPrivate *priv = maliit_attribute_extension_get_instance_private (extension);

    priv->id = 0;
    priv->filename = NULL;
    priv->attributes = g_hash_table_new_full (g_str_hash,
                                              g_str_equal,
                                              g_free,
                                              (GDestroyNotify) g_variant_unref);
    priv->registry = maliit_attribute_extension_registry_get_instance ();

    extension->priv = priv;
}

/**
 * maliit_attribute_extension_new:
 *
 * Creates new attribute extension, which is not associated with any file.
 *
 * Returns: (transfer full): The newly created
 * #MaliitAttributeExtension.
 */
MaliitAttributeExtension *
maliit_attribute_extension_new (void)
{
    return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
                                                     NULL));
}

/**
 * maliit_attribute_extension_new_with_id: (skip)
 * @id: An overriden id.
 *
 * Creates a new attribute extension with already existing id. Used
 * internally by #MaliitSettingsManager.
 *
 * Returns: (transfer full): The newly created
 * #MaliitAttributeExtension.
 */
MaliitAttributeExtension *
maliit_attribute_extension_new_with_id (int id)
{
    return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
                                                     "id", id,
                                                     NULL));
}

/**
 * maliit_attribute_extension_new_with_filename:
 * @filename: (transfer none) (type filename): Filename where overrides are stored.
 *
 * Creates new attribute extension, which is associated with file
 * given as @filename.
 *
 * Returns: (transfer full): The newly created
 * #MaliitAttributeExtension.
 */
MaliitAttributeExtension *
maliit_attribute_extension_new_with_filename (const gchar *filename)
{
    return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
                                                     "filename", filename,
                                                     NULL));
}

/**
 * maliit_attribute_extension_get_attributes:
 * @extension: (transfer none): The #MaliitAttributeExtension which attributes you want to get.
 *
 * Gets all attributes of this extension that were set previously with
 * maliit_attribute_extension_set_attribute().
 *
 * Returns: (transfer none) (element-type utf8 GLib.Variant): The #GHashTable
 * containing strings as keys and #GVariant<!-- -->s as values. Should not be
 * freed nor modified.
 */
GHashTable *
maliit_attribute_extension_get_attributes (MaliitAttributeExtension *extension)
{
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), NULL);

    return extension->priv->attributes;
}

/**
 * maliit_attribute_extension_get_filename:
 * @extension: (transfer none): The #MaliitAttributeExtension which filename you want to get.
 *
 * Gets filename of this extension that were set previously with
 * maliit_attribute_extension_new_with_filename().
 *
 * Returns: (transfer none) (type filename): The string being a
 * filename of this extension or %NULL. Returned string should not be
 * freed nor modified.
 */
const gchar *
maliit_attribute_extension_get_filename (MaliitAttributeExtension *extension)
{
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), NULL);

    return extension->priv->filename;
}

/**
 * maliit_attribute_extension_get_id:
 * @extension: (transfer none): The #MaliitAttributeExtension which ID you want to get.
 *
 * Gets ID of this extension.
 *
 * Returns: The ID of this extension.
 */
int
maliit_attribute_extension_get_id (MaliitAttributeExtension *extension)
{
    g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), -1);

    return extension->priv->id;
}

/**
 * maliit_attribute_extension_update_attribute:
 * @extension: (transfer none): The #MaliitAttributeExtension which attribute you want to update.
 * @key: (transfer none): Attribute name to update.
 * @value: (transfer none): Attribute value to update.
 *
 * Updates the @extension's attribute described by @key with
 * @value. This function always emits a
 * #MaliitAttributeExtension::extended-attribute-changed signal.
 */
void
maliit_attribute_extension_update_attribute (MaliitAttributeExtension *extension,
                                             const gchar *key,
                                             GVariant *value)
{
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
    g_return_if_fail (key != NULL);
    g_return_if_fail (value != NULL);

    g_hash_table_replace (extension->priv->attributes,
                          g_strdup (key),
                          g_variant_ref (value));

    g_signal_emit (extension,
                   signals[EXTENDED_ATTRIBUTE_CHANGED],
                   0,
                   key,
                   value);
}

/**
 * maliit_attribute_extension_set_attribute:
 * @extension: (transfer none): The #MaliitAttributeExtension which attribute you want to set.
 * @key: (transfer none): Attribute name to update.
 * @value: (transfer none): Attribute value to update.
 *
 * Sets an attribute in @extension described by @key to value in @value.
 */
void maliit_attribute_extension_set_attribute (MaliitAttributeExtension *extension,
                                               const gchar *key,
                                               GVariant *value)
{
    MaliitAttributeExtensionPrivate *priv;
    GHashTable *attributes;
    GVariant *orig_value;

    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
    g_return_if_fail (key != NULL);
    g_return_if_fail (value != NULL);

    priv = extension->priv;
    attributes = priv->attributes;

    if (!g_hash_table_lookup_extended (attributes, key, NULL, (gpointer *)&orig_value) ||
        !g_variant_equal (orig_value, value)) {

        g_hash_table_replace (attributes,
                              g_strdup (key),
                              g_variant_ref (value));

        maliit_attribute_extension_registry_extension_changed (priv->registry,
                                                               extension,
                                                               key,
                                                               value);
    }
}

/**
 * maliit_attribute_extension_attach_to_object:
 * @extension: (transfer none): The #MaliitAttributeExtension which you want to be attached.
 * @object: (transfer none): The #GObject to which @extension will be attached.
 *
 * Attaches @extension to @object, so input context can retrieve it
 * from @object. Note that attaching extensions to non-input
 * #GObject<!-- -->s does not have much sense.
 */
void
maliit_attribute_extension_attach_to_object (MaliitAttributeExtension *extension,
                                             GObject *object)
{
    g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
    g_return_if_fail (G_IS_OBJECT (object));

    g_object_set_qdata_full (object, MALIIT_ATTRIBUTE_EXTENSION_DATA_QUARK,
                             extension, g_object_unref);
}