| 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 "AccountValidator.h" |
| 19 | #include <QTimer> |
| 20 | |
| 21 | Q_LOGGING_CATEGORY(D_ACCOUNTS_VALIDATOR, "dekko.accounts.validator") |
| 22 | |
| 23 | AccountValidator::AccountValidator(QObject *parent) : QObject(parent), |
| 24 | m_inProgress(false), m_state(None), m_timer(new QTimer(this)) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | void AccountValidator::validateAccount(Account *account) |
| 29 | { |
| 30 | if (m_inProgress) { |
| 31 | qCWarning(D_ACCOUNTS_VALIDATOR) << "Validation already in progress"; |
| 32 | emit failed(AccountConfiguration::IMAP, ValidationAlreadyInProgress); |
| 33 | return; |
| 34 | } |
| 35 | setInProgress(true); |
| 36 | m_account = account; |
| 37 | init(); |
| 38 | if (m_account->accountId().isValid()) { |
| 39 | m_timer->start(60 * 1000); |
| 40 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Retrieving folder list for account" << m_account->id(); |
| 41 | m_retrievelAction->retrieveFolderList(m_account->accountId(), QMailFolderId(), true); |
| 42 | m_state = RetrieveFolderList; |
| 43 | } else { |
| 44 | qCWarning(D_ACCOUNTS_VALIDATOR) << "Validation failed for account" << m_account->id(); |
| 45 | emit validationFailed(); |
| 46 | emit failed(static_cast<AccountConfiguration *>(m_account->incoming())->serviceType(), AccountInvalid); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void AccountValidator::handleAccountActivity(QMailServiceAction::Activity activity) |
| 51 | { |
| 52 | if (static_cast<QObject *>(m_retrievelAction) == sender()) { |
| 53 | if (activity == QMailServiceAction::Successful) { |
| 54 | switch(m_state) { |
| 55 | case RetrieveFolderList: |
| 56 | { |
| 57 | // Success retrieving folders so create the standard folders |
| 58 | m_state = CreateStandardFolders; |
| 59 | QTimer::singleShot(2000, this, &AccountValidator::createStandardFolders); |
| 60 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Folder list retrieved"; |
| 61 | break; |
| 62 | } |
| 63 | case CreateStandardFolders: |
| 64 | { |
| 65 | // Incomings good |
| 66 | m_state = TransmitMessage; |
| 67 | QTimer::singleShot(1000, this, &AccountValidator::testTransmission); |
| 68 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Standard Folders created"; |
| 69 | break; |
| 70 | } |
| 71 | case TransmitMessage: |
| 72 | { |
| 73 | break; |
| 74 | } |
| 75 | case None: |
| 76 | break; |
| 77 | } |
| 78 | } else if (activity == QMailServiceAction::Failed) { |
| 79 | qCDebug(D_ACCOUNTS_VALIDATOR) << __func__ << "FAILED"; |
| 80 | AccountConfiguration *incoming = static_cast<AccountConfiguration *>(m_account->incoming()); |
| 81 | testFailed(incoming->serviceType(), m_retrievelAction->status()); |
| 82 | } |
| 83 | } else if (static_cast<QObject *>(m_transmitAction) == sender()) { |
| 84 | if (activity == QMailServiceAction::Successful) { |
| 85 | if (m_state == TransmitMessage) { |
| 86 | // Yay!!! |
| 87 | m_timer->stop(); |
| 88 | emit success(); |
| 89 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Transmission completed"; |
| 90 | setInProgress(false); |
| 91 | cleanUp(); |
| 92 | return; |
| 93 | } |
| 94 | } else if (activity == QMailServiceAction::Failed) { |
| 95 | qCWarning(D_ACCOUNTS_VALIDATOR) << "Transmission failed:" << m_retrievelAction->status().text; |
| 96 | AccountConfiguration *outgoing = static_cast<AccountConfiguration *>(m_account->outgoing()); |
| 97 | testFailed(outgoing->serviceType(), m_retrievelAction->status()); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void AccountValidator::createStandardFolders() |
| 103 | { |
| 104 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Creating standard folders for account: " << m_account->id(); |
| 105 | m_retrievelAction->createStandardFolders(m_account->accountId()); |
| 106 | } |
| 107 | |
| 108 | void AccountValidator::testTransmission() |
| 109 | { |
| 110 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Tesing transmission for account: " << m_account->id(); |
| 111 | m_transmitAction->transmitMessages(m_account->accountId()); |
| 112 | } |
| 113 | |
| 114 | void AccountValidator::testFailed(AccountConfiguration::ServiceType serviceType, QMailServiceAction::Status status) |
| 115 | { |
| 116 | Q_UNUSED(serviceType); |
| 117 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Test failed:" << status.errorCode << "-" << status.text; |
| 118 | if (!m_inProgress) { |
| 119 | return; |
| 120 | } |
| 121 | setInProgress(false); |
| 122 | m_timer->stop(); |
| 123 | emit validationFailed(); |
| 124 | emit failedActionStatus(status.errorCode); |
| 125 | cleanUp(); |
| 126 | } |
| 127 | |
| 128 | void AccountValidator::init() |
| 129 | { |
| 130 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Initialising validator"; |
| 131 | m_retrievelAction = new QMailRetrievalAction(this); |
| 132 | connect(m_retrievelAction, &QMailRetrievalAction::activityChanged, this, &AccountValidator::handleAccountActivity); |
| 133 | m_transmitAction = new QMailTransmitAction(this); |
| 134 | connect(m_transmitAction, &QMailTransmitAction::activityChanged, this, &AccountValidator::handleAccountActivity); |
| 135 | connect(m_timer, &QTimer::timeout, [=]() { |
| 136 | m_timer->stop(); |
| 137 | if (m_retrievelAction->isRunning()) { |
| 138 | m_retrievelAction->cancelOperation(); |
| 139 | } |
| 140 | if (m_transmitAction->isRunning()) { |
| 141 | m_transmitAction->cancelOperation(); |
| 142 | } |
| 143 | AccountConfiguration *conf = 0; |
| 144 | if (m_state == TransmitMessage) { |
| 145 | conf = static_cast<AccountConfiguration *>(m_account->outgoing()); |
| 146 | } else { |
| 147 | conf = static_cast<AccountConfiguration *>(m_account->incoming()); |
| 148 | } |
| 149 | emit failed(conf->serviceType(), Timeout); |
| 150 | setInProgress(false); |
| 151 | cleanUp(); |
| 152 | }); |
| 153 | } |
| 154 | |
| 155 | void AccountValidator::cleanUp() |
| 156 | { |
| 157 | qCDebug(D_ACCOUNTS_VALIDATOR) << "Cleaning up..."; |
| 158 | m_retrievelAction->deleteLater(); |
| 159 | m_transmitAction->deleteLater(); |
| 160 | } |
| 161 |