summaryrefslogtreecommitdiffstats
path: root/bus/configproxy.c
blob: 5ce930bfb6ac63f04324b4794f212532bfff9493 (plain)
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* vim:set et sts=4: */
/* ibus - The Input Bus
 * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
 *
 * 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 of the License, 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 <ibusinternal.h>
#include <ibusmarshalers.h>
#include "configproxy.h"

#define BUS_CONFIG_PROXY_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), BUS_TYPE_CONFIG_PROXY, BusConfigProxyPrivate))

enum {
    VALUE_CHANGED,
    LAST_SIGNAL,
};


/* BusConfigProxyPriv */
struct _BusConfigProxyPrivate {
    gpointer pad;
};
typedef struct _BusConfigProxyPrivate BusConfigProxyPrivate;

#if 0
struct _BusPair {
    GValue car;
    GValue cdr;
};
typedef struct _BusPair BusPair;
#endif

static guint    config_signals[LAST_SIGNAL] = { 0 };

#if 0
/* functions prototype */
static BusPair  *bus_pair_new                   (GType                  car_type,
                                                 GType                  cdr_type,
                                                 gpointer               car,
                                                 gpointer               cdr);
static BusPair  *bus_pair_copy                  (BusPair                *pair);
static void      bus_pair_free                  (BusPair                *pair);
#endif
static void      bus_config_proxy_class_init    (BusConfigProxyClass    *klass);
static void      bus_config_proxy_init          (BusConfigProxy         *config);
static void      bus_config_proxy_real_destroy  (BusConfigProxy         *config);

static gboolean bus_config_proxy_dbus_signal    (IBusProxy             *proxy,
                                                 DBusMessage            *message);

static IBusProxyClass  *parent_class = NULL;

#if 0
static BusPair *
bus_pair_new (GType     car_type,
              GType     cdr_type,
              gpointer  car,
              gpointer  cdr)
{

    g_assert (car_type == G_TYPE_STRING ||
              car_type == G_TYPE_INT ||
              car_type == G_TYPE_BOOLEAN ||
              car_type == G_TYPE_DOUBLE);
    g_assert (cdr_type == G_TYPE_STRING ||
              cdr_type == G_TYPE_INT ||
              cdr_type == G_TYPE_BOOLEAN ||
              cdr_type == G_TYPE_DOUBLE);
    g_assert (car != NULL);
    g_assert (cdr != NULL);

    BusPair *pair;

    pair = g_slice_new0 (BusPair);

    g_value_init (&(pair->car), car_type);
    g_value_init (&(pair->cdr), cdr_type);

    switch (car_type) {
    case G_TYPE_STRING:
        g_value_set_string (&(pair->car), *(gchar **)car);
        break;
    case G_TYPE_INT:
        g_value_set_int (&(pair->car), *(gint32 *)car);
        break;
    case G_TYPE_BOOLEAN:
        g_value_set_boolean (&(pair->car), *(gboolean *)car);
        break;
    case G_TYPE_DOUBLE:
        g_value_set_double (&(pair->car), *(gdouble *)car);
        break;
    }
    
    switch (cdr_type) {
    case G_TYPE_STRING:
        g_value_set_string (&(pair->cdr), *(gchar **)cdr);
        break;
    case G_TYPE_INT:
        g_value_set_int (&(pair->cdr), *(gint32 *)car);
        break;
    case G_TYPE_BOOLEAN:
        g_value_set_boolean (&(pair->cdr), *(gboolean *)cdr);
        break;
    case G_TYPE_DOUBLE:
        g_value_set_double (&(pair->cdr), *(gdouble *)cdr);
        break;
    }
    return pair;
}

static BusPair *
bus_pair_copy (BusPair *pair)
{
    g_assert (pair != NULL);
    
    BusPair *new_pair;

    new_pair = g_slice_new0 (BusPair);
    g_value_copy (&(pair->car), &(new_pair->car));
    g_value_copy (&(pair->cdr), &(new_pair->cdr));
    return new_pair;
}

static void
bus_pair_free (BusPair *pair)
{
    g_assert (pair == NULL);
    
    g_value_unset (&(pair->car));
    g_value_unset (&(pair->cdr));

    g_slice_free (BusPair, pair);
}

#endif

GType
bus_config_proxy_get_type (void)
{
    static GType type = 0;

    static const GTypeInfo type_info = {
        sizeof (BusConfigProxyClass),
        (GBaseInitFunc)     NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc)    bus_config_proxy_class_init,
        NULL,               /* class finalize */
        NULL,               /* class data */
        sizeof (BusConfigProxy),
        0,
        (GInstanceInitFunc) bus_config_proxy_init,
    };

    if (type == 0) {
        type = g_type_register_static (IBUS_TYPE_PROXY,
                    "BusConfigProxy",
                    &type_info,
                    (GTypeFlags)0);
    }
    return type;
}

BusConfigProxy *
bus_config_proxy_new (BusConnection *connection)
{
    g_assert (BUS_IS_CONNECTION (connection));
    
    GObject *obj;
    obj = g_object_new (BUS_TYPE_CONFIG_PROXY,
                        "name", NULL,
                        "path", IBUS_PATH_CONFIG,
                        "connection", connection,
                        NULL);

    return BUS_CONFIG_PROXY (obj);
}

static void
bus_config_proxy_class_init (BusConfigProxyClass *klass)
{
    IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);
    IBusProxyClass *proxy_class = IBUS_PROXY_CLASS (klass);


    parent_class = (IBusProxyClass *) g_type_class_peek_parent (klass);

    g_type_class_add_private (klass, sizeof (BusConfigProxyPrivate));

    ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_config_proxy_real_destroy;

    proxy_class->dbus_signal = bus_config_proxy_dbus_signal;
    
    /* install signals */
    config_signals[VALUE_CHANGED] =
        g_signal_new (I_("value-changed"),
            G_TYPE_FROM_CLASS (klass),
            G_SIGNAL_RUN_LAST,
            0,
            NULL, NULL,
            ibus_marshal_VOID__STRING_STRING_BOXED,
            G_TYPE_NONE,
            3,
            G_TYPE_STRING,
            G_TYPE_STRING,
            G_TYPE_VALUE | G_SIGNAL_TYPE_STATIC_SCOPE);
}

static void
bus_config_proxy_init (BusConfigProxy *config)
{
    BusConfigProxyPrivate *priv;
    priv = BUS_CONFIG_PROXY_GET_PRIVATE (config);
}

static void
bus_config_proxy_real_destroy (BusConfigProxy *config)
{
    BusConfigProxyPrivate *priv;
    priv = BUS_CONFIG_PROXY_GET_PRIVATE (config);
    
    ibus_proxy_call (IBUS_PROXY (config),
                     "Destroy",
                     DBUS_TYPE_INVALID);

    IBUS_OBJECT_CLASS(parent_class)->destroy (IBUS_OBJECT (config));
}

static void
_from_dbus_value (DBusMessageIter   *iter,
                  GValue            *value)
{
    g_assert (iter != NULL);
    g_assert (value != NULL);

    gint type;

    type = dbus_message_iter_get_arg_type (iter);
    
    switch (type) {
    case DBUS_TYPE_STRING:
        {
            gchar *v;
            g_value_init (value, G_TYPE_STRING);
            dbus_message_iter_get_basic (iter, &v);
            g_value_set_string (value, v);
        }
        break;
    case DBUS_TYPE_INT32:
        {
            gint v;
            g_value_init (value, G_TYPE_INT);
            dbus_message_iter_get_basic (iter, &v);
            g_value_set_int (value, v);
        }
        break;
    case DBUS_TYPE_BOOLEAN:
        {
            gboolean v;
            g_value_init (value, G_TYPE_BOOLEAN);
            dbus_message_iter_get_basic (iter, &v);
            g_value_set_boolean (value, v);
        }
        break;
    case DBUS_TYPE_DOUBLE:
        {
            gdouble v;
            g_value_init (value, G_TYPE_DOUBLE);
            dbus_message_iter_get_basic (iter, &v);
            g_value_set_double (value, v);
        }
        break;
    case DBUS_TYPE_ARRAY:
        {
            GValue v = { 0 };
            DBusMessageIter sub_iter;
            gint sub_type;
            GValueArray *array;

            
            sub_type = dbus_message_iter_get_element_type (iter);
            g_assert (sub_type == DBUS_TYPE_STRING ||
                      sub_type == DBUS_TYPE_INT32 ||
                      sub_type == DBUS_TYPE_BOOLEAN ||
                      sub_type == DBUS_TYPE_DOUBLE);
            
            g_value_init (value, G_TYPE_VALUE_ARRAY);
            array = g_value_array_new (0);            
            dbus_message_iter_recurse (iter, &sub_iter);
            while (dbus_message_iter_get_arg_type (&sub_iter) != DBUS_TYPE_INVALID) {
                _from_dbus_value (&sub_iter, &v);
                g_value_array_append (array, &v);
                dbus_message_iter_next (&sub_iter);
            }
            g_value_take_boxed (value, array);
            break;
        }
    default:
        g_assert_not_reached();
    }
}

static void
_to_dbus_value (DBusMessageIter *iter,
                const GValue    *value)
{
    switch (G_VALUE_TYPE (value)) {
    case G_TYPE_STRING:
        {
            const gchar * v = g_value_get_string (value);
            dbus_message_iter_append_basic (iter,
                                            DBUS_TYPE_STRING,
                                            &v);
        }
        break;
    case G_TYPE_INT:
        {
            gint v = g_value_get_int (value);
            dbus_message_iter_append_basic (iter,
                                            DBUS_TYPE_INT32,
                                            &v);
        }
        break;
    case G_TYPE_BOOLEAN:
        {
            gboolean v = g_value_get_boolean (value);
            dbus_message_iter_append_basic (iter,
                                            DBUS_TYPE_BOOLEAN,
                                            &v);
        }
        break;
    case G_TYPE_DOUBLE:
        {
            gdouble v = g_value_get_double (value);
            dbus_message_iter_append_basic (iter,
                                            DBUS_TYPE_DOUBLE,
                                            &v);
        }
        break;
    default:
        if (G_TYPE_VALUE_ARRAY == G_VALUE_TYPE (value)) {
            DBusMessageIter sub_iter;
            GType type = G_TYPE_INVALID;
            gint i;
            GValueArray *array = (GValueArray *)g_value_get_boxed (value);
            dbus_message_iter_open_container (iter, 
                                              DBUS_TYPE_ARRAY,
                                              "v",
                                              &sub_iter);
            if (array->n_values > 0) {
                type = G_VALUE_TYPE (&array->values[0]);
                g_assert (type == G_TYPE_STRING ||
                          type == G_TYPE_INT ||
                          type == G_TYPE_BOOLEAN ||
                          type == G_TYPE_DOUBLE);
            }
            for (i = 0; i < array->n_values; i++) {
                g_assert (type == G_VALUE_TYPE (&array->values[i]));
                _to_dbus_value (&sub_iter, &array->values[i]);
            }
            dbus_message_iter_close_container (iter, &sub_iter);
            break;
        }
        g_assert_not_reached();
    }
}

static gboolean
bus_config_proxy_dbus_signal (IBusProxy     *proxy,
                              DBusMessage   *message)
{
    g_assert (BUS_IS_CONFIG_PROXY (proxy));
    g_assert (message != NULL);
    
    BusConfigProxy *config;
    config = BUS_CONFIG_PROXY (proxy);
    
    if (dbus_message_is_signal (message, IBUS_INTERFACE_CONFIG, "ValueChanged")) {
        DBusMessageIter iter;
        gchar *section;
        gchar *name;
        GValue value = { 0 };
        gint type;

        dbus_message_iter_init (message, &iter);
        
        type = dbus_message_iter_get_arg_type (&iter);
        if (type != DBUS_TYPE_STRING) {
            g_warning ("Argument 1 of ValueChanged should be a string");
            return FALSE;
        }
        dbus_message_iter_get_basic (&iter, &section);
        
        if (type != DBUS_TYPE_STRING) {
            g_warning ("Argument 2 of ValueChanged should be a string");
            return FALSE;
        }
        dbus_message_iter_get_basic (&iter, &name);

        _from_dbus_value (&iter, &value);

        g_signal_emit (config,
                       config_signals[VALUE_CHANGED],
                       0,
                       section,
                       name,
                       &value);
        g_signal_stop_emission_by_name (config, "dbus-signal");
        return TRUE;
    }
    
    return FALSE;
}

gboolean
bus_config_proxy_get_value (BusConfigProxy  *config,
                            const gchar     *section,
                            const gchar     *name,
                            GValue          *value)
{
    g_assert (BUS_IS_CONFIG_PROXY (config));
    g_assert (section != NULL);
    g_assert (name != NULL);
    g_assert (value != NULL);

    DBusMessage *reply;
    IBusError *error;

    reply = ibus_proxy_call_with_reply_and_block (IBUS_PROXY (config),
                                                  "GetValue",
                                                  -1,
                                                  &error,
                                                  DBUS_TYPE_STRING, section,
                                                  DBUS_TYPE_STRING, name,
                                                  DBUS_TYPE_INVALID);
    if (reply == NULL) {
        g_warning ("%s: %s", error->name, error->message);
        ibus_error_free (error);
        return FALSE;
    }

    DBusMessageIter iter;
    dbus_message_iter_init (reply, &iter);
    _from_dbus_value (&iter, value);

    dbus_message_unref (reply);

    return TRUE;
}

gboolean
bus_config_proxy_set_value (BusConfigProxy  *config,
                            const gchar     *section,
                            const gchar     *name,
                            const GValue    *value)
{
    g_assert (BUS_IS_CONFIG_PROXY (config));
    g_assert (section != NULL);
    g_assert (name != NULL);
    g_assert (value != NULL);

    DBusMessage *message;
    DBusMessageIter iter;

    message = dbus_message_new_method_call (
                                ibus_proxy_get_name (IBUS_PROXY (config)),
                                ibus_proxy_get_path (IBUS_PROXY (config)),
                                ibus_proxy_get_interface (IBUS_PROXY (config)),
                                "SetValue");
    dbus_message_iter_init_append (message, &iter);
    _to_dbus_value (&iter, value);

    ibus_proxy_send (IBUS_PROXY (config), message);
    dbus_message_unref (message);

    return TRUE;
}