Changeset View
Changeset View
Standalone View
Standalone View
kcms/activities/BlacklistedApplicationsModel.cpp
Show All 23 Lines | |||||
24 | #include <QSet> | 24 | #include <QSet> | ||
25 | #include <QSqlDatabase> | 25 | #include <QSqlDatabase> | ||
26 | #include <QSqlQuery> | 26 | #include <QSqlQuery> | ||
27 | #include <QSqlError> | 27 | #include <QSqlError> | ||
28 | #include <QVariant> | 28 | #include <QVariant> | ||
29 | #include <QDebug> | 29 | #include <QDebug> | ||
30 | 30 | | |||
31 | #include <KService> | 31 | #include <KService> | ||
32 | #include <KConfig> | 32 | | ||
33 | #include <KSharedConfig> | 33 | #include "kactivitymanagerd_plugins_settings.h" | ||
34 | #include <KConfigGroup> | | |||
35 | 34 | | |||
36 | #include <utils/d_ptr_implementation.h> | 35 | #include <utils/d_ptr_implementation.h> | ||
37 | 36 | | |||
38 | #include "definitions.h" | 37 | #include "definitions.h" | ||
39 | 38 | | |||
40 | class BlacklistedApplicationsModel::Private { | 39 | class BlacklistedApplicationsModel::Private { | ||
41 | public: | 40 | public: | ||
42 | struct ApplicationData { | 41 | struct ApplicationData { | ||
43 | QString name; | 42 | QString name; | ||
44 | QString title; | 43 | QString title; | ||
45 | QString icon; | 44 | QString icon; | ||
46 | bool blocked; | 45 | bool blocked; | ||
47 | }; | 46 | }; | ||
48 | 47 | | |||
49 | QList<ApplicationData> applications; | 48 | QList<ApplicationData> applications; | ||
50 | QSqlDatabase database; | 49 | QSqlDatabase database; | ||
51 | 50 | | |||
52 | KSharedConfig::Ptr pluginConfig; | 51 | KActivityManagerdPluginsSettings *pluginConfig; | ||
53 | bool enabled; | 52 | bool enabled; | ||
54 | }; | 53 | }; | ||
55 | 54 | | |||
56 | BlacklistedApplicationsModel::BlacklistedApplicationsModel(QObject *parent) | 55 | BlacklistedApplicationsModel::BlacklistedApplicationsModel(QObject *parent) | ||
57 | : QAbstractListModel(parent) | 56 | : QAbstractListModel(parent) | ||
58 | { | 57 | { | ||
59 | d->enabled = false; | 58 | d->enabled = false; | ||
60 | d->pluginConfig = KSharedConfig::openConfig(QStringLiteral("kactivitymanagerd-pluginsrc")); | 59 | d->pluginConfig = new KActivityManagerdPluginsSettings; | ||
60 | | ||||
61 | const QString path | ||||
62 | = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | ||||
63 | + QStringLiteral("/kactivitymanagerd/resources/database"); | ||||
64 | d->database = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), QStringLiteral("plugins_sqlite_db_resources")); | ||||
65 | d->database.setDatabaseName(path); | ||||
61 | } | 66 | } | ||
62 | 67 | | |||
63 | BlacklistedApplicationsModel::~BlacklistedApplicationsModel() | 68 | BlacklistedApplicationsModel::~BlacklistedApplicationsModel() | ||
64 | { | 69 | { | ||
65 | } | 70 | } | ||
66 | 71 | | |||
67 | QHash<int, QByteArray> BlacklistedApplicationsModel::roleNames() const | 72 | QHash<int, QByteArray> BlacklistedApplicationsModel::roleNames() const | ||
68 | { | 73 | { | ||
69 | return { | 74 | return { | ||
70 | { ApplicationIdRole, "name"}, | 75 | { ApplicationIdRole, "name"}, | ||
71 | { Qt::DecorationRole, "icon"}, | 76 | { Qt::DecorationRole, "icon"}, | ||
72 | { Qt::DisplayRole, "title"}, | 77 | { Qt::DisplayRole, "title"}, | ||
73 | { BlockedApplicationRole, "blocked" } | 78 | { BlockedApplicationRole, "blocked" } | ||
74 | }; | 79 | }; | ||
75 | } | 80 | } | ||
76 | 81 | | |||
77 | void BlacklistedApplicationsModel::load() | 82 | void BlacklistedApplicationsModel::load() | ||
78 | { | 83 | { | ||
79 | // Loading plugin configuration | 84 | // Loading plugin configuration | ||
80 | 85 | d->pluginConfig->load(); | |||
81 | const auto config = d->pluginConfig->group(SQLITE_PLUGIN_CONFIG_KEY); | 86 | const auto defaultBlockedValue = d->pluginConfig->defaultBlockedByDefaultValue(); | ||
82 | 87 | auto blockedApplications = QSet<QString>::fromList(d->pluginConfig->blockedApplications()); | |||
83 | const auto defaultBlockedValue = config.readEntry("blocked-by-default", false); | 88 | auto allowedApplications = QSet<QString>::fromList(d->pluginConfig->allowedApplications()); | ||
84 | auto blockedApplications = QSet<QString>::fromList(config.readEntry("blocked-applications", QStringList())); | | |||
85 | auto allowedApplications = QSet<QString>::fromList(config.readEntry("allowed-applications", QStringList())); | | |||
86 | 89 | | |||
87 | // Reading new applications from the database | 90 | // Reading new applications from the database | ||
88 | 91 | | |||
89 | const QString path | | |||
90 | = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) | | |||
91 | + QStringLiteral("/kactivitymanagerd/resources/database"); | | |||
92 | | ||||
93 | d->database = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), QStringLiteral("plugins_sqlite_db_resources")); | | |||
94 | d->database.setDatabaseName(path); | | |||
95 | | ||||
96 | if (!d->database.open()) { | 92 | if (!d->database.open()) { | ||
97 | // qDebug() << "Failed to open the database" << path << d->database.lastError(); | 93 | // qDebug() << "Failed to open the database" << path << d->database.lastError(); | ||
98 | return; | 94 | return; | ||
99 | } | 95 | } | ||
100 | 96 | | |||
101 | auto query = d->database.exec(QStringLiteral("SELECT DISTINCT(initiatingAgent) FROM ResourceScoreCache ORDER BY initiatingAgent")); | 97 | auto query = d->database.exec(QStringLiteral("SELECT DISTINCT(initiatingAgent) FROM ResourceScoreCache ORDER BY initiatingAgent")); | ||
102 | 98 | | |||
103 | if (d->applications.length() > 0) { | 99 | if (d->applications.length() > 0) { | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | 121 | if (applications.length() > 0) { | |||
145 | } | 141 | } | ||
146 | 142 | | |||
147 | endInsertRows(); | 143 | endInsertRows(); | ||
148 | } | 144 | } | ||
149 | } | 145 | } | ||
150 | 146 | | |||
151 | void BlacklistedApplicationsModel::save() | 147 | void BlacklistedApplicationsModel::save() | ||
152 | { | 148 | { | ||
153 | auto config = d->pluginConfig->group(SQLITE_PLUGIN_CONFIG_KEY); | 149 | d->pluginConfig->save(); | ||
154 | QStringList blockedApplications; | 150 | emit changed(false); | ||
155 | QStringList allowedApplications; | | |||
156 | | ||||
157 | for (int i = 0; i < rowCount(); i++) { | | |||
158 | (d->applications[i].blocked ? blockedApplications : allowedApplications) | | |||
159 | << d->applications[i].name; | | |||
160 | } | | |||
161 | | ||||
162 | config.writeEntry("allowed-applications", allowedApplications); | | |||
163 | config.writeEntry("blocked-applications", blockedApplications); | | |||
164 | } | 151 | } | ||
165 | 152 | | |||
166 | void BlacklistedApplicationsModel::defaults() | 153 | void BlacklistedApplicationsModel::defaults() | ||
167 | { | 154 | { | ||
168 | for (int i = 0; i < rowCount(); i++) { | 155 | for (int i = 0; i < rowCount(); i++) { | ||
169 | d->applications[i].blocked = false; | 156 | d->applications[i].blocked = false; | ||
170 | } | 157 | } | ||
171 | 158 | | |||
172 | dataChanged(QAbstractListModel::index(0), | 159 | dataChanged(QAbstractListModel::index(0), | ||
173 | QAbstractListModel::index(rowCount() - 1)); | 160 | QAbstractListModel::index(rowCount() - 1)); | ||
161 | | ||||
162 | emit defaulted(true); | ||||
174 | } | 163 | } | ||
175 | 164 | | |||
176 | void BlacklistedApplicationsModel::toggleApplicationBlocked(int index) | 165 | void BlacklistedApplicationsModel::toggleApplicationBlocked(int index) | ||
177 | { | 166 | { | ||
178 | if (index > rowCount()) { | 167 | if (index > rowCount()) { | ||
179 | return; | 168 | return; | ||
180 | } | 169 | } | ||
181 | 170 | | |||
182 | d->applications[index].blocked = !d->applications[index].blocked; | 171 | d->applications[index].blocked = !d->applications[index].blocked; | ||
183 | dataChanged(QAbstractListModel::index(index), | 172 | dataChanged(QAbstractListModel::index(index), | ||
184 | QAbstractListModel::index(index)); | 173 | QAbstractListModel::index(index)); | ||
185 | 174 | | |||
186 | emit changed(); | 175 | QStringList blockedApplications; | ||
176 | QStringList allowedApplications; | ||||
177 | | ||||
178 | for (int i = 0; i < rowCount(); i++) { | ||||
179 | const auto name = d->applications[i].name; | ||||
180 | if (d->applications[i].blocked) { | ||||
181 | blockedApplications << name; | ||||
182 | } else { | ||||
183 | allowedApplications << name; | ||||
184 | } | ||||
185 | } | ||||
186 | | ||||
187 | d->pluginConfig->setBlockedApplications(blockedApplications); | ||||
188 | d->pluginConfig->setAllowedApplications(allowedApplications); | ||||
189 | | ||||
190 | const auto allowedApplicationsItem = d->pluginConfig->findItem("allowedApplications"); | ||||
191 | Q_ASSERT(allowedApplicationsItem); | ||||
192 | const auto blockedApplicationsItem = d->pluginConfig->findItem("allowedApplications"); | ||||
193 | Q_ASSERT(blockedApplicationsItem); | ||||
194 | | ||||
195 | emit changed(blockedApplicationsItem->isSaveNeeded() && allowedApplicationsItem->isSaveNeeded()); | ||||
196 | emit defaulted(blockedApplicationsItem->isDefault() && allowedApplicationsItem->isDefault()); | ||||
187 | } | 197 | } | ||
188 | 198 | | |||
189 | QVariant BlacklistedApplicationsModel::headerData(int section, Qt::Orientation orientation, int role) const | 199 | QVariant BlacklistedApplicationsModel::headerData(int section, Qt::Orientation orientation, int role) const | ||
190 | { | 200 | { | ||
191 | Q_UNUSED(section); | 201 | Q_UNUSED(section) | ||
192 | Q_UNUSED(orientation); | 202 | Q_UNUSED(orientation) | ||
193 | Q_UNUSED(role); | 203 | Q_UNUSED(role) | ||
194 | return QVariant(); | 204 | return QVariant(); | ||
195 | } | 205 | } | ||
196 | 206 | | |||
197 | QVariant BlacklistedApplicationsModel::data(const QModelIndex &modelIndex, int role) const | 207 | QVariant BlacklistedApplicationsModel::data(const QModelIndex &modelIndex, int role) const | ||
198 | { | 208 | { | ||
199 | const auto index = modelIndex.row(); | 209 | const auto index = modelIndex.row(); | ||
200 | 210 | | |||
201 | if (index > rowCount()) { | 211 | if (index > rowCount()) { | ||
Show All 17 Lines | |||||
219 | 229 | | |||
220 | case BlockedApplicationRole: | 230 | case BlockedApplicationRole: | ||
221 | return application.blocked; | 231 | return application.blocked; | ||
222 | } | 232 | } | ||
223 | } | 233 | } | ||
224 | 234 | | |||
225 | int BlacklistedApplicationsModel::rowCount(const QModelIndex &parent) const | 235 | int BlacklistedApplicationsModel::rowCount(const QModelIndex &parent) const | ||
226 | { | 236 | { | ||
227 | Q_UNUSED(parent); | 237 | Q_UNUSED(parent) | ||
228 | return d->applications.size(); | 238 | return d->applications.size(); | ||
229 | } | 239 | } | ||
230 | 240 | | |||
231 | bool BlacklistedApplicationsModel::enabled() const | 241 | bool BlacklistedApplicationsModel::enabled() const | ||
232 | { | 242 | { | ||
233 | return d->enabled; | 243 | return d->enabled; | ||
234 | } | 244 | } | ||
235 | 245 | | |||
236 | void BlacklistedApplicationsModel::setEnabled(bool enabled) | 246 | void BlacklistedApplicationsModel::setEnabled(bool enabled) | ||
237 | { | 247 | { | ||
238 | d->enabled = enabled; | 248 | d->enabled = enabled; | ||
239 | emit enabledChanged(enabled); | 249 | emit enabledChanged(enabled); | ||
240 | } | 250 | } | ||
241 | 251 | | |||
242 | // #include <BlacklistedApplicationsModel.moc> | 252 | // #include <BlacklistedApplicationsModel.moc> |