| 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 <MailServiceClient.h> |
| 19 | #include <QDebug> |
| 20 | #include <QPointer> |
| 21 | #include <qmailstore.h> |
| 22 | #include <QDBusConnection> |
| 23 | #include "MailServiceWorker.h" |
| 24 | #include "serviceutils.h" |
| 25 | #include "qmailnamespace.h" |
| 26 | |
| 27 | #define SERVICE "org.dekkoproject.Service" |
| 28 | #define SERVICE_PATH "/mail" |
| 29 | |
| 30 | static QPointer<Client> s_client; |
| 31 | Client *Client::instance() |
| 32 | { |
| 33 | if (s_client.isNull()) { |
| 34 | s_client = new Client(); |
| 35 | } |
| 36 | return s_client; |
| 37 | } |
| 38 | |
| 39 | QObject *Client::factory(QQmlEngine *engine, QJSEngine *scriptEngine) |
| 40 | { |
| 41 | Q_UNUSED(engine); |
| 42 | Q_UNUSED(scriptEngine); |
| 43 | return Client::instance(); |
| 44 | } |
| 45 | |
| 46 | Client::Client(QObject *parent) : QObject(parent), |
| 47 | m_service(0), |
| 48 | m_mService(0), |
| 49 | m_folderSyncsInProgress(0), |
| 50 | m_folderSyncErrorOccurred(false) |
| 51 | { |
| 52 | |
| 53 | MailServiceWorker::registerTypes(); |
| 54 | |
| 55 | m_mService = new MailServiceInterface(SERVICE, SERVICE_PATH, QDBusConnection::sessionBus()); |
| 56 | |
| 57 | m_service = new ClientService(this); |
| 58 | emit serviceChanged(); |
| 59 | |
| 60 | connect(m_mService, &MailServiceInterface::messagePartNowAvailable, this, &Client::messagePartNowAvailable); |
| 61 | connect(m_mService, &MailServiceInterface::messagePartFetchFailed, this, &Client::messagePartFetchFailed); |
| 62 | connect(m_mService, &MailServiceInterface::messagesNowAvailable, this, &Client::handleMessagesNowAvailable); |
| 63 | connect(m_mService, &MailServiceInterface::messageFetchFailed, this, &Client::handleMessageFetchFailed); |
| 64 | connect(m_mService, &MailServiceInterface::messagesSent, this, &Client::handleMessagesSent); |
| 65 | connect(m_mService, &MailServiceInterface::messageSendingFailed, this, &Client::handleMessageSendingFailed); |
| 66 | connect(m_mService, &MailServiceInterface::accountSynced, this, &Client::accountSynced); |
| 67 | connect(m_mService, &MailServiceInterface::foldersSynced, this, &Client::handleFoldersSynced); |
| 68 | connect(m_mService, &MailServiceInterface::foldersSyncFailed, this, &Client::handleFoldersSyncFailed); |
| 69 | connect(m_mService, &MailServiceInterface::syncAccountFailed, this, &Client::syncAccountFailed); |
| 70 | connect(m_mService, &MailServiceInterface::standardFoldersCreated, this, &Client::standardFoldersCreated); |
| 71 | connect(m_mService, &MailServiceInterface::actionFailed, this, &Client::handleFailure); |
| 72 | connect(m_mService, &MailServiceInterface::undoCountChanged, this, &Client::undoCountChanged); |
| 73 | } |
| 74 | |
| 75 | bool Client::hasUndoableActions() const |
| 76 | { |
| 77 | return m_mService->hasUndoableAction(); |
| 78 | } |
| 79 | |
| 80 | QString Client::undoDescription() const |
| 81 | { |
| 82 | return m_mService->undoDescription(); |
| 83 | } |
| 84 | |
| 85 | bool Client::hasConfiguredAccounts() |
| 86 | { |
| 87 | return QMailStore::instance()->countAccounts() > 0; |
| 88 | } |
| 89 | |
| 90 | void Client::deleteMessage(const int &msgId) |
| 91 | { |
| 92 | qDebug() << "Msg id to delete is: " << msgId; |
| 93 | deleteMessages(QMailMessageIdList() << QMailMessageId(msgId)); |
| 94 | } |
| 95 | |
| 96 | void Client::deleteMessages(const QMailMessageIdList &idList) |
| 97 | { |
| 98 | m_mService->deleteMessages(to_dbus_msglist(idList)); |
| 99 | } |
| 100 | |
| 101 | void Client::restoreMessage(const int &msgId) |
| 102 | { |
| 103 | m_mService->restoreMessage(QMailMessageId(msgId).toULongLong()); |
| 104 | } |
| 105 | |
| 106 | void Client::markMessageImportant(const int &msgId, const bool important) |
| 107 | { |
| 108 | markMessagesImportant(QMailMessageIdList() << QMailMessageId(msgId), important); |
| 109 | } |
| 110 | |
| 111 | void Client::markMessageRead(const int &msgId, const bool read) |
| 112 | { |
| 113 | markMessagesRead(QMailMessageIdList() << QMailMessageId(msgId), read); |
| 114 | } |
| 115 | |
| 116 | void Client::markMessageTodo(const int &msgId, const bool todo) |
| 117 | { |
| 118 | markMessagesTodo(QMailMessageIdList() << QMailMessageId(msgId), todo); |
| 119 | } |
| 120 | |
| 121 | void Client::markMessageDone(const int &msgId, const bool done) |
| 122 | { |
| 123 | markMessagesDone(QMailMessageIdList() << QMailMessageId(msgId), done); |
| 124 | } |
| 125 | |
| 126 | void Client::moveToStandardFolder(const int &msgId, const int &standardFolder) |
| 127 | { |
| 128 | moveToStandardFolder(QMailMessageIdList() << QMailMessageId(msgId), (Folder::FolderType)standardFolder); |
| 129 | } |
| 130 | |
| 131 | void Client::markStandardFolderRead(const int &standardFolder) |
| 132 | { |
| 133 | Q_FOREACH(auto &id, getEnabledAccountIds()) { |
| 134 | markStandardFolderRead(id.toULongLong(), standardFolder); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void Client::markStandardFolderRead(const quint64 &accountId, const int &standardFolder) |
| 139 | { |
| 140 | QMailAccountId id(accountId); |
| 141 | QMailAccount account(id); |
| 142 | QMailFolderId folder = account.standardFolder(Folder::folderFromType((Folder::FolderType)standardFolder)); |
| 143 | markFolderRead(folder); |
| 144 | } |
| 145 | |
| 146 | void Client::synchronizeAccount(const int &id) |
| 147 | { |
| 148 | synchronizeAccount(QMailAccountId(id)); |
| 149 | } |
| 150 | |
| 151 | void Client::syncStandardFolder(const int &standardFolder) |
| 152 | { |
| 153 | Q_FOREACH(auto &id, getEnabledAccountIds()) { |
| 154 | syncStandardFolder(id.toULongLong(), standardFolder); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void Client::syncStandardFolder(const quint64 &accountId, const int &standardFolder) |
| 159 | { |
| 160 | QMailAccountId id(accountId); |
| 161 | QMailAccount account(id); |
| 162 | QMailFolderId folder = account.standardFolder(Folder::folderFromType((Folder::FolderType)standardFolder)); |
| 163 | syncFolders(id, QMailFolderIdList() << folder); |
| 164 | } |
| 165 | |
| 166 | void Client::syncFolder(const quint64 &accountId, const quint64 &folderId) |
| 167 | { |
| 168 | QMailAccountId id(accountId); |
| 169 | QMailFolderId folder(folderId); |
| 170 | syncFolders(id, QMailFolderIdList() << folder); |
| 171 | } |
| 172 | |
| 173 | void Client::emptyTrash() |
| 174 | { |
| 175 | emptyTrash(getEnabledAccountIds()); |
| 176 | } |
| 177 | |
| 178 | void Client::emptyTrash(const int &accountId) |
| 179 | { |
| 180 | emptyTrash(QMailAccountIdList() << QMailAccountId(accountId)); |
| 181 | } |
| 182 | |
| 183 | void Client::markMessagesImportant(const QMailMessageIdList &idList, const bool important) |
| 184 | { |
| 185 | m_mService->markMessagesImportant(to_dbus_msglist(idList), important); |
| 186 | } |
| 187 | |
| 188 | void Client::markMessagesRead(const QMailMessageIdList &idList, const bool read) |
| 189 | { |
| 190 | m_mService->markMessagesRead(to_dbus_msglist(idList), read); |
| 191 | } |
| 192 | |
| 193 | void Client::markMessagesTodo(const QMailMessageIdList &idList, const bool todo) |
| 194 | { |
| 195 | m_mService->markMessagesTodo(to_dbus_msglist(idList), todo); |
| 196 | } |
| 197 | |
| 198 | void Client::markMessagesDone(const QMailMessageIdList &idList, const bool done) |
| 199 | { |
| 200 | m_mService->markMessagesDone(to_dbus_msglist(idList), done); |
| 201 | } |
| 202 | |
| 203 | void Client::createStandardFolders(const quint64 &accountId) |
| 204 | { |
| 205 | createStandardFolders(QMailAccountId(accountId)); |
| 206 | } |
| 207 | |
| 208 | void Client::createStandardFolders(const QMailAccountId &accountId) |
| 209 | { |
| 210 | m_mService->createStandardFolders(accountId.toULongLong()); |
| 211 | } |
| 212 | |
| 213 | void Client::markMessagesReplied(const QMailMessageIdList &idList, const bool all) |
| 214 | { |
| 215 | m_mService->markMessagesReplied(to_dbus_msglist(idList), all); |
| 216 | } |
| 217 | |
| 218 | void Client::markMessageForwarded(const QMailMessageIdList &idList) |
| 219 | { |
| 220 | m_mService->markMessageForwarded(to_dbus_msglist(idList)); |
| 221 | } |
| 222 | |
| 223 | void Client::markFolderRead(const QMailFolderId &id) |
| 224 | { |
| 225 | m_mService->markFolderRead(id.toULongLong()); |
| 226 | } |
| 227 | |
| 228 | void Client::emptyTrash(const QMailAccountIdList &ids) |
| 229 | { |
| 230 | m_mService->emptyTrash(to_dbus_accountlist(ids)); |
| 231 | } |
| 232 | |
| 233 | void Client::syncFolders(const QMailAccountId &accountId, const QMailFolderIdList &folders) |
| 234 | { |
| 235 | if (m_folderSyncsInProgress == 0) { |
| 236 | m_folderSyncErrorOccurred = false; |
| 237 | } |
| 238 | m_folderSyncsInProgress++; |
| 239 | m_mService->syncFolders(accountId.toULongLong(), to_dbus_folderlist(folders)); |
| 240 | } |
| 241 | |
| 242 | void Client::downloadMessagePart(const QMailMessagePart *msgPart) |
| 243 | { |
| 244 | qDebug() << "[Client]" << "Downloading message part" << msgPart->location().toString(true); |
| 245 | quint64 id = msgPart->location().containingMessageId().toULongLong(); |
| 246 | QString location = msgPart->location().toString(true); |
| 247 | m_mService->downloadMessagePart(id, location); |
| 248 | } |
| 249 | |
| 250 | void Client::downloadMessage(const QMailMessageId &msgId) |
| 251 | { |
| 252 | downloadMessages(QMailMessageIdList() << msgId); |
| 253 | } |
| 254 | |
| 255 | void Client::downloadMessages(const QMailMessageIdList &idList) |
| 256 | { |
| 257 | Q_UNUSED(idList); |
| 258 | } |
| 259 | |
| 260 | void Client::synchronizeAccount(const QMailAccountId &id) |
| 261 | { |
| 262 | m_mService->synchronizeAccount(id.toULongLong()); |
| 263 | } |
| 264 | |
| 265 | bool Client::addMessage(QMailMessage *msg) |
| 266 | { |
| 267 | return QMailStore::instance()->addMessage(msg); |
| 268 | } |
| 269 | |
| 270 | bool Client::removeMessage(const QMailMessageId &id, const QMailStore::MessageRemovalOption &option) |
| 271 | { |
| 272 | m_mService->removeMessage(id.toULongLong(), static_cast<int>(option)); |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | void Client::moveToStandardFolder(const QMailMessageIdList &msgIds, const Folder::FolderType &folder, const bool userTriggered) |
| 277 | { |
| 278 | m_mService->moveToStandardFolder(to_dbus_msglist(msgIds), static_cast<int>(folder), userTriggered); |
| 279 | } |
| 280 | |
| 281 | void Client::moveToFolder(const quint64 &msgId, const quint64 &folderId) |
| 282 | { |
| 283 | QMailMessageId id(msgId); |
| 284 | QMailFolderId folder(folderId); |
| 285 | |
| 286 | if (!id.isValid() || !folder.isValid()) { |
| 287 | return; |
| 288 | } |
| 289 | moveToFolder(QMailMessageIdList() << id, folder); |
| 290 | } |
| 291 | |
| 292 | void Client::moveToFolder(const QMailMessageIdList &ids, const QMailFolderId &folderId) |
| 293 | { |
| 294 | m_mService->moveToFolder(to_dbus_msglist(ids), folderId.toULongLong()); |
| 295 | } |
| 296 | |
| 297 | bool Client::detectStandardFolders(const quint64 &id) |
| 298 | { |
| 299 | auto aId = QMailAccountId(id); |
| 300 | if (!aId.isValid()) |
| 301 | return false; |
| 302 | return QMail::detectStandardFolders(aId); |
| 303 | } |
| 304 | |
| 305 | void Client::sendMessage(const QMailMessage &msg) |
| 306 | { |
| 307 | m_mService->sendMessage(msg.id().toULongLong()); |
| 308 | } |
| 309 | |
| 310 | void Client::sendPendingMessages() |
| 311 | { |
| 312 | m_mService->sendPendingMessages(); |
| 313 | } |
| 314 | |
| 315 | void Client::undoActions() |
| 316 | { |
| 317 | m_mService->undoActions(); |
| 318 | } |
| 319 | |
| 320 | void Client::pruneCache(const QMailMessageIdList &msgIds) |
| 321 | { |
| 322 | if (msgIds.isEmpty()) { |
| 323 | qDebug() << "[Client::pruneCache] >> No Messages to prune"; |
| 324 | } |
| 325 | m_mService->pruneCache(to_dbus_msglist(msgIds)); |
| 326 | } |
| 327 | |
| 328 | void Client::handleFailure(const quint64 &id, const int &statusCode, const QString &statusText) |
| 329 | { |
| 330 | QMailServiceAction::Status::ErrorCode s = static_cast<QMailServiceAction::Status::ErrorCode>(statusCode); |
| 331 | Error error; |
| 332 | switch(s) { |
| 333 | case QMailServiceAction::Status::ErrNoError: |
| 334 | error = Error::NoError; |
| 335 | break; |
| 336 | case QMailServiceAction::Status::ErrNotImplemented: |
| 337 | error = Error::UnknownError; |
| 338 | break; |
| 339 | case QMailServiceAction::Status::ErrFrameworkFault: |
| 340 | error = Error::FrameworkFault; |
| 341 | break; |
| 342 | case QMailServiceAction::Status::ErrSystemError: |
| 343 | error = Error::SystemError; |
| 344 | break; |
| 345 | case QMailServiceAction::Status::ErrInternalServer: |
| 346 | error = Error::InternalServerError; |
| 347 | break; |
| 348 | case QMailServiceAction::Status::ErrUnknownResponse: |
| 349 | error = Error::UnexpectedResponse; |
| 350 | break; |
| 351 | case QMailServiceAction::Status::ErrLoginFailed: |
| 352 | error = Error::LoginFailed; |
| 353 | break; |
| 354 | case QMailServiceAction::Status::ErrCancel: |
| 355 | error = Error::CancelError; |
| 356 | break; |
| 357 | case QMailServiceAction::Status::ErrFileSystemFull: |
| 358 | error = Error::FileSystemFull; |
| 359 | break; |
| 360 | case QMailServiceAction::Status::ErrNonexistentMessage: |
| 361 | error = Error::MessageNotExist; |
| 362 | break; |
| 363 | case QMailServiceAction::Status::ErrEnqueueFailed: |
| 364 | error = Error::EnqueueFailed; |
| 365 | break; |
| 366 | case QMailServiceAction::Status::ErrNoConnection: |
| 367 | error = Error::NoConnection; |
| 368 | break; |
| 369 | case QMailServiceAction::Status::ErrConnectionInUse: |
| 370 | error = Error::ConnectionInUse; |
| 371 | break; |
| 372 | case QMailServiceAction::Status::ErrConnectionNotReady: |
| 373 | error = Error::ConnectionNotReady; |
| 374 | break; |
| 375 | case QMailServiceAction::Status::ErrConfiguration: |
| 376 | error = Error::ConfigurationError; |
| 377 | break; |
| 378 | case QMailServiceAction::Status::ErrInvalidAddress: |
| 379 | error = Error::InvalidAddress; |
| 380 | break; |
| 381 | case QMailServiceAction::Status::ErrInvalidData: |
| 382 | error = Error::InvalidData; |
| 383 | break; |
| 384 | case QMailServiceAction::Status::ErrTimeout: |
| 385 | error = Error::TimeoutError; |
| 386 | break; |
| 387 | case QMailServiceAction::Status::ErrInternalStateReset: |
| 388 | error = Error::InternalStateReset; |
| 389 | break; |
| 390 | case QMailServiceAction::Status::ErrNoSslSupport: |
| 391 | error = Error::SslNotSupported; |
| 392 | break; |
| 393 | case QMailServiceAction::Status::ErrUntrustedCertificates: |
| 394 | error = Error::UntrustedCertificate; |
| 395 | break; |
| 396 | default: |
| 397 | error = Error::UnknownError; |
| 398 | break; |
| 399 | } |
| 400 | emit clientError(id, error, statusText); |
| 401 | } |
| 402 | |
| 403 | void Client::handleMessagesNowAvailable(const QList<quint64> &msgIds) |
| 404 | { |
| 405 | QMailMessageIdList messages = from_dbus_msglist(msgIds); |
| 406 | emit messagesNowAvailable(messages); |
| 407 | } |
| 408 | |
| 409 | void Client::handleMessageFetchFailed(const QList<quint64> &msgIds) |
| 410 | { |
| 411 | QMailMessageIdList messages = from_dbus_msglist(msgIds); |
| 412 | emit messageFetchFailed(messages); |
| 413 | } |
| 414 | |
| 415 | void Client::handleMessagesSent(const QList<quint64> &msgIds) |
| 416 | { |
| 417 | QMailMessageIdList messages = from_dbus_msglist(msgIds); |
| 418 | emit messagesSent(messages); |
| 419 | } |
| 420 | |
| 421 | void Client::handleMessageSendingFailed(const QList<quint64> &msgIds, const int &error) |
| 422 | { |
| 423 | QMailMessageIdList messages = from_dbus_msglist(msgIds); |
| 424 | QMailServiceAction::Status::ErrorCode err = static_cast<QMailServiceAction::Status::ErrorCode>(error); |
| 425 | emit messageSendingFailed(messages, err); |
| 426 | } |
| 427 | |
| 428 | void Client::handleFoldersSynced(const quint64 &accountId, const QList<quint64> &folderIds) |
| 429 | { |
| 430 | QMailFolderIdList folders = from_dbus_folderlist(folderIds); |
| 431 | m_folderSyncsInProgress--; |
| 432 | if(m_folderSyncsInProgress == 0) emit foldersSynced(m_folderSyncErrorOccurred); |
| 433 | } |
| 434 | |
| 435 | void Client::handleFoldersSyncFailed(const quint64 &accountId, const QList<quint64> &folderIds) |
| 436 | { |
| 437 | m_folderSyncErrorOccurred = true; |
| 438 | handleFoldersSynced(accountId, folderIds); |
| 439 | } |
| 440 | |
| 441 | QMailAccountIdList Client::getEnabledAccountIds() const |
| 442 | { |
| 443 | return QMailStore::instance()->queryAccounts(QMailAccountKey::messageType(QMailMessage::Email) |
| 444 | & QMailAccountKey::status(QMailAccount::Enabled), |
| 445 | QMailAccountSortKey::name()); |
| 446 | } |
| 447 |