panel.cpp 56.6 KB
Newer Older
1
/*
Xuetian Weng's avatar
Xuetian Weng committed
2
 *  Copyright (C) 2011-2012 Ni Hui <shuizhuyuanluo@126.com>
Xuetian Weng's avatar
Xuetian Weng committed
3
 *  Copyright (C) 2013-2014 Weng Xuetian <wengxt@gmail.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License or (at your option) version 3 or any later version
 *  accepted by the membership of KDE e.V. (or its successor approved
 *  by the membership of KDE e.V.), which shall act as a proxy
 *  defined in Section 14 of version 3 of the license.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

22
#include <locale.h>
23
24
25
#include <stdlib.h>
#include <string.h>
#include <ibus.h>
Xuetian Weng's avatar
Xuetian Weng committed
26
27
28
29
30
#include <string>
#include <QByteArray>
#include <QPair>
#include <QStringList>
#include <QDebug>
31
#include "panel.h"
Xuetian Weng's avatar
Xuetian Weng committed
32
33
34
35
36
#include "propertymanager.h"
#include "enginemanager.h"
#include "xkblayoutmanager.h"
#include "app.h"
#include "gtkaccelparse_p.h"
37

Xuetian Weng's avatar
Xuetian Weng committed
38
39
40
#ifndef DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED "org.freedesktop.DBus.Error.Failed"
#endif /* DBUS_ERROR_FAILED */
41
42
43
44
45
46
47

typedef struct _IBusPanelImpanelClass IBusPanelImpanelClass;

struct _IBusPanelImpanel {
    IBusPanelService    parent;
    IBusBus            *bus;
    GDBusConnection    *conn;
Xuetian Weng's avatar
Xuetian Weng committed
48
49
50
51
52
    PropertyManager* propManager;
    EngineManager* engineManager;
    XkbLayoutManager* xkbLayoutManager;
    App* app;
    gboolean useSystemKeyboardLayout;
Xuetian Weng's avatar
Xuetian Weng committed
53
    int selected;
54
55
56
57
58
59
};

struct _IBusPanelImpanelClass {
    IBusPanelServiceClass parent;
};

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
/* functions prototype */
static void         ibus_panel_impanel_class_init               (IBusPanelImpanelClass  *klass);
static void         ibus_panel_impanel_init                     (IBusPanelImpanel       *impanel);
static void         ibus_panel_impanel_destroy                  (IBusPanelImpanel       *impanel);

static void         ibus_panel_impanel_focus_in                 (IBusPanelService       *panel,
                                                                 const gchar            *input_context_path);
static void         ibus_panel_impanel_focus_out                (IBusPanelService       *panel,
                                                                 const gchar            *input_context_path);
static void         ibus_panel_impanel_register_properties      (IBusPanelService       *panel,
                                                                 IBusPropList           *prop_list);
static void         ibus_panel_impanel_real_register_properties (IBusPanelImpanel       *impanel);
static void         ibus_panel_impanel_set_cursor_location      (IBusPanelService       *panel,
                                                                 gint                    x,
                                                                 gint                    y,
                                                                 gint                    w,
                                                                 gint                    h);
static void         ibus_panel_impanel_update_auxiliary_text    (IBusPanelService       *panel,
                                                                 IBusText               *text,
                                                                 gboolean                visible);
static void         ibus_panel_impanel_update_lookup_table      (IBusPanelService       *panel,
                                                                 IBusLookupTable        *lookup_table,
                                                                 gboolean                visible);
static void         ibus_panel_impanel_update_preedit_text      (IBusPanelService       *panel,
                                                                 IBusText               *text,
                                                                 guint                   cursor_pos,
                                                                 gboolean                visible);
static void         ibus_panel_impanel_update_property          (IBusPanelService       *panel,
                                                                 IBusProperty           *prop);
static void         ibus_panel_impanel_cursor_down_lookup_table (IBusPanelService       *panel);
static void         ibus_panel_impanel_cursor_up_lookup_table   (IBusPanelService       *panel);
static void         ibus_panel_impanel_hide_auxiliary_text      (IBusPanelService       *panel);
static void         ibus_panel_impanel_hide_language_bar        (IBusPanelService       *panel);
static void         ibus_panel_impanel_hide_lookup_table        (IBusPanelService       *panel);
static void         ibus_panel_impanel_hide_preedit_text        (IBusPanelService       *panel);
static void         ibus_panel_impanel_page_down_lookup_table   (IBusPanelService       *panel);
static void         ibus_panel_impanel_page_up_lookup_table     (IBusPanelService       *panel);
static void         ibus_panel_impanel_reset                    (IBusPanelService       *panel);
static void         ibus_panel_impanel_show_auxiliary_text      (IBusPanelService       *panel);
static void         ibus_panel_impanel_show_language_bar        (IBusPanelService       *panel);
static void         ibus_panel_impanel_show_lookup_table        (IBusPanelService       *panel);
static void         ibus_panel_impanel_show_preedit_text        (IBusPanelService       *panel);
static void         ibus_panel_impanel_start_setup              (IBusPanelService       *panel);
static void         ibus_panel_impanel_state_changed            (IBusPanelService       *panel);

/* impanel signal handler function */
static void         ibus_panel_impanel_exec_im_menu             (IBusPanelImpanel* impanel);
static void         ibus_panel_impanel_exec_menu                (IBusPanelImpanel* impanel, IBusPropList* prop_list);


Xuetian Weng's avatar
Xuetian Weng committed
110
111
112
113

static void
impanel_set_engine(IBusPanelImpanel* impanel, const char* name);

Xuetian Weng's avatar
Xuetian Weng committed
114

115
static QByteArray
Xuetian Weng's avatar
Xuetian Weng committed
116
ibus_property_to_propstr (IBusProperty *property,
117
118
119
120
                          gboolean useSymbol = FALSE);

static QByteArray
ibus_engine_desc_to_logo_propstr(IBusEngineDesc* engine);
Xuetian Weng's avatar
Xuetian Weng committed
121

Xuetian Weng's avatar
Xuetian Weng committed
122
123
124
125
126
127
128
void
impanel_update_logo_by_engine(IBusPanelImpanel* impanel, IBusEngineDesc* engine_desc)
{
    if (!impanel->conn) {
        return;
    }

129
    QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
130
131
132

    g_dbus_connection_emit_signal (impanel->conn,
                                    NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateProperty",
133
                                    (g_variant_new ("(s)", propstr.constData())),
Xuetian Weng's avatar
Xuetian Weng committed
134
135
136
                                    NULL);
}

137
138
139
140
141
142
143
void
ibus_panel_impanel_set_bus (IBusPanelImpanel *impanel,
                            IBusBus          *bus)
{
    impanel->bus = bus;
}

Xuetian Weng's avatar
Xuetian Weng committed
144
145
146
147
148
void ibus_panel_impanel_set_app(IBusPanelImpanel* impanel, App* app)
{
    impanel->app = app;
}

Xuetian Weng's avatar
Xuetian Weng committed
149
150
void ibus_panel_impanel_accept(IBusPanelImpanel* impanel)
{
151
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
Xuetian Weng's avatar
Xuetian Weng committed
152
153
154
155
156
        impanel_set_engine(impanel, ibus_engine_desc_get_name(impanel->engineManager->engines()[impanel->selected]));
        impanel->selected = -1;
    }
}

157
void ibus_panel_impanel_navigate(IBusPanelImpanel* impanel, gboolean start, gboolean forward)
Xuetian Weng's avatar
Xuetian Weng committed
158
{
Xuetian Weng's avatar
Xuetian Weng committed
159
160
161
162
163
    if (start) {
        impanel->selected = -1;
    }

    if (impanel->engineManager->length() < 2) {
Xuetian Weng's avatar
Xuetian Weng committed
164
165
166
        return;
    }

Xuetian Weng's avatar
Xuetian Weng committed
167
168
169
    IBusEngineDesc* engine_desc = NULL;
    if (impanel->selected < 0) {
        engine_desc = ibus_bus_get_global_engine(impanel->bus);
170
    } else if (static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
Xuetian Weng's avatar
Xuetian Weng committed
171
        engine_desc = impanel->engineManager->engines()[impanel->selected];
172
        g_object_ref(engine_desc);
173
174
175
    }

    if (!engine_desc) {
Xuetian Weng's avatar
Xuetian Weng committed
176
        engine_desc = impanel->engineManager->engines()[0];
177
        g_object_ref(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
178
179
180
    }

    if (engine_desc) {
181
        const char* name = impanel->engineManager->navigate(engine_desc, forward);
Xuetian Weng's avatar
Xuetian Weng committed
182
        impanel->selected = impanel->engineManager->getIndexByName(name);
183
        g_object_unref(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
184
185
    } else {
        return;
Xuetian Weng's avatar
Xuetian Weng committed
186
187
    }

188
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
189
        ibus_panel_impanel_real_register_properties(impanel);
Xuetian Weng's avatar
Xuetian Weng committed
190
191
192
193
194
195
196
197
    }
}

void ibus_panel_impanel_move_next(IBusPanelImpanel* impanel)
{
    if (impanel->engineManager->length() >= 2) {
        impanel_set_engine(impanel, ibus_engine_desc_get_name(impanel->engineManager->engines()[1]));
    }
Xuetian Weng's avatar
Xuetian Weng committed
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
static GDBusNodeInfo *introspection_data = NULL;

static guint owner_id;

static const gchar introspection_xml[] =
    "<node>"
    "  <interface name='org.kde.kimpanel.inputmethod'>"
    "    <signal name='Enable'>"
    "      <arg type='b' name='enable'/>"
    "    </signal>"
    "    <signal name='RegisterProperties'>"
    "      <arg type='as' name='prop'/>"
    "    </signal>"
    "    <signal name='UpdateProperty'>"
    "      <arg type='s' name='prop'/>"
    "    </signal>"
    "    <signal name='RemoveProperty'>"
    "      <arg type='s' name='prop'/>"
    "    </signal>"
    "    <signal name='ShowAux'>"
    "      <arg type='b' name='toshow'/>"
    "    </signal>"
    "    <signal name='ShowLookupTable'>"
    "      <arg type='b' name='toshow'/>"
    "    </signal>"
    "    <signal name='ShowPreedit'>"
    "      <arg type='b' name='toshow'/>"
    "    </signal>"
    "    <signal name='UpdateAux'>"
    "      <arg type='s' name='text'/>"
    "      <arg type='s' name='attr'/>"
    "    </signal>"
Xuetian Weng's avatar
Xuetian Weng committed
233
234
235
    "    <signal name='UpdateLookupTableCursor'>"
    "      <arg type='i' name='pos'/>"
    "    </signal>"
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
    "    <signal name='UpdateLookupTable'>"
    "      <arg type='as' name='labels'/>"
    "      <arg type='as' name='candidates'/>"
    "      <arg type='as' name='attrs'/>"
    "      <arg type='b' name='hasprev'/>"
    "      <arg type='b' name='hasnext'/>"
    "    </signal>"
    "    <signal name='UpdatePreeditCaret'>"
    "      <arg type='i' name='pos'/>"
    "    </signal>"
    "    <signal name='UpdatePreeditText'>"
    "      <arg type='s' name='text'/>"
    "      <arg type='s' name='attr'/>"
    "    </signal>"
    "    <signal name='UpdateSpotLocation'>"
    "      <arg type='i' name='x'/>"
    "      <arg type='i' name='y'/>"
    "    </signal>"
    "    <signal name='ExecMenu'>"
    "      <arg type='as' name='actions'/>"
    "    </signal>"
    "  </interface>"
    "</node>";

static const char prop_sep[] = ":";

262
static QByteArray
263
264
265
ibus_property_args_to_propstr (const char *key,
                               const char *label,
                               const char *icon,
266
267
                               const char *tooltip,
                               const char *hint = "")
268
{
Xuetian Weng's avatar
Xuetian Weng committed
269
    QByteArray propstr("/IBus/");
Xuetian Weng's avatar
Xuetian Weng committed
270
271
    QByteArray str(key);
    str.replace(':', '!');
272
273
274
275

    App* app = static_cast<App*>(qApp);

    propstr += str;
276
    propstr += prop_sep;
277
    propstr += QByteArray(label).replace(':', '-').constData();
278
    propstr += prop_sep;
279
    propstr += app->normalizeIconName(QByteArray(icon).replace(':', '-'));
280
    propstr += prop_sep;
281
    propstr += QByteArray(tooltip).replace(':', '-').constData();
282
283
    propstr += prop_sep;
    propstr += QByteArray(hint).replace(':', '-').constData();
284
285

    return propstr;
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
static QByteArray
ibus_engine_desc_to_logo_propstr(IBusEngineDesc* engine)
{
    const gchar* label = "IBus";
    const gchar* tooltip = "";
    const gchar* icon = "input-keyboard";

    gchar xkbLabel[3];
    if (engine) {
        const gchar* iconname = ibus_engine_desc_get_icon(engine);
        if (iconname && iconname[0]) {
            icon = iconname;
        }

        if (strncmp("xkb:", ibus_engine_desc_get_name(engine), 4) == 0) {
            strncpy(xkbLabel, ibus_engine_desc_get_name(engine) + 4, 2);
            xkbLabel[2] = 0;
            int i = 0;
            while (xkbLabel[i]) {
                if (xkbLabel[i] == ':') {
                    xkbLabel[i] = 0;
                }
                i++;
            }
            label = xkbLabel;
            icon = "";
        }

        const gchar* longname = ibus_engine_desc_get_longname(engine);
        if (longname && longname[0]) {
            tooltip = longname;
        }
    }

    return ibus_property_args_to_propstr("Logo",
                                         label,
                                         icon,
                                         tooltip);
}

328
static QByteArray
329
ibus_property_to_propstr (IBusProperty *property,
330
                          gboolean useSymbol)
331
{
332
333
    const gchar* label = NULL;
    const gchar* tooltip = ibus_text_get_text (ibus_property_get_tooltip (property));
Xuetian Weng's avatar
Xuetian Weng committed
334
    const gchar* icon = ibus_property_get_icon (property);
335
336
337
338
339
340
341
342

    if (useSymbol) {
        label = ibus_text_get_text(ibus_property_get_symbol (property));
        if (!label || label[0] == '\0') {
            label = ibus_text_get_text(ibus_property_get_label(property));
        }
    } else {
        label = ibus_text_get_text(ibus_property_get_label(property));
Xuetian Weng's avatar
Xuetian Weng committed
343
    }
344

345
346
347
348
349
350
351
352
353
354
355
356
357
    const char* hint = "";
    if (ibus_property_get_prop_type(property) == PROP_TYPE_TOGGLE) {
        if (ibus_property_get_state(property) != PROP_STATE_CHECKED) {
            hint = "disable";
        }
    } else if (ibus_property_get_prop_type(property) == PROP_TYPE_RADIO) {
        if (ibus_property_get_state(property) == PROP_STATE_CHECKED) {
            hint = "checked";
        }
    }



358
359
360
    return ibus_property_args_to_propstr(ibus_property_get_key (property),
                                         label,
                                         icon,
361
362
                                         tooltip,
                                         hint);
363
364
}

365
static QByteArray
366
367
368
369
ibus_engine_desc_args_to_propstr (const char *name,
                                  const char *language,
                                  const char *longname,
                                  const char *icon,
370
                                  const char *description)
371
{
372
    QByteArray propstr("/IBus/Engine/");
Xuetian Weng's avatar
Xuetian Weng committed
373
374
    QByteArray data(name);
    data.replace(':', '!');
375
376
    propstr += data;
    propstr += prop_sep;
377
    if (language) {
378
379
        propstr += language;
        propstr += " - ";
380
    }
381
382
383
384
385
386
    propstr += longname;
    propstr += prop_sep;
    propstr += icon;
    propstr += prop_sep;
    propstr += description;
    return propstr;
387
388
}

389
390
static QByteArray
ibus_engine_desc_to_propstr (IBusEngineDesc *engine_desc)
391
{
392
393
394
395
396
    return ibus_engine_desc_args_to_propstr(ibus_engine_desc_get_name(engine_desc),
                                            ibus_engine_desc_get_language(engine_desc),
                                            ibus_engine_desc_get_longname(engine_desc),
                                            ibus_engine_desc_get_icon(engine_desc),
                                            ibus_engine_desc_get_description(engine_desc));
Xuetian Weng's avatar
Xuetian Weng committed
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
}

static void
impanel_get_default_engine(IBusPanelImpanel* impanel, char*** pengine_names, gsize* plen)
{
    GList* engines = ibus_bus_list_engines(impanel->bus);
    if (!engines) {
        *pengine_names = g_new0 (gchar*, 2);
        *plen = 1;
        (*pengine_names)[0] = g_strdup ("xkb:us::eng");
        return;
    }

    QList<QByteArray> engineList;
    impanel->xkbLayoutManager->getLayout();
    QStringList layouts = impanel->xkbLayoutManager->defaultLayout().split(',');
    QStringList variants = impanel->xkbLayoutManager->defaultVariant().split(',');

    for (int i = 0; i < layouts.size(); i ++ ) {
        QString variant;
        if (i < variants.size()) {
            variant = variants[i];
        }

        for (GList* engine = g_list_first(engines); engine != NULL ;
            engine = g_list_next(engine)) {
            IBusEngineDesc* desc = IBUS_ENGINE_DESC (engine->data);
            QByteArray name = ibus_engine_desc_get_name(desc);
            if (!name.startsWith("xkb:")) {
                continue;
            }

            if (QLatin1String(ibus_engine_desc_get_layout(desc)) == layouts[i] &&
                QLatin1String(ibus_engine_desc_get_layout_variant(desc)) == variant) {
                engineList << name;
            }
        }
    }
    const char* locale = setlocale(LC_CTYPE, NULL);
    if (!locale) {
        locale = "C";
    }

    QStringList localeList = QString(locale).split('.');
    QString lang = localeList.size() > 0 ? localeList[0] : "";

    bool added = false;
    for (GList* engine = g_list_first(engines); engine != NULL ;
        engine = g_list_next(engine)) {
        IBusEngineDesc* desc = IBUS_ENGINE_DESC (engine->data);
        QByteArray name = ibus_engine_desc_get_name(desc);
        if (name.startsWith("xkb:")) {
            continue;
        }

        if (QLatin1String(ibus_engine_desc_get_language(desc)) == lang &&
            ibus_engine_desc_get_rank(desc) > 0) {
            engineList << name;
            added = true;
        }
    }

    if (!added) {
        localeList = QString(lang).split('_');
        QString lang = localeList.size() > 0 ? localeList[0] : "";

        for (GList* engine = g_list_first(engines); engine != NULL ;
            engine = g_list_next(engine)) {
            IBusEngineDesc* desc = IBUS_ENGINE_DESC (engine->data);
            QByteArray name = ibus_engine_desc_get_name(desc);
            if (name.startsWith("xkb:")) {
                continue;
            }

            if (QLatin1String(ibus_engine_desc_get_language(desc)) == lang &&
                ibus_engine_desc_get_rank(desc) > 0) {
                engineList << name;
            }
        }
    }

    for (GList* engine = g_list_first(engines); engine != NULL ;
        engine = g_list_next(engine)) {
        IBusEngineDesc* desc = IBUS_ENGINE_DESC (engine->data);
        g_object_unref(desc);
    }

    g_list_free(engines);

    if (engineList.size() == 0) {
        *pengine_names = g_new0 (gchar*, 2);
        *plen = 1;
        (*pengine_names)[0] = g_strdup ("xkb:us::eng");
        return;
    } else {
        *pengine_names = g_new0 (gchar*, engineList.size() + 1);
        *plen = engineList.size();
        size_t i = 0;
        Q_FOREACH(const QByteArray& name, engineList) {
            (*pengine_names)[i] = g_strdup (name.constData());
            i ++;
        }
    }
}

static void
impanel_update_engines(IBusPanelImpanel* impanel, GVariant* var_engines) {
    gchar** engine_names = NULL;
    size_t len = 0;
    if (var_engines) {
        engine_names = g_variant_dup_strv(var_engines, &len);
    }
    if (len == 0) {
        g_strfreev(engine_names);
        engine_names = NULL;
    }

    if (!engine_names) {
        impanel_get_default_engine(impanel, &engine_names, &len);
        IBusConfig* config = ibus_bus_get_config(impanel->bus);
        GVariant* var = g_variant_new_strv(engine_names, len);
        ibus_config_set_value(config, "general", "preload_engines", var);
    }

    IBusEngineDesc** engines = ibus_bus_get_engines_by_names(impanel->bus, engine_names);
    g_strfreev(engine_names);

524
    impanel->engineManager->setEngines(engines);
Xuetian Weng's avatar
Xuetian Weng committed
525
526

    if (engines && engines[0]) {
527
        ibus_bus_set_global_engine(impanel->bus, ibus_engine_desc_get_name(engines[0]));
Xuetian Weng's avatar
Xuetian Weng committed
528
529
530
531
532
    }

    impanel->app->setDoGrab(len > 1);
}

Xuetian Weng's avatar
Xuetian Weng committed
533
534
535
536
537
static void
impanel_update_engines_order(IBusPanelImpanel* impanel, GVariant* var_engines) {
    const gchar** engine_names = NULL;
    size_t len = 0;
    engine_names = g_variant_get_strv(var_engines, &len);
538
539
    if (len) {
        impanel->engineManager->setOrder(engine_names, len);
Xuetian Weng's avatar
Xuetian Weng committed
540

541
542
543
        if (impanel->engineManager->engines()) {
            ibus_bus_set_global_engine(impanel->bus, ibus_engine_desc_get_name(impanel->engineManager->engines()[0]));
        }
Xuetian Weng's avatar
Xuetian Weng committed
544
    }
545
    g_free(engine_names);
Xuetian Weng's avatar
Xuetian Weng committed
546
547
}

Xuetian Weng's avatar
Xuetian Weng committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
static void
impanel_update_triggers(IBusPanelImpanel* impanel, GVariant* variant) {
    gchar** triggers = NULL;
    size_t len = 0;
    if (variant) {
        triggers = g_variant_dup_strv(variant, &len);
    }
    if (len == 0) {
        g_strfreev(triggers);
        triggers = 0;
    }
    if (!triggers) {
        triggers = g_new0 (gchar*, 2);
        len = 1;
        triggers[0] = g_strdup ("<Super>space");
    }

    QList<QPair<uint, uint> > triggersList;
    for (size_t i = 0; i < len; i ++) {
        guint key = 0;
        GdkModifierType mod = (GdkModifierType) 0;
        _gtk_accelerator_parse(triggers[i], &key, &mod);
        if (key) {
            triggersList << qMakePair<uint, uint>(key, (uint) mod);
        }
    }
    impanel->app->setTriggerKeys(triggersList);
}


static void
impanel_update_use_system_keyboard_layout(IBusPanelImpanel* impanel, GVariant* variant) {
    impanel->useSystemKeyboardLayout = g_variant_get_boolean(variant);
}
static void
impanel_update_use_global_engine(IBusPanelImpanel* impanel, GVariant* variant) {
    impanel->engineManager->setUseGlobalEngine(g_variant_get_boolean(variant));
}

static void
impanel_update_latin_layouts(IBusPanelImpanel* impanel, GVariant* variant) {
    if (!variant) {
        return;
    }
    gsize length;
    const gchar** variants = g_variant_get_strv(variant, &length);

    impanel->xkbLayoutManager->setLatinLayouts(variants, length);
596
    g_free(variants);
Xuetian Weng's avatar
Xuetian Weng committed
597
598
599
600
601
}

static void
impanel_config_value_changed_callback (IBusConfig* config, const gchar* section, const gchar* name, GVariant* value, gpointer user_data)
{
602
    Q_UNUSED(config);
Xuetian Weng's avatar
Xuetian Weng committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);

    if (g_strcmp0(section, "general") == 0 && g_strcmp0(name, "preload_engines") == 0) {
        impanel_update_engines(impanel, value);
    }
    else if (g_strcmp0(section, "general/hotkey") == 0 && g_strcmp0(name, "triggers") == 0) {
        impanel_update_triggers(impanel, value);
    } else if (g_strcmp0(section, "general") == 0 && g_strcmp0(name, "use-system-keyboard-layout") == 0) {
        impanel_update_use_system_keyboard_layout(impanel, value);
    } else if (g_strcmp0(section, "general") == 0 && g_strcmp0(name, "use-global-engine") == 0) {
        impanel_update_use_global_engine(impanel, value);
    }
}

static void
impanel_exit_callback (GDBusConnection *connection,
                                   const gchar     *sender_name,
                                   const gchar     *object_path,
                                   const gchar     *interface_name,
                                   const gchar     *signal_name,
                                   GVariant        *parameters,
                                   gpointer         user_data)
{
626
627
628
629
630
631
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);
    Q_UNUSED(parameters);
Xuetian Weng's avatar
Xuetian Weng committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
    if (impanel->bus) {
        ibus_bus_exit(impanel->bus, FALSE);
    }
}

static void
impanel_panel_created_callback (GDBusConnection *connection,
                                const gchar     *sender_name,
                                const gchar     *object_path,
                                const gchar     *interface_name,
                                const gchar     *signal_name,
                                GVariant        *parameters,
                                gpointer         user_data)
{
647
648
649
650
651
652
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);
    Q_UNUSED(parameters);
Xuetian Weng's avatar
Xuetian Weng committed
653
654
655
656
657
658
659
660
661
662
663
664
665
666
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
    ibus_panel_impanel_real_register_properties(impanel);

}

static void
impanel_set_engine(IBusPanelImpanel* impanel, const char* name)
{
    if (!name || !name[0]) {
        return;
    }
    if (ibus_bus_set_global_engine(impanel->bus, name)) {
        if (!impanel->useSystemKeyboardLayout) {
            IBusEngineDesc* engine_desc = ibus_bus_get_global_engine(impanel->bus);
Xuetian Weng's avatar
Xuetian Weng committed
667
668
669
            if (engine_desc) {
                impanel->xkbLayoutManager->setLayout(engine_desc);
            }
670
            g_object_unref(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
671
672
        }
        impanel->engineManager->setCurrentEngine(name);
673
674
    } else {
        qDebug() << "set engine failed.";
Xuetian Weng's avatar
Xuetian Weng committed
675
    }
676
677
678
679
680
681
682
683
684
685
686
}

static void
impanel_trigger_property_callback (GDBusConnection *connection,
                                   const gchar     *sender_name,
                                   const gchar     *object_path,
                                   const gchar     *interface_name,
                                   const gchar     *signal_name,
                                   GVariant        *parameters,
                                   gpointer         user_data)
{
687
688
689
690
691
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);
Xuetian Weng's avatar
Xuetian Weng committed
692
693
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
    gchar *s0 = NULL;
694
    g_variant_get (parameters, "(s)", &s0);
Xuetian Weng's avatar
Xuetian Weng committed
695
696
697
698
699
700
701
702
    if (!s0 || strlen(s0) <= 6)
        return;
    QByteArray prop_key(s0 + 6);// +6 to skip "/IBus/"
    prop_key.replace('!', ':');
    if (g_ascii_strncasecmp (prop_key.constData(), "Logo", 4) == 0)
        ibus_panel_impanel_exec_im_menu(impanel);
    else if (g_ascii_strncasecmp (prop_key.constData(), "Engine/", 7) == 0) {
        impanel_set_engine(impanel, prop_key.constData() + 7);
703
    }
Xuetian Weng's avatar
Xuetian Weng committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
    else {
        IBusProperty* property = impanel->propManager->property(prop_key.constData());
        if (property) {
            IBusPropState newstate = ibus_property_get_state(property);
            switch (ibus_property_get_prop_type(property)) {
                case PROP_TYPE_RADIO:
                case PROP_TYPE_TOGGLE:
                    if (ibus_property_get_prop_type(property) == PROP_TYPE_TOGGLE) {
                        if (newstate == PROP_STATE_CHECKED)
                            newstate = PROP_STATE_UNCHECKED;
                        else if (newstate == PROP_STATE_UNCHECKED)
                            newstate = PROP_STATE_CHECKED;
                    }
                    else if (ibus_property_get_prop_type(property) == PROP_TYPE_RADIO) {
                        newstate = PROP_STATE_CHECKED;
                    }
                case PROP_TYPE_NORMAL:
                    ibus_property_set_state(property, newstate);
                    ibus_panel_service_property_activate((IBusPanelService *)impanel, prop_key.constData(), newstate);
                    break;
                case PROP_TYPE_MENU:
                    ibus_panel_impanel_exec_menu(impanel, ibus_property_get_sub_props(property));
                case PROP_TYPE_SEPARATOR:
                    break;
                default:
                    break;
            }
        }
        else {
            ibus_panel_service_property_activate((IBusPanelService *)impanel, prop_key.constData(), PROP_STATE_CHECKED);
        }
    }
    g_free(s0);
737
738
}

739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
static void
impanel_select_candidate_callback (GDBusConnection *connection,
                                   const gchar     *sender_name,
                                   const gchar     *object_path,
                                   const gchar     *interface_name,
                                   const gchar     *signal_name,
                                   GVariant        *parameters,
                                   gpointer         user_data)
{
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);

    gint i;
    g_variant_get (parameters, "(i)", &i);
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
    ibus_panel_service_candidate_clicked((IBusPanelService *)impanel, i, 0, 0);
}

static void
impanel_prev_page_callback (GDBusConnection *connection,
                            const gchar     *sender_name,
                            const gchar     *object_path,
                            const gchar     *interface_name,
                            const gchar     *signal_name,
                            GVariant        *parameters,
                            gpointer         user_data)
{
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);
    Q_UNUSED(parameters);

    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
    ibus_panel_service_page_up((IBusPanelService *)impanel);
}

static void
impanel_next_page_callback (GDBusConnection *connection,
                            const gchar     *sender_name,
                            const gchar     *object_path,
                            const gchar     *interface_name,
                            const gchar     *signal_name,
                            GVariant        *parameters,
                            gpointer         user_data)
{
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);
    Q_UNUSED(parameters);

    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
    ibus_panel_service_page_down((IBusPanelService *)impanel);
}

800
801
802
803
804
805
806
807
808
static void
impanel_configure_callback (GDBusConnection *connection,
                            const gchar     *sender_name,
                            const gchar     *object_path,
                            const gchar     *interface_name,
                            const gchar     *signal_name,
                            GVariant        *parameters,
                            gpointer         user_data)
{
809
810
811
812
813
814
815
    Q_UNUSED(connection);
    Q_UNUSED(sender_name);
    Q_UNUSED(object_path);
    Q_UNUSED(interface_name);
    Q_UNUSED(signal_name);
    Q_UNUSED(parameters);
    Q_UNUSED(user_data);
816
817
818
819
820
821
822
823
824
825
826
827
    pid_t pid = fork();
    if (pid == 0) {
        execlp ("ibus-setup", "ibus-setup", (char *)0);
        exit (0);
    }
}

static void
on_bus_acquired (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
828
    Q_UNUSED(name);
Xuetian Weng's avatar
Xuetian Weng committed
829
830
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
    impanel->conn = connection;
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849

    g_dbus_connection_register_object (connection,
                                       "/kimpanel",
                                       introspection_data->interfaces[0],
                                       NULL,  /*&interface_vtable*/
                                       NULL,  /* user_data */
                                       NULL,  /* user_data_free_func */
                                       NULL); /* GError** */

    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "TriggerProperty",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_trigger_property_callback,
                                        user_data,
                                        NULL);
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "SelectCandidate",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_select_candidate_callback,
                                        user_data,
                                        NULL);
    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "LookupTablePageUp",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_prev_page_callback,
                                        user_data,
                                        NULL);
    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "LookupTablePageDown",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_next_page_callback,
                                        user_data,
                                        NULL);
Xuetian Weng's avatar
Xuetian Weng committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "PanelCreated",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_panel_created_callback,
                                        user_data,
                                        NULL);
    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "Exit",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_exit_callback,
                                        user_data,
                                        NULL);
900
901
902
903
904
905
906
907
908
909
    g_dbus_connection_signal_subscribe (connection,
                                        "org.kde.impanel",
                                        "org.kde.impanel",
                                        "Configure",
                                        "/org/kde/impanel",
                                        NULL,
                                        G_DBUS_SIGNAL_FLAGS_NONE,
                                        impanel_configure_callback,
                                        user_data,
                                        NULL);
Xuetian Weng's avatar
Xuetian Weng committed
910
911
912
913
914
915
916
917
918
919
920
921
922

    IBusConfig* config = ibus_bus_get_config(impanel->bus);
    if (config) {
        g_signal_connect_object(config, "value-changed", (GCallback) impanel_config_value_changed_callback, impanel, (GConnectFlags) 0);
        ibus_config_watch(config, "general", "preload_engines");
        ibus_config_watch(config, "general", "use-system-keyboard-layout");
        ibus_config_watch(config, "general/hotkey", "triggers");

        GVariant* var_engines = ibus_config_get_value(config, "general", "preload_engines");
        impanel_update_engines(impanel, var_engines);
        if (var_engines)
            g_variant_unref(var_engines);

Xuetian Weng's avatar
Xuetian Weng committed
923
924
925
926
927
928
        var_engines = ibus_config_get_value(config, "general", "engines-order");
        if (var_engines) {
            impanel_update_engines_order(impanel, var_engines);
            g_variant_unref(var_engines);
        }

Xuetian Weng's avatar
Xuetian Weng committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
        GVariant* var_triggers = ibus_config_get_value(config, "general/hotkey", "triggers");
        impanel_update_triggers(impanel, var_triggers);
        if (var_triggers)
            g_variant_unref(var_triggers);

        IBusConfig* config = ibus_bus_get_config(impanel->bus);
        GVariant* var_layouts = ibus_config_get_value(config, "general", "xkb-latin-layouts");
        if (var_layouts) {
            impanel_update_latin_layouts(impanel, var_layouts);
            g_variant_unref(var_layouts);
        }

        GVariant* var = ibus_config_get_value(config, "general", "use-system-keyboard-layout");
        if (var) {
            impanel_update_use_system_keyboard_layout(impanel, var);
            g_variant_unref(var);
        }

        var = ibus_config_get_value(config, "general", "use-global-engine");
        if (var) {
            impanel_update_use_global_engine(impanel, var);
            g_variant_unref(var);
        }
    }

    ibus_panel_impanel_real_register_properties(impanel);
955
956
957
958
959
960
961
}

static void
on_name_acquired (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
962
963
964
    Q_UNUSED(connection);
    Q_UNUSED(name);
    Q_UNUSED(user_data);
965
966
967
968
969
970
971
}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
972
973
974
    Q_UNUSED(connection);
    Q_UNUSED(name);
    Q_UNUSED(user_data);
975
976
977
978
979
980
    exit (1);
}

G_DEFINE_TYPE (IBusPanelImpanel, ibus_panel_impanel, IBUS_TYPE_PANEL_SERVICE)

static void
Xuetian Weng's avatar
Xuetian Weng committed
981
ibus_panel_impanel_class_init (IBusPanelImpanelClass *klass)
982
{
Xuetian Weng's avatar
Xuetian Weng committed
983
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014

    IBUS_OBJECT_CLASS (object_class)->destroy = (IBusObjectDestroyFunc) ibus_panel_impanel_destroy;
    IBUS_PANEL_SERVICE_CLASS (object_class)->focus_in                   = ibus_panel_impanel_focus_in;
    IBUS_PANEL_SERVICE_CLASS (object_class)->focus_out                  = ibus_panel_impanel_focus_out;
    IBUS_PANEL_SERVICE_CLASS (object_class)->register_properties        = ibus_panel_impanel_register_properties;
    IBUS_PANEL_SERVICE_CLASS (object_class)->set_cursor_location        = ibus_panel_impanel_set_cursor_location;
    IBUS_PANEL_SERVICE_CLASS (object_class)->update_auxiliary_text      = ibus_panel_impanel_update_auxiliary_text;
    IBUS_PANEL_SERVICE_CLASS (object_class)->update_lookup_table        = ibus_panel_impanel_update_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->update_preedit_text        = ibus_panel_impanel_update_preedit_text;
    IBUS_PANEL_SERVICE_CLASS (object_class)->update_property            = ibus_panel_impanel_update_property;
    IBUS_PANEL_SERVICE_CLASS (object_class)->cursor_down_lookup_table   = ibus_panel_impanel_cursor_down_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->cursor_up_lookup_table     = ibus_panel_impanel_cursor_up_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->hide_auxiliary_text        = ibus_panel_impanel_hide_auxiliary_text;
    IBUS_PANEL_SERVICE_CLASS (object_class)->hide_language_bar          = ibus_panel_impanel_hide_language_bar;
    IBUS_PANEL_SERVICE_CLASS (object_class)->hide_lookup_table          = ibus_panel_impanel_hide_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->hide_preedit_text          = ibus_panel_impanel_hide_preedit_text;
    IBUS_PANEL_SERVICE_CLASS (object_class)->page_down_lookup_table     = ibus_panel_impanel_page_down_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->page_up_lookup_table       = ibus_panel_impanel_page_up_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->reset                      = ibus_panel_impanel_reset;
    IBUS_PANEL_SERVICE_CLASS (object_class)->show_auxiliary_text        = ibus_panel_impanel_show_auxiliary_text;
    IBUS_PANEL_SERVICE_CLASS (object_class)->show_language_bar          = ibus_panel_impanel_show_language_bar;
    IBUS_PANEL_SERVICE_CLASS (object_class)->show_lookup_table          = ibus_panel_impanel_show_lookup_table;
    IBUS_PANEL_SERVICE_CLASS (object_class)->show_preedit_text          = ibus_panel_impanel_show_preedit_text;
    IBUS_PANEL_SERVICE_CLASS (object_class)->start_setup                = ibus_panel_impanel_start_setup;
    IBUS_PANEL_SERVICE_CLASS (object_class)->state_changed              = ibus_panel_impanel_state_changed;
}

static void
ibus_panel_impanel_init (IBusPanelImpanel *impanel)
{
    impanel->bus = NULL;
Xuetian Weng's avatar
Xuetian Weng committed
1015
1016
    impanel->app = NULL;
    impanel->useSystemKeyboardLayout = false;
Xuetian Weng's avatar
Xuetian Weng committed
1017
    impanel->selected = -1;
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027

    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
    owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
                               "org.kde.kimpanel.inputmethod",
                               G_BUS_NAME_OWNER_FLAGS_REPLACE,
                               on_bus_acquired,
                               on_name_acquired,
                               on_name_lost,
                               impanel, NULL);

Xuetian Weng's avatar
Xuetian Weng committed
1028
1029
1030
    impanel->propManager = new PropertyManager;
    impanel->engineManager = new EngineManager;
    impanel->xkbLayoutManager = new XkbLayoutManager;
1031
1032
1033
1034
1035
}

static void
ibus_panel_impanel_destroy (IBusPanelImpanel *impanel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1036
1037
1038
1039
1040
1041
    delete impanel->propManager;
    impanel->propManager = NULL;
    delete impanel->engineManager;
    impanel->engineManager = NULL;
    delete impanel->xkbLayoutManager;
    impanel->xkbLayoutManager = NULL;
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

    g_bus_unown_name (owner_id);
    g_dbus_node_info_unref (introspection_data);

    IBUS_OBJECT_CLASS (ibus_panel_impanel_parent_class)->destroy ((IBusObject *)impanel);
}

static void
ibus_panel_impanel_focus_in (IBusPanelService *panel,
                             const gchar      *input_context_path)
{
Xuetian Weng's avatar
Xuetian Weng committed
1053
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);
Xuetian Weng's avatar
Xuetian Weng committed
1054
1055
1056
    if (impanel->app->keyboardGrabbed()) {
        return;
    }
Xuetian Weng's avatar
Xuetian Weng committed
1057

1058
    IBusEngineDesc *engine_desc = ibus_bus_get_global_engine(impanel->bus);
Xuetian Weng's avatar
Xuetian Weng committed
1059
    impanel_update_logo_by_engine(impanel, engine_desc);
1060
    g_object_unref(engine_desc);
1061

Xuetian Weng's avatar
Xuetian Weng committed
1062
1063
1064
1065
    impanel->engineManager->setCurrentContext(input_context_path);
    if (!impanel->engineManager->useGlobalEngine()) {
        impanel_set_engine(impanel, impanel->engineManager->currentEngine().toUtf8().constData());
    }
1066
1067
1068
1069
1070
1071
}

static void
ibus_panel_impanel_focus_out (IBusPanelService *panel,
                              const gchar      *input_context_path)
{
1072
1073
    Q_UNUSED(panel);
    Q_UNUSED(input_context_path);
Xuetian Weng's avatar
Xuetian Weng committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);

    if (impanel->app->keyboardGrabbed()) {
        return;
    }

    if (impanel->engineManager->useGlobalEngine()) {
        return;
    }
    impanel->engineManager->setCurrentContext("");
1084
1085
1086
1087
1088
}

static void
ibus_panel_impanel_register_properties (IBusPanelService *panel,
                                        IBusPropList     *prop_list)
Xuetian Weng's avatar
Xuetian Weng committed
1089
1090
1091
1092
1093
1094
1095
1096
{
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);
    impanel->propManager->setProperties(prop_list);
    ibus_panel_impanel_real_register_properties(impanel);
}

static void
ibus_panel_impanel_real_register_properties(IBusPanelImpanel* impanel)
1097
{
1098
1099
1100
    if (!impanel->conn)
        return;

1101
1102
1103
1104
1105
1106
    IBusProperty* property = NULL;
    guint i = 0;

    GVariantBuilder builder;
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));

1107
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
1108
        auto engine_desc = impanel->engineManager->engines()[impanel->selected];
1109
1110
        QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
        g_variant_builder_add (&builder, "s", propstr.constData());
1111
    } else {
1112
        auto engine_desc = ibus_bus_get_global_engine(impanel->bus);
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
        QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
        g_variant_builder_add (&builder, "s", propstr.constData());

        IBusPropList* prop_list = impanel->propManager->properties();
        if (prop_list) {
            while ( ( property = ibus_prop_list_get( prop_list, i ) ) != NULL ) {
                propstr = ibus_property_to_propstr(property, TRUE);
                g_variant_builder_add (&builder, "s", propstr.constData());
                ++i;
            }
Xuetian Weng's avatar
Xuetian Weng committed
1123
        }
1124
        g_object_unref(engine_desc);
1125
1126
    }

Xuetian Weng's avatar
Xuetian Weng committed
1127
    g_dbus_connection_emit_signal (impanel->conn,
1128
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "RegisterProperties",
Xuetian Weng's avatar
Xuetian Weng committed
1129
                                   (g_variant_new ("(as)", &builder)),
1130
                                   NULL);
1131
}
1132

Xuetian Weng's avatar
Xuetian Weng committed
1133

1134
1135
1136
1137
1138
1139
1140
1141
static void
ibus_panel_impanel_set_cursor_location (IBusPanelService *panel,
                                        gint              x,
                                        gint              y,
                                        gint              w,
                                        gint              h)
{

Xuetian Weng's avatar
Xuetian Weng committed
1142
1143
1144
1145
1146
    g_dbus_connection_call(IBUS_PANEL_IMPANEL (panel)->conn,
                            "org.kde.impanel",
                            "/org/kde/impanel",
                            "org.kde.impanel2",
                            "SetSpotRect",
Xuetian Weng's avatar
Xuetian Weng committed
1147
                            (g_variant_new("(iiii)", x, y, w, h)),
Xuetian Weng's avatar
Xuetian Weng committed
1148
1149
1150
1151
1152
1153
                            NULL,
                            G_DBUS_CALL_FLAGS_NONE,
                           -1,           /* timeout */
                           NULL,
                           NULL,
                           NULL);
1154
1155
1156
1157
1158
1159
1160
}

static void
ibus_panel_impanel_update_auxiliary_text (IBusPanelService *panel,
                                          IBusText         *text,
                                          gboolean          visible)
{
Xuetian Weng's avatar
Xuetian Weng committed
1161
    const gchar* t = ibus_text_get_text (text);
1162
    const gchar *attr = "";
Xuetian Weng's avatar
Xuetian Weng committed
1163
1164
1165
1166
    IBusPanelImpanel* impanel = (IBusPanelImpanel*) panel;

    if (!impanel->conn)
        return;
1167

Xuetian Weng's avatar
Xuetian Weng committed
1168
    g_dbus_connection_emit_signal (impanel->conn,
1169
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateAux",
Xuetian Weng's avatar
Xuetian Weng committed
1170
                                   (g_variant_new ("(ss)", t, attr)),
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
                                   NULL);

    if (visible == 0)
        ibus_panel_impanel_hide_auxiliary_text(panel);
    else
        ibus_panel_impanel_show_auxiliary_text(panel);
}

static void
ibus_panel_impanel_update_lookup_table (IBusPanelService *panel,
                                        IBusLookupTable  *lookup_table,
                                        gboolean          visible)
{
Xuetian Weng's avatar
Xuetian Weng committed
1184
1185
1186
1187
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
    guint page_size = ibus_lookup_table_get_page_size(lookup_table);
    guint cursor_pos = ibus_lookup_table_get_cursor_pos(lookup_table);
    guint page = cursor_pos / page_size;
    guint start = page * page_size;
    guint end = start + page_size;
    guint num = ibus_lookup_table_get_number_of_candidates(lookup_table);
    if (end > num) {
        end = num;
    }

//     fprintf(stderr, "%d ~ %d pgsize %d num %d\n", start, end, page_size, num);

    guint i;

    gchar label[16][4];// WARNING large enough I think --- nihui
Xuetian Weng's avatar
Xuetian Weng committed
1203
    const gchar *candidate;
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218

    GVariantBuilder builder_labels;
    GVariantBuilder builder_candidates;
    GVariantBuilder builder_attrs;
    g_variant_builder_init (&builder_labels, G_VARIANT_TYPE ("as"));
    g_variant_builder_init (&builder_candidates, G_VARIANT_TYPE ("as"));
    g_variant_builder_init (&builder_attrs, G_VARIANT_TYPE ("as"));

    const gchar *attr = "";
    for (i = start; i < end; i++) {
        g_snprintf (label[i-start], 4, "%d", (i-start+1) % 10);
        // NOTE ibus always return NULL for ibus_lookup_table_get_label
//         label = ibus_lookup_table_get_label(lookup_table, i)->text;
        g_variant_builder_add (&builder_labels, "s", label[i-start]);

Xuetian Weng's avatar
Xuetian Weng committed
1219
        candidate = ibus_text_get_text (ibus_lookup_table_get_candidate (lookup_table, i));
1220
1221
1222
1223
1224
1225
1226
1227
        g_variant_builder_add (&builder_candidates, "s", candidate);

        g_variant_builder_add (&builder_attrs, "s", attr);
    }

    gboolean has_prev = 1;
    gboolean has_next = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1228
1229
1230
1231
1232
1233
    guint cursor_pos_in_page;
    if (ibus_lookup_table_is_cursor_visible(lookup_table))
        cursor_pos_in_page = cursor_pos % page_size;
    else
        cursor_pos_in_page = -1;

Xuetian Weng's avatar
Xuetian Weng committed
1234
1235
    gint orientation = ibus_lookup_table_get_orientation(lookup_table);
    if (orientation == IBUS_ORIENTATION_HORIZONTAL) {
1236
        orientation = 2;
Xuetian Weng's avatar
Xuetian Weng committed
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
    } else if (orientation == IBUS_ORIENTATION_VERTICAL) {
        orientation = 1;
    } else {
        orientation = 0;
    }

    g_dbus_connection_call(impanel->conn,
                           "org.kde.impanel",
                           "/org/kde/impanel",
                           "org.kde.impanel2",
                           "SetLookupTable",
                           (g_variant_new ("(asasasbbii)",
                                           &builder_labels,
                                           &builder_candidates,
                                           &builder_attrs,
                                           has_prev, has_next,
                                           cursor_pos_in_page, orientation)),
                           NULL,
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
                           NULL,
                           NULL,
                           NULL);
Xuetian Weng's avatar
Xuetian Weng committed
1260

1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
    if (visible == 0)
        ibus_panel_impanel_hide_lookup_table(panel);
    else
        ibus_panel_impanel_show_lookup_table(panel);
}

static void
ibus_panel_impanel_update_preedit_text (IBusPanelService *panel,
                                        IBusText         *text,
                                        guint             cursor_pos,
                                        gboolean          visible)
{
Xuetian Weng's avatar
Xuetian Weng committed
1273
1274
1275
1276
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

Xuetian Weng's avatar
Xuetian Weng committed
1277
    const gchar* t = ibus_text_get_text (text);
1278
1279
    const gchar *attr = "";

Xuetian Weng's avatar
Xuetian Weng committed
1280
    g_dbus_connection_emit_signal (impanel->conn,
1281
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditText",
Xuetian Weng's avatar
Xuetian Weng committed
1282
                                   (g_variant_new ("(ss)", t, attr)),
1283
1284
                                   NULL);

Xuetian Weng's avatar
Xuetian Weng committed
1285
    g_dbus_connection_emit_signal (impanel->conn,
1286
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditCaret",
Xuetian Weng's avatar
Xuetian Weng committed
1287
                                   (g_variant_new ("(i)", cursor_pos)),
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
                                   NULL);

    if (visible == 0)
        ibus_panel_impanel_hide_preedit_text(panel);
    else
        ibus_panel_impanel_show_preedit_text(panel);
}

static void
ibus_panel_impanel_update_property (IBusPanelService *panel,
                                    IBusProperty     *prop)
{
Xuetian Weng's avatar
Xuetian Weng committed
1300
1301
1302
1303
1304
1305
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

    impanel->propManager->updateProperty(prop);

1306
    QByteArray propstr = ibus_property_to_propstr(prop, TRUE);
1307

Xuetian Weng's avatar
Xuetian Weng committed
1308
    g_dbus_connection_emit_signal (impanel->conn,
1309
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateProperty",
1310
                                   (g_variant_new ("(s)", propstr.constData())),
1311
1312
1313
1314
1315
1316
                                   NULL);
}

static void
ibus_panel_impanel_cursor_down_lookup_table (IBusPanelService *panel)
{
1317
    Q_UNUSED(panel);
1318
1319
1320
1321
1322
}

static void
ibus_panel_impanel_cursor_up_lookup_table (IBusPanelService *panel)
{
1323
    Q_UNUSED(panel);
1324
1325
1326
1327
1328
}

static void
ibus_panel_impanel_hide_auxiliary_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1329
1330
1331
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1332
1333
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1334
    g_dbus_connection_emit_signal (impanel->conn,
1335
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowAux",
Xuetian Weng's avatar
Xuetian Weng committed
1336
                                   (g_variant_new ("(b)", toShow)),
1337
1338
1339
1340
1341
1342
                                   NULL);
}

static void
ibus_panel_impanel_hide_language_bar (IBusPanelService *panel)
{
1343
    Q_UNUSED(panel);
1344
1345
1346
1347
1348
}

static void
ibus_panel_impanel_hide_lookup_table (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1349
1350
1351
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1352
1353
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1354
    g_dbus_connection_emit_signal (impanel->conn,
1355
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowLookupTable",
Xuetian Weng's avatar
Xuetian Weng committed
1356
                                   (g_variant_new ("(b)", toShow)),
1357
1358
1359
1360
1361
1362
                                   NULL);
}

static void
ibus_panel_impanel_hide_preedit_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1363
1364
1365
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1366
1367
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1368
    g_dbus_connection_emit_signal (impanel->conn,
1369
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowPreedit",
Xuetian Weng's avatar
Xuetian Weng committed
1370
                                   (g_variant_new ("(b)", toShow)),
1371
1372
1373
1374
1375
1376
                                   NULL);
}

static void
ibus_panel_impanel_page_down_lookup_table (IBusPanelService *panel)
{
1377
    Q_UNUSED(panel);
1378
1379
1380
1381
1382
}

static void
ibus_panel_impanel_page_up_lookup_table (IBusPanelService *panel)
{
1383
    Q_UNUSED(panel);
1384
1385
1386
1387
1388
}

static void
ibus_panel_impanel_reset (IBusPanelService *panel)
{
1389
    Q_UNUSED(panel);
1390
1391
1392
1393
1394
}

static void
ibus_panel_impanel_show_auxiliary_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1395
1396
1397
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1398
1399
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1400
    g_dbus_connection_emit_signal (impanel->conn,
1401
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowAux",
Xuetian Weng's avatar
Xuetian Weng committed
1402
                                   (g_variant_new ("(b)", toShow)),
1403
1404
1405
1406
1407
1408
                                   NULL);
}

static void
ibus_panel_impanel_show_language_bar (IBusPanelService *panel)
{
1409
    Q_UNUSED(panel);
1410
1411
1412
1413
1414
}

static void
ibus_panel_impanel_show_lookup_table (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1415
1416
1417
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1418
1419
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1420
    g_dbus_connection_emit_signal (impanel->conn,
1421
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowLookupTable",
Xuetian Weng's avatar
Xuetian Weng committed
1422
                                   (g_variant_new ("(b)", toShow)),
1423
1424
1425
1426
1427
1428
                                   NULL);
}

static void
ibus_panel_impanel_show_preedit_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1429
1430
1431
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1432
1433
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1434
    g_dbus_connection_emit_signal (impanel->conn,
1435
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowPreedit",
Xuetian Weng's avatar
Xuetian Weng committed
1436
                                   (g_variant_new ("(b)", toShow)),
1437
1438
1439
1440
1441
1442
                                   NULL);
}

static void
ibus_panel_impanel_start_setup (IBusPanelService *panel)
{
1443
    Q_UNUSED(panel);
1444
1445
1446
1447
}

static void
ibus_panel_impanel_state_changed (IBusPanelService *panel)
1448
{
Xuetian Weng's avatar
Xuetian Weng committed
1449
1450
1451
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
Xuetian Weng's avatar
Xuetian Weng committed
1452
1453
1454
1455
1456

    if (impanel->app->keyboardGrabbed()) {
        return;
    }

Xuetian Weng's avatar
Xuetian Weng committed
1457
    IBusEngineDesc *engine_desc = ibus_bus_get_global_engine(impanel->bus);
1458
1459
    if (!engine_desc) {
        return;
Xuetian Weng's avatar
Xuetian Weng committed
1460
    }
1461

Xuetian Weng's avatar
Xuetian Weng committed
1462
    impanel_update_logo_by_engine(impanel, engine_desc);
1463

Xuetian Weng's avatar
Xuetian Weng committed
1464
    g_dbus_connection_emit_signal (impanel->conn,
1465
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "Enable",
Xuetian Weng's avatar
Xuetian Weng committed
1466
                                   (g_variant_new ("(b)", TRUE)),
1467
                                   NULL);
Xuetian Weng's avatar
Xuetian Weng committed
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484

    impanel->engineManager->moveToFirst(engine_desc);
    QStringList engineList = impanel->engineManager->engineOrder();

    gchar** engine_names = g_new0 (gchar*, engineList.size() + 1);
    size_t i = 0;
    Q_FOREACH(const QString& name, engineList) {
        engine_names[i] = g_strdup (name.toUtf8().constData());
        i ++;
    }

    IBusConfig* config = ibus_bus_get_config(impanel->bus);
    if (config) {
        GVariant* var = g_variant_new_strv(engine_names, engineList.size());
        ibus_config_set_value(config, "general", "engines-order", var);
    }
    g_strfreev(engine_names);
1485
    g_object_unref(engine_desc);
1486
1487
1488
}

static void
Xuetian Weng's avatar
Xuetian Weng committed
1489
1490
ibus_panel_impanel_exec_menu(IBusPanelImpanel* impanel,
                             IBusPropList* prop_list)
1491
{
Xuetian Weng's avatar
Xuetian Weng committed
1492
1493
    if (!impanel->conn)
        return;
1494

Xuetian Weng's avatar
Xuetian Weng committed
1495
1496
1497
1498
1499
1500
1501
1502
    GVariantBuilder builder;
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));

    int i = 0;
    while (true) {
        IBusProperty* prop = ibus_prop_list_get(prop_list, i);
        if (!prop)
            break;
1503
1504
        QByteArray propstr = ibus_property_to_propstr(prop);
        g_variant_builder_add (&builder, "s", propstr.constData());
Xuetian Weng's avatar
Xuetian Weng committed
1505
1506
        i ++;
    }
1507

Xuetian Weng's avatar
Xuetian Weng committed
1508
1509
1510
    g_dbus_connection_emit_signal (impanel->conn,
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ExecMenu",
                                   (g_variant_new ("(as)", &builder)),
1511
1512
1513
                                   NULL);
}

Xuetian Weng's avatar
Xuetian Weng committed
1514

1515
static void
Xuetian Weng's avatar
Xuetian Weng committed
1516
ibus_panel_impanel_exec_im_menu (IBusPanelImpanel* impanel)
1517
{
Xuetian Weng's avatar
Xuetian Weng committed
1518
1519
    if (!impanel->conn)
        return;
1520
1521
1522
1523

    GVariantBuilder builder;
    g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));

Xuetian Weng's avatar
Xuetian Weng committed
1524
1525
1526
1527
    IBusEngineDesc** engines = impanel->engineManager->engines();
    if (</