1
/* This file is part of Maliit framework
3
* Copyright (C) 2012 One Laptop per Child Association
5
* Contact: maliit-discuss@lists.maliit.org
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.
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.
23
#include "maliitattributeextension.h"
24
#include "maliitattributeextensionprivate.h"
25
#include "maliitattributeextensionregistry.h"
26
#include "maliitmarshallers.h"
29
* SECTION:maliitattributeextension
30
* @short_description: attribute extensions
31
* @title: MaliitAttributeExtension
33
* @include: maliit/maliitattributeextension.h
35
* #MaliitAttributeExtension class can be used by application to
36
* override some aspect of IM plugin currently used, like the looks of
40
struct _MaliitAttributeExtensionPrivate
44
GHashTable *attributes;
45
MaliitAttributeExtensionRegistry *registry;
48
G_DEFINE_TYPE (MaliitAttributeExtension, maliit_attribute_extension, G_TYPE_OBJECT)
52
EXTENDED_ATTRIBUTE_CHANGED,
66
static guint signals[LAST_SIGNAL] = { 0 };
69
maliit_attribute_extension_finalize (GObject *object)
71
MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
72
MaliitAttributeExtensionPrivate *priv = extension->priv;
74
g_free (priv->filename);
76
G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->finalize (object);
80
maliit_attribute_extension_dispose (GObject *object)
82
MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
83
MaliitAttributeExtensionPrivate *priv = extension->priv;
86
MaliitAttributeExtensionRegistry *registry = priv->registry;
88
priv->registry = NULL;
89
maliit_attribute_extension_registry_remove_extension (registry,
91
g_object_unref (registry);
94
if (priv->attributes) {
95
GHashTable *attributes = priv->attributes;
97
priv->attributes = NULL;
98
g_hash_table_unref (attributes);
101
G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->dispose (object);
105
maliit_attribute_extension_set_property (GObject *object,
110
MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
111
MaliitAttributeExtensionPrivate *priv = extension->priv;
115
priv->id = g_value_get_int (value);
118
g_free (extension->priv->filename);
119
priv->filename = g_value_dup_string (value);
121
/* PROP_ATTRIBUTES is read only. */
123
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129
maliit_attribute_extension_get_property (GObject *object,
134
MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
135
MaliitAttributeExtensionPrivate *priv = extension->priv;
139
g_value_set_int (value, priv->id);
142
g_value_set_string (value, priv->filename);
144
case PROP_ATTRIBUTES:
145
g_value_set_boxed (value, priv->attributes);
147
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153
maliit_attribute_extension_constructed (GObject *object)
155
static int id_counter = 0;
156
MaliitAttributeExtension *extension = MALIIT_ATTRIBUTE_EXTENSION (object);
157
MaliitAttributeExtensionPrivate *priv = extension->priv;
160
priv->id = id_counter++;
163
maliit_attribute_extension_registry_add_extension (priv->registry,
166
G_OBJECT_CLASS (maliit_attribute_extension_parent_class)->constructed (object);
170
maliit_attribute_extension_class_init (MaliitAttributeExtensionClass *extension_class)
172
GObjectClass *g_object_class = G_OBJECT_CLASS (extension_class);
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;
181
* MaliitAttributeExtension:id:
183
* ID of the extension.
185
g_object_class_install_property (g_object_class,
187
g_param_spec_int ("id",
188
"ID", /* TODO: mark as translatable? */
189
"ID of the extension", /* TODO: mark as translatable? */
195
G_PARAM_CONSTRUCT_ONLY |
196
G_PARAM_STATIC_NAME |
197
G_PARAM_STATIC_BLURB |
198
G_PARAM_STATIC_NICK));
201
* MaliitAttributeExtension:id:
203
* Path to file where definitions of overrides are defined.
205
g_object_class_install_property (g_object_class,
207
g_param_spec_string ("filename",
208
"Filename", /* TODO: mark as translatable? */
209
"Filename of the extension", /* TODO: mark as translatable? */
213
G_PARAM_CONSTRUCT_ONLY |
214
G_PARAM_STATIC_NAME |
215
G_PARAM_STATIC_BLURB |
216
G_PARAM_STATIC_NICK));
218
g_object_class_install_property (g_object_class,
220
g_param_spec_boxed ("attributes",
221
"Attributes", /* TODO: mark as translatable? */
222
"Attributes overrides", /* TODO: mark as translatable? */
225
G_PARAM_STATIC_NAME |
226
G_PARAM_STATIC_BLURB |
227
G_PARAM_STATIC_NICK));
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.
235
* Informs application that input method server has changed the
236
* extended attribute.
238
signals[EXTENDED_ATTRIBUTE_CHANGED] =
239
g_signal_new ("extended-attribute-changed",
240
MALIIT_TYPE_ATTRIBUTE_EXTENSION,
245
maliit_marshal_VOID__STRING_VARIANT,
251
g_type_class_add_private (extension_class, sizeof (MaliitAttributeExtensionPrivate));
255
maliit_attribute_extension_init (MaliitAttributeExtension *extension)
257
MaliitAttributeExtensionPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (extension,
258
MALIIT_TYPE_ATTRIBUTE_EXTENSION,
259
MaliitAttributeExtensionPrivate);
262
priv->filename = NULL;
263
priv->attributes = g_hash_table_new_full (g_str_hash,
266
(GDestroyNotify) g_variant_unref);
267
priv->registry = maliit_attribute_extension_registry_get_instance ();
269
extension->priv = priv;
273
* maliit_attribute_extension_new:
275
* Creates new attribute extension, which is not associated with any file.
277
* Returns: (transfer full): The newly created
278
* #MaliitAttributeExtension.
280
MaliitAttributeExtension *
281
maliit_attribute_extension_new (void)
283
return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
288
* maliit_attribute_extension_new_with_id: (skip)
289
* @id: An overriden id.
291
* Creates a new attribute extension with already existing id. Used
292
* internally by #MaliitSettingsManager.
294
* Returns: (transfer full): The newly created
295
* #MaliitAttributeExtension.
297
MaliitAttributeExtension *
298
maliit_attribute_extension_new_with_id (int id)
300
return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
306
* maliit_attribute_extension_new_with_filename:
307
* @filename: (transfer none) (type filename): Filename where overrides are stored.
309
* Creates new attribute extension, which is associated with file
310
* given as @filename.
312
* Returns: (transfer full): The newly created
313
* #MaliitAttributeExtension.
315
MaliitAttributeExtension *
316
maliit_attribute_extension_new_with_filename (const gchar *filename)
318
return MALIIT_ATTRIBUTE_EXTENSION (g_object_new (MALIIT_TYPE_ATTRIBUTE_EXTENSION,
319
"filename", filename,
324
* maliit_attribute_extension_get_attributes:
325
* @extension: (transfer none): The #MaliitAttributeExtension which attributes you want to get.
327
* Gets all attributes of this extension that were set previously with
328
* maliit_attribute_extension_set_attribute().
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.
335
maliit_attribute_extension_get_attributes (MaliitAttributeExtension *extension)
337
g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), NULL);
339
return extension->priv->attributes;
343
* maliit_attribute_extension_get_filename:
344
* @extension: (transfer none): The #MaliitAttributeExtension which filename you want to get.
346
* Gets filename of this extension that were set previously with
347
* maliit_attribute_extension_new_with_filename().
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.
354
maliit_attribute_extension_get_filename (MaliitAttributeExtension *extension)
356
g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), NULL);
358
return extension->priv->filename;
362
* maliit_attribute_extension_get_id:
363
* @extension: (transfer none): The #MaliitAttributeExtension which ID you want to get.
365
* Gets ID of this extension.
367
* Returns: The ID of this extension.
370
maliit_attribute_extension_get_id (MaliitAttributeExtension *extension)
372
g_return_val_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension), -1);
374
return extension->priv->id;
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.
383
* Updates the @extension's attribute described by @key with
384
* @value. This function always emits a
385
* #MaliitAttributeExtension::extended-attribute-changed signal.
388
maliit_attribute_extension_update_attribute (MaliitAttributeExtension *extension,
392
g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
393
g_return_if_fail (key != NULL);
394
g_return_if_fail (value != NULL);
396
g_hash_table_replace (extension->priv->attributes,
398
g_variant_ref (value));
400
g_signal_emit (extension,
401
signals[EXTENDED_ATTRIBUTE_CHANGED],
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.
413
* Sets an attribute in @extension described by @key to value in @value.
415
void maliit_attribute_extension_set_attribute (MaliitAttributeExtension *extension,
419
MaliitAttributeExtensionPrivate *priv;
420
GHashTable *attributes;
421
GVariant *orig_value;
423
g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
424
g_return_if_fail (key != NULL);
425
g_return_if_fail (value != NULL);
427
priv = extension->priv;
428
attributes = priv->attributes;
430
if (!g_hash_table_lookup_extended (attributes, key, NULL, (gpointer *)&orig_value) ||
431
!g_variant_equal (orig_value, value)) {
433
g_hash_table_replace (attributes,
435
g_variant_ref (value));
437
maliit_attribute_extension_registry_extension_changed (priv->registry,
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.
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.
454
maliit_attribute_extension_attach_to_object (MaliitAttributeExtension *extension,
457
g_return_if_fail (MALIIT_IS_ATTRIBUTE_EXTENSION (extension));
458
g_return_if_fail (G_IS_OBJECT (object));
460
g_object_set_qdata_full (object, MALIIT_ATTRIBUTE_EXTENSION_DATA_QUARK,
461
extension, g_object_unref);