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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
* Copyright (C) 2016 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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/>.
*
*/
#include <gio/gdesktopappinfo.h>
#include <QtCore/QTimer>
#include <QtCore/QDebug>
#include "click_applications_model.h"
#define GSETTINGS_APPS_SCHEMA_ID "com.lomiri.notifications.settings.applications"
#define GSETTINGS_APPLICATIONS_KEY "applications"
#define GSETTINGS_NOTIFICATIONS_SCHEMA_ID "com.lomiri.notifications.settings"
#define GSETTINGS_BASE_PATH "/com/lomiri/NotificationSettings/"
#define GSETTINGS_ENABLE_NOTIFICATIONS_KEY "enable-notifications"
#define GSETTINGS_SOUNDS_NOTIFY_KEY "use-sounds-notifications"
#define GSETTINGS_VIBRATIONS_NOTIFY_KEY "use-vibrations-notifications"
#define GSETTINGS_BUBBLES_NOTIFY_KEY "use-bubbles-notifications"
#define GSETTINGS_LIST_NOTIFY_KEY "use-list-notifications"
ClickApplicationsModel::ClickApplicationsModel(QObject* parent)
: QAbstractListModel(parent),
m_applications(0)
{
// This way populateModel can be override during tests
QTimer::singleShot(0, this, SLOT(populateModel()));
m_checkMissingDesktopDataTimer = new QTimer(this);
m_checkMissingDesktopDataTimer->setInterval(1000);
connect(m_checkMissingDesktopDataTimer, SIGNAL(timeout()), SLOT(checkMissingDesktopData()));
}
ClickApplicationsModel::~ClickApplicationsModel()
{
}
QHash<int, QByteArray> ClickApplicationsModel::roleNames() const
{
static QHash<int, QByteArray> roles;
if (roles.isEmpty()) {
roles[DisplayName] = "displayName";
roles[Icon] = "icon";
roles[EnableNotifications] = "enableNotifications";
roles[SoundsNotify] = "soundsNotify";
roles[VibrationsNotify] = "vibrationsNotify";
roles[BubblesNotify] = "bubblesNotify";
roles[ListNotify] = "listNotify";
}
return roles;
}
int ClickApplicationsModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return m_entries.count();
}
QVariant ClickApplicationsModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
const ClickApplicationEntry& entry = m_entries.at(index.row());
switch (role) {
case DisplayName:
return entry.displayName;
case Icon:
return entry.icon;
case EnableNotifications:
return entry.enableNotifications;
case SoundsNotify:
return entry.soundsNotify;
case VibrationsNotify:
return entry.vibrationsNotify;
case BubblesNotify:
return entry.bubblesNotify;
case ListNotify:
return entry.listNotify;
default:
return QVariant();
}
}
bool ClickApplicationsModel::setNotifyEnabled(int role, int idx, bool enabled)
{
if (idx < 0 || idx >= rowCount()) {
return false;
}
if (!saveNotifyEnabled(m_entries[idx], role, enabled)) {
return false;
}
QVector<int> roles;
roles << role;
if (role != EnableNotifications) {
if (!m_entries[idx].soundsNotify &&
!m_entries[idx].vibrationsNotify &&
!m_entries[idx].bubblesNotify &&
!m_entries[idx].listNotify) {
if (saveNotifyEnabled(m_entries[idx], EnableNotifications, false)) {
roles << EnableNotifications;
}
}
}
Q_EMIT dataChanged(this->index(idx, 0), this->index(idx, 0), roles);
return true;
}
bool ClickApplicationsModel::saveNotifyEnabled(ClickApplicationEntry& entry, int role, bool enabled)
{
QString path = GSETTINGS_BASE_PATH;
if (entry.appName.isEmpty()) {
path = path + "dpkg/" + entry.pkgName + "/";
} else {
path = path + entry.pkgName + "/" + entry.appName + "/";
}
QScopedPointer<QGSettings> settings(new QGSettings(GSETTINGS_NOTIFICATIONS_SCHEMA_ID, path.toUtf8().constData()));
switch (role) {
case EnableNotifications:
if (entry.enableNotifications == enabled) {
return false;
}
entry.enableNotifications = enabled;
settings->set(GSETTINGS_ENABLE_NOTIFICATIONS_KEY, enabled);
return true;
case SoundsNotify:
if (entry.soundsNotify == enabled) {
return false;
}
entry.soundsNotify = enabled;
settings->set(GSETTINGS_SOUNDS_NOTIFY_KEY, enabled);
return true;
case VibrationsNotify:
if (entry.vibrationsNotify == enabled) {
return false;
}
entry.vibrationsNotify = enabled;
settings->set(GSETTINGS_VIBRATIONS_NOTIFY_KEY, enabled);
return true;
case BubblesNotify:
if (entry.bubblesNotify == enabled) {
return false;
}
entry.bubblesNotify = enabled;
settings->set(GSETTINGS_BUBBLES_NOTIFY_KEY, enabled);
return true;
case ListNotify:
if (entry.listNotify == enabled) {
return false;
}
entry.listNotify = enabled;
settings->set(GSETTINGS_LIST_NOTIFY_KEY, enabled);
return true;
default:
return false;
}
}
bool ClickApplicationsModel::getApplicationDataFromDesktopFile(ClickApplicationEntry& entry)
{
QString desktopFile = entry.pkgName + ".desktop";
if (!entry.appName.isEmpty() && !entry.version.isEmpty()) {
desktopFile = entry.pkgName + "_" + entry.appName + "_" + entry.version + ".desktop";
}
GAppInfo* appInfo = (GAppInfo*)g_desktop_app_info_new(desktopFile.toUtf8().constData());
if (appInfo == nullptr) {
qWarning() << Q_FUNC_INFO << "[ERROR] Unable to get desktop file:" << desktopFile;
return false;
}
entry.displayName = g_strdup(g_app_info_get_display_name(appInfo));
GIcon* icon = g_app_info_get_icon(appInfo);
if (icon != nullptr) {
QString iconPath = g_icon_to_string(icon);
entry.icon = iconPath;
}
g_object_unref(appInfo);
return true;
}
void ClickApplicationsModel::getNotificationsSettings(ClickApplicationEntry& entry)
{
QString path = GSETTINGS_BASE_PATH;
if (entry.appName.isEmpty()) {
path = path + "dpkg/" + entry.pkgName + "/";
} else {
path = path + entry.pkgName + "/" + entry.appName + "/";
}
QScopedPointer<QGSettings> settings(new QGSettings(GSETTINGS_NOTIFICATIONS_SCHEMA_ID, path.toUtf8().constData()));
entry.enableNotifications = settings->get(GSETTINGS_ENABLE_NOTIFICATIONS_KEY).toBool();
entry.soundsNotify = settings->get(GSETTINGS_SOUNDS_NOTIFY_KEY).toBool();
entry.vibrationsNotify = settings->get(GSETTINGS_VIBRATIONS_NOTIFY_KEY).toBool();
entry.bubblesNotify = settings->get(GSETTINGS_BUBBLES_NOTIFY_KEY).toBool();
entry.listNotify = settings->get(GSETTINGS_LIST_NOTIFY_KEY).toBool();
}
bool ClickApplicationsModel::parseApplicationKeyFromSettings(ClickApplicationEntry& entry, const QString& appEntry)
{
QStringList entryData = appEntry.split('/');
if (entryData.size() != 3) {
return false;
}
if (entryData[0] == "dpkg" && entryData[2] == "0") {
// Legacy dpkg application
entry.pkgName = entryData[1];
} else {
entry.pkgName = entryData[0];
entry.appName = entryData[1];
entry.version = entryData[2];
}
return true;
}
int ClickApplicationsModel::getIndexByApplicationData(ClickApplicationEntry& entry)
{
for (int i = 0; i < rowCount(); ++i) {
if (m_entries.at(i).pkgName != entry.pkgName) {
continue;
}
if (m_entries.at(i).appName != entry.appName) {
continue;
}
return i;
}
return -1;
}
void ClickApplicationsModel::addMissingDesktopDataEntry(ClickApplicationEntry& entry)
{
m_missingDesktopDataEntries << entry;
m_checkMissingDesktopDataTimer->start();
}
void ClickApplicationsModel::addEntry(ClickApplicationEntry& entry)
{
getNotificationsSettings(entry);
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_entries << entry;
endInsertRows();
Q_EMIT rowCountChanged();
}
void ClickApplicationsModel::removeEntryByIndex(int index)
{
beginRemoveRows(QModelIndex(), index, index);
m_entries.removeAt(index);
endRemoveRows();
Q_EMIT rowCountChanged();
}
void ClickApplicationsModel::populateModel()
{
m_applications.reset(new QGSettings(GSETTINGS_APPS_SCHEMA_ID));
connect(m_applications.data(), SIGNAL(changed(const QString&)), SLOT(onApplicationsListChanged(const QString&)));
Q_FOREACH (QString appEntry, m_applications->get(GSETTINGS_APPLICATIONS_KEY).toStringList()) {
ClickApplicationEntry entry;
if (!parseApplicationKeyFromSettings(entry, appEntry)) {
continue;
}
if (!getApplicationDataFromDesktopFile(entry)) {
addMissingDesktopDataEntry(entry);
continue;
}
addEntry(entry);
}
}
void ClickApplicationsModel::onApplicationsListChanged(const QString& key) {
if (key != GSETTINGS_APPLICATIONS_KEY) {
return;
}
//Check for removed entries
for (int i = rowCount() - 1; i >= 0; --i) {
bool removed = true;
Q_FOREACH (QString appEntry, m_applications->get(GSETTINGS_APPLICATIONS_KEY).toStringList()) {
ClickApplicationEntry entry;
if (!parseApplicationKeyFromSettings(entry, appEntry)) {
continue;
}
if (m_entries.at(i).pkgName == entry.pkgName && m_entries.at(i).appName == entry.appName) {
removed = false;
continue;
}
}
if (!removed) {
continue;
}
removeEntryByIndex(i);
}
//Check for added entries
Q_FOREACH (QString appEntry, m_applications->get(GSETTINGS_APPLICATIONS_KEY).toStringList()) {
ClickApplicationEntry entry;
if (!parseApplicationKeyFromSettings(entry, appEntry)) {
continue;
}
if (getIndexByApplicationData(entry) >= 0) {
continue;
}
if (!getApplicationDataFromDesktopFile(entry)) {
addMissingDesktopDataEntry(entry);
continue;
}
addEntry(entry);
}
}
void ClickApplicationsModel::checkMissingDesktopData()
{
QList<ClickApplicationEntry> stillMissing;
while (!m_missingDesktopDataEntries.isEmpty()) {
ClickApplicationEntry entry = m_missingDesktopDataEntries.takeFirst();
if (!getApplicationDataFromDesktopFile(entry)) {
stillMissing << entry;
} else {
addEntry(entry);
}
}
if (stillMissing.isEmpty()) {
m_checkMissingDesktopDataTimer->stop();
} else {
m_missingDesktopDataEntries.append(stillMissing);
}
}
|