| 1 | /* Copyright (C) 2016 - 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 "MessageSet.h" |
| 19 | #include <QDebug> |
| 20 | #include <QDBusPendingReply> |
| 21 | #include <qmailstore.h> |
| 22 | #include <qmailnamespace.h> |
| 23 | #include "MailServiceClient.h" |
| 24 | #include "serviceutils.h" |
| 25 | |
| 26 | MessageSet::MessageSet(QObject *parent) : QObject(parent), m_children(0), |
| 27 | m_unreadCount(0), m_totalCount(0) |
| 28 | { |
| 29 | m_children = new QQmlObjectListModel<MessageSet>(this); |
| 30 | connect(m_children, &QQmlObjectListModel<MessageSet>::countChanged, |
| 31 | this, &MessageSet::descendentsCountChanged); |
| 32 | connect(QMailStore::instance(), |
| 33 | SIGNAL(folderContentsModified(const QMailFolderIdList&)), |
| 34 | this, |
| 35 | SIGNAL(countChanged())); |
| 36 | connect(this, &MessageSet::countChanged, this, &MessageSet::updateCounts); |
| 37 | } |
| 38 | |
| 39 | int MessageSet::unreadCount() |
| 40 | { |
| 41 | return m_unreadCount; |
| 42 | } |
| 43 | |
| 44 | int MessageSet::totalCount() |
| 45 | { |
| 46 | return m_totalCount; |
| 47 | } |
| 48 | |
| 49 | void MessageSet::setMessageKey(const QMailMessageKey &key) |
| 50 | { |
| 51 | if (m_key == key) |
| 52 | return; |
| 53 | m_key = key; |
| 54 | emit messageKeyChanged(); |
| 55 | updateCounts(); |
| 56 | } |
| 57 | |
| 58 | void MessageSet::setDisplayName(const QString &displayName) |
| 59 | { |
| 60 | if (m_name == displayName) |
| 61 | return; |
| 62 | m_name = displayName; |
| 63 | emit displayNameChanged(); |
| 64 | } |
| 65 | |
| 66 | void MessageSet::updateCounts() |
| 67 | { |
| 68 | //unread |
| 69 | QMailMessageKey unreadKey; |
| 70 | if (hasDecendents()) { |
| 71 | unreadKey = descendentsKey().value<QMailMessageKey>() & QMailMessageKey::status(QMailMessage::Read, QMailDataComparator::Excludes); |
| 72 | } else { |
| 73 | unreadKey = m_key & QMailMessageKey::status(QMailMessage::Read, QMailDataComparator::Excludes); |
| 74 | } |
| 75 | |
| 76 | QDBusPendingReply<int> unreadReply = Client::instance()->bus()->totalCount(msg_key_bytes(unreadKey)); |
| 77 | |
| 78 | QDBusPendingCallWatcher *unreadWatcher = new QDBusPendingCallWatcher(unreadReply, this); |
| 79 | connect(unreadWatcher, &QDBusPendingCallWatcher::finished, this, &MessageSet::updateUnreadCount); |
| 80 | |
| 81 | //total |
| 82 | QMailMessageKey totalKey; |
| 83 | if (hasDecendents()) { |
| 84 | totalKey = descendentsKey().value<QMailMessageKey>(); |
| 85 | } else { |
| 86 | totalKey = m_key; |
| 87 | } |
| 88 | |
| 89 | QDBusPendingReply<int> totalReply = Client::instance()->bus()->totalCount(msg_key_bytes(totalKey)); |
| 90 | |
| 91 | QDBusPendingCallWatcher *totalWatcher = new QDBusPendingCallWatcher(totalReply, this); |
| 92 | connect(totalWatcher, &QDBusPendingCallWatcher::finished, this, &MessageSet::updateTotalCount); |
| 93 | } |
| 94 | |
| 95 | void MessageSet::updateUnreadCount(QDBusPendingCallWatcher *call) |
| 96 | { |
| 97 | QDBusPendingReply<int> reply = *call; |
| 98 | if (reply.isError()) { |
| 99 | qDebug() << "[MessageSet::updateUnreadCount] >> Reply error"; |
| 100 | call->deleteLater(); |
| 101 | return; |
| 102 | } |
| 103 | m_unreadCount = reply.value(); |
| 104 | emit unreadCountChanged(); |
| 105 | call->deleteLater(); |
| 106 | } |
| 107 | |
| 108 | void MessageSet::updateTotalCount(QDBusPendingCallWatcher *call) |
| 109 | { |
| 110 | QDBusPendingReply<int> reply = *call; |
| 111 | if (reply.isError()) { |
| 112 | qDebug() << "[MessageSet::updateUnreadCount] >> Reply error"; |
| 113 | call->deleteLater(); |
| 114 | return; |
| 115 | } |
| 116 | m_totalCount = reply.value(); |
| 117 | emit totalCountChanged(); |
| 118 | call->deleteLater(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | StandardFolderSet::StandardFolderSet(QObject *parent) : MessageSet(parent), |
| 123 | m_type(StandardFolder) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | void StandardFolderSet::setType(StandardFolderSet::FolderType type) |
| 128 | { |
| 129 | if (m_type == type) |
| 130 | return; |
| 131 | m_type = type; |
| 132 | emit standardFolderChanged(); |
| 133 | } |
| 134 | |
| 135 | void StandardFolderSet::trackAccountChanges() |
| 136 | { |
| 137 | connect(QMailStore::instance(), &QMailStore::accountsAdded, this, &StandardFolderSet::accountsAdded); |
| 138 | connect(QMailStore::instance(), &QMailStore::accountsRemoved, this, &StandardFolderSet::accountsRemove); |
| 139 | connect(QMailStore::instance(), &QMailStore::accountsUpdated, this, &StandardFolderSet::accountsChanged); |
| 140 | } |
| 141 | |
| 142 | void StandardFolderSet::accountsAdded(const QMailAccountIdList &idList) |
| 143 | { |
| 144 | if (queryEnabledAccounts().isEmpty()) { |
| 145 | return; |
| 146 | } |
| 147 | Q_FOREACH(auto &id, idList) { |
| 148 | if (!m_inboxList.contains(id)) { |
| 149 | auto set = new StandardFolderSet(); |
| 150 | set->setType(SpecialUseInboxFolder); |
| 151 | set->initNoDecendents(QMailAccount(id).name(), createAccountDescendentKey(id, QMailFolder::InboxFolder)); |
| 152 | m_children->append(set); |
| 153 | m_inboxList.append(id); |
| 154 | } else { |
| 155 | qDebug() << "[StandardFolderSet]" << __func__ << "Account with same id already a descendent"; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void StandardFolderSet::accountsRemove(const QMailAccountIdList &idList) |
| 161 | { |
| 162 | Q_FOREACH(auto id, idList) { |
| 163 | int index = m_inboxList.indexOf(id); |
| 164 | if (index == -1) { |
| 165 | continue; |
| 166 | } |
| 167 | m_children->remove(index); |
| 168 | m_inboxList.removeAt(index); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void StandardFolderSet::accountsChanged(const QMailAccountIdList &idList) |
| 173 | { |
| 174 | auto enabledIds = queryEnabledAccounts(); |
| 175 | |
| 176 | Q_FOREACH(auto &id, idList) { |
| 177 | if (!enabledIds.contains(id)) { |
| 178 | int index = m_inboxList.indexOf(id); |
| 179 | if (index == -1) |
| 180 | continue; |
| 181 | m_children->remove(index); |
| 182 | m_inboxList.removeAt(index); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | Q_FOREACH(auto &enabledId, enabledIds) { |
| 187 | int index = m_inboxList.indexOf(enabledId); |
| 188 | if (index == -1) { |
| 189 | // FIXME: We shouldn't need to do this here or in appendInboxDescendents |
| 190 | // but it works around bug |
| 191 | QMail::detectStandardFolders(enabledId); |
| 192 | auto set = new StandardFolderSet(); |
| 193 | set->setType(SpecialUseInboxFolder); |
| 194 | set->initNoDecendents(QMailAccount(enabledId).name(), createAccountDescendentKey(enabledId, QMailFolder::InboxFolder)); |
| 195 | m_children->append(set); |
| 196 | m_inboxList.append(enabledId); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void StandardFolderSet::appendInboxDescendents() |
| 202 | { |
| 203 | Q_FOREACH(const QMailAccountId &id, queryEnabledAccounts()) { |
| 204 | QMail::detectStandardFolders(id); |
| 205 | auto set = new StandardFolderSet(); |
| 206 | set->setType(SpecialUseInboxFolder); |
| 207 | set->initNoDecendents(QMailAccount(id).name(), createAccountDescendentKey(id, QMailFolder::InboxFolder)); |
| 208 | m_children->append(set); |
| 209 | m_inboxList.append(id); |
| 210 | } |
| 211 | updateCounts(); |
| 212 | } |
| 213 | |
| 214 | QMailMessageKey StandardFolderSet::createAccountDescendentKey(const QMailAccountId &id, const QMailFolder::StandardFolder &folderType) |
| 215 | { |
| 216 | QMailAccount account(id); |
| 217 | QMailFolderId folderId = account.standardFolder(folderType); |
| 218 | QMailFolderKey inboxKey = QMailFolderKey::id(folderId, QMailDataComparator::Equal); |
| 219 | QMailMessageKey excludeRemovedKey = QMailMessageKey::status(QMailMessage::Removed, QMailDataComparator::Excludes); |
| 220 | QMailMessageKey msgKey = QMailMessageKey::parentFolderId(inboxKey) & excludeRemovedKey; |
| 221 | if (msgKey.isEmpty()) { |
| 222 | return QMailMessageKey::nonMatchingKey(); |
| 223 | } |
| 224 | return msgKey; |
| 225 | } |
| 226 | |
| 227 | QMailAccountIdList StandardFolderSet::queryEnabledAccounts() |
| 228 | { |
| 229 | return QMailStore::instance()->queryAccounts(QMailAccountKey::messageType(QMailMessage::Email) |
| 230 | & QMailAccountKey::status(QMailAccount::Enabled), |
| 231 | QMailAccountSortKey::name()); |
| 232 | } |
| 233 | |
| 234 | void StandardFolderSet::init(const QString &displayName, const QMailMessageKey &messageKey) |
| 235 | { |
| 236 | m_name = displayName; |
| 237 | m_key = messageKey; |
| 238 | emit displayNameChanged(); |
| 239 | emit messageKeyChanged(); |
| 240 | |
| 241 | switch(m_type) { |
| 242 | case StandardFolder: |
| 243 | break; |
| 244 | case SpecialUseInboxFolder: |
| 245 | { |
| 246 | appendInboxDescendents(); |
| 247 | trackAccountChanges(); |
| 248 | break; |
| 249 | } |
| 250 | case SpecialUseDraftsFolder: |
| 251 | case SpecialUseJunkFolder: |
| 252 | case SpecialUseOutboxFolder: |
| 253 | case SpecialUseSentFolder: |
| 254 | case SpecialUseTrashFolder: |
| 255 | break; |
| 256 | } |
| 257 | updateCounts(); |
| 258 | } |
| 259 | |
| 260 | void StandardFolderSet::initNoDecendents(const QString &displayName, const QMailMessageKey &messageKey) |
| 261 | { |
| 262 | m_name = displayName; |
| 263 | m_key = messageKey; |
| 264 | emit displayNameChanged(); |
| 265 | emit messageKeyChanged(); |
| 266 | } |
| 267 | |
| 268 | QVariant StandardFolderSet::descendentsKey() |
| 269 | { |
| 270 | QMailFolderIdList descendentFolders; |
| 271 | Q_FOREACH(const QMailAccountId &id, queryEnabledAccounts()) { |
| 272 | QMailAccount account(id); |
| 273 | QMailFolderId folderId; |
| 274 | if (m_type == SpecialUseInboxFolder) { |
| 275 | folderId = account.standardFolder(QMailFolder::InboxFolder); |
| 276 | if (folderId.isValid()) { |
| 277 | descendentFolders << account.standardFolder(QMailFolder::InboxFolder); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | QMailMessageKey excludeRemovedKey = QMailMessageKey::status(QMailMessage::Removed, QMailDataComparator::Excludes); |
| 282 | QMailFolderKey inboxKey = QMailFolderKey::id(descendentFolders, QMailDataComparator::Includes); |
| 283 | QMailMessageKey messageKey = QMailMessageKey::parentFolderId(inboxKey) & excludeRemovedKey; |
| 284 | return messageKey; |
| 285 | } |
| 286 | |
| 287 | SmartFolderSet::SmartFolderSet(QObject *parent) : MessageSet(parent), |
| 288 | m_type(SmartTodoFolder), m_timer(0) |
| 289 | { |
| 290 | } |
| 291 | |
| 292 | void SmartFolderSet::init(const QString &displayName, const QMailMessageKey &messageKey) |
| 293 | { |
| 294 | m_name = displayName; |
| 295 | m_key = messageKey; |
| 296 | emit displayNameChanged(); |
| 297 | emit messageKeyChanged(); |
| 298 | |
| 299 | if (m_type == SmartTodayFolder) { |
| 300 | if (!m_timer) { |
| 301 | m_timer = new QTimer(this); |
| 302 | m_timer->setInterval(10000); |
| 303 | connect(m_timer, &QTimer::timeout, this, &SmartFolderSet::updateDescription); |
| 304 | m_timer->start(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (m_type == SmartFolderSet::SmartTodoFolder) { |
| 309 | connect(this, &SmartFolderSet::totalCountChanged, this, &SmartFolderSet::smartCountChanged); |
| 310 | } else { |
| 311 | connect(this, &SmartFolderSet::unreadCountChanged, this, &SmartFolderSet::smartCountChanged); |
| 312 | } |
| 313 | updateCounts(); |
| 314 | } |
| 315 | |
| 316 | SmartFolderSet::SmartFolderType SmartFolderSet::type() const |
| 317 | { |
| 318 | return m_type; |
| 319 | } |
| 320 | |
| 321 | void SmartFolderSet::setType(SmartFolderSet::SmartFolderType type) |
| 322 | { |
| 323 | if (m_type == type) |
| 324 | return; |
| 325 | |
| 326 | m_type = type; |
| 327 | emit typeChanged(type); |
| 328 | } |
| 329 | |
| 330 | void SmartFolderSet::updateDescription() |
| 331 | { |
| 332 | m_name = tr("Today, %1").arg(QDateTime::currentDateTime().toString("ddd d")); |
| 333 | emit displayNameChanged(); |
| 334 | } |
| 335 | |
| 336 | int SmartFolderSet::smartCount() |
| 337 | { |
| 338 | if (m_type == SmartFolderSet::SmartTodoFolder) { |
| 339 | return totalCount(); |
| 340 | } else { |
| 341 | return unreadCount(); |
| 342 | } |
| 343 | } |