| 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 "ClientService.h" |
| 19 | #include <qmaildisconnected.h> |
| 20 | |
| 21 | ClientService::ClientService(QObject *parent) : QObject(parent), |
| 22 | m_undoQueue(0), m_undoTimer(0), m_exportAction(0) |
| 23 | { |
| 24 | m_undoTimer = new QTimer(this); |
| 25 | m_undoTimer->setInterval(5000); |
| 26 | m_undoTimer->setSingleShot(true); |
| 27 | connect(m_undoTimer, SIGNAL(timeout()), this, SLOT(exportMailStoreUpdate())); |
| 28 | m_undoQueue = new QQmlObjectListModel<ClientServiceAction>(this); |
| 29 | connect(m_undoQueue, &QQmlObjectListModel<ClientServiceAction>::countChanged, |
| 30 | this, &ClientService::undoableCountChanged); |
| 31 | m_serviceQueue = new QQmlObjectListModel<ClientServiceAction>(this); |
| 32 | m_serviceWatcher = new ClientServiceWatcher(this); |
| 33 | m_serviceWatcher->setWatchQueue(m_serviceQueue); |
| 34 | connect(m_serviceWatcher, &ClientServiceWatcher::processNext, this, &ClientService::processNextAction); |
| 35 | connect(m_serviceWatcher, &ClientServiceWatcher::messagePartFetched, this, &ClientService::messagePartFetched); |
| 36 | connect(m_serviceWatcher, &ClientServiceWatcher::messagePartFetchFailed, this, &ClientService::messagePartFetchFailed); |
| 37 | connect(m_serviceWatcher, &ClientServiceWatcher::messagesFetched, this, &ClientService::messagesFetched); |
| 38 | connect(m_serviceWatcher, &ClientServiceWatcher::messageFetchFailed, this, &ClientService::messageFetchFailed); |
| 39 | connect(m_serviceWatcher, &ClientServiceWatcher::checkSendMailQueue, this, &ClientService::sendAnyQueuedMail); |
| 40 | connect(m_serviceWatcher, &ClientServiceWatcher::accountSynced, this, &ClientService::accountSynced); |
| 41 | connect(m_serviceWatcher, &ClientServiceWatcher::syncAccountFailed, this, &ClientService::syncAccountFailed); |
| 42 | connect(m_serviceWatcher, &ClientServiceWatcher::standardFoldersCreated, this, &ClientService::standardFoldersCreated); |
| 43 | connect(m_serviceWatcher, &ClientServiceWatcher::foldersSynced, this, &ClientService::foldersSynced); |
| 44 | connect(m_serviceWatcher, &ClientServiceWatcher::foldersSyncFailed, this, &ClientService::foldersSyncFailed); |
| 45 | connect(this, &ClientService::messagesSent, this, &ClientService::markSentRead); |
| 46 | emit queueChanged(); |
| 47 | } |
| 48 | |
| 49 | bool ClientService::hasUndoableAction() |
| 50 | { |
| 51 | bool hasUndoAction = false; |
| 52 | foreach(ClientServiceAction *action, m_undoQueue->toList()) { |
| 53 | if (action->actionType() == ClientServiceAction::Undoable) { |
| 54 | hasUndoAction = true; |
| 55 | } |
| 56 | } |
| 57 | return hasUndoAction; |
| 58 | } |
| 59 | |
| 60 | QString ClientService::undoDescription() |
| 61 | { |
| 62 | int undoCount = 0; |
| 63 | bool isDeleteAction = false; |
| 64 | bool isMoveAction = false; |
| 65 | foreach(ClientServiceAction *action, m_undoQueue->toList()) { |
| 66 | if (action->actionType() == ClientServiceAction::Undoable) { |
| 67 | undoCount += qobject_cast<UndoableAction *>(action)->itemCount(); |
| 68 | if (action->serviceActionType() == ClientServiceAction::DeleteAction) { |
| 69 | isDeleteAction = true; |
| 70 | } |
| 71 | if (action->serviceActionType() == ClientServiceAction::MoveAction) { |
| 72 | isMoveAction = true; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (isDeleteAction) { |
| 78 | QString msg = undoCount > 1 ? tr("messages") : tr("message"); |
| 79 | return QStringLiteral("%1 %2 deleted").arg(QString::number(undoCount), msg); |
| 80 | } |
| 81 | if (isMoveAction) { |
| 82 | QString msg = undoCount > 1 ? tr("messages") : tr("message"); |
| 83 | return QStringLiteral("%1 %2 moved").arg(QString::number(undoCount), msg); |
| 84 | } |
| 85 | return QString(); |
| 86 | } |
| 87 | |
| 88 | void ClientService::deleteMessages(const QMailMessageIdList &ids) |
| 89 | { |
| 90 | if (ids.isEmpty()) { |
| 91 | return; |
| 92 | } |
| 93 | qDebug() << "Deleting " << ids.count() << "messages"; |
| 94 | // these are undoable so add to the undo queue once processed |
| 95 | DeleteMessagesAction *action = new DeleteMessagesAction(this, ids); |
| 96 | action->process(); |
| 97 | m_undoQueue->append(action); |
| 98 | } |
| 99 | |
| 100 | void ClientService::restoreMessage(const QMailMessageId &id) |
| 101 | { |
| 102 | if (id.isValid()) { |
| 103 | QMailDisconnected::restoreToPreviousFolder(id); |
| 104 | exportMailStoreUpdate(QMailMessage(id).parentAccountId()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void ClientService::markMessagesImportant(const QMailMessageIdList &msgIds, const bool important) |
| 109 | { |
| 110 | if (msgIds.isEmpty()) { |
| 111 | return; |
| 112 | } |
| 113 | FlagsAction *flagAction = new FlagsAction(this, msgIds, FlagsAction::FlagStarred, important ? FlagsAction::Apply : FlagsAction::Remove); |
| 114 | flagAction->process(); |
| 115 | exportMailStoreUpdate(flagAction->accountIds()); |
| 116 | flagAction->deleteLater(); |
| 117 | } |
| 118 | |
| 119 | void ClientService::markMessagesRead(const QMailMessageIdList &msgIds, const bool read) |
| 120 | { |
| 121 | if (msgIds.isEmpty()) { |
| 122 | return; |
| 123 | } |
| 124 | FlagsAction *flagAction = new FlagsAction(this, msgIds, FlagsAction::FlagRead, read ? FlagsAction::Apply : FlagsAction::Remove); |
| 125 | flagAction->process(); |
| 126 | exportMailStoreUpdate(flagAction->accountIds()); |
| 127 | flagAction->deleteLater(); |
| 128 | } |
| 129 | |
| 130 | void ClientService::markMessagesTodo(const QMailMessageIdList &msgIds, const bool todo) |
| 131 | { |
| 132 | if (msgIds.isEmpty()) { |
| 133 | return; |
| 134 | } |
| 135 | FlagsAction *flagAction = new FlagsAction(this, msgIds, FlagsAction::FlagTodo, todo ? FlagsAction::Apply : FlagsAction::Remove); |
| 136 | flagAction->process(); |
| 137 | exportMailStoreUpdate(flagAction->accountIds()); |
| 138 | flagAction->deleteLater(); |
| 139 | } |
| 140 | |
| 141 | void ClientService::markMessagesDone(const QMailMessageIdList &msgIds, const bool done) |
| 142 | { |
| 143 | if (msgIds.isEmpty()) { |
| 144 | return; |
| 145 | } |
| 146 | QMailMessageIdList todosToRemove; |
| 147 | Q_FOREACH(auto &id, msgIds) { |
| 148 | QMailMessage msg(id); |
| 149 | msg.setCustomField("task-done", QString::number(done ? 1 : 0)); |
| 150 | if ((msg.status() & QMailMessage::Todo)) { |
| 151 | todosToRemove << id; |
| 152 | } |
| 153 | QMailStore::instance()->updateMessage(&msg); |
| 154 | } |
| 155 | if (!todosToRemove.isEmpty()) { |
| 156 | markMessagesTodo(todosToRemove, !done); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void ClientService::markMessagesReplied(const QMailMessageIdList &idList, const bool all) |
| 161 | { |
| 162 | if (idList.isEmpty()) { |
| 163 | return; |
| 164 | } |
| 165 | quint64 applyMask = 0; |
| 166 | quint64 removeMask = 0; |
| 167 | if (all) { |
| 168 | applyMask = (QMailMessage::Replied | QMailMessage::RepliedAll); |
| 169 | } else { |
| 170 | applyMask = QMailMessage::Replied; |
| 171 | } |
| 172 | QMailDisconnected::flagMessages(idList, applyMask, removeMask, "Marking messages replied"); |
| 173 | } |
| 174 | |
| 175 | void ClientService::markMessageForwarded(const QMailMessageIdList &idList) |
| 176 | { |
| 177 | if (idList.isEmpty()) { |
| 178 | return; |
| 179 | } |
| 180 | quint64 applyMask = 0; |
| 181 | quint64 removeMask = 0; |
| 182 | applyMask = QMailMessage::Forwarded; |
| 183 | QMailDisconnected::flagMessages(idList, applyMask, removeMask, "Marking messages forwarded"); |
| 184 | } |
| 185 | |
| 186 | void ClientService::markFolderRead(const QMailFolderId &id) |
| 187 | { |
| 188 | QMailMessageKey folderKey = QMailMessageKey::parentFolderId(id) & |
| 189 | QMailMessageKey::status( |
| 190 | QMailMessage::Removed | QMailMessage::Read | QMailMessage::ReadElsewhere, |
| 191 | QMailDataComparator::Excludes); |
| 192 | auto msgList = QMailStore::instance()->queryMessages(folderKey); |
| 193 | qDebug() << "MARKING " << msgList.size() << "AS READ"; |
| 194 | markMessagesRead(msgList, true); |
| 195 | } |
| 196 | |
| 197 | void ClientService::downloadMessagePart(const QMailMessageId &id, const QString &location) |
| 198 | { |
| 199 | QMailMessagePart::Location part(location); |
| 200 | if (!part.isValid(true)) { |
| 201 | return; |
| 202 | } |
| 203 | enqueue(new FetchMessagePartAction(this, id, location)); |
| 204 | } |
| 205 | |
| 206 | void ClientService::downloadMessages(const QMailMessageIdList &msgIds) |
| 207 | { |
| 208 | if (msgIds.isEmpty()) { |
| 209 | return; |
| 210 | } |
| 211 | enqueue(new FetchMessagesAction(this, msgIds)); |
| 212 | } |
| 213 | |
| 214 | void ClientService::sendMessage(const QMailMessage &msg) |
| 215 | { |
| 216 | // place it in outbox local storage. The service will pick up it's been stored |
| 217 | // and trigger the transmit action. |
| 218 | enqueue(new OutboxAction(this, msg)); |
| 219 | } |
| 220 | |
| 221 | void ClientService::createStandardFolders(const QMailAccountId &id) |
| 222 | { |
| 223 | if (id.isValid()) { |
| 224 | enqueue(new CreateStandardFoldersAction(this, id)); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void ClientService::moveToStandardFolder(const QMailMessageIdList &msgIds, const Folder::FolderType &folder, const bool userTriggered) |
| 229 | { |
| 230 | MoveToStandardFolderAction *action = new MoveToStandardFolderAction(this, msgIds, Folder::folderFromType(folder)); |
| 231 | action->process(); |
| 232 | if (userTriggered) { |
| 233 | m_undoQueue->append(action); |
| 234 | } else { |
| 235 | // exportMailStoreUpdate(action->accountIds()); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void ClientService::moveToFolder(const QMailMessageIdList &msgIds, const QMailFolderId &folder) |
| 240 | { |
| 241 | MoveToFolderAction *action = new MoveToFolderAction(this, msgIds, folder); |
| 242 | action->process(); |
| 243 | m_undoQueue->append(action); |
| 244 | } |
| 245 | |
| 246 | void ClientService::synchronizeAccount(const QMailAccountId &id) |
| 247 | { |
| 248 | if (!id.isValid()) { |
| 249 | // TODO: emit sync failed in case anything is listening |
| 250 | return; |
| 251 | } |
| 252 | enqueue(new AccountSyncAction(this, id)); |
| 253 | } |
| 254 | |
| 255 | void ClientService::emptyTrash(const QMailAccountIdList &ids) |
| 256 | { |
| 257 | Q_FOREACH(auto &id, ids) { |
| 258 | enqueue(new EmptyTrashAction(this, id)); |
| 259 | } |
| 260 | exportMailStoreUpdate(ids); |
| 261 | } |
| 262 | |
| 263 | void ClientService::syncFolders(const QMailAccountId &accountId, const QMailFolderIdList &folders) |
| 264 | { |
| 265 | if (accountId.isValid()) { |
| 266 | enqueue(new FolderSyncAction(this, accountId, folders)); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void ClientService::removeMessage(const QMailMessageId &id, const QMailStore::MessageRemovalOption &option) |
| 271 | { |
| 272 | QMailStore::instance()->removeMessage(id, option); |
| 273 | } |
| 274 | |
| 275 | int ClientService::countMessages(const QMailMessageKey &key) |
| 276 | { |
| 277 | return QMailStore::instance()->countMessages(key); |
| 278 | } |
| 279 | |
| 280 | void ClientService::undoableCountChanged() |
| 281 | { |
| 282 | emit undoCountChanged(); |
| 283 | if (m_undoQueue->isEmpty()) { |
| 284 | m_undoTimer->stop(); |
| 285 | return; |
| 286 | } else if (m_undoTimer->isActive()) { |
| 287 | // restart the timer |
| 288 | m_undoTimer->stop(); |
| 289 | } |
| 290 | m_undoTimer->start(); |
| 291 | } |
| 292 | |
| 293 | void ClientService::rollBackMailStoreUpdates(const QMailAccountIdList &accounts) |
| 294 | { |
| 295 | if (accounts.isEmpty()) { |
| 296 | return; |
| 297 | } |
| 298 | Q_FOREACH(auto &id, accounts) { |
| 299 | if (QMailDisconnected::updatesOutstanding(id)) { |
| 300 | QMailDisconnected::rollBackUpdates(id); |
| 301 | } |
| 302 | emit updatesRolledBack(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void ClientService::markSentRead(const QMailMessageIdList &ids) |
| 307 | { |
| 308 | markMessagesRead(ids, true); |
| 309 | } |
| 310 | |
| 311 | void ClientService::sendAnyQueuedMail() |
| 312 | { |
| 313 | QMailMessageKey outboxFilter(QMailMessageKey::status(QMailMessage::Outbox) & ~QMailMessageKey::status(QMailMessage::Trash)); |
| 314 | foreach (const QMailMessageMetaData &metaData, QMailStore::instance()->messagesMetaData( |
| 315 | outboxFilter, QMailMessageKey::ParentAccountId, QMailStore::ReturnDistinct)) { |
| 316 | SendPendingMessagesAction *action = new SendPendingMessagesAction(this, metaData.parentAccountId()); |
| 317 | connect(action, &SendPendingMessagesAction::messagesSent, this, &ClientService::messagesSent); |
| 318 | connect(action, &SendPendingMessagesAction::messageSendingFailed, this, &ClientService::messageSendingFailed); |
| 319 | enqueue(action); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void ClientService::undoActions() |
| 325 | { |
| 326 | m_undoTimer->stop(); |
| 327 | if (!m_undoQueue->isEmpty()) { |
| 328 | // get list of accountids to rollback updates on |
| 329 | // first get the total number of enabled accounts. |
| 330 | int size = QMailStore::instance()->queryAccounts(QMailAccountKey::messageType(QMailMessage::Email) |
| 331 | & QMailAccountKey::status(QMailAccount::Enabled), |
| 332 | QMailAccountSortKey::name()).count(); |
| 333 | |
| 334 | QMailAccountIdList accounts; |
| 335 | foreach(ClientServiceAction *action, m_undoQueue->toList()) { |
| 336 | Q_FOREACH(const QMailAccountId &id, qobject_cast<UndoableAction *>(action)->accountIds()) { |
| 337 | if (!accounts.contains(id)) { |
| 338 | accounts.append(id); |
| 339 | if (accounts.count() == size) { |
| 340 | break; // early |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | rollBackMailStoreUpdates(accounts); |
| 346 | m_undoQueue->clear(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void ClientService::exportMailStoreUpdate() |
| 351 | { |
| 352 | QMailAccountIdList accounts; |
| 353 | foreach(ClientServiceAction *action, m_undoQueue->toList()) { |
| 354 | Q_FOREACH(const QMailAccountId &id, qobject_cast<UndoableAction *>(action)->accountIds()) { |
| 355 | if (!accounts.contains(id)) { |
| 356 | accounts.append(id); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | exportMailStoreUpdate(accounts); |
| 361 | m_undoQueue->clear(); |
| 362 | } |
| 363 | |
| 364 | void ClientService::exportMailStoreUpdate(const QMailAccountIdList &ids) |
| 365 | { |
| 366 | foreach(const QMailAccountId &id, ids) { |
| 367 | exportMailStoreUpdate(id); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void ClientService::exportMailStoreUpdate(const QMailAccountId &id) |
| 372 | { |
| 373 | if (id.isValid()) { |
| 374 | qDebug() << "Valid account id: " << id.toULongLong(); |
| 375 | if (!exportQueuedForAccountId(id)) { |
| 376 | enqueue(new ExportUpdatesAction(this, id)); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void ClientService::enqueue(ClientServiceAction *action) |
| 382 | { |
| 383 | const bool queueWasEmpty = m_serviceQueue->isEmpty(); |
| 384 | qDebug() << "Enqueuing action"; |
| 385 | m_serviceQueue->enqueue(action); |
| 386 | if (queueWasEmpty) { |
| 387 | qDebug() << "Queue was empty processing next action"; |
| 388 | processNextServiceAction(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | bool ClientService::exportQueuedForAccountId(const QMailAccountId &id) |
| 393 | { |
| 394 | bool queued = false; |
| 395 | Q_FOREACH(ClientServiceAction *action, m_serviceQueue->toList()) { |
| 396 | if (action->metaObject()->className() == QStringLiteral("ExportUpdatesAction")) { |
| 397 | ExportUpdatesAction *exportAction = static_cast<ExportUpdatesAction *>(action); |
| 398 | if (exportAction->accountId() == id) { |
| 399 | if (action == m_serviceQueue->first()) { |
| 400 | if (m_serviceQueue->first()->isRunning()) { |
| 401 | qDebug() << "Action queued but currently running, so queue another"; |
| 402 | queued = false; // already running so we need to queue another |
| 403 | continue; |
| 404 | } |
| 405 | } |
| 406 | qDebug() << "Action already queued for "<< id; |
| 407 | queued = true; // already queued; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | return queued; |
| 412 | } |
| 413 | |
| 414 | void ClientService::processNextAction() |
| 415 | { |
| 416 | QTimer::singleShot(0, this, SLOT(processNextServiceAction())); |
| 417 | } |
| 418 | |
| 419 | void ClientService::processNextServiceAction() |
| 420 | { |
| 421 | if (m_serviceQueue->isEmpty()) { |
| 422 | qDebug() << "Action queue empty nothing to do :-)"; |
| 423 | return; |
| 424 | } |
| 425 | qDebug() << "Processing next service action;"; |
| 426 | qDebug() << "Queue size is: " << m_serviceQueue->size(); |
| 427 | if (!m_serviceQueue->first()->isRunning()) { |
| 428 | connect(m_serviceQueue->first(), &ClientServiceAction::activityChanged, m_serviceWatcher, &ClientServiceWatcher::activityChanged); |
| 429 | m_serviceQueue->first()->process(); |
| 430 | } else { |
| 431 | qDebug() << "Action already running, cannot start another until it's done."; |
| 432 | } |
| 433 | } |