| 1 | /* Copyright (C) 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "notificationwatcher.h" |
| 19 | #include <QJsonArray> |
| 20 | #include <QCoreApplication> |
| 21 | #include <qmailstore.h> |
| 22 | #include "pushclient.h" |
| 23 | |
| 24 | #define CFG_NAMESPACE "dekko-notify" |
| 25 | #define NOTIFY_ENABLED "enabled" |
| 26 | #define NOTIFY_TYPE "type" |
| 27 | |
| 28 | NotificationWatcher::NotificationWatcher(QObject *parent) : QObject(parent), |
| 29 | m_started(QDateTime::currentDateTime()), |
| 30 | m_accounts(new Accounts) |
| 31 | { |
| 32 | m_accounts->setFilter(Accounts::CanReceive); |
| 33 | connect(QMailStore::instance(), SIGNAL(messagesAdded(QMailMessageIdList)), this, SLOT(handleNewMessages(QMailMessageIdList))); |
| 34 | connect(QMailStore::instance(), SIGNAL(messagesUpdated(QMailMessageIdList)), this, SLOT(handleUpdatedMessages(QMailMessageIdList))); |
| 35 | connect(QMailStore::instance(), SIGNAL(messagesRemoved(QMailMessageIdList)), this, SLOT(handleRemovedMessages(QMailMessageIdList))); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | void NotificationWatcher::handleNewMessages(const QMailMessageIdList &msgList) |
| 40 | { |
| 41 | |
| 42 | |
| 43 | QList<QMailMessage> notifiableMessages; |
| 44 | Q_FOREACH(const QMailMessageId &id, msgList) { |
| 45 | QMailMessageMetaData d(id); |
| 46 | if (notifyEnabled(d) && canPostNotification(d)) { |
| 47 | notifiableMessages << QMailMessage(id); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (notifiableMessages.empty()) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | //don't spam too much user |
| 56 | QJsonObject message; |
| 57 | if (notifiableMessages.count() > 1) { |
| 58 | message = buildSummaryMessage(tr("%1 New messages").arg(msgList.count())); |
| 59 | } else { |
| 60 | message = buildMessage(notifiableMessages.at(0)); |
| 61 | } |
| 62 | |
| 63 | PushClient::instance()->send(message); |
| 64 | } |
| 65 | |
| 66 | void NotificationWatcher::handleUpdatedMessages(const QMailMessageIdList &msgList) |
| 67 | { |
| 68 | Q_FOREACH(const QMailMessageId &id, msgList) { |
| 69 | QMailMessageMetaData d(id); |
| 70 | const QString tag = getTag(id); |
| 71 | if (PushClient::instance()->hasTag(tag)) { |
| 72 | if ((d.status() & QMailMessage::Read) || (d.status() & QMailMessage::ReadElsewhere)) { |
| 73 | PushClient::instance()->clearPersistent(tag); |
| 74 | } |
| 75 | // We should also check if the message is still new but we have now received the preview |
| 76 | // then we can update the notification with the new content. |
| 77 | // Probably requires a remove & re-post but with no popup |
| 78 | if (notifyEnabled(d) && canPostNotification(d)) { |
| 79 | // TODO!! |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void NotificationWatcher::handleRemovedMessages(const QMailMessageIdList &msgList) |
| 86 | { |
| 87 | Q_FOREACH(const QMailMessageId &id, msgList) { |
| 88 | const QString tag = getTag(id); |
| 89 | if (PushClient::instance()->hasTag(tag)) { |
| 90 | PushClient::instance()->clearPersistent(tag); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | QJsonObject NotificationWatcher::buildSummaryMessage(const QString &msg) { |
| 96 | |
| 97 | |
| 98 | QString appDir = QCoreApplication::applicationDirPath(); |
| 99 | appDir.replace(QRegExp("dekko2.dekkoproject/.*"), "dekko2.dekkoproject/current/"); |
| 100 | QString icon = QString("file://").append(appDir).append("dekko.png"); |
| 101 | |
| 102 | QJsonObject c; |
| 103 | c["summary"] = msg; |
| 104 | c["popup"] = true; |
| 105 | c["persist"] = true; |
| 106 | c["icon"] = icon; |
| 107 | QJsonArray actions = QJsonArray(); |
| 108 | QString actionUri = QStringLiteral("appid://dekko2.dekkoproject/dekko/current-user-version"); |
| 109 | actions.append(actionUri); |
| 110 | c["actions"] = actions; |
| 111 | |
| 112 | QJsonObject notification; |
| 113 | notification["card"] = c; |
| 114 | notification["sound"] = true; |
| 115 | notification["vibrate"] = vibrate(); |
| 116 | QJsonObject message; |
| 117 | message["notification"] = notification; |
| 118 | return message; |
| 119 | } |
| 120 | |
| 121 | QJsonObject NotificationWatcher::buildMessage(const QMailMessage &msg) |
| 122 | { |
| 123 | |
| 124 | QJsonObject notification; |
| 125 | notification["tag"] = getTag(msg.id()); |
| 126 | notification["card"] = card(msg); |
| 127 | notification["sound"] = true; |
| 128 | notification["vibrate"] = vibrate(); |
| 129 | // notification["emblem-counter"] = emblemCounter(); |
| 130 | QJsonObject message; |
| 131 | message["notification"] = notification; |
| 132 | return message; |
| 133 | } |
| 134 | |
| 135 | QJsonObject NotificationWatcher::card(const QMailMessage &msg) |
| 136 | { |
| 137 | QJsonObject c; |
| 138 | c["summary"] = msg.from().name(); |
| 139 | c["body"] = msg.subject() + "\n" + msg.preview(); |
| 140 | c["popup"] = true; |
| 141 | c["persist"] = true; |
| 142 | Q_UNUSED(msg); |
| 143 | // c["timestamp"] = timestamp; |
| 144 | QJsonArray actions = QJsonArray(); |
| 145 | const QString pId = QString::number(msg.parentAccountId().toULongLong()); |
| 146 | const QString fId = QString::number(msg.parentFolderId().toULongLong()); |
| 147 | const QString mId = QString::number(msg.id().toULongLong()); |
| 148 | // QString actionUri = QString("dekko://notify/%1/%2/%3").arg(pId, fId, mId); |
| 149 | QString actionUri = QStringLiteral("appid://dekko2.dekkoproject/dekko/current-user-version"); |
| 150 | actions.append(actionUri); |
| 151 | c["actions"] = actions; |
| 152 | return c; |
| 153 | } |
| 154 | |
| 155 | QJsonObject NotificationWatcher::vibrate() |
| 156 | { |
| 157 | QJsonObject v; |
| 158 | v["pattern"] = QJsonArray() << 200 << 100; |
| 159 | v["duration"] = 200; |
| 160 | v["repeat"] = 2; |
| 161 | return v; |
| 162 | } |
| 163 | |
| 164 | QJsonObject NotificationWatcher::emblemCounter() |
| 165 | { |
| 166 | QJsonObject e; |
| 167 | e["count"] = 5; |
| 168 | e["visible"] = true; |
| 169 | return e; |
| 170 | } |
| 171 | |
| 172 | bool NotificationWatcher::notifyEnabled(const QMailMessageMetaData &meta) |
| 173 | { |
| 174 | const quint64 id = meta.parentAccountId().toULongLong(); |
| 175 | Account *account = getAccount(id); |
| 176 | if (account) { |
| 177 | const QString &value = account->customValue(CFG_NAMESPACE, NOTIFY_ENABLED); |
| 178 | if (!value.isEmpty()) { |
| 179 | return value.toInt() > 0; |
| 180 | } |
| 181 | account->setCustomValue(CFG_NAMESPACE, NOTIFY_ENABLED, QString::number(1)); |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | NotificationWatcher::NotificationType NotificationWatcher::notificationType(const QMailMessageMetaData &meta) |
| 188 | { |
| 189 | const quint64 id = meta.parentAccountId().toULongLong(); |
| 190 | Account *account = getAccount(id); |
| 191 | if (account) { |
| 192 | const QString &value = account->customValue(CFG_NAMESPACE, NOTIFY_TYPE); |
| 193 | if (!value.isEmpty()) { |
| 194 | int type = value.toInt(); |
| 195 | if (type < 0 || type > 3) { |
| 196 | return NONE; |
| 197 | } |
| 198 | return (NotificationType)type; |
| 199 | } |
| 200 | account->setCustomValue(CFG_NAMESPACE, NOTIFY_TYPE, QString::number(INBOX_ONLY)); |
| 201 | return INBOX_ONLY; |
| 202 | } |
| 203 | return NONE; |
| 204 | } |
| 205 | |
| 206 | bool NotificationWatcher::canPostNotification(const QMailMessageMetaData &meta) |
| 207 | { |
| 208 | const quint64 id = meta.parentAccountId().toULongLong(); |
| 209 | Account *account = getAccount(id); |
| 210 | bool result = false; |
| 211 | if (!account) |
| 212 | return result; |
| 213 | |
| 214 | if (meta.date().toLocalTime() < m_started) { |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | if ((meta.status() & QMailMessage::Incoming) && (meta.status() & QMailMessage::New)) { |
| 219 | |
| 220 | switch (notificationType(meta)) { |
| 221 | case NotificationType::INBOX_ONLY: |
| 222 | { |
| 223 | QMailFolder folder(meta.parentFolderId()); |
| 224 | QMailAccount account(folder.parentAccountId()); |
| 225 | if (account.standardFolders().values().contains(folder.id())) { |
| 226 | QMailFolder::StandardFolder standardFolder = account.standardFolders().key(folder.id()); |
| 227 | result = standardFolder == QMailFolder::InboxFolder; |
| 228 | } |
| 229 | break; |
| 230 | } |
| 231 | case NotificationType::CONTACTS_ONLY: |
| 232 | case NotificationType::EVERYTHING: |
| 233 | { |
| 234 | result = true; |
| 235 | break; |
| 236 | } |
| 237 | case NotificationType::NONE: |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | Account *NotificationWatcher::getAccount(const quint64 &id) |
| 246 | { |
| 247 | if (m_accounts->hasAccount(id)) { |
| 248 | return m_accounts->get(id); |
| 249 | } |
| 250 | return Q_NULLPTR; |
| 251 | } |
| 252 | |
| 253 | QString NotificationWatcher::getTag(const QMailMessageId &id) |
| 254 | { |
| 255 | return QString("dekko-%1").arg(QString::number(id.toULongLong())); |
| 256 | } |
| 257 | |
| 258 |