forked from lmq3342xja/fcitx-qimpanel
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kimpanelagent.cpp
310 lines (264 loc) · 10.5 KB
/
kimpanelagent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/***************************************************************************
* Copyright (C) 2009 by Wang Hoi <zealot.hoi@gmail.com> *
* Copyright (C) 2011 by CSSlayer <wengxt@gmail.com> *
* *
* 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) any later version. *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "kimpanelagent.h"
#include "impaneladaptor.h"
// Qt
#include <QByteArray>
#include <QList>
#include <QMap>
#include <QString>
#include <QStringList>
#include <QVariant>
#include "kimpanelagent.h"
PanelAgent::PanelAgent(QObject *parent)
: QObject(parent)
,adaptor(new ImpanelAdaptor(this))
,adaptor2(new Impanel2Adaptor(this))
,watcher(new QDBusServiceWatcher(this))
{
watcher->setConnection(QDBusConnection::sessionBus());
watcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
QDBusConnection::connectToBus(QDBusConnection::SessionBus, "kimpanel_bus").registerObject("/org/kde/impanel", this);
QDBusConnection::connectToBus(QDBusConnection::SessionBus, "kimpanel_bus").registerService("org.kde.impanel");
// directly connect to corresponding signal
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"Enable", this, SIGNAL(enable(bool)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"ShowPreedit", this, SIGNAL(showPreedit(bool)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"ShowAux", this, SIGNAL(showAux(bool)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"ShowLookupTable", this, SIGNAL(showLookupTable(bool)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdateLookupTableCursor", this, SIGNAL(updateLookupTableCursor(int)));
// do some serialization
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdateLookupTable", this, SLOT(UpdateLookupTable(QStringList,
QStringList, QStringList, bool, bool)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdatePreeditCaret", this, SIGNAL(updatePreeditCaret(int)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdatePreeditText", this, SLOT(UpdatePreeditText(QString, QString)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdateAux", this, SLOT(UpdateAux(QString, QString)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdateSpotLocation", this, SIGNAL(updateSpotLocation(int, int)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdateScreen", this, SLOT(UpdateScreen(int)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"UpdateProperty", this, SLOT(UpdateProperty(QString)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"RegisterProperties", this, SLOT(RegisterProperties(QStringList)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"ExecDialog", this, SLOT(ExecDialog(QString)));
QDBusConnection("kimpanel_bus").connect("", "", "org.kde.kimpanel.inputmethod",
"ExecMenu", this, SLOT(ExecMenu(QStringList)));
connect(watcher, SIGNAL(serviceUnregistered(QString)), this, SLOT(serviceUnregistered(QString)));
}
PanelAgent::~PanelAgent()
{
// destructor
QDBusConnection::disconnectFromBus("kimpanel_bus");
}
void PanelAgent::serviceUnregistered(const QString& service)
{
if (service == m_currentService) {
watcher->setWatchedServices(QStringList());
cached_props.clear();
m_currentService = QString();
emit showAux(false);
emit showPreedit(false);
emit showLookupTable(false);
emit registerProperties(QList<KimpanelProperty>());
}
}
void PanelAgent::configure()
{
emit Configure();
}
void PanelAgent::lookupTablePageDown()
{
emit LookupTablePageDown();
}
void PanelAgent::lookupTablePageUp()
{
emit LookupTablePageUp();
}
void PanelAgent::movePreeditCaret(int pos)
{
emit MovePreeditCaret(pos);
}
void PanelAgent::triggerProperty(const QString& key)
{
emit TriggerProperty(key);
}
void PanelAgent::selectCandidate(int idx)
{
emit SelectCandidate(idx);
}
static QList<TextAttribute> String2AttrList(const QString &str)
{
QList<TextAttribute> result;
if (str.isEmpty()) {
return result;
}
foreach(const QString & s, str.split(';')) {
TextAttribute attr;
QStringList list = s.split(':');
if (list.size() < 4)
continue;
switch (list.at(0).toInt()) {
case 0:
attr.type = TextAttribute::None;
break;
case 1:
attr.type = TextAttribute::Decorate;
break;
case 2:
attr.type = TextAttribute::Foreground;
break;
case 3:
attr.type = TextAttribute::Background;
break;
default:
attr.type = TextAttribute::None;
}
attr.start = list.at(1).toInt();
attr.length = list.at(2).toInt();
attr.value = list.at(3).toInt();
result << attr;
}
return result;
}
static KimpanelProperty String2Property(const QString &str)
{
KimpanelProperty result;
QStringList list = str.split(':');
if (list.size() < 4)
return result;
result.key = list.at(0);
result.label = list.at(1);
result.icon = list.at(2);
result.tip = list.at(3);
if (list.size() >= 5)
result.menu = list.at(4);
//X {
//X result.state = KimpanelProperty::None;
//X int n = list.at(4).toInt();
//X if (n & KimpanelProperty::Active)
//X result.state &= KimpanelProperty::Active;
//X if (n & KimpanelProperty::Visible)
//X result.state &= KimpanelProperty::Visible;
//X }
return result;
}
static KimpanelLookupTable Args2LookupTable(const QStringList &labels, const QStringList &candis, const QStringList &attrs, bool has_prev, bool has_next)
{
Q_ASSERT(labels.size() == candis.size());
Q_ASSERT(labels.size() == attrs.size());
KimpanelLookupTable result;
for (int i = 0; i < labels.size(); i++) {
KimpanelLookupTable::Entry entry;
entry.label = labels.at(i);
entry.text = candis.at(i);
entry.attr = String2AttrList(attrs.at(i));
result.entries << entry;
}
result.has_prev = has_prev;
result.has_next = has_next;
return result;
}
void PanelAgent::created()
{
emit PanelCreated();
emit PanelCreated2();
}
void PanelAgent::exit()
{
emit Exit();
}
void PanelAgent::restart()
{
emit Restart();
}
void PanelAgent::reloadConfig()
{
emit ReloadConfig();
}
void PanelAgent::UpdateLookupTable(const QStringList &labels,
const QStringList &candis,
const QStringList &attrlists,
bool has_prev, bool has_next)
{
emit updateLookupTable(Args2LookupTable(labels, candis, attrlists, has_prev, has_next));
}
void PanelAgent::UpdatePreeditText(const QString &text, const QString &attr)
{
emit updatePreeditText(text, String2AttrList(attr));
}
void PanelAgent::UpdateAux(const QString &text, const QString &attr)
{
emit updateAux(text, String2AttrList(attr));
}
void PanelAgent::UpdateScreen(int screen_id)
{
Q_UNUSED(screen_id);
}
void PanelAgent::UpdateProperty(const QString &prop)
{
emit updateProperty(String2Property(prop));
}
void PanelAgent::RegisterProperties(const QStringList &props)
{
const QDBusMessage& msg = message();
if (msg.service() != m_currentService) {
watcher->removeWatchedService(m_currentService);
m_currentService = msg.service();
watcher->addWatchedService(m_currentService);
}
if (cached_props != props) {
cached_props = props;
QList<KimpanelProperty> list;
foreach(const QString & prop, props) {
list << String2Property(prop);
}
emit registerProperties(list);
}
}
void PanelAgent::ExecDialog(const QString &prop)
{
emit execDialog(String2Property(prop));
}
void PanelAgent::ExecMenu(const QStringList &entries)
{
QList<KimpanelProperty> list;
foreach(const QString & entry, entries) {
list << String2Property(entry);
}
emit execMenu(list);
}
void PanelAgent::SetSpotRect(int x, int y, int w, int h)
{
emit updateSpotRect(x, y, w, h);
}
void PanelAgent::SetLookupTable(const QStringList& labels, const QStringList& candis, const QStringList& attrlists, bool hasPrev, bool hasNext, int cursor, int layout)
{
emit updateLookupTableFull(Args2LookupTable(labels, candis, attrlists, hasPrev, hasNext), cursor, layout);
}