~ubuntu-branches/debian/sid/ibus/sid

« back to all changes in this revision

Viewing changes to .pc/ibus-541492-xkb.patch/bus/ibusimpl.c

  • Committer: Package Import Robot
  • Author(s): Osamu Aoki, Aron Xu, Osamu Aoki
  • Date: 2014-08-04 22:42:19 UTC
  • mfrom: (1.5.7)
  • Revision ID: package-import@ubuntu.com-20140804224219-mjprlql1jdwi5sxm
Tags: 1.5.8-1
[ Aron Xu ]
* Allow parallel building

[ Osamu Aoki ]
* Update ibus document with Qt5 support

[ Aron Xu ]
* Imported Upstream version 1.5.8
* Remove patch for chasing HEAD, imported new upstream release
* Remove the patch for updating IBusKeymap jp, applied upstream
* Drop gtk2 version of the password patch because it's never executed
* Drop ibus-541492-xkb.patch
* Drop ibus-530711-preload-sys.patch
* Drop ibus-810211-no-switch-by-no-trigger.patch
* Drop 999-update-po.patch

[ Osamu Aoki ]
* linux-any for ibus-wayland
* Remove some symbols matching ibus-541492-xkb.patch
* Remove old patch for GTK+ 3.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2
 
/* vim:set et sts=4: */
3
 
/* ibus - The Input Bus
4
 
 * Copyright (C) 2008-2013 Peng Huang <shawn.p.huang@gmail.com>
5
 
 * Copyright (C) 2008-2013 Red Hat, Inc.
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 License, 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 Free Software
19
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
20
 
 * USA
21
 
 */
22
 
 
23
 
#include "ibusimpl.h"
24
 
 
25
 
#include <locale.h>
26
 
#include <signal.h>
27
 
#include <strings.h>
28
 
#include <sys/types.h>
29
 
#include <sys/wait.h>
30
 
#include <unistd.h>
31
 
 
32
 
#include "connection.h"
33
 
#include "dbusimpl.h"
34
 
#include "factoryproxy.h"
35
 
#include "global.h"
36
 
#include "inputcontext.h"
37
 
#include "panelproxy.h"
38
 
#include "server.h"
39
 
#include "types.h"
40
 
 
41
 
struct _BusIBusImpl {
42
 
    IBusService parent;
43
 
    /* instance members */
44
 
    GHashTable *factory_dict;
45
 
 
46
 
    /* registered components */
47
 
    GList *registered_components;
48
 
    GList *contexts;
49
 
 
50
 
    /* a fake input context for global engine support */
51
 
    BusInputContext *fake_context;
52
 
    
53
 
    /* a list of engines that are started by a user (without the --ibus
54
 
     * command line flag.) */
55
 
    GList *register_engine_list;
56
 
 
57
 
    /* if TRUE, ibus-daemon uses a keysym translated by the system
58
 
     * (i.e. XKB) as-is. otherwise, ibus-daemon itself converts keycode
59
 
     * into keysym. */
60
 
    gboolean use_sys_layout;
61
 
 
62
 
    gboolean embed_preedit_text;
63
 
 
64
 
    IBusRegistry    *registry;
65
 
 
66
 
    /* a list of BusComponent objects that are created from component XML
67
 
     * files (or from the cache of them). */
68
 
    GList *components;
69
 
 
70
 
    /* a mapping from an engine name (e.g. 'pinyin') to the corresponding
71
 
     * IBusEngineDesc object. */
72
 
    GHashTable *engine_table;
73
 
 
74
 
    BusInputContext *focused_context;
75
 
    BusPanelProxy   *panel;
76
 
 
77
 
    /* a default keymap of ibus-daemon (usually "us") which is used only
78
 
     * when use_sys_layout is FALSE. */
79
 
    IBusKeymap      *keymap;
80
 
 
81
 
    gboolean use_global_engine;
82
 
    gchar *global_engine_name;
83
 
    gchar *global_previous_engine_name;
84
 
};
85
 
 
86
 
struct _BusIBusImplClass {
87
 
    IBusServiceClass parent;
88
 
 
89
 
    /* class members */
90
 
};
91
 
 
92
 
enum {
93
 
    LAST_SIGNAL,
94
 
};
95
 
 
96
 
enum {
97
 
    PROP_0,
98
 
};
99
 
 
100
 
/*
101
 
static guint            _signals[LAST_SIGNAL] = { 0 };
102
 
*/
103
 
 
104
 
/* functions prototype */
105
 
static void     bus_ibus_impl_destroy   (BusIBusImpl        *ibus);
106
 
static void     bus_ibus_impl_service_method_call
107
 
                                        (IBusService        *service,
108
 
                                         GDBusConnection    *connection,
109
 
                                         const gchar        *sender,
110
 
                                         const gchar        *object_path,
111
 
                                         const gchar        *interface_name,
112
 
                                         const gchar        *method_name,
113
 
                                         GVariant           *parameters,
114
 
                                         GDBusMethodInvocation
115
 
                                                            *invocation);
116
 
static GVariant *
117
 
                bus_ibus_impl_service_get_property
118
 
                                        (IBusService        *service,
119
 
                                         GDBusConnection    *connection,
120
 
                                         const gchar        *sender,
121
 
                                         const gchar        *object_path,
122
 
                                         const gchar        *interface_name,
123
 
                                         const gchar        *property_name,
124
 
                                         GError            **error);
125
 
static gboolean
126
 
                bus_ibus_impl_service_set_property
127
 
                                        (IBusService        *service,
128
 
                                         GDBusConnection    *connection,
129
 
                                         const gchar        *sender,
130
 
                                         const gchar        *object_path,
131
 
                                         const gchar        *interface_name,
132
 
                                         const gchar        *property_name,
133
 
                                         GVariant           *value,
134
 
                                         GError            **error);
135
 
static void     bus_ibus_impl_registry_init
136
 
                                        (BusIBusImpl        *ibus);
137
 
static void     bus_ibus_impl_registry_changed
138
 
                                        (BusIBusImpl        *ibus);
139
 
static void     bus_ibus_impl_registry_destroy
140
 
                                        (BusIBusImpl        *ibus);
141
 
static void     bus_ibus_impl_component_name_owner_changed
142
 
                                        (BusIBusImpl        *ibus,
143
 
                                         const gchar        *name,
144
 
                                         const gchar        *old_name,
145
 
                                         const gchar        *new_name);
146
 
static void     bus_ibus_impl_global_engine_changed
147
 
                                        (BusIBusImpl        *ibus);
148
 
static void     bus_ibus_impl_set_context_engine_from_desc
149
 
                                        (BusIBusImpl        *ibus,
150
 
                                         BusInputContext    *context,
151
 
                                         IBusEngineDesc     *desc);
152
 
static BusInputContext
153
 
               *bus_ibus_impl_create_input_context
154
 
                                        (BusIBusImpl        *ibus,
155
 
                                         BusConnection      *connection,
156
 
                                         const gchar        *client);
157
 
static IBusEngineDesc
158
 
               *bus_ibus_impl_get_engine_desc
159
 
                                        (BusIBusImpl        *ibus,
160
 
                                         const gchar        *engine_name);
161
 
static void     bus_ibus_impl_set_focused_context
162
 
                                        (BusIBusImpl        *ibus,
163
 
                                         BusInputContext    *context);
164
 
static void     bus_ibus_impl_property_changed
165
 
                                        (BusIBusImpl        *service,
166
 
                                         const gchar        *property_name,
167
 
                                         GVariant           *value);
168
 
/* some callback functions */
169
 
static void     _context_engine_changed_cb
170
 
                                        (BusInputContext    *context,
171
 
                                         BusIBusImpl        *ibus);
172
 
 
173
 
/* The interfaces available in this class, which consists of a list of
174
 
 * methods this class implements and a list of signals this class may emit.
175
 
 * Method calls to the interface that are not defined in this XML will
176
 
 * be automatically rejected by the GDBus library (see src/ibusservice.c
177
 
 * for details.)
178
 
 * Implement org.freedesktop.DBus.Properties
179
 
 * http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties */
180
 
static const gchar introspection_xml[] =
181
 
    "<node>\n"
182
 
    "  <interface name='org.freedesktop.IBus'>\n"
183
 
    "    <property name='Address' type='s' access='read' />\n"
184
 
    "    <property name='CurrentInputContext' type='o' access='read' />\n"
185
 
    "    <property name='Engines' type='av' access='read' />\n"
186
 
    "    <property name='GlobalEngine' type='v' access='read' />\n"
187
 
    "    <property name='PreloadEngines' type='as' access='write'>\n"
188
 
    "      <annotation\n"
189
 
    "          name='org.freedesktop.DBus.Property.EmitsChangedSignal'\n"
190
 
    "          value='true' />\n"
191
 
    "    </property>\n"
192
 
    "    <property name='EmbedPreeditText' type='b' access='readwrite'>\n"
193
 
    "      <annotation\n"
194
 
    "          name='org.freedesktop.DBus.Property.EmitsChangedSignal'\n"
195
 
    "          value='true' />\n"
196
 
    "    </property>\n"
197
 
    "    <method name='CreateInputContext'>\n"
198
 
    "      <arg direction='in'  type='s' name='client_name' />\n"
199
 
    "      <arg direction='out' type='o' name='object_path' />\n"
200
 
    "    </method>\n"
201
 
    "    <method name='RegisterComponent'>\n"
202
 
    "      <arg direction='in'  type='v' name='component' />\n"
203
 
    "    </method>\n"
204
 
    "    <method name='GetEnginesByNames'>\n"
205
 
    "      <arg direction='in'  type='as' name='names' />\n"
206
 
    "      <arg direction='out' type='av' name='engines' />\n"
207
 
    "    </method>\n"
208
 
    "    <method name='Exit'>\n"
209
 
    "      <arg direction='in'  type='b' name='restart' />\n"
210
 
    "    </method>\n"
211
 
    "    <method name='Ping'>\n"
212
 
    "      <arg direction='in'  type='v' name='data' />\n"
213
 
    "      <arg direction='out' type='v' name='data' />\n"
214
 
    "    </method>\n"
215
 
    "    <method name='SetGlobalEngine'>\n"
216
 
    "      <arg direction='in'  type='s' name='engine_name' />\n"
217
 
    "    </method>\n"
218
 
    "    <signal name='RegistryChanged'>\n"
219
 
    "    </signal>\n"
220
 
    "    <signal name='GlobalEngineChanged'>\n"
221
 
    "      <arg type='s' name='engine_name' />\n"
222
 
    "    </signal>\n"
223
 
    "    <property name='ActiveEngines' type='av' access='read' />\n"
224
 
    "    <method name='GetAddress'>\n"
225
 
    "      <arg direction='out' type='s' name='address' />\n"
226
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
227
 
    "    </method>\n"
228
 
    "    <method name='CurrentInputContext'>\n"
229
 
    "      <arg direction='out' type='o' name='object_path' />\n"
230
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
231
 
    "    </method>\n"
232
 
    "    <method name='ListEngines'>\n"
233
 
    "      <arg direction='out' type='av' name='engines' />\n"
234
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
235
 
    "    </method>\n"
236
 
    "    <method name='ListActiveEngines'>\n"
237
 
    "      <arg direction='out' type='av' name='engines' />\n"
238
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
239
 
    "    </method>\n"
240
 
    "    <method name='GetUseSysLayout'>\n"
241
 
    "      <arg direction='out' type='b' name='enabled' />\n"
242
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
243
 
    "    </method>\n"
244
 
    "    <method name='GetUseGlobalEngine'>\n"
245
 
    "      <arg direction='out' type='b' name='enabled' />\n"
246
 
    "    </method>\n"
247
 
    "    <method name='IsGlobalEngineEnabled'>\n"
248
 
    "      <arg direction='out' type='b' name='enabled' />\n"
249
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
250
 
    "    </method>\n"
251
 
    "    <method name='GetGlobalEngine'>\n"
252
 
    "      <arg direction='out' type='v' name='desc' />\n"
253
 
    "      <annotation name='org.freedesktop.DBus.Deprecated' value='true'/>\n"
254
 
    "    </method>\n"
255
 
    "  </interface>\n"
256
 
    "</node>\n";
257
 
 
258
 
 
259
 
G_DEFINE_TYPE (BusIBusImpl, bus_ibus_impl, IBUS_TYPE_SERVICE)
260
 
 
261
 
static void
262
 
bus_ibus_impl_class_init (BusIBusImplClass *class)
263
 
{
264
 
    IBUS_OBJECT_CLASS (class)->destroy =
265
 
            (IBusObjectDestroyFunc) bus_ibus_impl_destroy;
266
 
 
267
 
    /* override the parent class's implementation. */
268
 
    IBUS_SERVICE_CLASS (class)->service_method_call =
269
 
            bus_ibus_impl_service_method_call;
270
 
    IBUS_SERVICE_CLASS (class)->service_get_property =
271
 
            bus_ibus_impl_service_get_property;
272
 
    IBUS_SERVICE_CLASS (class)->service_set_property =
273
 
            bus_ibus_impl_service_set_property;
274
 
    /* register the xml so that bus_ibus_impl_service_method_call will be
275
 
     * called on a method call defined in the xml (e.g. 'GetAddress'.) */
276
 
    ibus_service_class_add_interfaces (IBUS_SERVICE_CLASS (class),
277
 
                                       introspection_xml);
278
 
}
279
 
 
280
 
/**
281
 
 * _panel_destroy_cb:
282
 
 *
283
 
 * A callback function which is called when (1) the connection to the panel process is terminated,
284
 
 * or (2) ibus_proxy_destroy (ibus->panel); is called. See src/ibusproxy.c for details.
285
 
 */
286
 
static void
287
 
_panel_destroy_cb (BusPanelProxy *panel,
288
 
                   BusIBusImpl   *ibus)
289
 
{
290
 
    g_assert (BUS_IS_PANEL_PROXY (panel));
291
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
292
 
 
293
 
    g_return_if_fail (ibus->panel == panel);
294
 
 
295
 
    ibus->panel = NULL;
296
 
    g_object_unref (panel);
297
 
}
298
 
 
299
 
static void
300
 
_registry_changed_cb (IBusRegistry *registry,
301
 
                      BusIBusImpl  *ibus)
302
 
{
303
 
    bus_ibus_impl_registry_changed (ibus);
304
 
}
305
 
 
306
 
/*
307
 
 * _dbus_name_owner_changed_cb:
308
 
 *
309
 
 * A callback function to be called when the name-owner-changed signal is sent to the dbus object.
310
 
 * This usually means a client (e.g. a panel/config/engine process or an application) is connected/disconnected to/from the bus.
311
 
 */
312
 
static void
313
 
_dbus_name_owner_changed_cb (BusDBusImpl   *dbus,
314
 
                             BusConnection *orig_connection,
315
 
                             const gchar   *name,
316
 
                             const gchar   *old_name,
317
 
                             const gchar   *new_name,
318
 
                             BusIBusImpl   *ibus)
319
 
{
320
 
    g_assert (BUS_IS_DBUS_IMPL (dbus));
321
 
    g_assert (name != NULL);
322
 
    g_assert (old_name != NULL);
323
 
    g_assert (new_name != NULL);
324
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
325
 
 
326
 
    if (g_strcmp0 (name, IBUS_SERVICE_PANEL) == 0) {
327
 
        if (g_strcmp0 (new_name, "") != 0) {
328
 
            /* a Panel process is started. */
329
 
            BusConnection *connection;
330
 
            BusInputContext *context = NULL;
331
 
 
332
 
            if (ibus->panel != NULL) {
333
 
                ibus_proxy_destroy ((IBusProxy *) ibus->panel);
334
 
                /* panel should be NULL after destroy. See _panel_destroy_cb for details. */
335
 
                g_assert (ibus->panel == NULL);
336
 
            }
337
 
 
338
 
            connection = bus_dbus_impl_get_connection_by_name (BUS_DEFAULT_DBUS, new_name);
339
 
            g_return_if_fail (connection != NULL);
340
 
 
341
 
            ibus->panel = bus_panel_proxy_new (connection);
342
 
 
343
 
            g_signal_connect (ibus->panel,
344
 
                              "destroy",
345
 
                              G_CALLBACK (_panel_destroy_cb),
346
 
                              ibus);
347
 
 
348
 
            if (ibus->focused_context != NULL) {
349
 
                context = ibus->focused_context;
350
 
            }
351
 
            else if (ibus->use_global_engine) {
352
 
                context = ibus->fake_context;
353
 
            }
354
 
 
355
 
            if (context != NULL) {
356
 
                BusEngineProxy *engine;
357
 
 
358
 
                bus_panel_proxy_focus_in (ibus->panel, context);
359
 
 
360
 
                engine = bus_input_context_get_engine (context);
361
 
                if (engine != NULL) {
362
 
                    IBusPropList *prop_list =
363
 
                        bus_engine_proxy_get_properties (engine);
364
 
                    bus_panel_proxy_register_properties (ibus->panel,
365
 
                                                         prop_list);
366
 
                }
367
 
            }
368
 
        }
369
 
    }
370
 
 
371
 
    bus_ibus_impl_component_name_owner_changed (ibus, name, old_name, new_name);
372
 
}
373
 
 
374
 
/**
375
 
 * bus_ibus_impl_init:
376
 
 *
377
 
 * The constructor of BusIBusImpl. Initialize all member variables of a BusIBusImpl object.
378
 
 */
379
 
static void
380
 
bus_ibus_impl_init (BusIBusImpl *ibus)
381
 
{
382
 
    ibus->factory_dict = g_hash_table_new_full (
383
 
                            g_str_hash,
384
 
                            g_str_equal,
385
 
                            NULL,
386
 
                            (GDestroyNotify) g_object_unref);
387
 
 
388
 
    ibus->fake_context = bus_input_context_new (NULL, "fake");
389
 
    g_object_ref_sink (ibus->fake_context);
390
 
    bus_dbus_impl_register_object (BUS_DEFAULT_DBUS,
391
 
                                   (IBusService *) ibus->fake_context);
392
 
    bus_input_context_set_capabilities (ibus->fake_context,
393
 
                                        IBUS_CAP_PREEDIT_TEXT |
394
 
                                        IBUS_CAP_FOCUS |
395
 
                                        IBUS_CAP_SURROUNDING_TEXT);
396
 
    g_signal_connect (ibus->fake_context,
397
 
                      "engine-changed",
398
 
                      G_CALLBACK (_context_engine_changed_cb),
399
 
                      ibus);
400
 
    bus_input_context_focus_in (ibus->fake_context);
401
 
 
402
 
    ibus->register_engine_list = NULL;
403
 
    ibus->contexts = NULL;
404
 
    ibus->focused_context = NULL;
405
 
    ibus->panel = NULL;
406
 
 
407
 
    ibus->keymap = ibus_keymap_get ("us");
408
 
 
409
 
    ibus->use_sys_layout = TRUE;
410
 
    ibus->embed_preedit_text = TRUE;
411
 
    ibus->use_global_engine = TRUE;
412
 
    ibus->global_engine_name = NULL;
413
 
    ibus->global_previous_engine_name = NULL;
414
 
 
415
 
    /* focus the fake_context, if use_global_engine is enabled. */
416
 
    if (ibus->use_global_engine)
417
 
        bus_ibus_impl_set_focused_context (ibus, ibus->fake_context);
418
 
 
419
 
    g_signal_connect (BUS_DEFAULT_DBUS,
420
 
                      "name-owner-changed",
421
 
                      G_CALLBACK (_dbus_name_owner_changed_cb),
422
 
                      ibus);
423
 
 
424
 
    bus_ibus_impl_registry_init (ibus);
425
 
}
426
 
 
427
 
/**
428
 
 * bus_ibus_impl_destroy:
429
 
 *
430
 
 * The destructor of BusIBusImpl.
431
 
 */
432
 
static void
433
 
bus_ibus_impl_destroy (BusIBusImpl *ibus)
434
 
{
435
 
    pid_t pid;
436
 
    glong timeout;
437
 
    gint status;
438
 
    gboolean flag;
439
 
 
440
 
    g_list_foreach (ibus->components, (GFunc) bus_component_stop, NULL);
441
 
 
442
 
    pid = 0;
443
 
    timeout = 0;
444
 
    flag = FALSE;
445
 
    while (1) {
446
 
        while ((pid = waitpid (0, &status, WNOHANG)) > 0);
447
 
 
448
 
        if (pid == -1) { /* all children finished */
449
 
            break;
450
 
        }
451
 
        if (pid == 0) { /* no child status changed */
452
 
            g_usleep (1000);
453
 
            timeout += 1000;
454
 
            if (timeout >= G_USEC_PER_SEC) {
455
 
                if (flag == FALSE) {
456
 
                    gpointer old;
457
 
                    old = signal (SIGTERM, SIG_IGN);
458
 
                    /* send TERM signal to the whole process group (i.e. engines, panel, and config daemon.) */
459
 
                    kill (-getpid (), SIGTERM);
460
 
                    signal (SIGTERM, old);
461
 
                    flag = TRUE;
462
 
                }
463
 
                else {
464
 
                    g_warning ("Not every child processes exited!");
465
 
                    break;
466
 
                }
467
 
            }
468
 
        }
469
 
    }
470
 
 
471
 
    g_list_free_full (ibus->register_engine_list, g_object_unref);
472
 
    ibus->register_engine_list = NULL;
473
 
 
474
 
    if (ibus->factory_dict != NULL) {
475
 
        g_hash_table_destroy (ibus->factory_dict);
476
 
        ibus->factory_dict = NULL;
477
 
    }
478
 
 
479
 
    if (ibus->keymap != NULL) {
480
 
        g_object_unref (ibus->keymap);
481
 
        ibus->keymap = NULL;
482
 
    }
483
 
 
484
 
    g_free (ibus->global_engine_name);
485
 
    ibus->global_engine_name = NULL;
486
 
 
487
 
    g_free (ibus->global_previous_engine_name);
488
 
    ibus->global_previous_engine_name = NULL;
489
 
 
490
 
    if (ibus->fake_context) {
491
 
        g_object_unref (ibus->fake_context);
492
 
        ibus->fake_context = NULL;
493
 
    }
494
 
 
495
 
    bus_ibus_impl_registry_destroy (ibus);
496
 
 
497
 
    IBUS_OBJECT_CLASS (bus_ibus_impl_parent_class)->destroy (IBUS_OBJECT (ibus));
498
 
}
499
 
 
500
 
/**
501
 
 * _ibus_get_address:
502
 
 *
503
 
 * Implement the "Address" method call of the org.freedesktop.IBus interface.
504
 
 */
505
 
static GVariant *
506
 
_ibus_get_address (BusIBusImpl     *ibus,
507
 
                   GDBusConnection *connection,
508
 
                   GError         **error)
509
 
{
510
 
    if (error) {
511
 
        *error = NULL;
512
 
    }
513
 
 
514
 
    return g_variant_new_string (bus_server_get_address ());
515
 
}
516
 
 
517
 
static void
518
 
_ibus_get_address_depre (BusIBusImpl           *ibus,
519
 
                         GVariant              *parameters,
520
 
                         GDBusMethodInvocation *invocation)
521
 
{
522
 
    GDBusConnection *connection =
523
 
            g_dbus_method_invocation_get_connection (invocation);
524
 
    GError *error = NULL;
525
 
    GVariant *variant = NULL;
526
 
 
527
 
    g_warning ("org.freedesktop.IBus.GetAddress() is deprecated!");
528
 
 
529
 
    variant = _ibus_get_address (ibus, connection, &error);
530
 
 
531
 
    g_dbus_method_invocation_return_value (invocation,
532
 
            g_variant_new ("(s)", g_variant_get_string (variant, NULL)));
533
 
 
534
 
    g_variant_unref (variant);
535
 
}
536
 
 
537
 
static IBusEngineDesc *
538
 
_find_engine_desc_by_name (BusIBusImpl *ibus,
539
 
                           const gchar *engine_name)
540
 
{
541
 
    IBusEngineDesc *desc = NULL;
542
 
    GList *p;
543
 
 
544
 
    /* find engine in registered engine list */
545
 
    for (p = ibus->register_engine_list; p != NULL; p = p->next) {
546
 
        desc = (IBusEngineDesc *) p->data;
547
 
        if (g_strcmp0 (ibus_engine_desc_get_name (desc), engine_name) == 0)
548
 
            return desc;
549
 
    }
550
 
 
551
 
    return NULL;
552
 
}
553
 
 
554
 
/**
555
 
 * _context_request_engine_cb:
556
 
 *
557
 
 * A callback function to be called when the "request-engine" signal is sent to the context.
558
 
 */
559
 
static IBusEngineDesc *
560
 
_context_request_engine_cb (BusInputContext *context,
561
 
                            const gchar     *engine_name,
562
 
                            BusIBusImpl     *ibus)
563
 
{
564
 
    if (engine_name == NULL || engine_name[0] == '\0')
565
 
        engine_name = DEFAULT_ENGINE;
566
 
 
567
 
    return bus_ibus_impl_get_engine_desc (ibus, engine_name);
568
 
}
569
 
 
570
 
/**
571
 
 * bus_ibus_impl_get_engine_desc:
572
 
 *
573
 
 * Get the IBusEngineDesc by engine_name. If the engine_name is NULL, return
574
 
 * a default engine desc.
575
 
 */
576
 
static IBusEngineDesc *
577
 
bus_ibus_impl_get_engine_desc (BusIBusImpl *ibus,
578
 
                               const gchar *engine_name)
579
 
{
580
 
    g_return_val_if_fail (engine_name != NULL, NULL);
581
 
    g_return_val_if_fail (engine_name[0] != '\0', NULL);
582
 
 
583
 
    IBusEngineDesc *desc = _find_engine_desc_by_name (ibus, engine_name);
584
 
    if (desc == NULL) {
585
 
        desc = (IBusEngineDesc *) g_hash_table_lookup (ibus->engine_table,
586
 
                                                       engine_name);
587
 
    }
588
 
    return desc;
589
 
}
590
 
 
591
 
static void
592
 
bus_ibus_impl_set_context_engine_from_desc (BusIBusImpl     *ibus,
593
 
                                            BusInputContext *context,
594
 
                                            IBusEngineDesc  *desc)
595
 
{
596
 
    bus_input_context_set_engine_by_desc (context,
597
 
                                          desc,
598
 
                                          g_gdbus_timeout, /* timeout in msec. */
599
 
                                          NULL, /* we do not cancel the call. */
600
 
                                          NULL, /* use the default callback function. */
601
 
                                          NULL);
602
 
}
603
 
 
604
 
/**
605
 
 * bus_ibus_impl_set_focused_context:
606
 
 *
607
 
 * Set the current focused context.
608
 
 */
609
 
static void
610
 
bus_ibus_impl_set_focused_context (BusIBusImpl     *ibus,
611
 
                                   BusInputContext *context)
612
 
{
613
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
614
 
    g_assert (context == NULL || BUS_IS_INPUT_CONTEXT (context));
615
 
    g_assert (context == NULL || bus_input_context_get_capabilities (context) & IBUS_CAP_FOCUS);
616
 
 
617
 
    /* Do noting if it is focused context. */
618
 
    if (ibus->focused_context == context) {
619
 
        return;
620
 
    }
621
 
 
622
 
    BusEngineProxy *engine = NULL;
623
 
 
624
 
    if (ibus->focused_context) {
625
 
        if (ibus->use_global_engine) {
626
 
            /* dettach engine from the focused context */
627
 
            engine = bus_input_context_get_engine (ibus->focused_context);
628
 
            if (engine) {
629
 
                g_object_ref (engine);
630
 
                bus_input_context_set_engine (ibus->focused_context, NULL);
631
 
            }
632
 
        }
633
 
 
634
 
        if (ibus->panel != NULL)
635
 
            bus_panel_proxy_focus_out (ibus->panel, ibus->focused_context);
636
 
 
637
 
        g_object_unref (ibus->focused_context);
638
 
        ibus->focused_context = NULL;
639
 
    }
640
 
 
641
 
    if (context == NULL && ibus->use_global_engine) {
642
 
        context = ibus->fake_context;
643
 
    }
644
 
 
645
 
    if (context) {
646
 
        ibus->focused_context = (BusInputContext *) g_object_ref (context);
647
 
        /* attach engine to the focused context */
648
 
        if (engine != NULL) {
649
 
            bus_input_context_set_engine (context, engine);
650
 
            bus_input_context_enable (context);
651
 
        }
652
 
 
653
 
        if (ibus->panel != NULL)
654
 
            bus_panel_proxy_focus_in (ibus->panel, context);
655
 
    }
656
 
 
657
 
    if (engine != NULL)
658
 
        g_object_unref (engine);
659
 
}
660
 
 
661
 
static void
662
 
bus_ibus_impl_set_global_engine (BusIBusImpl    *ibus,
663
 
                                 BusEngineProxy *engine)
664
 
{
665
 
    if (!ibus->use_global_engine)
666
 
        return;
667
 
 
668
 
    if (ibus->focused_context) {
669
 
        bus_input_context_set_engine (ibus->focused_context, engine);
670
 
    } else if (ibus->fake_context) {
671
 
        bus_input_context_set_engine (ibus->fake_context, engine);
672
 
    }
673
 
}
674
 
 
675
 
static void
676
 
bus_ibus_impl_set_global_engine_by_name (BusIBusImpl *ibus,
677
 
                                         const gchar *name)
678
 
{
679
 
    if (!ibus->use_global_engine)
680
 
        return;
681
 
 
682
 
    BusInputContext *context =
683
 
            ibus->focused_context != NULL ? ibus->focused_context : ibus->fake_context;
684
 
 
685
 
    if (context == NULL) {
686
 
        return;
687
 
    }
688
 
 
689
 
    if (g_strcmp0 (name, ibus->global_engine_name) == 0) {
690
 
        /* If the user requested the same global engine, then we just enable the
691
 
         * original one. */
692
 
        bus_input_context_enable (context);
693
 
        return;
694
 
    }
695
 
 
696
 
    /* If there is a focused input context, then we just change the engine of
697
 
     * the focused context, which will then change the global engine
698
 
     * automatically. Otherwise, we need to change the global engine directly.
699
 
     */
700
 
    IBusEngineDesc *desc = NULL;
701
 
    desc = bus_ibus_impl_get_engine_desc (ibus, name);
702
 
    if (desc != NULL) {
703
 
        bus_ibus_impl_set_context_engine_from_desc (ibus,
704
 
                                                    context,
705
 
                                                    desc);
706
 
    }
707
 
}
708
 
 
709
 
/* When preload_engines and register_engiens are changed, this function
710
 
 * will check the global engine. If necessay, it will change the global engine.
711
 
 */
712
 
static void
713
 
bus_ibus_impl_check_global_engine (BusIBusImpl *ibus)
714
 
{
715
 
    GList *engine_list = NULL;
716
 
 
717
 
    /* do nothing */
718
 
    if (!ibus->use_global_engine)
719
 
        return;
720
 
 
721
 
    /* The current global engine is not removed, so do nothing. */
722
 
    if (ibus->global_engine_name != NULL &&
723
 
        _find_engine_desc_by_name (ibus, ibus->global_engine_name)) {
724
 
        return;
725
 
    }
726
 
 
727
 
    /* If the previous engine is available, then just switch to it. */
728
 
    if (ibus->global_previous_engine_name != NULL &&
729
 
        _find_engine_desc_by_name (ibus, ibus->global_previous_engine_name)) {
730
 
        bus_ibus_impl_set_global_engine_by_name (
731
 
            ibus, ibus->global_previous_engine_name);
732
 
        return;
733
 
    }
734
 
 
735
 
    /* Just switch to the fist engine in the list. */
736
 
    engine_list = ibus->register_engine_list;
737
 
 
738
 
    if (engine_list) {
739
 
        IBusEngineDesc *engine_desc = (IBusEngineDesc *)engine_list->data;
740
 
        bus_ibus_impl_set_global_engine_by_name (ibus,
741
 
                        ibus_engine_desc_get_name (engine_desc));
742
 
        return;
743
 
    }
744
 
 
745
 
    /* No engine available? Just disable global engine. */
746
 
    bus_ibus_impl_set_global_engine (ibus, NULL);
747
 
}
748
 
 
749
 
/**
750
 
 * _context_engine_changed_cb:
751
 
 *
752
 
 * A callback function to be called when the "engine-changed" signal is sent to the context.
753
 
 * Update global engine as well if necessary.
754
 
 */
755
 
static void
756
 
_context_engine_changed_cb (BusInputContext *context,
757
 
                            BusIBusImpl     *ibus)
758
 
{
759
 
    if (!ibus->use_global_engine)
760
 
        return;
761
 
 
762
 
    if ((context == ibus->focused_context) ||
763
 
        (ibus->focused_context == NULL && context == ibus->fake_context)) {
764
 
        BusEngineProxy *engine = bus_input_context_get_engine (context);
765
 
        if (engine != NULL) {
766
 
            /* only set global engine if engine is not NULL */
767
 
            const gchar *name = ibus_engine_desc_get_name (bus_engine_proxy_get_desc (engine));
768
 
            if (g_strcmp0 (name, ibus->global_engine_name) == 0)
769
 
                return;
770
 
            g_free (ibus->global_previous_engine_name);
771
 
            ibus->global_previous_engine_name = ibus->global_engine_name;
772
 
            ibus->global_engine_name = g_strdup (name);
773
 
            bus_ibus_impl_global_engine_changed (ibus);
774
 
        }
775
 
    }
776
 
}
777
 
 
778
 
/**
779
 
 * _context_focus_in_cb:
780
 
 *
781
 
 * A callback function to be called when the "focus-in" signal is sent to the context.
782
 
 * If necessary, enables the global engine on the context and update ibus->focused_context.
783
 
 */
784
 
static void
785
 
_context_focus_in_cb (BusInputContext *context,
786
 
                      BusIBusImpl     *ibus)
787
 
{
788
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
789
 
    g_assert (BUS_IS_INPUT_CONTEXT (context));
790
 
 
791
 
    /* Do nothing if context does not support focus.
792
 
     * The global engine shoule be detached from the focused context. */
793
 
    if ((bus_input_context_get_capabilities (context) & IBUS_CAP_FOCUS) == 0) {
794
 
        return;
795
 
    }
796
 
 
797
 
    bus_ibus_impl_set_focused_context (ibus, context);
798
 
}
799
 
 
800
 
/**
801
 
 * _context_focus_out_cb:
802
 
 *
803
 
 * A callback function to be called when the "focus-out" signal is sent to the context.
804
 
 */
805
 
static void
806
 
_context_focus_out_cb (BusInputContext    *context,
807
 
                       BusIBusImpl        *ibus)
808
 
{
809
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
810
 
    g_assert (BUS_IS_INPUT_CONTEXT (context));
811
 
 
812
 
    /* Do noting if context does not support focus.
813
 
     * Actually, the context should emit focus signals, if it does not support focus */
814
 
    if ((bus_input_context_get_capabilities (context) & IBUS_CAP_FOCUS) == 0) {
815
 
        return;
816
 
    }
817
 
 
818
 
    /* Do noting if it is not focused context. */
819
 
    if (ibus->focused_context != context) {
820
 
        return;
821
 
    }
822
 
 
823
 
    bus_ibus_impl_set_focused_context (ibus, NULL);
824
 
}
825
 
 
826
 
/**
827
 
 * _context_destroy_cb:
828
 
 *
829
 
 * A callback function to be called when the "destroy" signal is sent to the
830
 
 * context.
831
 
 */
832
 
static void
833
 
_context_destroy_cb (BusInputContext    *context,
834
 
                     BusIBusImpl        *ibus)
835
 
{
836
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
837
 
    g_assert (BUS_IS_INPUT_CONTEXT (context));
838
 
 
839
 
    if (context == ibus->focused_context)
840
 
        bus_ibus_impl_set_focused_context (ibus, NULL);
841
 
 
842
 
    if (ibus->panel &&
843
 
        bus_input_context_get_capabilities (context) & IBUS_CAP_FOCUS)
844
 
        bus_panel_proxy_destroy_context (ibus->panel, context);
845
 
 
846
 
    ibus->contexts = g_list_remove (ibus->contexts, context);
847
 
    g_object_unref (context);
848
 
}
849
 
 
850
 
/**
851
 
 * bus_ibus_impl_create_input_context:
852
 
 * @client: A name of a client. e.g. "gtk-im"
853
 
 * @returns: A BusInputContext object.
854
 
 *
855
 
 * Create a new BusInputContext object for the client.
856
 
 */
857
 
static BusInputContext *
858
 
bus_ibus_impl_create_input_context (BusIBusImpl   *ibus,
859
 
                                    BusConnection *connection,
860
 
                                    const gchar   *client)
861
 
{
862
 
    BusInputContext *context = bus_input_context_new (connection, client);
863
 
    g_object_ref_sink (context);
864
 
    ibus->contexts = g_list_append (ibus->contexts, context);
865
 
 
866
 
    /* Installs glib signal handlers so that the ibus object could be notified when e.g. an IBus.InputContext D-Bus method is called. */
867
 
    static const struct {
868
 
        gchar *name;
869
 
        GCallback callback;
870
 
    } signals [] = {
871
 
        { "request-engine", G_CALLBACK (_context_request_engine_cb) },
872
 
        { "engine-changed", G_CALLBACK (_context_engine_changed_cb) },
873
 
        { "focus-in",       G_CALLBACK (_context_focus_in_cb) },
874
 
        { "focus-out",      G_CALLBACK (_context_focus_out_cb) },
875
 
        { "destroy",        G_CALLBACK (_context_destroy_cb) },
876
 
    };
877
 
 
878
 
    gint i;
879
 
    for (i = 0; i < G_N_ELEMENTS (signals); i++) {
880
 
        g_signal_connect (context,
881
 
                          signals[i].name,
882
 
                          signals[i].callback,
883
 
                          ibus);
884
 
    }
885
 
 
886
 
    bus_input_context_enable (context);
887
 
 
888
 
    /* register the context object so that the object could handle IBus.InputContext method calls. */
889
 
    bus_dbus_impl_register_object (BUS_DEFAULT_DBUS,
890
 
                                   (IBusService *) context);
891
 
    g_object_ref (context);
892
 
    return context;
893
 
}
894
 
 
895
 
/**
896
 
 * _ibus_create_input_context:
897
 
 *
898
 
 * Implement the "CreateInputContext" method call of the org.freedesktop.IBus interface.
899
 
 */
900
 
static void
901
 
_ibus_create_input_context (BusIBusImpl           *ibus,
902
 
                            GVariant              *parameters,
903
 
                            GDBusMethodInvocation *invocation)
904
 
{
905
 
    const gchar *client_name = NULL;  // e.g. "gtk-im"
906
 
    g_variant_get (parameters, "(&s)", &client_name);
907
 
 
908
 
    BusConnection *connection =
909
 
            bus_connection_lookup (g_dbus_method_invocation_get_connection (invocation));
910
 
    BusInputContext *context =
911
 
            bus_ibus_impl_create_input_context (ibus,
912
 
                                                connection,
913
 
                                                client_name);
914
 
    if (context) {
915
 
        const gchar *path = ibus_service_get_object_path ((IBusService *) context);
916
 
        /* the format-string 'o' is for a D-Bus object path. */
917
 
        g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", path));
918
 
        g_object_unref (context);
919
 
    }
920
 
    else {
921
 
        g_dbus_method_invocation_return_error (invocation,
922
 
                                               G_DBUS_ERROR,
923
 
                                               G_DBUS_ERROR_FAILED,
924
 
                                               "Create input context failed!");
925
 
    }
926
 
}
927
 
 
928
 
/**
929
 
 * _ibus_get_current_input_context:
930
 
 *
931
 
 * Implement the "CurrentInputContext" method call of the
932
 
 * org.freedesktop.IBus interface.
933
 
 */
934
 
static GVariant *
935
 
_ibus_get_current_input_context (BusIBusImpl     *ibus,
936
 
                                 GDBusConnection *connection,
937
 
                                 GError         **error)
938
 
{
939
 
    if (error) {
940
 
        *error = NULL;
941
 
    }
942
 
 
943
 
    if (!ibus->focused_context)
944
 
    {
945
 
        g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
946
 
                     "No focused input context");
947
 
        return NULL;
948
 
    }
949
 
    else {
950
 
        const gchar *path = ibus_service_get_object_path (
951
 
                (IBusService *) ibus->focused_context);
952
 
        /* the format-string 'o' is for a D-Bus object path. */
953
 
        return g_variant_new_object_path (path);
954
 
    }
955
 
}
956
 
 
957
 
static void
958
 
_ibus_current_input_context_depre (BusIBusImpl           *ibus,
959
 
                                   GVariant              *parameters,
960
 
                                   GDBusMethodInvocation *invocation)
961
 
{
962
 
    GDBusConnection *connection =
963
 
            g_dbus_method_invocation_get_connection (invocation);
964
 
    GVariant *variant = NULL;
965
 
    GError *error = NULL;
966
 
 
967
 
    g_warning ("org.freedesktop.IBus.CurrentInputContext() is deprecated!");
968
 
 
969
 
    variant = _ibus_get_current_input_context (ibus, connection, &error);
970
 
 
971
 
    if (variant == NULL) {
972
 
        g_dbus_method_invocation_return_error (
973
 
                invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
974
 
                "%s", error->message);
975
 
        g_error_free (error);
976
 
    } else {
977
 
        const gchar *path = g_variant_get_string (variant, NULL);
978
 
        g_dbus_method_invocation_return_value (invocation,
979
 
                                               g_variant_new ("(o)", path));
980
 
        g_variant_unref (variant);
981
 
    }
982
 
}
983
 
 
984
 
static void
985
 
_component_destroy_cb (BusComponent *component,
986
 
                       BusIBusImpl  *ibus)
987
 
{
988
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
989
 
    g_assert (BUS_IS_COMPONENT (component));
990
 
 
991
 
    ibus->registered_components = g_list_remove (ibus->registered_components, component);
992
 
 
993
 
    /* remove engines from engine_list */
994
 
    GList *engines = bus_component_get_engines (component);
995
 
    GList *p;
996
 
    for (p = engines; p != NULL; p = p->next) {
997
 
        if (g_list_find (ibus->register_engine_list, p->data)) {
998
 
            ibus->register_engine_list = g_list_remove (ibus->register_engine_list, p->data);
999
 
            g_object_unref (p->data);
1000
 
        }
1001
 
    }
1002
 
    g_list_free (engines);
1003
 
 
1004
 
    g_object_unref (component);
1005
 
 
1006
 
    bus_ibus_impl_check_global_engine (ibus);
1007
 
}
1008
 
 
1009
 
/**
1010
 
 * _ibus_register_component:
1011
 
 *
1012
 
 * Implement the "RegisterComponent" method call of the
1013
 
 * org.freedesktop.IBus interface.
1014
 
 */
1015
 
static void
1016
 
_ibus_register_component (BusIBusImpl           *ibus,
1017
 
                          GVariant              *parameters,
1018
 
                          GDBusMethodInvocation *invocation)
1019
 
{
1020
 
    GVariant *variant = g_variant_get_child_value (parameters, 0);
1021
 
    IBusComponent *component =
1022
 
            (IBusComponent *) ibus_serializable_deserialize (variant);
1023
 
 
1024
 
    if (!IBUS_IS_COMPONENT (component)) {
1025
 
        if (component)
1026
 
            g_object_unref (component);
1027
 
        g_dbus_method_invocation_return_error (
1028
 
                invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
1029
 
                "The first argument should be an IBusComponent.");
1030
 
        return;
1031
 
    }
1032
 
 
1033
 
    BusConnection *connection = bus_connection_lookup (
1034
 
            g_dbus_method_invocation_get_connection (invocation));
1035
 
    BusFactoryProxy *factory = bus_factory_proxy_new (connection);
1036
 
 
1037
 
    if (factory == NULL) {
1038
 
        g_object_unref (component);
1039
 
        g_dbus_method_invocation_return_error (
1040
 
                invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
1041
 
                "Create factory failed.");
1042
 
        return;
1043
 
    }
1044
 
 
1045
 
    g_object_ref_sink (component);
1046
 
 
1047
 
    BusComponent *buscomp = bus_component_new (component, factory);
1048
 
    bus_component_set_destroy_with_factory (buscomp, TRUE);
1049
 
    g_object_unref (component);
1050
 
    g_object_unref (factory);
1051
 
 
1052
 
    ibus->registered_components = g_list_append (ibus->registered_components,
1053
 
                                                g_object_ref_sink (buscomp));
1054
 
    GList *engines = bus_component_get_engines (buscomp);
1055
 
    g_list_foreach (engines, (GFunc) g_object_ref, NULL);
1056
 
    ibus->register_engine_list = g_list_concat (ibus->register_engine_list,
1057
 
                                               engines);
1058
 
 
1059
 
    g_signal_connect (buscomp,
1060
 
                      "destroy",
1061
 
                      G_CALLBACK (_component_destroy_cb),
1062
 
                      ibus);
1063
 
 
1064
 
    g_dbus_method_invocation_return_value (invocation, NULL);
1065
 
}
1066
 
 
1067
 
/**
1068
 
 * _ibus_get_engines:
1069
 
 *
1070
 
 * Implement the "Engines" method call of the org.freedesktop.IBus interface.
1071
 
 */
1072
 
static GVariant *
1073
 
_ibus_get_engines (BusIBusImpl     *ibus,
1074
 
                   GDBusConnection *connection,
1075
 
                   GError         **error)
1076
 
{
1077
 
    GVariantBuilder builder;
1078
 
    GList *engines = NULL;
1079
 
    GList *p;
1080
 
 
1081
 
    if (error) {
1082
 
        *error = NULL;
1083
 
    }
1084
 
 
1085
 
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
1086
 
 
1087
 
    engines = g_hash_table_get_values (ibus->engine_table);
1088
 
 
1089
 
    for (p = engines; p != NULL; p = p->next) {
1090
 
        g_variant_builder_add (
1091
 
                &builder, "v",
1092
 
                ibus_serializable_serialize ((IBusSerializable *) p->data));
1093
 
    }
1094
 
 
1095
 
    g_list_free (engines);
1096
 
 
1097
 
    return g_variant_builder_end (&builder);
1098
 
}
1099
 
 
1100
 
static void
1101
 
_ibus_list_engines_depre (BusIBusImpl           *ibus,
1102
 
                          GVariant              *parameters,
1103
 
                          GDBusMethodInvocation *invocation)
1104
 
{
1105
 
    GDBusConnection *connection =
1106
 
            g_dbus_method_invocation_get_connection (invocation);
1107
 
    GError *error = NULL;
1108
 
    GVariant *variant = NULL;
1109
 
 
1110
 
    g_warning ("org.freedesktop.IBus.ListEngines() is deprecated!");
1111
 
 
1112
 
    variant = _ibus_get_engines (ibus, connection, &error);
1113
 
 
1114
 
    g_dbus_method_invocation_return_value (invocation,
1115
 
                                           g_variant_new ("(@av)", variant));
1116
 
}
1117
 
 
1118
 
/**
1119
 
 * _ibus_get_engines_by_names:
1120
 
 *
1121
 
 * Implement the "GetEnginesByNames" method call of the org.freedesktop.IBus interface.
1122
 
 */
1123
 
static void
1124
 
_ibus_get_engines_by_names (BusIBusImpl           *ibus,
1125
 
                            GVariant              *parameters,
1126
 
                            GDBusMethodInvocation *invocation)
1127
 
{
1128
 
    const gchar **names = NULL;
1129
 
    g_variant_get (parameters, "(^a&s)", &names);
1130
 
 
1131
 
    g_assert (names != NULL);
1132
 
 
1133
 
    gint i = 0;
1134
 
    GVariantBuilder builder;
1135
 
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
1136
 
    while (names[i] != NULL) {
1137
 
        IBusEngineDesc *desc = (IBusEngineDesc *) g_hash_table_lookup (
1138
 
                ibus->engine_table, names[i++]);
1139
 
        if (desc == NULL)
1140
 
            continue;
1141
 
        g_variant_builder_add (
1142
 
                &builder,
1143
 
                "v",
1144
 
                ibus_serializable_serialize ((IBusSerializable *)desc));
1145
 
    }
1146
 
    g_dbus_method_invocation_return_value (invocation, g_variant_new ("(av)", &builder));
1147
 
}
1148
 
 
1149
 
/**
1150
 
 * _ibus_get_active_engines:
1151
 
 *
1152
 
 * Implement the "ActiveEngines" method call of the
1153
 
 * org.freedesktop.IBus interface.
1154
 
 */
1155
 
static GVariant *
1156
 
_ibus_get_active_engines (BusIBusImpl     *ibus,
1157
 
                          GDBusConnection *connection,
1158
 
                          GError         **error)
1159
 
{
1160
 
    GVariantBuilder builder;
1161
 
    GList *p;
1162
 
 
1163
 
    if (error) {
1164
 
        *error = NULL;
1165
 
    }
1166
 
 
1167
 
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
1168
 
 
1169
 
    for (p = ibus->register_engine_list; p != NULL; p = p->next) {
1170
 
        g_variant_builder_add (
1171
 
                &builder, "v",
1172
 
                ibus_serializable_serialize ((IBusSerializable *) p->data));
1173
 
    }
1174
 
 
1175
 
    return g_variant_builder_end (&builder);
1176
 
}
1177
 
 
1178
 
static void
1179
 
_ibus_list_active_engines_depre (BusIBusImpl           *ibus,
1180
 
                                 GVariant              *parameters,
1181
 
                                 GDBusMethodInvocation *invocation)
1182
 
{
1183
 
    GDBusConnection *connection =
1184
 
            g_dbus_method_invocation_get_connection (invocation);
1185
 
    GError *error = NULL;
1186
 
    GVariant *variant = NULL;
1187
 
 
1188
 
    g_warning ("org.freedesktop.IBus.ListActiveEngines() is deprecated!");
1189
 
 
1190
 
    variant = _ibus_get_active_engines (ibus, connection, &error);
1191
 
 
1192
 
    g_dbus_method_invocation_return_value (invocation,
1193
 
                                           g_variant_new ("(@av)", variant));
1194
 
}
1195
 
 
1196
 
/**
1197
 
 * _ibus_exit:
1198
 
 *
1199
 
 * Implement the "Exit" method call of the org.freedesktop.IBus interface.
1200
 
 */
1201
 
static void
1202
 
_ibus_exit (BusIBusImpl           *ibus,
1203
 
            GVariant              *parameters,
1204
 
            GDBusMethodInvocation *invocation)
1205
 
{
1206
 
    gboolean restart = FALSE;
1207
 
    g_variant_get (parameters, "(b)", &restart);
1208
 
 
1209
 
    g_dbus_method_invocation_return_value (invocation, NULL);
1210
 
 
1211
 
    /* Make sure the reply has been sent out before exit */
1212
 
    g_dbus_connection_flush_sync (
1213
 
            g_dbus_method_invocation_get_connection (invocation),
1214
 
            NULL,
1215
 
            NULL);
1216
 
 
1217
 
    bus_server_quit (restart);
1218
 
}
1219
 
 
1220
 
/**
1221
 
 * _ibus_ping:
1222
 
 *
1223
 
 * Implement the "Ping" method call of the org.freedesktop.IBus interface.
1224
 
 */
1225
 
static void
1226
 
_ibus_ping (BusIBusImpl           *ibus,
1227
 
            GVariant              *parameters,
1228
 
            GDBusMethodInvocation *invocation)
1229
 
{
1230
 
    g_dbus_method_invocation_return_value (invocation, parameters);
1231
 
}
1232
 
 
1233
 
/**
1234
 
 * _ibus_get_use_sys_layout:
1235
 
 *
1236
 
 * Implement the "UseSysLayout" method call of the
1237
 
 * org.freedesktop.IBus interface.
1238
 
 */
1239
 
static GVariant *
1240
 
_ibus_get_use_sys_layout (BusIBusImpl     *ibus,
1241
 
                          GDBusConnection *connection,
1242
 
                          GError         **error)
1243
 
{
1244
 
    if (error) {
1245
 
        *error = NULL;
1246
 
    }
1247
 
 
1248
 
    return g_variant_new_boolean (ibus->use_sys_layout);
1249
 
}
1250
 
 
1251
 
static void
1252
 
_ibus_get_use_sys_layout_depre (BusIBusImpl           *ibus,
1253
 
                                GVariant              *parameters,
1254
 
                                GDBusMethodInvocation *invocation)
1255
 
{
1256
 
    GDBusConnection *connection =
1257
 
            g_dbus_method_invocation_get_connection (invocation);
1258
 
    GError *error = NULL;
1259
 
    GVariant *variant = NULL;
1260
 
 
1261
 
    g_warning ("org.freedesktop.IBus.GetUseSysLayout() is deprecated!");
1262
 
 
1263
 
    variant = _ibus_get_use_sys_layout (ibus, connection, &error);
1264
 
 
1265
 
    g_dbus_method_invocation_return_value (invocation,
1266
 
            g_variant_new ("(b)", g_variant_get_boolean (variant)));
1267
 
 
1268
 
    g_variant_unref (variant);
1269
 
}
1270
 
 
1271
 
/**
1272
 
 * _ibus_get_use_global_engine:
1273
 
 *
1274
 
 * Implement the "UseGlobalEngine" method call of the
1275
 
 * org.freedesktop.IBus interface.
1276
 
 */
1277
 
static GVariant *
1278
 
_ibus_get_use_global_engine (BusIBusImpl     *ibus,
1279
 
                             GDBusConnection *connection,
1280
 
                             GError         **error)
1281
 
{
1282
 
    if (error) {
1283
 
        *error = NULL;
1284
 
    }
1285
 
 
1286
 
    return g_variant_new_boolean (ibus->use_global_engine);
1287
 
}
1288
 
 
1289
 
static void
1290
 
_ibus_get_use_global_engine_depre (BusIBusImpl           *ibus,
1291
 
                                   GVariant              *parameters,
1292
 
                                   GDBusMethodInvocation *invocation)
1293
 
{
1294
 
    GDBusConnection *connection =
1295
 
            g_dbus_method_invocation_get_connection (invocation);
1296
 
    GError *error = NULL;
1297
 
    GVariant *variant = NULL;
1298
 
 
1299
 
    g_warning ("org.freedesktop.IBus.GetUseGlobalEngine() is deprecated!");
1300
 
 
1301
 
    variant = _ibus_get_use_global_engine (ibus, connection, &error);
1302
 
 
1303
 
    g_dbus_method_invocation_return_value (invocation,
1304
 
            g_variant_new ("(b)", g_variant_get_boolean (variant)));
1305
 
 
1306
 
    g_variant_unref (variant);
1307
 
}
1308
 
 
1309
 
/**
1310
 
 * _ibus_get_global_engine:
1311
 
 *
1312
 
 * Implement the "GlobalEngine" method call of the
1313
 
 * org.freedesktop.IBus interface.
1314
 
 */
1315
 
static GVariant *
1316
 
_ibus_get_global_engine (BusIBusImpl     *ibus,
1317
 
                         GDBusConnection *connection,
1318
 
                         GError         **error)
1319
 
{
1320
 
    IBusEngineDesc *desc = NULL;
1321
 
 
1322
 
    if (error) {
1323
 
        *error = NULL;
1324
 
    }
1325
 
 
1326
 
    do {
1327
 
        if (!ibus->use_global_engine)
1328
 
            break;
1329
 
        BusInputContext *context = ibus->focused_context;
1330
 
        if (context == NULL)
1331
 
            context = ibus->fake_context;
1332
 
 
1333
 
        desc = bus_input_context_get_engine_desc (context);
1334
 
 
1335
 
        if (desc == NULL)
1336
 
            break;
1337
 
 
1338
 
        GVariant *variant = ibus_serializable_serialize (
1339
 
                (IBusSerializable *) desc);
1340
 
        // Set type "v" for introspection_xml.
1341
 
        return g_variant_new_variant (variant);
1342
 
    } while (0);
1343
 
 
1344
 
    g_set_error (error,
1345
 
                 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
1346
 
                 "No global engine.");
1347
 
    return NULL;
1348
 
}
1349
 
 
1350
 
static void
1351
 
_ibus_get_global_engine_depre (BusIBusImpl           *ibus,
1352
 
                               GVariant              *parameters,
1353
 
                               GDBusMethodInvocation *invocation)
1354
 
{
1355
 
    GDBusConnection *connection =
1356
 
            g_dbus_method_invocation_get_connection (invocation);
1357
 
    GError *error = NULL;
1358
 
    GVariant *variant = NULL;
1359
 
 
1360
 
    g_warning ("org.freedesktop.IBus.GetGlobalEngine() is deprecated!");
1361
 
 
1362
 
    variant = _ibus_get_global_engine (ibus, connection, &error);
1363
 
 
1364
 
    if (variant == NULL) {
1365
 
        g_dbus_method_invocation_return_error (
1366
 
                invocation,
1367
 
                G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
1368
 
                "%s", error->message);
1369
 
        g_error_free (error);
1370
 
    } else {
1371
 
        GVariant *retval = g_variant_get_variant (variant);
1372
 
        g_dbus_method_invocation_return_value (invocation,
1373
 
                g_variant_new ("(v)", retval));
1374
 
        g_variant_unref (variant);
1375
 
    }
1376
 
}
1377
 
 
1378
 
struct _SetGlobalEngineData {
1379
 
    BusIBusImpl *ibus;
1380
 
    GDBusMethodInvocation *invocation;
1381
 
};
1382
 
typedef struct _SetGlobalEngineData SetGlobalEngineData;
1383
 
 
1384
 
static void
1385
 
_ibus_set_global_engine_ready_cb (BusInputContext       *context,
1386
 
                                  GAsyncResult          *res,
1387
 
                                  SetGlobalEngineData   *data)
1388
 
{
1389
 
    BusIBusImpl *ibus = data->ibus;
1390
 
 
1391
 
    GError *error = NULL;
1392
 
    if (!bus_input_context_set_engine_by_desc_finish (context, res, &error)) {
1393
 
        g_error_free (error);
1394
 
        g_dbus_method_invocation_return_error (data->invocation,
1395
 
                                               G_DBUS_ERROR,
1396
 
                                               G_DBUS_ERROR_FAILED,
1397
 
                                               "Set global engine failed.");
1398
 
    }
1399
 
    else {
1400
 
        g_dbus_method_invocation_return_value (data->invocation, NULL);
1401
 
 
1402
 
        if (ibus->use_global_engine && (context != ibus->focused_context)) {
1403
 
            /* context and ibus->focused_context don't match. This means that
1404
 
             * the focus is moved before _ibus_set_global_engine() asynchronous
1405
 
             * call finishes. In this case, the engine for the context currently
1406
 
             * being focused hasn't been updated. Update the engine here so that
1407
 
             * subsequent _ibus_get_global_engine() call could return a
1408
 
             * consistent engine name. */
1409
 
            BusEngineProxy *engine = bus_input_context_get_engine (context);
1410
 
            if (engine && ibus->focused_context != NULL) {
1411
 
                g_object_ref (engine);
1412
 
                bus_input_context_set_engine (context, NULL);
1413
 
                bus_input_context_set_engine (ibus->focused_context, engine);
1414
 
                g_object_unref (engine);
1415
 
            }
1416
 
        }
1417
 
    }
1418
 
 
1419
 
    g_object_unref (ibus);
1420
 
    g_slice_free (SetGlobalEngineData, data);
1421
 
}
1422
 
 
1423
 
/**
1424
 
 * _ibus_set_global_engine:
1425
 
 *
1426
 
 * Implement the "SetGlobalEngine" method call of the
1427
 
 * org.freedesktop.IBus interface.
1428
 
 */
1429
 
static void
1430
 
_ibus_set_global_engine (BusIBusImpl           *ibus,
1431
 
                         GVariant              *parameters,
1432
 
                         GDBusMethodInvocation *invocation)
1433
 
{
1434
 
    if (!ibus->use_global_engine) {
1435
 
        g_dbus_method_invocation_return_error (invocation,
1436
 
                        G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
1437
 
                        "Global engine feature is disabled.");
1438
 
        return;
1439
 
    }
1440
 
 
1441
 
    BusInputContext *context = ibus->focused_context;
1442
 
    if (context == NULL)
1443
 
        context = ibus->fake_context;
1444
 
 
1445
 
    const gchar *engine_name = NULL;
1446
 
    g_variant_get (parameters, "(&s)", &engine_name);
1447
 
 
1448
 
    IBusEngineDesc *desc = bus_ibus_impl_get_engine_desc(ibus, engine_name);
1449
 
    if (desc == NULL) {
1450
 
        g_dbus_method_invocation_return_error (invocation,
1451
 
                                               G_DBUS_ERROR,
1452
 
                                               G_DBUS_ERROR_FAILED,
1453
 
                                               "Can not find engine %s.",
1454
 
                                               engine_name);
1455
 
        return;
1456
 
    }
1457
 
 
1458
 
    SetGlobalEngineData *data = g_slice_new0 (SetGlobalEngineData);
1459
 
    data->ibus = g_object_ref (ibus);
1460
 
    data->invocation = invocation;
1461
 
    bus_input_context_set_engine_by_desc (context,
1462
 
                                          desc,
1463
 
                                          g_gdbus_timeout, /* timeout in msec. */
1464
 
                                          NULL, /* we do not cancel the call. */
1465
 
                                          (GAsyncReadyCallback) _ibus_set_global_engine_ready_cb,
1466
 
                                          data);
1467
 
}
1468
 
 
1469
 
/**
1470
 
 * _ibus_get_global_engine_enabled:
1471
 
 *
1472
 
 * Implement the "GlobalEngineEnabled" method call of the
1473
 
 * org.freedesktop.IBus interface.
1474
 
 */
1475
 
static GVariant *
1476
 
_ibus_get_global_engine_enabled (BusIBusImpl     *ibus,
1477
 
                                 GDBusConnection *connection,
1478
 
                                 GError         **error)
1479
 
{
1480
 
    gboolean enabled = FALSE;
1481
 
 
1482
 
    if (error) {
1483
 
        *error = NULL;
1484
 
    }
1485
 
 
1486
 
    do {
1487
 
        if (!ibus->use_global_engine)
1488
 
            break;
1489
 
 
1490
 
        BusInputContext *context = ibus->focused_context;
1491
 
        if (context == NULL)
1492
 
            context = ibus->fake_context;
1493
 
        if (context == NULL)
1494
 
            break;
1495
 
 
1496
 
        enabled = TRUE;
1497
 
    } while (0);
1498
 
 
1499
 
    return g_variant_new_boolean (enabled);
1500
 
}
1501
 
 
1502
 
static void
1503
 
_ibus_is_global_engine_enabled_depre (BusIBusImpl           *ibus,
1504
 
                                      GVariant              *parameters,
1505
 
                                      GDBusMethodInvocation *invocation)
1506
 
{
1507
 
    GDBusConnection *connection =
1508
 
            g_dbus_method_invocation_get_connection (invocation);
1509
 
    GError *error = NULL;
1510
 
    GVariant *variant = NULL;
1511
 
 
1512
 
    g_warning ("org.freedesktop.IBus.IsGlobalEngineEnabled() is deprecated!");
1513
 
 
1514
 
    variant = _ibus_get_global_engine_enabled (ibus, connection, &error);
1515
 
 
1516
 
    g_dbus_method_invocation_return_value (invocation,
1517
 
            g_variant_new ("(b)", g_variant_get_boolean (variant)));
1518
 
 
1519
 
    g_variant_unref (variant);
1520
 
}
1521
 
 
1522
 
/**
1523
 
 * _ibus_set_preload_engines:
1524
 
 *
1525
 
 * Implement the "PreloadEngines" method call of the
1526
 
 * org.freedesktop.IBus interface.
1527
 
 */
1528
 
static gboolean
1529
 
_ibus_set_preload_engines (BusIBusImpl     *ibus,
1530
 
                           GDBusConnection *connection,
1531
 
                           GVariant        *value,
1532
 
                           GError         **error)
1533
 
{
1534
 
    int i, j;
1535
 
    const gchar **names = NULL;
1536
 
    IBusEngineDesc *desc = NULL;
1537
 
    BusComponent *component = NULL;
1538
 
    BusFactoryProxy *factory = NULL;
1539
 
    GPtrArray *array = g_ptr_array_new ();
1540
 
 
1541
 
    if (error) {
1542
 
        *error = NULL;
1543
 
    }
1544
 
 
1545
 
    g_variant_get (value, "^a&s", &names);
1546
 
 
1547
 
    for (i = 0; names[i] != NULL; i++) {
1548
 
        gboolean has_component = FALSE;
1549
 
 
1550
 
        desc = bus_ibus_impl_get_engine_desc(ibus, names[i]);
1551
 
 
1552
 
        if (desc == NULL) {
1553
 
            g_set_error (error,
1554
 
                         G_DBUS_ERROR,
1555
 
                         G_DBUS_ERROR_FAILED,
1556
 
                         "Can not find engine %s.",
1557
 
                         names[i]);
1558
 
            g_ptr_array_free (array, FALSE);
1559
 
            return FALSE;
1560
 
        }
1561
 
 
1562
 
        component = bus_component_from_engine_desc (desc);
1563
 
        factory = bus_component_get_factory (component);
1564
 
 
1565
 
        if (factory != NULL) {
1566
 
            continue;
1567
 
        }
1568
 
 
1569
 
        for (j = 0; j < array->len; j++) {
1570
 
            if (component == (BusComponent *) g_ptr_array_index (array, j)) {
1571
 
                has_component = TRUE;
1572
 
                break;
1573
 
            }
1574
 
        }
1575
 
 
1576
 
        if (!has_component) {
1577
 
            g_ptr_array_add (array, component);
1578
 
        }
1579
 
    }
1580
 
 
1581
 
    for (j = 0; j < array->len; j++) {
1582
 
        bus_component_start ((BusComponent *) g_ptr_array_index (array, j),
1583
 
                             g_verbose);
1584
 
    }
1585
 
 
1586
 
    g_ptr_array_free (array, FALSE);
1587
 
 
1588
 
    bus_ibus_impl_property_changed (ibus, "PreloadEngines", value);
1589
 
 
1590
 
    return TRUE;
1591
 
}
1592
 
 
1593
 
/**
1594
 
 * _ibus_get_embed_preedit_text:
1595
 
 *
1596
 
 * Implement the "EmbedPreeditText" method call of
1597
 
 * the org.freedesktop.IBus interface.
1598
 
 */
1599
 
static GVariant *
1600
 
_ibus_get_embed_preedit_text (BusIBusImpl     *ibus,
1601
 
                              GDBusConnection *connection,
1602
 
                              GError         **error)
1603
 
{
1604
 
    if (error) {
1605
 
        *error = NULL;
1606
 
    }
1607
 
 
1608
 
    return g_variant_new_boolean (ibus->embed_preedit_text);
1609
 
}
1610
 
 
1611
 
/**
1612
 
 * _ibus_set_embed_preedit_text:
1613
 
 *
1614
 
 * Implement the "EmbedPreeditText" method call of
1615
 
 * the org.freedesktop.IBus interface.
1616
 
 */
1617
 
static gboolean
1618
 
_ibus_set_embed_preedit_text (BusIBusImpl     *ibus,
1619
 
                              GDBusConnection *connection,
1620
 
                              GVariant        *value,
1621
 
                              GError         **error)
1622
 
{
1623
 
    if (error) {
1624
 
        *error = NULL;
1625
 
    }
1626
 
 
1627
 
    gboolean embed_preedit_text = g_variant_get_boolean (value);
1628
 
    if (embed_preedit_text != ibus->embed_preedit_text) {
1629
 
        ibus->embed_preedit_text = embed_preedit_text;
1630
 
        bus_ibus_impl_property_changed (ibus, "EmbedPreeditText", value);
1631
 
    }
1632
 
 
1633
 
    return TRUE;
1634
 
}
1635
 
 
1636
 
/**
1637
 
 * bus_ibus_impl_service_method_call:
1638
 
 *
1639
 
 * Handle a D-Bus method call whose destination and interface name are
1640
 
 * both "org.freedesktop.IBus"
1641
 
 */
1642
 
static void
1643
 
bus_ibus_impl_service_method_call (IBusService           *service,
1644
 
                                   GDBusConnection       *connection,
1645
 
                                   const gchar           *sender,
1646
 
                                   const gchar           *object_path,
1647
 
                                   const gchar           *interface_name,
1648
 
                                   const gchar           *method_name,
1649
 
                                   GVariant              *parameters,
1650
 
                                   GDBusMethodInvocation *invocation)
1651
 
{
1652
 
    if (g_strcmp0 (interface_name, IBUS_INTERFACE_IBUS) != 0) {
1653
 
        IBUS_SERVICE_CLASS (bus_ibus_impl_parent_class)->service_method_call (
1654
 
                        service, connection, sender, object_path,
1655
 
                        interface_name, method_name,
1656
 
                        parameters, invocation);
1657
 
        return;
1658
 
    }
1659
 
 
1660
 
    /* all methods in the xml definition above should be listed here. */
1661
 
    static const struct {
1662
 
        const gchar *method_name;
1663
 
        void (* method_callback) (BusIBusImpl *,
1664
 
                                  GVariant *,
1665
 
                                  GDBusMethodInvocation *);
1666
 
    } methods [] =  {
1667
 
        /* IBus interface */
1668
 
        { "CreateInputContext",    _ibus_create_input_context },
1669
 
        { "RegisterComponent",     _ibus_register_component },
1670
 
        { "GetEnginesByNames",     _ibus_get_engines_by_names },
1671
 
        { "Exit",                  _ibus_exit },
1672
 
        { "Ping",                  _ibus_ping },
1673
 
        { "SetGlobalEngine",       _ibus_set_global_engine },
1674
 
        /* Start of deprecated methods */
1675
 
        { "GetAddress",            _ibus_get_address_depre },
1676
 
        { "CurrentInputContext",   _ibus_current_input_context_depre },
1677
 
        { "ListEngines",           _ibus_list_engines_depre },
1678
 
        { "ListActiveEngines",     _ibus_list_active_engines_depre },
1679
 
        { "GetUseSysLayout",       _ibus_get_use_sys_layout_depre },
1680
 
        { "GetUseGlobalEngine",    _ibus_get_use_global_engine_depre },
1681
 
        { "GetGlobalEngine",       _ibus_get_global_engine_depre },
1682
 
        { "IsGlobalEngineEnabled", _ibus_is_global_engine_enabled_depre },
1683
 
        /* End of deprecated methods */
1684
 
    };
1685
 
 
1686
 
    gint i;
1687
 
    for (i = 0; i < G_N_ELEMENTS (methods); i++) {
1688
 
        if (g_strcmp0 (methods[i].method_name, method_name) == 0) {
1689
 
            methods[i].method_callback ((BusIBusImpl *) service,
1690
 
                                        parameters,
1691
 
                                        invocation);
1692
 
            return;
1693
 
        }
1694
 
    }
1695
 
 
1696
 
    g_warning ("service_method_call received an unknown method: %s",
1697
 
               method_name ? method_name : "(null)");
1698
 
}
1699
 
 
1700
 
/**
1701
 
 * bus_ibus_impl_service_get_property:
1702
 
 *
1703
 
 * Handle org.freedesktop.DBus.Properties.Get
1704
 
 */
1705
 
static GVariant *
1706
 
bus_ibus_impl_service_get_property (IBusService     *service,
1707
 
                                    GDBusConnection *connection,
1708
 
                                    const gchar     *sender,
1709
 
                                    const gchar     *object_path,
1710
 
                                    const gchar     *interface_name,
1711
 
                                    const gchar     *property_name,
1712
 
                                    GError         **error)
1713
 
{
1714
 
    gint i;
1715
 
 
1716
 
    static const struct {
1717
 
        const gchar *method_name;
1718
 
        GVariant * (* method_callback) (BusIBusImpl *,
1719
 
                                        GDBusConnection *,
1720
 
                                        GError **);
1721
 
    } methods [] =  {
1722
 
        { "Address",               _ibus_get_address },
1723
 
        { "CurrentInputContext",   _ibus_get_current_input_context },
1724
 
        { "Engines",               _ibus_get_engines },
1725
 
        { "ActiveEngines",         _ibus_get_active_engines },
1726
 
        { "GlobalEngine",          _ibus_get_global_engine },
1727
 
        { "EmbedPreeditText",      _ibus_get_embed_preedit_text },
1728
 
    };
1729
 
 
1730
 
    if (g_strcmp0 (interface_name, IBUS_INTERFACE_IBUS) != 0) {
1731
 
        return IBUS_SERVICE_CLASS (
1732
 
                bus_ibus_impl_parent_class)->service_get_property (
1733
 
                        service, connection, sender, object_path,
1734
 
                        interface_name, property_name,
1735
 
                        error);
1736
 
    }
1737
 
 
1738
 
    for (i = 0; i < G_N_ELEMENTS (methods); i++) {
1739
 
        if (g_strcmp0 (methods[i].method_name, property_name) == 0) {
1740
 
            return methods[i].method_callback ((BusIBusImpl *) service,
1741
 
                                               connection,
1742
 
                                               error);
1743
 
        }
1744
 
    }
1745
 
 
1746
 
    g_warning ("service_get_property received an unknown property: %s",
1747
 
               property_name ? property_name : "(null)");
1748
 
    return NULL;
1749
 
}
1750
 
 
1751
 
/**
1752
 
 * bus_ibus_impl_service_set_property:
1753
 
 *
1754
 
 * Handle org.freedesktop.DBus.Properties.Set
1755
 
 */
1756
 
static gboolean
1757
 
bus_ibus_impl_service_set_property (IBusService     *service,
1758
 
                                    GDBusConnection *connection,
1759
 
                                    const gchar     *sender,
1760
 
                                    const gchar     *object_path,
1761
 
                                    const gchar     *interface_name,
1762
 
                                    const gchar     *property_name,
1763
 
                                    GVariant        *value,
1764
 
                                    GError         **error)
1765
 
{
1766
 
    gint i;
1767
 
 
1768
 
    static const struct {
1769
 
        const gchar *method_name;
1770
 
        gboolean (* method_callback) (BusIBusImpl *,
1771
 
                                      GDBusConnection *,
1772
 
                                      GVariant *,
1773
 
                                      GError **);
1774
 
    } methods [] =  {
1775
 
        { "PreloadEngines",        _ibus_set_preload_engines },
1776
 
        { "EmbedPreeditText",      _ibus_set_embed_preedit_text },
1777
 
    };
1778
 
 
1779
 
    if (g_strcmp0 (interface_name, IBUS_INTERFACE_IBUS) != 0) {
1780
 
        return IBUS_SERVICE_CLASS (
1781
 
                bus_ibus_impl_parent_class)->service_set_property (
1782
 
                        service, connection, sender, object_path,
1783
 
                        interface_name, property_name,
1784
 
                        value, error);
1785
 
    }
1786
 
 
1787
 
    for (i = 0; i < G_N_ELEMENTS (methods); i++) {
1788
 
        if (g_strcmp0 (methods[i].method_name, property_name) == 0) {
1789
 
            return methods[i].method_callback ((BusIBusImpl *) service,
1790
 
                                               connection,
1791
 
                                               value,
1792
 
                                               error);
1793
 
        }
1794
 
    }
1795
 
 
1796
 
    g_warning ("service_set_property received an unknown property: %s",
1797
 
               property_name ? property_name : "(null)");
1798
 
    return FALSE;
1799
 
}
1800
 
 
1801
 
BusIBusImpl *
1802
 
bus_ibus_impl_get_default (void)
1803
 
{
1804
 
    static BusIBusImpl *ibus = NULL;
1805
 
 
1806
 
    if (ibus == NULL) {
1807
 
        ibus = (BusIBusImpl *) g_object_new (BUS_TYPE_IBUS_IMPL,
1808
 
                                             "object-path", IBUS_PATH_IBUS,
1809
 
                                             NULL);
1810
 
    }
1811
 
    return ibus;
1812
 
}
1813
 
 
1814
 
BusFactoryProxy *
1815
 
bus_ibus_impl_lookup_factory (BusIBusImpl *ibus,
1816
 
                              const gchar *path)
1817
 
{
1818
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
1819
 
 
1820
 
    BusFactoryProxy *factory;
1821
 
 
1822
 
    factory = (BusFactoryProxy *) g_hash_table_lookup (ibus->factory_dict, path);
1823
 
 
1824
 
    return factory;
1825
 
}
1826
 
 
1827
 
IBusKeymap *
1828
 
bus_ibus_impl_get_keymap (BusIBusImpl *ibus)
1829
 
{
1830
 
 
1831
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
1832
 
 
1833
 
    return ibus->keymap;
1834
 
}
1835
 
 
1836
 
/**
1837
 
 * bus_ibus_impl_registry_init:
1838
 
 *
1839
 
 * Initialize IBusRegistry.
1840
 
 */
1841
 
static void
1842
 
bus_ibus_impl_registry_init (BusIBusImpl *ibus)
1843
 
{
1844
 
    GList *p;
1845
 
    GList *components;
1846
 
    IBusRegistry *registry = ibus_registry_new ();
1847
 
 
1848
 
    ibus->registry = NULL;
1849
 
    ibus->components = NULL;
1850
 
    ibus->engine_table = g_hash_table_new (g_str_hash, g_str_equal);
1851
 
 
1852
 
    if (g_strcmp0 (g_cache, "none") == 0) {
1853
 
        /* Only load registry, but not read and write cache. */
1854
 
        ibus_registry_load (registry);
1855
 
    }
1856
 
    else if (g_strcmp0 (g_cache, "refresh") == 0) {
1857
 
        /* Load registry and overwrite the cache. */
1858
 
        ibus_registry_load (registry);
1859
 
        ibus_registry_save_cache (registry, TRUE);
1860
 
    }
1861
 
    else if (g_strcmp0 (g_cache, "auto") == 0) {
1862
 
        /* Load registry from cache. If the cache does not exist or
1863
 
         * it is outdated, then generate it.
1864
 
         */
1865
 
        if (ibus_registry_load_cache (registry, FALSE) == FALSE ||
1866
 
            ibus_registry_check_modification (registry)) {
1867
 
 
1868
 
            ibus_object_destroy (IBUS_OBJECT (registry));
1869
 
            registry = ibus_registry_new ();
1870
 
 
1871
 
            if (ibus_registry_load_cache (registry, TRUE) == FALSE ||
1872
 
                ibus_registry_check_modification (registry)) {
1873
 
 
1874
 
                ibus_object_destroy (IBUS_OBJECT (registry));
1875
 
                registry = ibus_registry_new ();
1876
 
                ibus_registry_load (registry);
1877
 
                ibus_registry_save_cache (registry, TRUE);
1878
 
            }
1879
 
        }
1880
 
    }
1881
 
 
1882
 
    ibus->registry = registry;
1883
 
    components = ibus_registry_get_components (registry);
1884
 
 
1885
 
    for (p = components; p != NULL; p = p->next) {
1886
 
        IBusComponent *component = (IBusComponent *) p->data;
1887
 
        BusComponent *buscomp = bus_component_new (component,
1888
 
                                                   NULL /* factory */);
1889
 
        GList *engines = NULL;
1890
 
        GList *p1;
1891
 
 
1892
 
        g_object_ref_sink (buscomp);
1893
 
        ibus->components = g_list_append (ibus->components, buscomp);
1894
 
 
1895
 
        engines = bus_component_get_engines (buscomp);
1896
 
        for (p1 = engines; p1 != NULL; p1 = p1->next) {
1897
 
            IBusEngineDesc *desc = (IBusEngineDesc *) p1->data;
1898
 
            const gchar *name = ibus_engine_desc_get_name (desc);
1899
 
            if (g_hash_table_lookup (ibus->engine_table, name) == NULL) {
1900
 
                g_hash_table_insert (ibus->engine_table,
1901
 
                                     (gpointer) name,
1902
 
                                     desc);
1903
 
            } else {
1904
 
                g_message ("Engine %s is already registered by other component",
1905
 
                           name);
1906
 
            }
1907
 
        }
1908
 
        g_list_free (engines);
1909
 
    }
1910
 
 
1911
 
    g_list_free (components);
1912
 
 
1913
 
    g_signal_connect (ibus->registry,
1914
 
                      "changed",
1915
 
                      G_CALLBACK (_registry_changed_cb),
1916
 
                      ibus);
1917
 
    ibus_registry_start_monitor_changes (ibus->registry);
1918
 
}
1919
 
 
1920
 
static void
1921
 
bus_ibus_impl_registry_destroy (BusIBusImpl *ibus)
1922
 
{
1923
 
    g_list_free_full (ibus->components, g_object_unref);
1924
 
    ibus->components = NULL;
1925
 
 
1926
 
    g_hash_table_destroy (ibus->engine_table);
1927
 
    ibus->engine_table = NULL;
1928
 
 
1929
 
    ibus_object_destroy (IBUS_OBJECT (ibus->registry));
1930
 
    ibus->registry = NULL;
1931
 
}
1932
 
 
1933
 
static gint
1934
 
_component_is_name_cb (BusComponent *component,
1935
 
                       const gchar  *name)
1936
 
{
1937
 
    g_assert (BUS_IS_COMPONENT (component));
1938
 
    g_assert (name);
1939
 
 
1940
 
    return g_strcmp0 (bus_component_get_name (component), name);
1941
 
}
1942
 
 
1943
 
static void
1944
 
bus_ibus_impl_component_name_owner_changed (BusIBusImpl *ibus,
1945
 
                                            const gchar *name,
1946
 
                                            const gchar *old_name,
1947
 
                                            const gchar *new_name)
1948
 
{
1949
 
    BusComponent *component;
1950
 
    BusFactoryProxy *factory;
1951
 
 
1952
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
1953
 
    g_assert (name);
1954
 
    g_assert (old_name);
1955
 
    g_assert (new_name);
1956
 
 
1957
 
    component = bus_ibus_impl_lookup_component_by_name (ibus, name);
1958
 
 
1959
 
    if (component == NULL) {
1960
 
        /* name is a unique name, or a well-known name we don't know. */
1961
 
        return;
1962
 
    }
1963
 
 
1964
 
    if (g_strcmp0 (old_name, "") != 0) {
1965
 
        /* the component is stopped. */
1966
 
        factory = bus_component_get_factory (component);
1967
 
 
1968
 
        if (factory != NULL) {
1969
 
            ibus_proxy_destroy ((IBusProxy *) factory);
1970
 
        }
1971
 
    }
1972
 
 
1973
 
    if (g_strcmp0 (new_name, "") != 0) {
1974
 
        /* the component is started. */
1975
 
        BusConnection *connection =
1976
 
                bus_dbus_impl_get_connection_by_name (BUS_DEFAULT_DBUS,
1977
 
                                                      new_name);
1978
 
        if (connection == NULL)
1979
 
            return;
1980
 
 
1981
 
        factory = bus_factory_proxy_new (connection);
1982
 
        if (factory == NULL)
1983
 
            return;
1984
 
        bus_component_set_factory (component, factory);
1985
 
        g_object_unref (factory);
1986
 
    }
1987
 
}
1988
 
 
1989
 
BusComponent *
1990
 
bus_ibus_impl_lookup_component_by_name (BusIBusImpl *ibus,
1991
 
                                        const gchar *name)
1992
 
{
1993
 
    GList *p;
1994
 
 
1995
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
1996
 
    g_assert (name);
1997
 
 
1998
 
    p = g_list_find_custom (ibus->components,
1999
 
                            name,
2000
 
                            (GCompareFunc) _component_is_name_cb);
2001
 
    if (p) {
2002
 
        return (BusComponent *) p->data;
2003
 
    }
2004
 
    else {
2005
 
        return NULL;
2006
 
    }
2007
 
}
2008
 
 
2009
 
/**
2010
 
 * bus_ibus_impl_property_changed
2011
 
 *
2012
 
 * Send a "PropertiesChanged" D-Bus signal for a property to buses
2013
 
 * (connections) that are listening to the signal.
2014
 
 */
2015
 
static void
2016
 
bus_ibus_impl_property_changed (BusIBusImpl *service,
2017
 
                                const gchar *property_name,
2018
 
                                GVariant    *value)
2019
 
{
2020
 
    GDBusMessage *message =
2021
 
        g_dbus_message_new_signal ("/org/freedesktop/IBus",
2022
 
                                   "org.freedesktop.DBus.Properties",
2023
 
                                   "PropertiesChanged");
2024
 
 
2025
 
    /* set a non-zero serial to make libdbus happy */
2026
 
    g_dbus_message_set_serial (message, 1);
2027
 
    g_dbus_message_set_sender (message, "org.freedesktop.IBus");
2028
 
 
2029
 
    GVariantBuilder *builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
2030
 
    g_variant_builder_add (builder, "{sv}", property_name, value);
2031
 
    g_dbus_message_set_body (message,
2032
 
                             g_variant_new ("(sa{sv}as)",
2033
 
                                            "org.freedesktop.IBus",
2034
 
                                            builder,
2035
 
                                            NULL));
2036
 
 
2037
 
    bus_dbus_impl_dispatch_message_by_rule (BUS_DEFAULT_DBUS, message, NULL);
2038
 
    g_object_unref (message);
2039
 
}
2040
 
 
2041
 
/**
2042
 
 * bus_ibus_impl_emit_signal:
2043
 
 *
2044
 
 * Send a D-Bus signal to buses (connections) that are listening to the signal.
2045
 
 */
2046
 
static void
2047
 
bus_ibus_impl_emit_signal (BusIBusImpl *ibus,
2048
 
                           const gchar *signal_name,
2049
 
                           GVariant    *parameters)
2050
 
{
2051
 
    GDBusMessage *message = g_dbus_message_new_signal ("/org/freedesktop/IBus",
2052
 
                                                       "org.freedesktop.IBus",
2053
 
                                                       signal_name);
2054
 
    /* set a non-zero serial to make libdbus happy */
2055
 
    g_dbus_message_set_serial (message, 1);
2056
 
    g_dbus_message_set_sender (message, "org.freedesktop.IBus");
2057
 
    if (parameters)
2058
 
        g_dbus_message_set_body (message, parameters);
2059
 
    bus_dbus_impl_dispatch_message_by_rule (BUS_DEFAULT_DBUS, message, NULL);
2060
 
    g_object_unref (message);
2061
 
}
2062
 
 
2063
 
static void
2064
 
bus_ibus_impl_registry_changed (BusIBusImpl *ibus)
2065
 
{
2066
 
    bus_ibus_impl_emit_signal (ibus, "RegistryChanged", NULL);
2067
 
}
2068
 
 
2069
 
static void
2070
 
bus_ibus_impl_global_engine_changed (BusIBusImpl *ibus)
2071
 
{
2072
 
    const gchar *name = ibus->global_engine_name ? ibus->global_engine_name : "";
2073
 
    bus_ibus_impl_emit_signal (ibus, "GlobalEngineChanged",
2074
 
                               g_variant_new ("(s)", name));
2075
 
}
2076
 
 
2077
 
gboolean
2078
 
bus_ibus_impl_is_use_sys_layout (BusIBusImpl *ibus)
2079
 
{
2080
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
2081
 
 
2082
 
    return ibus->use_sys_layout;
2083
 
}
2084
 
 
2085
 
gboolean
2086
 
bus_ibus_impl_is_embed_preedit_text (BusIBusImpl *ibus)
2087
 
{
2088
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
2089
 
 
2090
 
    return ibus->embed_preedit_text;
2091
 
}
2092
 
 
2093
 
BusInputContext *
2094
 
bus_ibus_impl_get_focused_input_context (BusIBusImpl *ibus)
2095
 
{
2096
 
    g_assert (BUS_IS_IBUS_IMPL (ibus));
2097
 
 
2098
 
    return ibus->focused_context;
2099
 
}