panel.cpp 57.7 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
#define IBUS_SCHEMA_GENERAL "org.freedesktop.ibus.general"
#define IBUS_SCHEMA_HOTKEY  "org.freedesktop.ibus.general.hotkey"
#define IBUS_SCHEMA_PANEL   "org.freedesktop.ibus.panel"

46
47
48
49
50
51
typedef struct _IBusPanelImpanelClass IBusPanelImpanelClass;

struct _IBusPanelImpanel {
    IBusPanelService    parent;
    IBusBus            *bus;
    GDBusConnection    *conn;
Xuetian Weng's avatar
Xuetian Weng committed
52
53
54
55
56
    PropertyManager* propManager;
    EngineManager* engineManager;
    XkbLayoutManager* xkbLayoutManager;
    App* app;
    gboolean useSystemKeyboardLayout;
Xuetian Weng's avatar
Xuetian Weng committed
57
    int selected;
58
59
    GSettings *settings_general;
    GSettings *settings_hotkey;
60
61
62
63
64
65
};

struct _IBusPanelImpanelClass {
    IBusPanelServiceClass parent;
};

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* 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
116
117
118
119

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

Xuetian Weng's avatar
Xuetian Weng committed
120

121
static QByteArray
Xuetian Weng's avatar
Xuetian Weng committed
122
ibus_property_to_propstr (IBusProperty *property,
123
124
125
126
                          gboolean useSymbol = FALSE);

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

Xuetian Weng's avatar
Xuetian Weng committed
128
129
130
131
132
133
134
void
impanel_update_logo_by_engine(IBusPanelImpanel* impanel, IBusEngineDesc* engine_desc)
{
    if (!impanel->conn) {
        return;
    }

135
    QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
136
137

    g_dbus_connection_emit_signal (impanel->conn,
138
                                    nullptr, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateProperty",
139
                                    (g_variant_new ("(s)", propstr.constData())),
140
                                    nullptr);
Xuetian Weng's avatar
Xuetian Weng committed
141
142
}

143
144
145
146
147
148
149
void
ibus_panel_impanel_set_bus (IBusPanelImpanel *impanel,
                            IBusBus          *bus)
{
    impanel->bus = bus;
}

Xuetian Weng's avatar
Xuetian Weng committed
150
151
152
153
154
void ibus_panel_impanel_set_app(IBusPanelImpanel* impanel, App* app)
{
    impanel->app = app;
}

Xuetian Weng's avatar
Xuetian Weng committed
155
156
void ibus_panel_impanel_accept(IBusPanelImpanel* impanel)
{
157
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
Xuetian Weng's avatar
Xuetian Weng committed
158
159
160
161
162
        impanel_set_engine(impanel, ibus_engine_desc_get_name(impanel->engineManager->engines()[impanel->selected]));
        impanel->selected = -1;
    }
}

163
void ibus_panel_impanel_navigate(IBusPanelImpanel* impanel, gboolean start, gboolean forward)
Xuetian Weng's avatar
Xuetian Weng committed
164
{
Xuetian Weng's avatar
Xuetian Weng committed
165
166
167
168
169
    if (start) {
        impanel->selected = -1;
    }

    if (impanel->engineManager->length() < 2) {
Xuetian Weng's avatar
Xuetian Weng committed
170
171
172
        return;
    }

173
    IBusEngineDesc* engine_desc = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
174
175
    if (impanel->selected < 0) {
        engine_desc = ibus_bus_get_global_engine(impanel->bus);
176
    } else if (static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
Xuetian Weng's avatar
Xuetian Weng committed
177
        engine_desc = impanel->engineManager->engines()[impanel->selected];
178
        g_object_ref(engine_desc);
179
180
181
    }

    if (!engine_desc) {
Xuetian Weng's avatar
Xuetian Weng committed
182
        engine_desc = impanel->engineManager->engines()[0];
183
        g_object_ref(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
184
185
186
    }

    if (engine_desc) {
187
        const char* name = impanel->engineManager->navigate(engine_desc, forward);
Xuetian Weng's avatar
Xuetian Weng committed
188
        impanel->selected = impanel->engineManager->getIndexByName(name);
189
        g_object_unref(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
190
191
    } else {
        return;
Xuetian Weng's avatar
Xuetian Weng committed
192
193
    }

194
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
195
        ibus_panel_impanel_real_register_properties(impanel);
Xuetian Weng's avatar
Xuetian Weng committed
196
197
198
199
200
201
202
203
    }
}

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
204
205
206
}


207
static GDBusNodeInfo *introspection_data = nullptr;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

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
239
240
241
    "    <signal name='UpdateLookupTableCursor'>"
    "      <arg type='i' name='pos'/>"
    "    </signal>"
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    "    <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[] = ":";

268
static QByteArray
269
270
271
ibus_property_args_to_propstr (const char *key,
                               const char *label,
                               const char *icon,
272
273
                               const char *tooltip,
                               const char *hint = "")
274
{
Xuetian Weng's avatar
Xuetian Weng committed
275
    QByteArray propstr("/IBus/");
Xuetian Weng's avatar
Xuetian Weng committed
276
277
    QByteArray str(key);
    str.replace(':', '!');
278
279
280
281

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

    propstr += str;
282
    propstr += prop_sep;
283
    propstr += QByteArray(label).replace(':', '-').constData();
284
    propstr += prop_sep;
285
    propstr += app->normalizeIconName(QByteArray(icon).replace(':', '-'));
286
    propstr += prop_sep;
287
    propstr += QByteArray(tooltip).replace(':', '-').constData();
288
289
    propstr += prop_sep;
    propstr += QByteArray(hint).replace(':', '-').constData();
290
291

    return propstr;
292
293
}

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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);
}

334
static QByteArray
335
ibus_property_to_propstr (IBusProperty *property,
336
                          gboolean useSymbol)
337
{
338
    const gchar* label = nullptr;
339
    const gchar* tooltip = ibus_text_get_text (ibus_property_get_tooltip (property));
Xuetian Weng's avatar
Xuetian Weng committed
340
    const gchar* icon = ibus_property_get_icon (property);
341
342
343
344
345
346
347
348

    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
349
    }
350

351
352
353
354
355
356
357
358
359
360
361
362
363
    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";
        }
    }



364
365
366
    return ibus_property_args_to_propstr(ibus_property_get_key (property),
                                         label,
                                         icon,
367
368
                                         tooltip,
                                         hint);
369
370
}

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

395
396
static QByteArray
ibus_engine_desc_to_propstr (IBusEngineDesc *engine_desc)
397
{
398
399
400
401
402
    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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
}

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];
        }

427
        for (GList* engine = g_list_first(engines); engine != nullptr ;
Xuetian Weng's avatar
Xuetian Weng committed
428
429
430
431
432
433
434
435
436
437
438
439
440
            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;
            }
        }
    }
441
    const char* locale = setlocale(LC_CTYPE, nullptr);
Xuetian Weng's avatar
Xuetian Weng committed
442
443
444
445
446
447
448
449
    if (!locale) {
        locale = "C";
    }

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

    bool added = false;
450
    for (GList* engine = g_list_first(engines); engine != nullptr ;
Xuetian Weng's avatar
Xuetian Weng committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
        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] : "";

469
        for (GList* engine = g_list_first(engines); engine != nullptr ;
Xuetian Weng's avatar
Xuetian Weng committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
            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;
            }
        }
    }

484
    for (GList* engine = g_list_first(engines); engine != nullptr ;
Xuetian Weng's avatar
Xuetian Weng committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        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 ++;
        }
    }
}

508
509
510
511
512
513
514
515
516
bool contains(gchar** strlist, const gchar* str)
{
    for (; strlist; ++strlist) {
        if (g_strcmp0(*strlist, str) == 0)
            return true;
    }
    return false;
}

Xuetian Weng's avatar
Xuetian Weng committed
517
518
static void
impanel_update_engines(IBusPanelImpanel* impanel, GVariant* var_engines) {
519
    gchar** engine_names = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
520
521
522
523
524
525
    size_t len = 0;
    if (var_engines) {
        engine_names = g_variant_dup_strv(var_engines, &len);
    }
    if (len == 0) {
        g_strfreev(engine_names);
526
        engine_names = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
527
528
529
530
531
    }

    if (!engine_names) {
        impanel_get_default_engine(impanel, &engine_names, &len);
        GVariant* var = g_variant_new_strv(engine_names, len);
532
        g_settings_set_value(impanel->settings_general, "preload-engines", var);
Xuetian Weng's avatar
Xuetian Weng committed
533
534
535
536
    }

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

537
    impanel->engineManager->setEngines(engines);
538
    if (engines && engines[0] && (!ibus_bus_get_global_engine(impanel->bus) || !contains(engine_names, ibus_engine_desc_get_name(ibus_bus_get_global_engine(impanel->bus))))) {
539
        ibus_bus_set_global_engine(impanel->bus, ibus_engine_desc_get_name(engines[0]));
Xuetian Weng's avatar
Xuetian Weng committed
540
    }
541
    g_strfreev(engine_names);
Xuetian Weng's avatar
Xuetian Weng committed
542
543
544
545

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

Xuetian Weng's avatar
Xuetian Weng committed
546
547
static void
impanel_update_engines_order(IBusPanelImpanel* impanel, GVariant* var_engines) {
548
    const gchar** engine_names = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
549
550
    size_t len = 0;
    engine_names = g_variant_get_strv(var_engines, &len);
551
552
    if (len) {
        impanel->engineManager->setOrder(engine_names, len);
Xuetian Weng's avatar
Xuetian Weng committed
553

554
555
556
        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
557
    }
558
    g_free(engine_names);
Xuetian Weng's avatar
Xuetian Weng committed
559
560
}

Xuetian Weng's avatar
Xuetian Weng committed
561
562
static void
impanel_update_triggers(IBusPanelImpanel* impanel, GVariant* variant) {
563
    gchar** triggers = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
564
565
566
567
568
569
    size_t len = 0;
    if (variant) {
        triggers = g_variant_dup_strv(variant, &len);
    }
    if (len == 0) {
        g_strfreev(triggers);
570
        triggers = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
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
599
600
601
602
603
604
605
606
607
608
    }
    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);
609
    g_free(variants);
Xuetian Weng's avatar
Xuetian Weng committed
610
611
612
}

static void
613
impanel_settings_changed_callback (GSettings* settings, const gchar* key, gpointer user_data)
Xuetian Weng's avatar
Xuetian Weng committed
614
615
{
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
616
    gchar *schema = nullptr;
617
618
619
    GVariant *value = g_settings_get_value (settings, key);

    g_object_get(G_OBJECT(settings), "schema", &schema, NULL);
Xuetian Weng's avatar
Xuetian Weng committed
620

621
    if (g_strcmp0(schema, IBUS_SCHEMA_GENERAL) == 0 && g_strcmp0(key, "preload-engines") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
622
623
        impanel_update_engines(impanel, value);
    }
624
    else if (g_strcmp0(schema, IBUS_SCHEMA_HOTKEY) == 0 && g_strcmp0(key, "triggers") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
625
        impanel_update_triggers(impanel, value);
626
    } else if (g_strcmp0(schema, IBUS_SCHEMA_GENERAL) == 0 && g_strcmp0(key, "use-system-keyboard-layout") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
627
        impanel_update_use_system_keyboard_layout(impanel, value);
628
    } else if (g_strcmp0(schema, IBUS_SCHEMA_GENERAL) == 0 && g_strcmp0(key, "use-global-engine") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
629
630
        impanel_update_use_global_engine(impanel, value);
    }
631
    g_free (schema);
Xuetian Weng's avatar
Xuetian Weng committed
632
633
634
635
636
637
638
639
640
641
642
}

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)
{
643
644
645
646
647
648
    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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
    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)
{
664
665
666
667
668
669
    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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
    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
684
685
686
            if (engine_desc) {
                impanel->xkbLayoutManager->setLayout(engine_desc);
            }
687
            g_object_unref(engine_desc);
Xuetian Weng's avatar
Xuetian Weng committed
688
689
        }
        impanel->engineManager->setCurrentEngine(name);
690
691
    } else {
        qDebug() << "set engine failed.";
Xuetian Weng's avatar
Xuetian Weng committed
692
    }
693
694
695
696
697
698
699
700
701
702
703
}

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)
{
704
705
706
707
708
    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
709
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
710
    gchar *s0 = nullptr;
711
    g_variant_get (parameters, "(s)", &s0);
Xuetian Weng's avatar
Xuetian Weng committed
712
713
714
715
716
717
718
719
    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);
720
    }
Xuetian Weng's avatar
Xuetian Weng committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
    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);
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
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);
}

817
818
819
820
821
822
823
824
825
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)
{
826
827
828
829
830
831
832
    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);
833
834
    pid_t pid = fork();
    if (pid == 0) {
835
        execlp ("ibus-setup", "ibus-setup", (char *)nullptr);
836
837
838
839
840
841
842
843
844
        exit (0);
    }
}

static void
on_bus_acquired (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
845
    Q_UNUSED(name);
Xuetian Weng's avatar
Xuetian Weng committed
846
847
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
    impanel->conn = connection;
848
849
850
851

    g_dbus_connection_register_object (connection,
                                       "/kimpanel",
                                       introspection_data->interfaces[0],
852
853
854
855
                                       nullptr,  /*&interface_vtable*/
                                       nullptr,  /* user_data */
                                       nullptr,  /* user_data_free_func */
                                       nullptr); /* GError** */
856
857
858
859
860
861

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

928
929
930
931
932
    GVariant* var_engines = g_settings_get_value(impanel->settings_general, "preload-engines");
    impanel_update_engines(impanel, var_engines);
    if (var_engines) {
        g_variant_unref(var_engines);
    }
Xuetian Weng's avatar
Xuetian Weng committed
933

934
935
936
937
938
    var_engines = g_settings_get_value(impanel->settings_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
939

940
941
942
943
944
    GVariant* var_triggers = g_settings_get_value(impanel->settings_hotkey, "triggers");
    impanel_update_triggers(impanel, var_triggers);
    if (var_triggers) {
        g_variant_unref(var_triggers);
    }
Xuetian Weng's avatar
Xuetian Weng committed
945

946
947
948
949
950
    GVariant* var_layouts = g_settings_get_value(impanel->settings_general, "xkb-latin-layouts");
    if (var_layouts) {
        impanel_update_latin_layouts(impanel, var_layouts);
        g_variant_unref(var_layouts);
    }
Xuetian Weng's avatar
Xuetian Weng committed
951

952
953
954
955
956
957
958
959
960
961
    GVariant* var = g_settings_get_value(impanel->settings_general, "use-system-keyboard-layout");
    if (var) {
        impanel_update_use_system_keyboard_layout(impanel, var);
        g_variant_unref(var);
    }

    var = g_settings_get_value(impanel->settings_general, "use-global-engine");
    if (var) {
        impanel_update_use_global_engine(impanel, var);
        g_variant_unref(var);
Xuetian Weng's avatar
Xuetian Weng committed
962
963
964
    }

    ibus_panel_impanel_real_register_properties(impanel);
965
966
967
968
969
970
971
}

static void
on_name_acquired (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
981
}

static void
on_name_lost (GDBusConnection *connection,
              const gchar     *name,
              gpointer         user_data)
{
982
983
984
    Q_UNUSED(connection);
    Q_UNUSED(name);
    Q_UNUSED(user_data);
985
986
987
988
989
990
    exit (1);
}

G_DEFINE_TYPE (IBusPanelImpanel, ibus_panel_impanel, IBUS_TYPE_PANEL_SERVICE)

static void
Xuetian Weng's avatar
Xuetian Weng committed
991
ibus_panel_impanel_class_init (IBusPanelImpanelClass *klass)
992
{
Xuetian Weng's avatar
Xuetian Weng committed
993
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023

    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)
{
1024
1025
    impanel->bus = nullptr;
    impanel->app = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
1026
    impanel->useSystemKeyboardLayout = false;
Xuetian Weng's avatar
Xuetian Weng committed
1027
    impanel->selected = -1;
1028

1029
    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, nullptr);
1030
1031
1032
1033
1034
1035
    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,
1036
                               impanel, nullptr);
1037

Xuetian Weng's avatar
Xuetian Weng committed
1038
1039
1040
    impanel->propManager = new PropertyManager;
    impanel->engineManager = new EngineManager;
    impanel->xkbLayoutManager = new XkbLayoutManager;
1041
1042
1043
1044
    impanel->settings_general = g_settings_new (IBUS_SCHEMA_GENERAL);
    impanel->settings_hotkey = g_settings_new (IBUS_SCHEMA_HOTKEY);
    g_signal_connect(impanel->settings_general, "changed", G_CALLBACK (impanel_settings_changed_callback), impanel);
    g_signal_connect(impanel->settings_hotkey, "changed", G_CALLBACK (impanel_settings_changed_callback), impanel);
1045
1046
1047
1048
1049
}

static void
ibus_panel_impanel_destroy (IBusPanelImpanel *impanel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1050
    delete impanel->propManager;
1051
    impanel->propManager = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
1052
    delete impanel->engineManager;
1053
    impanel->engineManager = nullptr;
Xuetian Weng's avatar
Xuetian Weng committed
1054
    delete impanel->xkbLayoutManager;
1055
    impanel->xkbLayoutManager = nullptr;
1056

1057
1058
1059
1060
1061
    g_signal_handlers_disconnect_by_func (impanel->settings_general, (gpointer)impanel_settings_changed_callback, impanel);
    g_signal_handlers_disconnect_by_func (impanel->settings_hotkey, (gpointer)impanel_settings_changed_callback, impanel);
    g_clear_object (&impanel->settings_general);
    g_clear_object (&impanel->settings_hotkey);

1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
    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
1072
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);
Xuetian Weng's avatar
Xuetian Weng committed
1073
1074
1075
    if (impanel->app->keyboardGrabbed()) {
        return;
    }
Xuetian Weng's avatar
Xuetian Weng committed
1076

1077
1078
1079
1080
1081
    auto engine_desc = ibus_bus_get_global_engine(impanel->bus);
    if (engine_desc) {
        impanel_update_logo_by_engine(impanel, engine_desc);
        g_object_unref(engine_desc);
    }
1082

Xuetian Weng's avatar
Xuetian Weng committed
1083
1084
1085
1086
    impanel->engineManager->setCurrentContext(input_context_path);
    if (!impanel->engineManager->useGlobalEngine()) {
        impanel_set_engine(impanel, impanel->engineManager->currentEngine().toUtf8().constData());
    }
1087
1088
1089
1090
1091
1092
}

static void
ibus_panel_impanel_focus_out (IBusPanelService *panel,
                              const gchar      *input_context_path)
{
1093
1094
    Q_UNUSED(panel);
    Q_UNUSED(input_context_path);
Xuetian Weng's avatar
Xuetian Weng committed
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);

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

    if (impanel->engineManager->useGlobalEngine()) {
        return;
    }
    impanel->engineManager->setCurrentContext("");
1105
1106
1107
1108
1109
}

static void
ibus_panel_impanel_register_properties (IBusPanelService *panel,
                                        IBusPropList     *prop_list)
Xuetian Weng's avatar
Xuetian Weng committed
1110
1111
1112
1113
1114
1115
1116
1117
{
    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)
1118
{
1119
1120
1121
    if (!impanel->conn)
        return;

1122
    IBusProperty* property = nullptr;
1123
1124
1125
1126
1127
    guint i = 0;

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

1128
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
1129
        auto engine_desc = impanel->engineManager->engines()[impanel->selected];
1130
1131
        QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
        g_variant_builder_add (&builder, "s", propstr.constData());
1132
    } else {
1133
        QByteArray propstr;
1134
        auto engine_desc = ibus_bus_get_global_engine(impanel->bus);
1135
1136
1137
1138
1139
        if (engine_desc) {
            propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
            g_variant_builder_add (&builder, "s", propstr.constData());
            g_object_unref(engine_desc);
        }
1140
1141
1142

        IBusPropList* prop_list = impanel->propManager->properties();
        if (prop_list) {
1143
            while ( ( property = ibus_prop_list_get( prop_list, i ) ) != nullptr ) {
1144
1145
1146
1147
                propstr = ibus_property_to_propstr(property, TRUE);
                g_variant_builder_add (&builder, "s", propstr.constData());
                ++i;
            }
Xuetian Weng's avatar
Xuetian Weng committed
1148
        }
1149
1150
    }

Xuetian Weng's avatar
Xuetian Weng committed
1151
    g_dbus_connection_emit_signal (impanel->conn,
1152
                                   nullptr, "/kimpanel", "org.kde.kimpanel.inputmethod", "RegisterProperties",
Xuetian Weng's avatar
Xuetian Weng committed
1153
                                   (g_variant_new ("(as)", &builder)),
1154
                                   nullptr);
1155
}
1156

Xuetian Weng's avatar
Xuetian Weng committed
1157

1158
1159
1160
1161
1162
1163
1164
1165
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
1166
1167
1168
1169
1170
    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
1171
                            (g_variant_new("(iiii)", x, y, w, h)),
1172
                            nullptr,
Xuetian Weng's avatar
Xuetian Weng committed
1173
1174
                            G_DBUS_CALL_FLAGS_NONE,
                           -1,           /* timeout */
1175
1176
1177
                           nullptr,
                           nullptr,
                           nullptr);
1178
1179
1180
1181
1182
1183
1184
}

static void
ibus_panel_impanel_update_auxiliary_text (IBusPanelService *panel,
                                          IBusText         *text,
                                          gboolean          visible)
{
Xuetian Weng's avatar
Xuetian Weng committed
1185
    const gchar* t = ibus_text_get_text (text);
1186
    const gchar *attr = "";
Xuetian Weng's avatar
Xuetian Weng committed
1187
1188
1189
1190
    IBusPanelImpanel* impanel = (IBusPanelImpanel*) panel;

    if (!impanel->conn)
        return;
1191

Xuetian Weng's avatar
Xuetian Weng committed
1192
    g_dbus_connection_emit_signal (impanel->conn,
1193
                                   nullptr, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateAux",
Xuetian Weng's avatar
Xuetian Weng committed
1194
                                   (g_variant_new ("(ss)", t, attr)),
1195
                                   nullptr);
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207

    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
1208
1209
1210
1211
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    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
1227
    const gchar *candidate;
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242

    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
1243
        candidate = ibus_text_get_text (ibus_lookup_table_get_candidate (lookup_table, i));
1244
1245
1246
1247
1248
1249
1250
1251
        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
1252
1253
1254
1255
1256
1257
    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
1258
1259
    gint orientation = ibus_lookup_table_get_orientation(lookup_table);
    if (orientation == IBUS_ORIENTATION_HORIZONTAL) {
1260
        orientation = 2;
Xuetian Weng's avatar
Xuetian Weng committed
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
    } 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)),
1278
                           nullptr,
Xuetian Weng's avatar
Xuetian Weng committed
1279
1280
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
1281
1282
1283
                           nullptr,
                           nullptr,
                           nullptr);
Xuetian Weng's avatar
Xuetian Weng committed
1284

1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
    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
1297
1298
1299
1300
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

Xuetian Weng's avatar
Xuetian Weng committed
1301
    const gchar* t = ibus_text_get_text (text);
1302
1303
    const gchar *attr = "";

Xuetian Weng's avatar
Xuetian Weng committed
1304
    g_dbus_connection_emit_signal (impanel->conn,
1305
                                   nullptr, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditText",
Xuetian Weng's avatar
Xuetian Weng committed
1306
                                   (g_variant_new ("(ss)", t, attr)),
1307
                                   nullptr);
1308

Xuetian Weng's avatar
Xuetian Weng committed
1309
    g_dbus_connection_emit_signal (impanel->conn,
1310
                                   nullptr, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditCaret",
Xuetian Weng's avatar
Xuetian Weng committed
1311
                                   (g_variant_new ("(i)", cursor_pos)),
1312
                                   nullptr);
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323

    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
1324
1325
1326
1327
1328
1329
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

    impanel->propManager->updateProperty(prop);

1330
    QByteArray propstr = ibus_property_to_propstr(prop, TRUE);
1331

Xuetian Weng's avatar
Xuetian Weng committed
1332
    g_dbus_connection_emit_signal (impanel->conn,
1333
                                   nullptr, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateProperty",
1334
                                   (g_variant_new ("(s)", propstr.constData())),