panel.cpp 56.4 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
173
174
    }

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

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

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

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
195
196
197
}


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
230
231
232
    "    <signal name='UpdateLookupTableCursor'>"
    "      <arg type='i' name='pos'/>"
    "    </signal>"
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
    "    <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[] = ":";

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

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

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

    return propstr;
283
284
}

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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);
}

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

    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
340
    }
341

342
343
344
345
346
347
348
349
350
351
352
353
354
    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";
        }
    }



355
356
357
    return ibus_property_args_to_propstr(ibus_property_get_key (property),
                                         label,
                                         icon,
358
359
                                         tooltip,
                                         hint);
360
361
}

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

386
387
static QByteArray
ibus_engine_desc_to_propstr (IBusEngineDesc *engine_desc)
388
{
389
390
391
392
393
    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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
}

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);

521
    impanel->engineManager->setEngines(engines);
Xuetian Weng's avatar
Xuetian Weng committed
522
523

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

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

Xuetian Weng's avatar
Xuetian Weng committed
530
531
532
533
534
535
536
537
538
539
540
541
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);
    if (len == 0) {
        return;
    }

    impanel->engineManager->setOrder(engine_names, len);

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

Xuetian Weng's avatar
Xuetian Weng committed
546
547
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
596
597
598
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);
}

static void
impanel_config_value_changed_callback (IBusConfig* config, const gchar* section, const gchar* name, GVariant* value, gpointer user_data)
{
599
    Q_UNUSED(config);
Xuetian Weng's avatar
Xuetian Weng committed
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
    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)
{
623
624
625
626
627
628
    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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
    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)
{
644
645
646
647
648
649
    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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
    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
664
665
666
            if (engine_desc) {
                impanel->xkbLayoutManager->setLayout(engine_desc);
            }
Xuetian Weng's avatar
Xuetian Weng committed
667
668
        }
        impanel->engineManager->setCurrentEngine(name);
669
670
    } else {
        qDebug() << "set engine failed.";
Xuetian Weng's avatar
Xuetian Weng committed
671
    }
672
673
674
675
676
677
678
679
680
681
682
}

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)
{
683
684
685
686
687
    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
688
689
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
    gchar *s0 = NULL;
690
    g_variant_get (parameters, "(s)", &s0);
Xuetian Weng's avatar
Xuetian Weng committed
691
692
693
694
695
696
697
698
    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);
699
    }
Xuetian Weng's avatar
Xuetian Weng committed
700
701
702
703
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
    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);
733
734
}

735
736
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
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);
}

796
797
798
799
800
801
802
803
804
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)
{
805
806
807
808
809
810
811
    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);
812
813
814
815
816
817
818
819
820
821
822
823
    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)
{
824
    Q_UNUSED(name);
Xuetian Weng's avatar
Xuetian Weng committed
825
826
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
    impanel->conn = connection;
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845

    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);
846
847
848
849
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
    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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    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);
896
897
898
899
900
901
902
903
904
905
    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
906
907
908
909
910
911
912
913
914
915
916
917
918

    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
919
920
921
922
923
924
        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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
        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);
951
952
953
954
955
956
957
}

static void
on_name_acquired (GDBusConnection *connection,
                  const gchar     *name,
                  gpointer         user_data)
{
958
959
960
    Q_UNUSED(connection);
    Q_UNUSED(name);
    Q_UNUSED(user_data);
961
962
963
964
965
966
967
}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
968
969
970
    Q_UNUSED(connection);
    Q_UNUSED(name);
    Q_UNUSED(user_data);
971
972
973
974
975
976
    exit (1);
}

G_DEFINE_TYPE (IBusPanelImpanel, ibus_panel_impanel, IBUS_TYPE_PANEL_SERVICE)

static void
Xuetian Weng's avatar
Xuetian Weng committed
977
ibus_panel_impanel_class_init (IBusPanelImpanelClass *klass)
978
{
Xuetian Weng's avatar
Xuetian Weng committed
979
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
980
981
982
983
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

    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
1011
1012
    impanel->app = NULL;
    impanel->useSystemKeyboardLayout = false;
Xuetian Weng's avatar
Xuetian Weng committed
1013
    impanel->selected = -1;
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023

    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
1024
1025
1026
    impanel->propManager = new PropertyManager;
    impanel->engineManager = new EngineManager;
    impanel->xkbLayoutManager = new XkbLayoutManager;
1027
1028
1029
1030
1031
}

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

    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
1049
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);
Xuetian Weng's avatar
Xuetian Weng committed
1050
1051
1052
    if (impanel->app->keyboardGrabbed()) {
        return;
    }
Xuetian Weng's avatar
Xuetian Weng committed
1053

1054
    IBusEngineDesc *engine_desc = ibus_bus_get_global_engine(impanel->bus);
Xuetian Weng's avatar
Xuetian Weng committed
1055
    impanel_update_logo_by_engine(impanel, engine_desc);
1056

Xuetian Weng's avatar
Xuetian Weng committed
1057
1058
1059
1060
    impanel->engineManager->setCurrentContext(input_context_path);
    if (!impanel->engineManager->useGlobalEngine()) {
        impanel_set_engine(impanel, impanel->engineManager->currentEngine().toUtf8().constData());
    }
1061
1062
1063
1064
1065
1066
}

static void
ibus_panel_impanel_focus_out (IBusPanelService *panel,
                              const gchar      *input_context_path)
{
1067
1068
    Q_UNUSED(panel);
    Q_UNUSED(input_context_path);
Xuetian Weng's avatar
Xuetian Weng committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);

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

    if (impanel->engineManager->useGlobalEngine()) {
        return;
    }
    impanel->engineManager->setCurrentContext("");
1079
1080
1081
1082
1083
}

static void
ibus_panel_impanel_register_properties (IBusPanelService *panel,
                                        IBusPropList     *prop_list)
Xuetian Weng's avatar
Xuetian Weng committed
1084
1085
1086
1087
1088
1089
1090
1091
{
    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)
1092
{
1093
1094
1095
    if (!impanel->conn)
        return;

1096
1097
1098
1099
1100
1101
    IBusProperty* property = NULL;
    guint i = 0;

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

1102
1103
1104
    IBusEngineDesc* engine_desc = NULL;
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
        engine_desc = impanel->engineManager->engines()[impanel->selected];
1105
1106
        QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
        g_variant_builder_add (&builder, "s", propstr.constData());
1107
1108
    } else {
        engine_desc = ibus_bus_get_global_engine(impanel->bus);
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
        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
1119
        }
1120
1121
    }

Xuetian Weng's avatar
Xuetian Weng committed
1122
    g_dbus_connection_emit_signal (impanel->conn,
1123
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "RegisterProperties",
Xuetian Weng's avatar
Xuetian Weng committed
1124
                                   (g_variant_new ("(as)", &builder)),
1125
                                   NULL);
1126
}
1127

Xuetian Weng's avatar
Xuetian Weng committed
1128

1129
1130
1131
1132
1133
1134
1135
1136
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
1137
1138
1139
1140
1141
    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
1142
                            (g_variant_new("(iiii)", x, y, w, h)),
Xuetian Weng's avatar
Xuetian Weng committed
1143
1144
1145
1146
1147
1148
                            NULL,
                            G_DBUS_CALL_FLAGS_NONE,
                           -1,           /* timeout */
                           NULL,
                           NULL,
                           NULL);
1149
1150
1151
1152
1153
1154
1155
}

static void
ibus_panel_impanel_update_auxiliary_text (IBusPanelService *panel,
                                          IBusText         *text,
                                          gboolean          visible)
{
Xuetian Weng's avatar
Xuetian Weng committed
1156
    const gchar* t = ibus_text_get_text (text);
1157
    const gchar *attr = "";
Xuetian Weng's avatar
Xuetian Weng committed
1158
1159
1160
1161
    IBusPanelImpanel* impanel = (IBusPanelImpanel*) panel;

    if (!impanel->conn)
        return;
1162

Xuetian Weng's avatar
Xuetian Weng committed
1163
    g_dbus_connection_emit_signal (impanel->conn,
1164
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateAux",
Xuetian Weng's avatar
Xuetian Weng committed
1165
                                   (g_variant_new ("(ss)", t, attr)),
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
                                   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
1179
1180
1181
1182
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
    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
1198
    const gchar *candidate;
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213

    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
1214
        candidate = ibus_text_get_text (ibus_lookup_table_get_candidate (lookup_table, i));
1215
1216
1217
1218
1219
1220
1221
1222
        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
1223
1224
1225
1226
1227
1228
    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
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
    gint orientation = ibus_lookup_table_get_orientation(lookup_table);
    if (orientation == IBUS_ORIENTATION_HORIZONTAL) {
        orientation = 2; 
    } 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
1255

1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
    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
1268
1269
1270
1271
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

Xuetian Weng's avatar
Xuetian Weng committed
1272
    const gchar* t = ibus_text_get_text (text);
1273
1274
    const gchar *attr = "";

Xuetian Weng's avatar
Xuetian Weng committed
1275
    g_dbus_connection_emit_signal (impanel->conn,
1276
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditText",
Xuetian Weng's avatar
Xuetian Weng committed
1277
                                   (g_variant_new ("(ss)", t, attr)),
1278
1279
                                   NULL);

Xuetian Weng's avatar
Xuetian Weng committed
1280
    g_dbus_connection_emit_signal (impanel->conn,
1281
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditCaret",
Xuetian Weng's avatar
Xuetian Weng committed
1282
                                   (g_variant_new ("(i)", cursor_pos)),
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
                                   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
1295
1296
1297
1298
1299
1300
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

    impanel->propManager->updateProperty(prop);

1301
    QByteArray propstr = ibus_property_to_propstr(prop, TRUE);
1302

Xuetian Weng's avatar
Xuetian Weng committed
1303
    g_dbus_connection_emit_signal (impanel->conn,
1304
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateProperty",
1305
                                   (g_variant_new ("(s)", propstr.constData())),
1306
1307
1308
1309
1310
1311
                                   NULL);
}

static void
ibus_panel_impanel_cursor_down_lookup_table (IBusPanelService *panel)
{
1312
    Q_UNUSED(panel);
1313
1314
1315
1316
1317
}

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

static void
ibus_panel_impanel_hide_auxiliary_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1324
1325
1326
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1327
1328
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1329
    g_dbus_connection_emit_signal (impanel->conn,
1330
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowAux",
Xuetian Weng's avatar
Xuetian Weng committed
1331
                                   (g_variant_new ("(b)", toShow)),
1332
1333
1334
1335
1336
1337
                                   NULL);
}

static void
ibus_panel_impanel_hide_language_bar (IBusPanelService *panel)
{
1338
    Q_UNUSED(panel);
1339
1340
1341
1342
1343
}

static void
ibus_panel_impanel_hide_lookup_table (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1344
1345
1346
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1347
1348
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1349
    g_dbus_connection_emit_signal (impanel->conn,
1350
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowLookupTable",
Xuetian Weng's avatar
Xuetian Weng committed
1351
                                   (g_variant_new ("(b)", toShow)),
1352
1353
1354
1355
1356
1357
                                   NULL);
}

static void
ibus_panel_impanel_hide_preedit_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1358
1359
1360
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1361
1362
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1363
    g_dbus_connection_emit_signal (impanel->conn,
1364
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowPreedit",
Xuetian Weng's avatar
Xuetian Weng committed
1365
                                   (g_variant_new ("(b)", toShow)),
1366
1367
1368
1369
1370
1371
                                   NULL);
}

static void
ibus_panel_impanel_page_down_lookup_table (IBusPanelService *panel)
{
1372
    Q_UNUSED(panel);
1373
1374
1375
1376
1377
}

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

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

static void
ibus_panel_impanel_show_auxiliary_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1390
1391
1392
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1393
1394
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1395
    g_dbus_connection_emit_signal (impanel->conn,
1396
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowAux",
Xuetian Weng's avatar
Xuetian Weng committed
1397
                                   (g_variant_new ("(b)", toShow)),
1398
1399
1400
1401
1402
1403
                                   NULL);
}

static void
ibus_panel_impanel_show_language_bar (IBusPanelService *panel)
{
1404
    Q_UNUSED(panel);
1405
1406
1407
1408
1409
}

static void
ibus_panel_impanel_show_lookup_table (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1410
1411
1412
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1413
1414
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1415
    g_dbus_connection_emit_signal (impanel->conn,
1416
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowLookupTable",
Xuetian Weng's avatar
Xuetian Weng committed
1417
                                   (g_variant_new ("(b)", toShow)),
1418
1419
1420
1421
1422
1423
                                   NULL);
}

static void
ibus_panel_impanel_show_preedit_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1424
1425
1426
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1427
1428
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1429
    g_dbus_connection_emit_signal (impanel->conn,
1430
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowPreedit",
Xuetian Weng's avatar
Xuetian Weng committed
1431
                                   (g_variant_new ("(b)", toShow)),
1432
1433
1434
1435
1436
1437
                                   NULL);
}

static void
ibus_panel_impanel_start_setup (IBusPanelService *panel)
{
1438
    Q_UNUSED(panel);
1439
1440
1441
1442
}

static void
ibus_panel_impanel_state_changed (IBusPanelService *panel)
1443
{
Xuetian Weng's avatar
Xuetian Weng committed
1444
1445
1446
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
Xuetian Weng's avatar
Xuetian Weng committed
1447
1448
1449
1450
1451

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

Xuetian Weng's avatar
Xuetian Weng committed
1452
    IBusEngineDesc *engine_desc = ibus_bus_get_global_engine(impanel->bus);
1453
1454
    if (!engine_desc) {
        return;
Xuetian Weng's avatar
Xuetian Weng committed
1455
    }
1456

Xuetian Weng's avatar
Xuetian Weng committed
1457
    impanel_update_logo_by_engine(impanel, engine_desc);
1458

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

    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);
1480
1481
1482
}

static void
Xuetian Weng's avatar
Xuetian Weng committed
1483
1484
ibus_panel_impanel_exec_menu(IBusPanelImpanel* impanel,
                             IBusPropList* prop_list)
1485
{
Xuetian Weng's avatar
Xuetian Weng committed
1486
1487
    if (!impanel->conn)
        return;
1488

Xuetian Weng's avatar
Xuetian Weng committed
1489
1490
1491
1492
1493
1494
1495
1496
    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;
1497
1498
        QByteArray propstr = ibus_property_to_propstr(prop);
        g_variant_builder_add (&builder, "s", propstr.constData());
Xuetian Weng's avatar
Xuetian Weng committed
1499
1500
        i ++;
    }
1501

Xuetian Weng's avatar
Xuetian Weng committed
1502
1503
1504
    g_dbus_connection_emit_signal (impanel->conn,
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ExecMenu",
                                   (g_variant_new ("(as)", &builder)),
1505
1506
1507
                                   NULL);
}

Xuetian Weng's avatar
Xuetian Weng committed
1508

1509
static void
Xuetian Weng's avatar
Xuetian Weng committed
1510
ibus_panel_impanel_exec_im_menu (IBusPanelImpanel* impanel)
1511
{
Xuetian Weng's avatar
Xuetian Weng committed
1512
1513
    if (!impanel->conn)
        return;
1514
1515
1516
1517

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

Xuetian Weng's avatar
Xuetian Weng committed
1518
1519
1520
1521
    IBusEngineDesc** engines = impanel->engineManager->engines();
    if (engines) {
        int i = 0;
        while (engines[i]) {
1522
1523
            QByteArray propstr = ibus_engine_desc_to_propstr(engines[i]);
            g_variant_builder_add (&builder, "s", propstr.constData());
Xuetian Weng's avatar
Xuetian Weng committed
1524
1525
            i ++;
        }
1526
1527
    }

Xuetian Weng's avatar
Xuetian Weng committed
1528
    g_dbus_connection_emit_signal (impanel->conn,
1529
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ExecMenu",
Xuetian Weng's avatar
Xuetian Weng committed
1530
                                   (g_variant_new ("(as)", &builder)),
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
                                   NULL);
}

IBusPanelImpanel *
ibus_panel_impanel_new (GDBusConnection    *connection)
{
    IBusPanelImpanel *panel;
    panel = (IBusPanelImpanel *) g_object_new (IBUS_TYPE_PANEL_IMPANEL,
                                               "object-path", IBUS_PATH_PANEL,
                                               "connection", connection,
                                               NULL);
    return panel;
}
Xuetian Weng's avatar
Xuetian Weng committed
1544