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

« back to all changes in this revision

Viewing changes to maliit-glib/maliitsettingsmanager.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 Openismus GmbH
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 "maliitsettingsmanager.h"
24
 
#include "maliitpluginsettingsprivate.h"
25
 
#include "maliitattributeextensionprivate.h"
26
 
#include "meego-im-connector.h"
27
 
 
28
 
#include <dbus/dbus-glib.h>
29
 
 
30
 
/**
31
 
 * SECTION:maliitsettingsmanager
32
 
 * @short_description: settings manager
33
 
 * @title: MaliitSettingsManager
34
 
 * @see_also: #MaliitSettingsEntry, #MaliitPluginSettings
35
 
 * @stability: Stable
36
 
 * @include: maliit/maliitsettingsmanager.h
37
 
 *
38
 
 * The #MaliitSettingsManager handles requesting and receiving plugin
39
 
 * settings from maliit-server and notifying when those settings has
40
 
 * changed.
41
 
 */
42
 
 
43
 
static gchar* preferred_description_locale = 0;
44
 
 
45
 
struct _MaliitSettingsManagerPrivate
46
 
{
47
 
    MaliitAttributeExtension *settings_list_changed;
48
 
    guint attribute_changed_signal_id;
49
 
    MeegoImConnector *connector;
50
 
};
51
 
 
52
 
G_DEFINE_TYPE (MaliitSettingsManager, maliit_settings_manager, G_TYPE_OBJECT)
53
 
 
54
 
enum
55
 
{
56
 
    PLUGIN_SETTINGS_RECEIVED,
57
 
    CONNECTED,
58
 
    DISCONNECTED,
59
 
 
60
 
    LAST_SIGNAL
61
 
};
62
 
 
63
 
static guint signals[LAST_SIGNAL] = { 0 };
64
 
 
65
 
static void
66
 
maliit_settings_manager_finalize (GObject *object)
67
 
{
68
 
    G_OBJECT_CLASS (maliit_settings_manager_parent_class)->finalize (object);
69
 
}
70
 
 
71
 
static void
72
 
maliit_settings_manager_dispose (GObject *object)
73
 
{
74
 
    MaliitSettingsManager *manager = MALIIT_SETTINGS_MANAGER (object);
75
 
 
76
 
    g_clear_object (&manager->priv->settings_list_changed);
77
 
 
78
 
    G_OBJECT_CLASS (maliit_settings_manager_parent_class)->dispose (object);
79
 
}
80
 
 
81
 
static void
82
 
maliit_settings_manager_class_init (MaliitSettingsManagerClass *manager_class)
83
 
{
84
 
    GObjectClass *g_object_class = G_OBJECT_CLASS (manager_class);
85
 
 
86
 
    g_object_class->finalize = maliit_settings_manager_finalize;
87
 
    g_object_class->dispose = maliit_settings_manager_dispose;
88
 
 
89
 
    /**
90
 
     * MaliitSettingsManager::plugin-settings-received:
91
 
     * @manager: The #MaliitSettingsManager emitting the signal.
92
 
     * @settings: (type GLib.List) (element-type Maliit.PluginSettings): Gotten settings.
93
 
     *
94
 
     * Emitted after call to
95
 
     * maliit_settings_manager_load_plugin_settings() and when the
96
 
     * plugin list changes on the server.
97
 
     */
98
 
    signals[PLUGIN_SETTINGS_RECEIVED] =
99
 
        g_signal_new ("plugin-settings-received",
100
 
                      MALIIT_TYPE_SETTINGS_MANAGER,
101
 
                      G_SIGNAL_RUN_FIRST,
102
 
                      0,
103
 
                      NULL,
104
 
                      NULL,
105
 
                      g_cclosure_marshal_VOID__POINTER,
106
 
                      G_TYPE_NONE,
107
 
                      1,
108
 
                      G_TYPE_POINTER);
109
 
 
110
 
    /**
111
 
     * MaliitSettingsManager::connected:
112
 
     * @manager: The #MaliitSettingsManager emitting the signal.
113
 
     *
114
 
     * Emitted when connection to maliit-server is established.
115
 
     */
116
 
    signals[CONNECTED] =
117
 
        g_signal_new ("connected",
118
 
                      MALIIT_TYPE_SETTINGS_MANAGER,
119
 
                      G_SIGNAL_RUN_FIRST,
120
 
                      0,
121
 
                      NULL,
122
 
                      NULL,
123
 
                      g_cclosure_marshal_VOID__VOID,
124
 
                      G_TYPE_NONE,
125
 
                      0);
126
 
 
127
 
    /**
128
 
     * MaliitSettingsManager::disconnected:
129
 
     * @manager: The #MaliitSettingsManager emitting the signal.
130
 
     *
131
 
     * Emitted when connection to maliit-server is broken.
132
 
     */
133
 
    signals[DISCONNECTED] =
134
 
        g_signal_new ("disconnected",
135
 
                      MALIIT_TYPE_SETTINGS_MANAGER,
136
 
                      G_SIGNAL_RUN_FIRST,
137
 
                      0,
138
 
                      NULL,
139
 
                      NULL,
140
 
                      g_cclosure_marshal_VOID__VOID,
141
 
                      G_TYPE_NONE,
142
 
                      0);
143
 
 
144
 
    g_type_class_add_private (manager_class, sizeof (MaliitSettingsManagerPrivate));
145
 
}
146
 
 
147
 
static void
148
 
on_connection_established (MaliitSettingsManager *manager,
149
 
                          gpointer user_data G_GNUC_UNUSED)
150
 
{
151
 
    g_signal_emit(manager, signals[CONNECTED], 0);
152
 
}
153
 
 
154
 
static void
155
 
on_connection_dropped (MaliitSettingsManager *manager,
156
 
                       gpointer user_data G_GNUC_UNUSED)
157
 
{
158
 
    g_signal_emit(manager, signals[DISCONNECTED], 0);
159
 
}
160
 
 
161
 
static gboolean
162
 
dbus_plugin_info_is_valid (GValueArray *plugin_info)
163
 
{
164
 
    static GType expected_types[] = {
165
 
        G_TYPE_STRING, /* description language */
166
 
        G_TYPE_STRING, /* plugin name */
167
 
        G_TYPE_STRING, /* plugin description */
168
 
        G_TYPE_INT, /* extension id */
169
 
        G_TYPE_INVALID /* settings entries, set in the first run */
170
 
    };
171
 
 
172
 
    guint iter;
173
 
 
174
 
    if (expected_types[4] == G_TYPE_INVALID) {
175
 
        GType attributes_gtype = dbus_g_type_get_map("GHashTable",
176
 
                                                     G_TYPE_STRING,
177
 
                                                     G_TYPE_VALUE);
178
 
        GType entry_gtype = dbus_g_type_get_struct("GValueArray",
179
 
                                                   G_TYPE_STRING,
180
 
                                                   G_TYPE_STRING,
181
 
                                                   G_TYPE_INT,
182
 
                                                   G_TYPE_BOOLEAN,
183
 
                                                   G_TYPE_VALUE,
184
 
                                                   attributes_gtype,
185
 
                                                   G_TYPE_INVALID);
186
 
 
187
 
        expected_types[4] = dbus_g_type_get_collection("GPtrArray",
188
 
                                                       entry_gtype);
189
 
    }
190
 
 
191
 
    if (!plugin_info) {
192
 
        return FALSE;
193
 
    }
194
 
 
195
 
    if (plugin_info->n_values != G_N_ELEMENTS (expected_types)) {
196
 
        return FALSE;
197
 
    }
198
 
 
199
 
    for (iter = 0; iter < G_N_ELEMENTS (expected_types); ++iter) {
200
 
        GValue *value = g_value_array_get_nth (plugin_info, iter);
201
 
 
202
 
        if (!value) {
203
 
            return FALSE;
204
 
        }
205
 
        if (!G_VALUE_HOLDS (value, expected_types[iter])) {
206
 
            return FALSE;
207
 
        }
208
 
    }
209
 
 
210
 
    return TRUE;
211
 
}
212
 
 
213
 
static void
214
 
on_plugins_loaded (MaliitSettingsManager *manager,
215
 
                   GPtrArray *plugin_settings,
216
 
                   gpointer user_data G_GNUC_UNUSED)
217
 
{
218
 
    guint iter;
219
 
    GList *result;
220
 
    GHashTable *extensions;
221
 
 
222
 
    if (!plugin_settings) {
223
 
        return;
224
 
    }
225
 
 
226
 
    result = NULL;
227
 
    extensions = g_hash_table_new (g_direct_hash,
228
 
                                   g_direct_equal);
229
 
 
230
 
    for (iter = 0; iter < plugin_settings->len; ++iter) {
231
 
        GValueArray *plugin_info = g_ptr_array_index (plugin_settings, iter);
232
 
        const gchar *plugin_name;
233
 
        int extension_id;
234
 
 
235
 
 
236
 
        if (!dbus_plugin_info_is_valid (plugin_info)) {
237
 
            g_warning ("Received invalid plugin informations");
238
 
            continue;
239
 
        }
240
 
 
241
 
        plugin_name = g_value_get_string (g_value_array_get_nth (plugin_info, 1));
242
 
        extension_id = g_value_get_int(g_value_array_get_nth (plugin_info, 3));
243
 
 
244
 
        if (!g_strcmp0(plugin_name, "@settings")) {
245
 
            MaliitSettingsManagerPrivate *priv = manager->priv;
246
 
 
247
 
            if (priv->attribute_changed_signal_id) {
248
 
                if (priv->settings_list_changed) {
249
 
                    g_signal_handler_disconnect (priv->settings_list_changed,
250
 
                                                 priv->attribute_changed_signal_id);
251
 
                    g_clear_object (&priv->settings_list_changed);
252
 
                }
253
 
                priv->attribute_changed_signal_id = 0;
254
 
            }
255
 
 
256
 
            priv->settings_list_changed = maliit_attribute_extension_new_with_id (extension_id);
257
 
            priv->attribute_changed_signal_id = g_signal_connect_swapped (priv->settings_list_changed,
258
 
                                                                          "extended-attribute-changed",
259
 
                                                                          G_CALLBACK (maliit_settings_manager_load_plugin_settings),
260
 
                                                                          manager);
261
 
        } else {
262
 
            MaliitAttributeExtension *extension = g_hash_table_lookup (extensions,
263
 
                                                                       GINT_TO_POINTER (extension_id));
264
 
 
265
 
            if (!extension) {
266
 
                extension = maliit_attribute_extension_new_with_id (extension_id);
267
 
                g_hash_table_insert (extensions, GINT_TO_POINTER (extension_id), extension);
268
 
            }
269
 
 
270
 
            result = g_list_prepend (result,
271
 
                                     maliit_plugin_settings_new_from_dbus_data (plugin_info,
272
 
                                                                                extension));
273
 
        }
274
 
    }
275
 
 
276
 
    result = g_list_reverse (result);
277
 
    g_signal_emit (manager,
278
 
                   signals[PLUGIN_SETTINGS_RECEIVED],
279
 
                   0,
280
 
                   result);
281
 
 
282
 
    g_list_free_full (result,
283
 
                      g_object_unref);
284
 
}
285
 
 
286
 
static void
287
 
maliit_settings_manager_init (MaliitSettingsManager *manager)
288
 
{
289
 
    MeegoIMProxy *proxy;
290
 
    MeegoIMContextDbusObj *context_dbus;
291
 
    MaliitSettingsManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
292
 
                                                                      MALIIT_TYPE_SETTINGS_MANAGER,
293
 
                                                                      MaliitSettingsManagerPrivate);
294
 
 
295
 
    priv->settings_list_changed = NULL;
296
 
    priv->attribute_changed_signal_id = 0;
297
 
    priv->connector = meego_im_connector_get_singleton ();
298
 
    proxy = priv->connector->proxy;
299
 
    context_dbus = priv->connector->dbusobj;
300
 
 
301
 
    g_signal_connect_swapped (proxy,
302
 
                              "connection-established",
303
 
                              G_CALLBACK (on_connection_established),
304
 
                              manager);
305
 
    g_signal_connect_swapped (proxy,
306
 
                              "connection-dropped",
307
 
                              G_CALLBACK(on_connection_dropped),
308
 
                              manager);
309
 
    g_signal_connect_swapped (context_dbus,
310
 
                              "plugin-settings-loaded",
311
 
                              G_CALLBACK(on_plugins_loaded),
312
 
                              manager);
313
 
 
314
 
    manager->priv = priv;
315
 
}
316
 
 
317
 
/**
318
 
 * maliit_settings_manager_new:
319
 
 *
320
 
 * Creates new settings manager.
321
 
 *
322
 
 * Returns: (transfer full): The newly created
323
 
 * #MaliitSettingsManager.
324
 
 */
325
 
MaliitSettingsManager *
326
 
maliit_settings_manager_new (void)
327
 
{
328
 
    return MALIIT_SETTINGS_MANAGER (g_object_new (MALIIT_TYPE_SETTINGS_MANAGER,
329
 
                                                  NULL));
330
 
}
331
 
 
332
 
/**
333
 
 * maliit_settings_manager_load_plugin_settings:
334
 
 * @manager: (transfer none): The #MaliitSettingsManager.
335
 
 *
336
 
 * Request the list of settings from maliit-server.
337
 
 * The settings will be returned async via the MaliitServerManager::plugin-settings-received signal
338
 
 */
339
 
void
340
 
maliit_settings_manager_load_plugin_settings (MaliitSettingsManager *manager)
341
 
{
342
 
    g_return_if_fail (MALIIT_IS_SETTINGS_MANAGER (manager));
343
 
 
344
 
    meego_im_proxy_load_plugin_settings (manager->priv->connector->proxy,
345
 
                                         maliit_settings_manager_get_preferred_description_locale ());
346
 
}
347
 
 
348
 
/**
349
 
 * maliit_settings_manager_set_preferred_description_locale:
350
 
 * @locale_name: (transfer none): The new preferred locale.
351
 
 *
352
 
 * Sets the preferred locale for human-readable descriptions. The setting is
353
 
 * valid for all instances of #MaliitSettingsManager in the process.
354
 
 * Note that the server may not always be able to return info in the requested locale.
355
 
 */
356
 
void
357
 
maliit_settings_manager_set_preferred_description_locale (const gchar *locale_name)
358
 
{
359
 
    g_return_if_fail (locale_name != NULL);
360
 
 
361
 
    if (preferred_description_locale) {
362
 
        g_free (preferred_description_locale);
363
 
    }
364
 
    preferred_description_locale = g_strdup (locale_name);
365
 
}
366
 
 
367
 
/**
368
 
 * maliit_settings_manager_get_preferred_description_locale:
369
 
 *
370
 
 * Gets the preferred locale for human-readable description.
371
 
 *
372
 
 * Returns: (transfer none): The string being a current preferred
373
 
 * locale. Returned string should not be freed nor modified.
374
 
 */
375
 
const gchar *
376
 
maliit_settings_manager_get_preferred_description_locale (void)
377
 
{
378
 
    if (!preferred_description_locale) {
379
 
        preferred_description_locale = g_strdup("en");
380
 
    }
381
 
 
382
 
    return preferred_description_locale;
383
 
}