panel.cpp 57 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
138

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

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

Xuetian Weng's avatar
Xuetian Weng committed
173
174
175
    IBusEngineDesc* engine_desc = NULL;
    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
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 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
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
339
    const gchar* label = NULL;
    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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
}

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);
        GVariant* var = g_variant_new_strv(engine_names, len);
523
        g_settings_set_value(impanel->settings_general, "preload-engines", var);
Xuetian Weng's avatar
Xuetian Weng committed
524
525
526
527
528
    }

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

529
    impanel->engineManager->setEngines(engines);
Xuetian Weng's avatar
Xuetian Weng committed
530
531

    if (engines && engines[0]) {
532
        ibus_bus_set_global_engine(impanel->bus, ibus_engine_desc_get_name(engines[0]));
Xuetian Weng's avatar
Xuetian Weng committed
533
534
535
536
537
    }

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

Xuetian Weng's avatar
Xuetian Weng committed
538
539
540
541
542
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);
543
544
    if (len) {
        impanel->engineManager->setOrder(engine_names, len);
Xuetian Weng's avatar
Xuetian Weng committed
545

546
547
548
        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
549
    }
550
    g_free(engine_names);
Xuetian Weng's avatar
Xuetian Weng committed
551
552
}

Xuetian Weng's avatar
Xuetian Weng committed
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
599
600
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);
601
    g_free(variants);
Xuetian Weng's avatar
Xuetian Weng committed
602
603
604
}

static void
605
impanel_settings_changed_callback (GSettings* settings, const gchar* key, gpointer user_data)
Xuetian Weng's avatar
Xuetian Weng committed
606
607
{
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
608
609
610
611
    gchar *schema = NULL;
    GVariant *value = g_settings_get_value (settings, key);

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

613
    if (g_strcmp0(schema, IBUS_SCHEMA_GENERAL) == 0 && g_strcmp0(key, "preload-engines") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
614
615
        impanel_update_engines(impanel, value);
    }
616
    else if (g_strcmp0(schema, IBUS_SCHEMA_HOTKEY) == 0 && g_strcmp0(key, "triggers") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
617
        impanel_update_triggers(impanel, value);
618
    } else if (g_strcmp0(schema, IBUS_SCHEMA_GENERAL) == 0 && g_strcmp0(key, "use-system-keyboard-layout") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
619
        impanel_update_use_system_keyboard_layout(impanel, value);
620
    } else if (g_strcmp0(schema, IBUS_SCHEMA_GENERAL) == 0 && g_strcmp0(key, "use-global-engine") == 0) {
Xuetian Weng's avatar
Xuetian Weng committed
621
622
        impanel_update_use_global_engine(impanel, value);
    }
623
    g_free (schema);
Xuetian Weng's avatar
Xuetian Weng committed
624
625
626
627
628
629
630
631
632
633
634
}

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

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)
{
696
697
698
699
700
    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
701
702
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(user_data);
    gchar *s0 = NULL;
703
    g_variant_get (parameters, "(s)", &s0);
Xuetian Weng's avatar
Xuetian Weng committed
704
705
706
707
708
709
710
711
    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);
712
    }
Xuetian Weng's avatar
Xuetian Weng committed
713
714
715
716
717
718
719
720
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
    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);
746
747
}

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
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);
}

809
810
811
812
813
814
815
816
817
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)
{
818
819
820
821
822
823
824
    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);
825
826
827
828
829
830
831
832
833
834
835
836
    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)
{
837
    Q_UNUSED(name);
Xuetian Weng's avatar
Xuetian Weng committed
838
839
    IBusPanelImpanel* impanel = ((IBusPanelImpanel *)user_data);
    impanel->conn = connection;
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

    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);
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
    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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
    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);
909
910
911
912
913
914
915
916
917
918
    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
919

920
921
922
923
924
    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
925

926
927
928
929
930
    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
931

932
933
934
935
936
    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
937

938
939
940
941
942
    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
943

944
945
946
947
948
949
950
951
952
953
    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
954
955
956
    }

    ibus_panel_impanel_real_register_properties(impanel);
957
958
959
960
961
962
963
}

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

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

G_DEFINE_TYPE (IBusPanelImpanel, ibus_panel_impanel, IBUS_TYPE_PANEL_SERVICE)

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

    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
1017
1018
    impanel->app = NULL;
    impanel->useSystemKeyboardLayout = false;
Xuetian Weng's avatar
Xuetian Weng committed
1019
    impanel->selected = -1;
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029

    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
1030
1031
1032
    impanel->propManager = new PropertyManager;
    impanel->engineManager = new EngineManager;
    impanel->xkbLayoutManager = new XkbLayoutManager;
1033
1034
1035
1036
    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);
1037
1038
1039
1040
1041
}

static void
ibus_panel_impanel_destroy (IBusPanelImpanel *impanel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1042
1043
1044
1045
1046
1047
    delete impanel->propManager;
    impanel->propManager = NULL;
    delete impanel->engineManager;
    impanel->engineManager = NULL;
    delete impanel->xkbLayoutManager;
    impanel->xkbLayoutManager = NULL;
1048

1049
1050
1051
1052
1053
    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);

1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
    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
1064
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);
Xuetian Weng's avatar
Xuetian Weng committed
1065
1066
1067
    if (impanel->app->keyboardGrabbed()) {
        return;
    }
Xuetian Weng's avatar
Xuetian Weng committed
1068

1069
    IBusEngineDesc *engine_desc = ibus_bus_get_global_engine(impanel->bus);
Xuetian Weng's avatar
Xuetian Weng committed
1070
    impanel_update_logo_by_engine(impanel, engine_desc);
1071
    g_object_unref(engine_desc);
1072

Xuetian Weng's avatar
Xuetian Weng committed
1073
1074
1075
1076
    impanel->engineManager->setCurrentContext(input_context_path);
    if (!impanel->engineManager->useGlobalEngine()) {
        impanel_set_engine(impanel, impanel->engineManager->currentEngine().toUtf8().constData());
    }
1077
1078
1079
1080
1081
1082
}

static void
ibus_panel_impanel_focus_out (IBusPanelService *panel,
                              const gchar      *input_context_path)
{
1083
1084
    Q_UNUSED(panel);
    Q_UNUSED(input_context_path);
Xuetian Weng's avatar
Xuetian Weng committed
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL (panel);

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

    if (impanel->engineManager->useGlobalEngine()) {
        return;
    }
    impanel->engineManager->setCurrentContext("");
1095
1096
1097
1098
1099
}

static void
ibus_panel_impanel_register_properties (IBusPanelService *panel,
                                        IBusPropList     *prop_list)
Xuetian Weng's avatar
Xuetian Weng committed
1100
1101
1102
1103
1104
1105
1106
1107
{
    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)
1108
{
1109
1110
1111
    if (!impanel->conn)
        return;

1112
1113
1114
1115
1116
1117
    IBusProperty* property = NULL;
    guint i = 0;

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

1118
    if (impanel->selected >= 0 && static_cast<size_t>(impanel->selected) < impanel->engineManager->length()) {
1119
        auto engine_desc = impanel->engineManager->engines()[impanel->selected];
1120
1121
        QByteArray propstr = ibus_engine_desc_to_logo_propstr(engine_desc);
        g_variant_builder_add (&builder, "s", propstr.constData());
1122
    } else {
1123
        auto engine_desc = ibus_bus_get_global_engine(impanel->bus);
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
        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
1134
        }
1135
        g_object_unref(engine_desc);
1136
1137
    }

Xuetian Weng's avatar
Xuetian Weng committed
1138
    g_dbus_connection_emit_signal (impanel->conn,
1139
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "RegisterProperties",
Xuetian Weng's avatar
Xuetian Weng committed
1140
                                   (g_variant_new ("(as)", &builder)),
1141
                                   NULL);
1142
}
1143

Xuetian Weng's avatar
Xuetian Weng committed
1144

1145
1146
1147
1148
1149
1150
1151
1152
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
1153
1154
1155
1156
1157
    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
1158
                            (g_variant_new("(iiii)", x, y, w, h)),
Xuetian Weng's avatar
Xuetian Weng committed
1159
1160
1161
1162
1163
1164
                            NULL,
                            G_DBUS_CALL_FLAGS_NONE,
                           -1,           /* timeout */
                           NULL,
                           NULL,
                           NULL);
1165
1166
1167
1168
1169
1170
1171
}

static void
ibus_panel_impanel_update_auxiliary_text (IBusPanelService *panel,
                                          IBusText         *text,
                                          gboolean          visible)
{
Xuetian Weng's avatar
Xuetian Weng committed
1172
    const gchar* t = ibus_text_get_text (text);
1173
    const gchar *attr = "";
Xuetian Weng's avatar
Xuetian Weng committed
1174
1175
1176
1177
    IBusPanelImpanel* impanel = (IBusPanelImpanel*) panel;

    if (!impanel->conn)
        return;
1178

Xuetian Weng's avatar
Xuetian Weng committed
1179
    g_dbus_connection_emit_signal (impanel->conn,
1180
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateAux",
Xuetian Weng's avatar
Xuetian Weng committed
1181
                                   (g_variant_new ("(ss)", t, attr)),
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
                                   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
1195
1196
1197
1198
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
    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
1214
    const gchar *candidate;
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229

    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
1230
        candidate = ibus_text_get_text (ibus_lookup_table_get_candidate (lookup_table, i));
1231
1232
1233
1234
1235
1236
1237
1238
        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
1239
1240
1241
1242
1243
1244
    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
1245
1246
    gint orientation = ibus_lookup_table_get_orientation(lookup_table);
    if (orientation == IBUS_ORIENTATION_HORIZONTAL) {
1247
        orientation = 2;
Xuetian Weng's avatar
Xuetian Weng committed
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
    } 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
1271

1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
    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
1284
1285
1286
1287
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

Xuetian Weng's avatar
Xuetian Weng committed
1288
    const gchar* t = ibus_text_get_text (text);
1289
1290
    const gchar *attr = "";

Xuetian Weng's avatar
Xuetian Weng committed
1291
    g_dbus_connection_emit_signal (impanel->conn,
1292
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditText",
Xuetian Weng's avatar
Xuetian Weng committed
1293
                                   (g_variant_new ("(ss)", t, attr)),
1294
1295
                                   NULL);

Xuetian Weng's avatar
Xuetian Weng committed
1296
    g_dbus_connection_emit_signal (impanel->conn,
1297
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdatePreeditCaret",
Xuetian Weng's avatar
Xuetian Weng committed
1298
                                   (g_variant_new ("(i)", cursor_pos)),
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
                                   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
1311
1312
1313
1314
1315
1316
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;

    impanel->propManager->updateProperty(prop);

1317
    QByteArray propstr = ibus_property_to_propstr(prop, TRUE);
1318

Xuetian Weng's avatar
Xuetian Weng committed
1319
    g_dbus_connection_emit_signal (impanel->conn,
1320
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "UpdateProperty",
1321
                                   (g_variant_new ("(s)", propstr.constData())),
1322
1323
1324
1325
1326
1327
                                   NULL);
}

static void
ibus_panel_impanel_cursor_down_lookup_table (IBusPanelService *panel)
{
1328
    Q_UNUSED(panel);
1329
1330
1331
1332
1333
}

static void
ibus_panel_impanel_cursor_up_lookup_table (IBusPanelService *panel)
{
1334
    Q_UNUSED(panel);
1335
1336
1337
1338
1339
}

static void
ibus_panel_impanel_hide_auxiliary_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1340
1341
1342
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1343
1344
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1345
    g_dbus_connection_emit_signal (impanel->conn,
1346
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowAux",
Xuetian Weng's avatar
Xuetian Weng committed
1347
                                   (g_variant_new ("(b)", toShow)),
1348
1349
1350
1351
1352
1353
                                   NULL);
}

static void
ibus_panel_impanel_hide_language_bar (IBusPanelService *panel)
{
1354
    Q_UNUSED(panel);
1355
1356
1357
1358
1359
}

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

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

static void
ibus_panel_impanel_hide_preedit_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1374
1375
1376
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1377
1378
    gboolean toShow = 0;

Xuetian Weng's avatar
Xuetian Weng committed
1379
    g_dbus_connection_emit_signal (impanel->conn,
1380
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowPreedit",
Xuetian Weng's avatar
Xuetian Weng committed
1381
                                   (g_variant_new ("(b)", toShow)),
1382
1383
1384
1385
1386
1387
                                   NULL);
}

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

static void
ibus_panel_impanel_page_up_lookup_table (IBusPanelService *panel)
{
1394
    Q_UNUSED(panel);
1395
1396
1397
1398
1399
}

static void
ibus_panel_impanel_reset (IBusPanelService *panel)
{
1400
    Q_UNUSED(panel);
1401
1402
1403
1404
1405
}

static void
ibus_panel_impanel_show_auxiliary_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1406
1407
1408
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1409
1410
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1411
    g_dbus_connection_emit_signal (impanel->conn,
1412
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowAux",
Xuetian Weng's avatar
Xuetian Weng committed
1413
                                   (g_variant_new ("(b)", toShow)),
1414
1415
1416
1417
1418
1419
                                   NULL);
}

static void
ibus_panel_impanel_show_language_bar (IBusPanelService *panel)
{
1420
    Q_UNUSED(panel);
1421
1422
1423
1424
1425
}

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

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

static void
ibus_panel_impanel_show_preedit_text (IBusPanelService *panel)
{
Xuetian Weng's avatar
Xuetian Weng committed
1440
1441
1442
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
1443
1444
    gboolean toShow = 1;

Xuetian Weng's avatar
Xuetian Weng committed
1445
    g_dbus_connection_emit_signal (impanel->conn,
1446
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "ShowPreedit",
Xuetian Weng's avatar
Xuetian Weng committed
1447
                                   (g_variant_new ("(b)", toShow)),
1448
1449
1450
1451
1452
1453
                                   NULL);
}

static void
ibus_panel_impanel_start_setup (IBusPanelService *panel)
{
1454
    Q_UNUSED(panel);
1455
1456
1457
1458
}

static void
ibus_panel_impanel_state_changed (IBusPanelService *panel)
1459
{
Xuetian Weng's avatar
Xuetian Weng committed
1460
1461
1462
    IBusPanelImpanel* impanel = IBUS_PANEL_IMPANEL(panel);
    if (!impanel->conn)
        return;
Xuetian Weng's avatar
Xuetian Weng committed
1463
1464
1465
1466
1467

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

Xuetian Weng's avatar
Xuetian Weng committed
1468
    IBusEngineDesc *engine_desc = ibus_bus_get_global_engine(impanel->bus);
1469
1470
    if (!engine_desc) {
        return;
Xuetian Weng's avatar
Xuetian Weng committed
1471
    }
1472

Xuetian Weng's avatar
Xuetian Weng committed
1473
    impanel_update_logo_by_engine(impanel, engine_desc);
1474

Xuetian Weng's avatar
Xuetian Weng committed
1475
    g_dbus_connection_emit_signal (impanel->conn,
1476
                                   NULL, "/kimpanel", "org.kde.kimpanel.inputmethod", "Enable",
Xuetian Weng's avatar
Xuetian Weng committed
1477
                                   (g_variant_new ("(b)", TRUE)),
1478
                                   NULL);
Xuetian Weng's avatar
Xuetian Weng committed
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488