Debian Codegraph @

system-settings-plugin/plugin.cpp

raw
1 /*
2 * Copyright (C) 2015 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 3, as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranties of
12 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "plugin.h"
20
21 #include <Accounts/Account>
22 #include <Accounts/Manager>
23 #include <Accounts/Provider>
24 #include <Accounts/Service>
25 #include <QDebug>
26 #include <QStringList>
27 #include <LomiriSystemSettings/ItemBase>
28 #include <libintl.h>
29
30 using namespace LomiriSystemSettings;
31
32 class Item: public ItemBase
33 {
34 Q_OBJECT
35
36 public:
37 Item(const QVariantMap &staticData, QObject *parent = 0);
38 ~Item();
39
40 private:
41 void computeKeywords();
42 };
43
44 Item::Item(const QVariantMap &staticData, QObject *parent):
45 ItemBase(staticData, parent)
46 {
47 computeKeywords();
48 }
49
50 Item::~Item()
51 {
52 }
53
54 static QStringList translations(const QString &text, const QString &domain)
55 {
56 /* Return a list of keywords based on a translatable name:
57 * - the untranslated text (lowercase, split into words)
58 * - the translated text (lowercase, split into words)
59 */
60 QStringList keys;
61 keys = text.toLower().split(" ", Qt::SkipEmptyParts);
62 if (!domain.isEmpty()) {
63 QByteArray baText = text.toUtf8();
64 QByteArray baDomain = domain.toUtf8();
65 QString translated = QString::fromUtf8(dgettext(baDomain.constData(),
66 baText.constData()));
67 if (translated != text) {
68 keys.append(translated.toLower().split(" ",
69 Qt::SkipEmptyParts));
70 }
71 }
72 return keys;
73 }
74
75 void Item::computeKeywords()
76 {
77 Accounts::Manager *manager = new Accounts::Manager;
78
79 QStringList keywords;
80
81 /* List available providers, and add their names to the search keywords */
82 Q_FOREACH(const Accounts::Provider &provider, manager->providerList()) {
83 keywords.append(provider.name().toLower());
84 keywords.append(translations(provider.displayName(),
85 provider.trCatalog()));
86 }
87
88 /* Same for services */
89 Q_FOREACH(const Accounts::Service &service, manager->serviceList()) {
90 keywords.append(service.name().toLower());
91 keywords.append(translations(service.displayName(),
92 service.trCatalog()));
93 }
94
95 /* Also add the account display names */
96 Q_FOREACH(Accounts::AccountId id, manager->accountList()) {
97 Accounts::Account *account = manager->account(id);
98 if (Q_UNLIKELY(!account)) continue;
99 QString name = account->displayName().toLower();
100 if (!name.isEmpty()) {
101 keywords.append(name);
102 }
103 }
104
105 delete manager;
106
107 setKeywords(keywords);
108 }
109
110 ItemBase *Plugin::createItem(const QVariantMap &staticData,
111 QObject *parent)
112 {
113 return new Item(staticData, parent);
114 }
115
116 #include "plugin.moc"