-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmimonscreenplugins.cpp
More file actions
320 lines (260 loc) · 9.67 KB
/
mimonscreenplugins.cpp
File metadata and controls
320 lines (260 loc) · 9.67 KB
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
311
312
313
314
315
316
317
318
319
320
/* * This file is part of Maliit framework *
*
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* and appearing in the file LICENSE.LGPL included in the packaging
* of this file.
*/
#include "mimonscreenplugins.h"
#include <QLocale>
#include <QString>
#include <QSet>
#include <QDebug>
#include <algorithm>
#include <tr1/functional>
using namespace std::tr1::placeholders;
namespace
{
const char * const EnabledSubViews = MALIIT_CONFIG_ROOT"onscreen/enabled";
const char * const ActiveSubView = MALIIT_CONFIG_ROOT"onscreen/active";
bool equalPlugin(const MImOnScreenPlugins::SubView &subView, const QString &plugin)
{
return subView.plugin == plugin;
}
bool notEqualPlugin(const MImOnScreenPlugins::SubView &subView, const QString &plugin)
{
return subView.plugin != plugin;
}
QStringList toSettings(const QList<MImOnScreenPlugins::SubView> &subViews)
{
QStringList result;
Q_FOREACH(const MImOnScreenPlugins::SubView &subView, subViews) {
result.push_back(subView.plugin + ":" + subView.id);
}
return result;
}
QList<MImOnScreenPlugins::SubView> fromSettings(const QStringList& list)
{
QList<MImOnScreenPlugins::SubView> result;
Q_FOREACH (const QString &value, list) {
QString plugin = value.section(':', 0, 0);
QString subview = value.section(':', 1, -1);
result.push_back(MImOnScreenPlugins::SubView(plugin, subview));
}
return result;
}
QSet<QString> findEnabledPlugins(const QList<MImOnScreenPlugins::SubView> &enabledSubViews)
{
QSet<QString> result;
Q_FOREACH (const MImOnScreenPlugins::SubView &subView, enabledSubViews) {
result.insert(subView.plugin);
}
return result;
}
}
MImOnScreenPlugins::SubView::SubView()
: plugin()
, id()
{}
MImOnScreenPlugins::SubView::SubView(const QString &new_plugin,
const QString &new_id)
: plugin(new_plugin)
, id(new_id)
{}
bool MImOnScreenPlugins::SubView::operator==(const MImOnScreenPlugins::SubView &other) const
{
return (plugin == other.plugin
&& id == other.id);
}
MImOnScreenPlugins::MImOnScreenPlugins():
QObject(),
mAvailableSubViews(),
mEnabledSubViews(),
mLastEnabledSubViews(),
mActiveSubView(),
mEnabledSubViewsSettings(EnabledSubViews),
mActiveSubViewSettings(ActiveSubView),
mAllSubviewsEnabled(false)
{
connect(&mEnabledSubViewsSettings, SIGNAL(valueChanged()),
this, SLOT(updateEnabledSubviews()));
connect(&mActiveSubViewSettings, SIGNAL(valueChanged()),
this, SLOT(updateActiveSubview()));
updateEnabledSubviews();
updateActiveSubview();
}
bool MImOnScreenPlugins::isEnabled(const QString &plugin) const
{
QList<MImOnScreenPlugins::SubView> mEnabledAndAvailableSubViews;
std::remove_copy_if(mEnabledSubViews.begin(), mEnabledSubViews.end(),
std::back_inserter(mEnabledAndAvailableSubViews),
std::tr1::bind(&MImOnScreenPlugins::isSubViewUnavailable, this, _1));
return std::find_if(mEnabledAndAvailableSubViews.begin(), mEnabledAndAvailableSubViews.end(),
std::tr1::bind(equalPlugin, _1, plugin)) != mEnabledAndAvailableSubViews.end();
}
bool MImOnScreenPlugins::isSubViewEnabled(const SubView &subView) const
{
return mEnabledSubViews.contains(subView);
}
QList<MImOnScreenPlugins::SubView> MImOnScreenPlugins::enabledSubViews() const
{
return mEnabledSubViews;
}
QList<MImOnScreenPlugins::SubView> MImOnScreenPlugins::enabledSubViews(const QString &plugin) const
{
QList<MImOnScreenPlugins::SubView> result;
std::remove_copy_if(mEnabledSubViews.begin(), mEnabledSubViews.end(),
std::back_inserter(result), std::tr1::bind(notEqualPlugin, _1, plugin));
return result;
}
void MImOnScreenPlugins::setEnabledSubViews(const QList<MImOnScreenPlugins::SubView> &subViews)
{
mEnabledSubViewsSettings.set(QVariant(toSettings(subViews)));
}
void MImOnScreenPlugins::setAutoEnabledSubViews(const QList<MImOnScreenPlugins::SubView> &subViews)
{
// Update the enabled subviews list without saving the configuration to disk
mEnabledSubViews = subViews;
}
void MImOnScreenPlugins::updateAvailableSubViews(const QList<SubView> &availableSubViews)
{
mAvailableSubViews = availableSubViews;
autoDetectActiveSubView();
}
bool MImOnScreenPlugins::isSubViewAvailable(const SubView &subview) const
{
return mAvailableSubViews.contains(subview);
}
bool MImOnScreenPlugins::isSubViewUnavailable(const SubView &subview) const
{
return !mAvailableSubViews.contains(subview);
}
void MImOnScreenPlugins::updateEnabledSubviews()
{
const QStringList &list = mEnabledSubViewsSettings.value().toStringList();
const QList<SubView> oldEnabledSubviews = mEnabledSubViews;
mEnabledSubViews = fromSettings(list);
// Changed subviews cause emission of enabledPluginsChanged() signal
// because some subview from the setting might not really exists and therefore
// changed subview might have implications to enabled plugins.
if (mEnabledSubViews != oldEnabledSubviews) {
Q_EMIT enabledPluginsChanged();
}
}
void MImOnScreenPlugins::updateActiveSubview()
{
const QString &active = mActiveSubViewSettings.value().toString();
if (active.isEmpty()) {
mActiveSubView = MImOnScreenPlugins::SubView(MALIIT_DEFAULT_PLUGIN);
return;
}
const QList<SubView> &activeList = fromSettings(QStringList() << active);
const MImOnScreenPlugins::SubView &subView = activeList.first();
if (mActiveSubView == subView) {
return;
}
setAutoActiveSubView(subView);
}
const MImOnScreenPlugins::SubView MImOnScreenPlugins::activeSubView()
{
return mActiveSubView;
}
void MImOnScreenPlugins::setActiveSubView(const MImOnScreenPlugins::SubView &subView)
{
if (mActiveSubView == subView)
return;
mActiveSubView = subView;
QList<MImOnScreenPlugins::SubView> subViews;
subViews << subView;
mActiveSubViewSettings.set(toSettings(subViews));
Q_EMIT activeSubViewChanged();
}
void MImOnScreenPlugins::setAutoActiveSubView(const MImOnScreenPlugins::SubView &subView)
{
// Update the active subview without writing the configuration to disk
if (mActiveSubView == subView)
return;
mActiveSubView = subView;
Q_EMIT activeSubViewChanged();
}
void MImOnScreenPlugins::setAllSubViewsEnabled(bool enable)
{
if (mAllSubviewsEnabled != enable) {
mAllSubviewsEnabled = enable;
if (mAllSubviewsEnabled) {
mLastEnabledSubViews = mEnabledSubViews;
} else {
if (not mLastEnabledSubViews.contains(mActiveSubView)) {
mLastEnabledSubViews.append(mActiveSubView);
}
}
setEnabledSubViews(mAllSubviewsEnabled ? mAvailableSubViews
: mLastEnabledSubViews);
}
}
void MImOnScreenPlugins::autoDetectActiveSubView()
{
// If no subviews are enabled by the configuration, try to auto-detect
// them.
if (enabledSubViews().empty()) {
autoDetectEnabledSubViews();
}
// If we still don't have an enabled subview, enable the first available
// one.
if (enabledSubViews().empty()) {
MImOnScreenPlugins::SubView subView = mAvailableSubViews.first();
setAutoEnabledSubViews(QList<MImOnScreenPlugins::SubView>() << subView);
}
// If we have an active subview in the configuration, check that it is
// enabled
// If we don't have an active subview, auto-activate the first enabled
// one.
if (mActiveSubView.id.isEmpty() || !isSubViewEnabled(mActiveSubView)) {
MImOnScreenPlugins::SubView subView = enabledSubViews().first();
setAutoActiveSubView(subView);
}
}
void MImOnScreenPlugins::autoDetectEnabledSubViews()
{
const QString &plugin = mActiveSubView.plugin;
QList<MImOnScreenPlugins::SubView> to_enable;
// Try to auto-detect subviews for the selected plugin by looking for
// subviews that coincide with the languages selected for use on the
// system.
// FIXME: This works for the keyboard plugin, but won't work everywhere.
// The methodology for auto-configuring subviews should be somehow
// plugin-dictated.
QStringList langs = QLocale::system().uiLanguages();
Q_FOREACH (QString lang, langs) {
// Convert to lower case, remove any .utf8 suffix, and use _ as
// the separator between language and country.
lang = lang.split('.')[0].toLower().replace("-", "_");
MImOnScreenPlugins::SubView subView(plugin, lang);
// First try the language code as-is
if (isSubViewAvailable(subView) && !to_enable.contains(subView)) {
to_enable << subView;
continue;
}
// See if we get a match if we expand "de" to "de_de"
if (!lang.contains('_')) {
subView.id = lang + "_" + lang;
if (isSubViewAvailable(subView) && !to_enable.contains(subView)) {
to_enable << subView;
}
continue;
}
// See if we get a match if we trim "de_at" to "de"
subView.id = lang.split("_").first();
if (isSubViewAvailable(subView) && !to_enable.contains(subView)) {
to_enable << subView;
}
}
if (!to_enable.isEmpty()) {
setAutoEnabledSubViews(to_enable);
}
}