| 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 "SubmissionManager.h" |
| 19 | #include <qmailmessage.h> |
| 20 | #include <qmailstore.h> |
| 21 | |
| 22 | SubmissionManager::SubmissionManager(QObject *parent) : QObject(parent), |
| 23 | m_builder(Q_NULLPTR) |
| 24 | { |
| 25 | connect(Client::instance(), &Client::messagesSent, this, &SubmissionManager::messageSent); |
| 26 | |
| 27 | m_timer.setInterval(30000); |
| 28 | connect(&m_timer, &QTimer::timeout, [=](){saveDraft(false);}); |
| 29 | } |
| 30 | |
| 31 | QObject *SubmissionManager::builder() const |
| 32 | { |
| 33 | return m_builder; |
| 34 | } |
| 35 | |
| 36 | bool SubmissionManager::canSend() const |
| 37 | { |
| 38 | return m_builder->hasRecipients() && !m_builder->subject()->textDocument()->isEmpty(); |
| 39 | } |
| 40 | |
| 41 | void SubmissionManager::setBuilder(QObject *builder) |
| 42 | { |
| 43 | MessageBuilder *b = qobject_cast<MessageBuilder *>(builder); |
| 44 | if (m_builder == b) |
| 45 | return; |
| 46 | |
| 47 | m_builder = b; |
| 48 | connect(m_builder, &MessageBuilder::maybeStartSaveTimer, this, &SubmissionManager::maybeStartSaveTimer); |
| 49 | emit builderChanged(); |
| 50 | } |
| 51 | |
| 52 | void SubmissionManager::send() |
| 53 | { |
| 54 | if (!hasBuilder() || !hasIdentities()) { |
| 55 | qDebug() << "No builder or identity"; |
| 56 | return; |
| 57 | } |
| 58 | if (!canSend()) { |
| 59 | qDebug() << "Can't send yet, invalid message"; |
| 60 | emit error(Error::InvalidMessage); |
| 61 | return; |
| 62 | } |
| 63 | qDebug() << "Stopping timer"; |
| 64 | m_timer.stop(); |
| 65 | // So we first save it as the *final* draft and then use that draft message as the one to send |
| 66 | qDebug() << "Saving final draft"; |
| 67 | saveDraft(false); |
| 68 | |
| 69 | qDebug() << "Final draft id valid? " << m_builder->lastDraftId().isValid(); |
| 70 | // Ok so this function doesn't actually send anything. All we |
| 71 | // are actually doing is grabbing the constructed message, checking if we can reference |
| 72 | // it externally from the imap server for the lemonade profile stuff and handing it |
| 73 | // over to the Client service which will manage the storage and submission at the |
| 74 | // most appropriate time. |
| 75 | qDebug() << "Constructing message"; |
| 76 | QMailMessage msg(m_builder->lastDraftId()); |
| 77 | qDebug() << "Msg valid> " << msg.id().isValid(); |
| 78 | // qDebug() << msg.toRfc2822(QMailMessage::TransmissionFormat); |
| 79 | QMailAccount account(msg.parentAccountId()); |
| 80 | if ((account.status() & QMailAccount::CanReferenceExternalData) && |
| 81 | (account.status() & QMailAccount::CanTransmitViaReference) && |
| 82 | account.standardFolder(QMailFolder::SentFolder).isValid() && |
| 83 | QMailFolder(account.standardFolder(QMailFolder::SentFolder)).id().isValid()) { |
| 84 | qDebug() << "Enabling transmit from external"; |
| 85 | msg.setStatus(QMailMessage::TransmitFromExternal, true); |
| 86 | } |
| 87 | // we also need to update the Replied RepliedToAll and Forwarded status masks if appropriate |
| 88 | // TODO: move this to after the message has been sent. i.e on SubmissionManager::messageSent() signal |
| 89 | if (msg.inResponseTo().isValid()) { |
| 90 | QMailMessageId inReplyTo(msg.inResponseTo()); |
| 91 | if (inReplyTo.isValid()) { |
| 92 | QMailMessage src(inReplyTo); |
| 93 | if (msg.responseType() == QMailMessage::Forward) { |
| 94 | Client::instance()->markMessageForwarded(QMailMessageIdList() << src.id()); |
| 95 | } else { |
| 96 | Client::instance()->markMessagesReplied( |
| 97 | QMailMessageIdList() << src.id(), |
| 98 | (msg.responseType() == QMailMessage::ReplyToAll)); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | qDebug() << "Queuing message to send"; |
| 103 | Client::instance()->sendMessage(msg); |
| 104 | emit messageQueued(); |
| 105 | } |
| 106 | |
| 107 | void SubmissionManager::saveDraft(const bool userTriggered) |
| 108 | { |
| 109 | if (!hasBuilder() || !hasIdentities() || !selectedIdentityIsValid()) { |
| 110 | return; |
| 111 | } |
| 112 | if (!userTriggered) { |
| 113 | emit savingDraftSilently(); |
| 114 | } |
| 115 | |
| 116 | QMailMessage msg = m_builder->message(); |
| 117 | msg.setStatus(QMailMessage::Draft, true); |
| 118 | msg.setStatus(QMailMessage::Outbox, false); |
| 119 | bool saved = false; |
| 120 | if (!msg.id().isValid()) { |
| 121 | // local only |
| 122 | qDebug() << "LOCAL ONLY"; |
| 123 | msg.setStatus(QMailMessage::LocalOnly, true); |
| 124 | saved = Client::instance()->addMessage(&msg); |
| 125 | qDebug() << "Local message added"; |
| 126 | } else { |
| 127 | qDebug() << "UPDATING DRAFT"; |
| 128 | QMailMessageId id = msg.id(); |
| 129 | msg.setId(QMailMessageId()); |
| 130 | msg.setStatus(QMailMessage::LocalOnly, true); |
| 131 | msg.setServerUid(QString()); |
| 132 | qDebug() << "Adding new draft"; |
| 133 | saved = Client::instance()->addMessage(&msg); |
| 134 | qDebug() << "Removing old draft"; |
| 135 | Client::instance()->removeMessage(id, QMailStore::CreateRemovalRecord); |
| 136 | qDebug() << "Draft updated"; |
| 137 | } |
| 138 | // now queue an update at some point with the server. It doesn't |
| 139 | // have to be immediate as we have it localy |
| 140 | if (saved) { |
| 141 | qDebug() << "Draft saved. Moving to drafts folder"; |
| 142 | Client::instance()->moveToStandardFolder(QMailMessageIdList() << msg.id(), Folder::SpecialUseDraftsFolder, false); |
| 143 | if (userTriggered) { |
| 144 | emit draftSaved(); |
| 145 | } else { |
| 146 | emit draftSavedSilently(); |
| 147 | } |
| 148 | qDebug() << "SETTING LAST ID: " << msg.id(); |
| 149 | m_builder->setLastDraftId(msg.id()); |
| 150 | } else { |
| 151 | // TODO: emit an error |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void SubmissionManager::messageSent(const QMailMessageIdList &ids) |
| 156 | { |
| 157 | qDebug() << ids.count() << "messages sent"; |
| 158 | } |
| 159 | |
| 160 | void SubmissionManager::respondToMessage(const SubmissionManager::ResponseType &type, const quint64 &msgId) |
| 161 | { |
| 162 | respondToMessage(type, QMailMessageId(msgId)); |
| 163 | } |
| 164 | |
| 165 | void SubmissionManager::respondToMessage(const SubmissionManager::ResponseType &type, const QMailMessageId &msgId) |
| 166 | { |
| 167 | if (!hasBuilder()) { |
| 168 | qWarning() << "Builder not ready"; |
| 169 | return; |
| 170 | } |
| 171 | if (!msgId.isValid()) { |
| 172 | qWarning() << "Invalid msg id"; |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | MessageBuilder::ReplyType rt; |
| 177 | switch(type) { |
| 178 | case Reply: |
| 179 | rt = MessageBuilder::ReplyType::Reply; |
| 180 | break; |
| 181 | case ReplyAll: |
| 182 | rt = MessageBuilder::ReplyType::ReplyAll; |
| 183 | break; |
| 184 | case ReplyList: |
| 185 | rt = MessageBuilder::ReplyType::ReplyList; |
| 186 | break; |
| 187 | default: |
| 188 | qWarning() << "Unexpected responsetype."; |
| 189 | Q_ASSERT(false); |
| 190 | return; |
| 191 | } |
| 192 | m_builder->buildResponse(rt, QMailMessage(msgId)); |
| 193 | // Normally we would trigger this from the UI side once the first recipient has been added |
| 194 | // but replies are generated on the c++ side so trigger it here. |
| 195 | maybeStartSaveTimer(); |
| 196 | } |
| 197 | |
| 198 | void SubmissionManager::forwardMessage(const SubmissionManager::ResponseType &type, const QMailMessageId &msgId) |
| 199 | { |
| 200 | if (!hasBuilder()) { |
| 201 | qWarning() << "Builder not ready"; |
| 202 | return; |
| 203 | } |
| 204 | if (!msgId.isValid()) { |
| 205 | qWarning() << "Invalid msg id"; |
| 206 | return; |
| 207 | } |
| 208 | MessageBuilder::ForwardType ft; |
| 209 | switch(type) { |
| 210 | case ForwardInline: |
| 211 | ft = MessageBuilder::ForwardType::Inline; |
| 212 | break; |
| 213 | case ForwardAsAttachment: |
| 214 | ft = MessageBuilder::ForwardType::Attach; |
| 215 | break; |
| 216 | default: |
| 217 | qWarning() << "Unexpected forward type."; |
| 218 | Q_ASSERT(false); |
| 219 | return; |
| 220 | } |
| 221 | m_builder->buildForward(ft, QMailMessage(msgId)); |
| 222 | } |
| 223 | |
| 224 | void SubmissionManager::reloadDraft(const quint64 &msgId) |
| 225 | { |
| 226 | reloadDraft(QMailMessageId(msgId)); |
| 227 | } |
| 228 | |
| 229 | void SubmissionManager::reloadDraft(const QMailMessageId &msgId) |
| 230 | { |
| 231 | if (!hasBuilder()) { |
| 232 | qWarning() << "Builder not ready"; |
| 233 | return; |
| 234 | } |
| 235 | if (!msgId.isValid() || !(QMailMessage(msgId).status() & QMailMessage::Draft)) { |
| 236 | qWarning() << "THis isn't a draft message. Aborting!"; |
| 237 | return; |
| 238 | } |
| 239 | m_builder->setLastDraftId(msgId); |
| 240 | m_builder->reloadLastDraftId(); |
| 241 | } |
| 242 | |
| 243 | void SubmissionManager::forwardMessage(const SubmissionManager::ResponseType &type, const quint64 &msgId) |
| 244 | { |
| 245 | forwardMessage(type, QMailMessageId(msgId)); |
| 246 | } |
| 247 | |
| 248 | void SubmissionManager::reset() |
| 249 | { |
| 250 | if (m_timer.isActive()) { |
| 251 | m_timer.stop(); |
| 252 | } |
| 253 | m_builder->reset(); |
| 254 | } |
| 255 | |
| 256 | void SubmissionManager::discard() |
| 257 | { |
| 258 | m_timer.stop(); |
| 259 | QMailMessageId lastId = m_builder->lastDraftId(); |
| 260 | if (lastId.isValid()) { |
| 261 | Client::instance()->removeMessage(lastId, QMailStore::CreateRemovalRecord); |
| 262 | } |
| 263 | reset(); |
| 264 | emit messageDiscarded(); |
| 265 | } |
| 266 | |
| 267 | bool SubmissionManager::hasBuilder() |
| 268 | { |
| 269 | if (m_builder == Q_NULLPTR) { |
| 270 | // requires a constructed message builder |
| 271 | emit error(Error::NoBuilder); |
| 272 | return false; |
| 273 | } else { |
| 274 | return true; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | bool SubmissionManager::hasIdentities() |
| 279 | { |
| 280 | if (!m_builder->hasIdentities()) { |
| 281 | emit error(Error::NoIdentities); |
| 282 | return false; |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | bool SubmissionManager::selectedIdentityIsValid() |
| 288 | { |
| 289 | if (m_builder->selectedIdentity() == -1) { |
| 290 | emit error(Error::InvalidIdentity); |
| 291 | return false; |
| 292 | } |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | void SubmissionManager::maybeStartSaveTimer() |
| 297 | { |
| 298 | if (m_builder->hasRecipients()) { |
| 299 | if (m_timer.isActive()) { |
| 300 | return; |
| 301 | } |
| 302 | // trigger a save 3 seconds from now as to not cause that nasty block |
| 303 | // when the models are updating |
| 304 | QTimer::singleShot(3000, this, [=](){ saveDraft(false); } ); |
| 305 | m_timer.start(); |
| 306 | } |
| 307 | } |
| 308 |