| 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 "pushclient.h" |
| 19 | #include <QPointer> |
| 20 | #include <QByteArray> |
| 21 | #include <QDBusMessage> |
| 22 | #include <QJsonDocument> |
| 23 | #include <QDebug> |
| 24 | |
| 25 | #define PUSH_APP_ID "dekko2.dekkoproject_dekko" |
| 26 | #define PUSH_SERVICE "com.lomiri.PushNotifications" |
| 27 | #define POSTAL_SERVICE "com.lomiri.Postal" |
| 28 | #define PUSH_PATH "/com/lomiri/PushNotifications" |
| 29 | #define POSTAL_PATH "/com/lomiri/Postal" |
| 30 | #define PUSH_IFACE "com.lomiri.PushNotifications" |
| 31 | #define POSTAL_IFACE "com.lomiri.Postal" |
| 32 | |
| 33 | static QPointer<PushClient> s_client; |
| 34 | PushClient *PushClient::instance() |
| 35 | { |
| 36 | if (s_client.isNull()) { |
| 37 | s_client = new PushClient(); |
| 38 | } |
| 39 | return s_client; |
| 40 | } |
| 41 | |
| 42 | PushClient::PushClient(QObject *parent) : QObject(parent), |
| 43 | m_conn(QDBusConnection::sessionBus()) |
| 44 | { |
| 45 | // m_tags = getPersistent(); |
| 46 | updateCount(); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | //shamelessly stolen from accounts-polld |
| 51 | bool PushClient::send(const QJsonObject &message) |
| 52 | { |
| 53 | QDBusMessage msg = QDBusMessage::createMethodCall(POSTAL_SERVICE, |
| 54 | makePath(PUSH_APP_ID), |
| 55 | POSTAL_IFACE, |
| 56 | "Post"); |
| 57 | msg << PUSH_APP_ID; |
| 58 | QByteArray data = QJsonDocument(message).toJson(QJsonDocument::Compact); |
| 59 | msg << QString::fromUtf8(data); |
| 60 | |
| 61 | qDebug() << "[POST] >> " << msg; |
| 62 | |
| 63 | QDBusMessage reply = m_conn.call(msg); |
| 64 | if (reply.type() == QDBusMessage::ErrorMessage) { |
| 65 | qDebug() << "[POST ERROR] " << reply.errorMessage(); |
| 66 | return false; |
| 67 | } |
| 68 | qDebug() << "[POST SUCCESS] >> Message posted."; |
| 69 | QJsonObject n = message.value("notification").toObject(); |
| 70 | QString tag = n.value("tag").toString(); |
| 71 | updateCount(tag); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool PushClient::update(const QString &tag, const QJsonObject &message) |
| 76 | { |
| 77 | if (hasTag(tag)) { |
| 78 | clearPersistent(tag); |
| 79 | } |
| 80 | return send(message); |
| 81 | } |
| 82 | |
| 83 | bool PushClient::hasTag(const QString &tag) |
| 84 | { |
| 85 | return m_tags.contains(tag); |
| 86 | } |
| 87 | |
| 88 | bool PushClient::clearPersistent(const QString &tag) |
| 89 | { |
| 90 | if (m_tags.contains(tag)) { |
| 91 | qDebug() << "[REMOVE] >> Removing message: " << tag; |
| 92 | QDBusMessage message = QDBusMessage::createMethodCall(POSTAL_SERVICE, |
| 93 | makePath(PUSH_APP_ID), |
| 94 | POSTAL_IFACE, |
| 95 | "ClearPersistent"); |
| 96 | message << PUSH_APP_ID; |
| 97 | message << tag; |
| 98 | |
| 99 | QDBusMessage reply = m_conn.call(message); |
| 100 | if (reply.type() == QDBusMessage::ErrorMessage) { |
| 101 | qDebug() << "[REMOVE ERROR] " << reply.errorMessage(); |
| 102 | return false; |
| 103 | } |
| 104 | qDebug() << "[REMOVE SUCCESS] Notification removed"; |
| 105 | return updateCount(tag, true); |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool PushClient::updateCount(const QString &tag, const bool remove) |
| 111 | { |
| 112 | qDebug() << "[COUNT] >> Updating launcher count"; |
| 113 | if (!tag.isEmpty()) { |
| 114 | if (!remove && !m_tags.contains(tag)) { |
| 115 | qDebug() << "[COUNT] >> Tag not yet in persistent list. adding it now: " << tag; |
| 116 | m_tags << tag; |
| 117 | } |
| 118 | |
| 119 | if (remove && m_tags.contains(tag)) { |
| 120 | qDebug() << "[COUNT] >> Removing tag from persistent list: " << tag; |
| 121 | m_tags.removeAll(tag); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | bool visible = m_tags.count() != 0; |
| 126 | QDBusMessage message = QDBusMessage::createMethodCall(POSTAL_SERVICE, |
| 127 | makePath(PUSH_APP_ID), |
| 128 | POSTAL_IFACE, |
| 129 | "SetCounter"); |
| 130 | message << PUSH_APP_ID << m_tags.count() << visible; |
| 131 | bool result = m_conn.send(message); |
| 132 | if (result) { |
| 133 | qDebug() << "[COUNT] >> Updated."; |
| 134 | } |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | //shamelessly stolen from accounts-polld |
| 139 | QByteArray PushClient::makePath(const QString &appId) |
| 140 | { |
| 141 | QByteArray path(QByteArrayLiteral("/com/lomiri/Postal/")); |
| 142 | |
| 143 | QByteArray pkg = appId.split('_').first().toUtf8(); |
| 144 | for (int i = 0; i < pkg.count(); i++) { |
| 145 | char buffer[10]; |
| 146 | char c = pkg[i]; |
| 147 | switch (c) { |
| 148 | case '+': |
| 149 | case '.': |
| 150 | case '-': |
| 151 | case ':': |
| 152 | case '~': |
| 153 | case '_': |
| 154 | sprintf(buffer, "_%.2x", c); |
| 155 | path += buffer; |
| 156 | break; |
| 157 | default: |
| 158 | path += c; |
| 159 | } |
| 160 | } |
| 161 | qDebug() << "[PATH] >> " << path; |
| 162 | return path; |
| 163 | } |
| 164 | |
| 165 | QStringList PushClient::getPersistent() |
| 166 | { |
| 167 | QDBusMessage message = QDBusMessage::createMethodCall(POSTAL_SERVICE, |
| 168 | makePath(PUSH_APP_ID), |
| 169 | POSTAL_IFACE, |
| 170 | "ListPersistent"); |
| 171 | message << PUSH_APP_ID; |
| 172 | QDBusMessage reply = m_conn.call(message); |
| 173 | if (reply.type() == QDBusMessage::ErrorMessage) { |
| 174 | qDebug() << reply.errorMessage(); |
| 175 | return QStringList(); |
| 176 | } |
| 177 | QStringList tags = reply.arguments()[0].toStringList(); |
| 178 | qDebug() << "[TAGS] >> " << tags; |
| 179 | return tags; |
| 180 | } |