| 1 | /* Copyright (C) 2015 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License as |
| 5 | published by the Free Software Foundation; either version 2 of |
| 6 | the License or (at your option) version 3 |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #include "serverconfiguration.h" |
| 17 | #include <QMetaEnum> |
| 18 | #include <QMetaObject> |
| 19 | |
| 20 | ServerConfiguration::ServerConfiguration(QObject *parent) : |
| 21 | QObject(parent), |
| 22 | m_imapPort(0), |
| 23 | m_smtpPort(0), |
| 24 | m_imapMethod(NetworkMethod::INVALID), |
| 25 | m_smtpMethod(NetworkMethod::INVALID) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | QString ServerConfiguration::imapHost() const |
| 30 | { |
| 31 | return m_imapHost; |
| 32 | } |
| 33 | |
| 34 | void ServerConfiguration::setImapHost(const QString &hostname) |
| 35 | { |
| 36 | if (hostname.isEmpty()) { |
| 37 | return; |
| 38 | } |
| 39 | if (hostname != m_imapHost) { |
| 40 | m_imapHost = hostname; |
| 41 | emit configChanged(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | int ServerConfiguration::imapPort() const |
| 46 | { |
| 47 | return m_imapPort; |
| 48 | } |
| 49 | |
| 50 | void ServerConfiguration::setImapPort(const int &port) |
| 51 | { |
| 52 | if (port != m_imapPort) { |
| 53 | m_imapPort = port; |
| 54 | emit configChanged(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | ServerConfiguration::NetworkMethod ServerConfiguration::imapMethod() const |
| 59 | { |
| 60 | return m_imapMethod; |
| 61 | } |
| 62 | |
| 63 | void ServerConfiguration::setImapMethod(const ServerConfiguration::NetworkMethod &method) |
| 64 | { |
| 65 | if (method != m_imapMethod) { |
| 66 | m_imapMethod = method; |
| 67 | emit configChanged(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | QString ServerConfiguration::smtpHost() const |
| 72 | { |
| 73 | return m_smtpHost; |
| 74 | } |
| 75 | |
| 76 | void ServerConfiguration::setSmtpHost(const QString &hostname) |
| 77 | { |
| 78 | if (hostname.isEmpty()) { |
| 79 | return; |
| 80 | } |
| 81 | if (hostname != m_smtpHost) { |
| 82 | m_smtpHost = hostname; |
| 83 | emit configChanged(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int ServerConfiguration::smtpPort() const |
| 88 | { |
| 89 | return m_smtpPort; |
| 90 | } |
| 91 | |
| 92 | void ServerConfiguration::setSmtpPort(const int &port) |
| 93 | { |
| 94 | if (port != m_smtpPort) { |
| 95 | m_smtpPort = port; |
| 96 | emit configChanged(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ServerConfiguration::NetworkMethod ServerConfiguration::smtpMethod() const |
| 101 | { |
| 102 | return m_smtpMethod; |
| 103 | } |
| 104 | |
| 105 | void ServerConfiguration::setSmtpMethod(const ServerConfiguration::NetworkMethod &method) |
| 106 | { |
| 107 | if (method != m_smtpMethod) { |
| 108 | m_smtpMethod = method; |
| 109 | emit configChanged(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool ServerConfiguration::isValid() const |
| 114 | { |
| 115 | bool valid = true; |
| 116 | if (m_imapHost.isEmpty() || m_smtpHost.isEmpty()) { |
| 117 | valid = false; |
| 118 | } |
| 119 | if (m_imapPort == 0 || m_smtpPort == 0) { |
| 120 | valid = false; |
| 121 | } |
| 122 | if (m_imapMethod == NetworkMethod::INVALID || m_smtpMethod == NetworkMethod::INVALID) { |
| 123 | valid = false; |
| 124 | } |
| 125 | return valid; |
| 126 | |
| 127 | } |
| 128 | |
| 129 | QVariantMap ServerConfiguration::toMap() const |
| 130 | { |
| 131 | if (!isValid()) { |
| 132 | return QVariantMap(); |
| 133 | } |
| 134 | |
| 135 | auto networkMethodToString = [](const NetworkMethod method) -> QString { |
| 136 | const QMetaObject &metaObj = ServerConfiguration::staticMetaObject; |
| 137 | int index = metaObj.indexOfEnumerator("NetworkMethod"); |
| 138 | QMetaEnum metaEnum = metaObj.enumerator(index); |
| 139 | return metaEnum.valueToKey(method); |
| 140 | }; |
| 141 | |
| 142 | QVariantMap imapconfig; |
| 143 | imapconfig["hostname"] = m_imapHost; |
| 144 | imapconfig["port"] = m_imapPort; |
| 145 | imapconfig["method"] = networkMethodToString(m_imapMethod); |
| 146 | QVariantMap smtpconfig; |
| 147 | smtpconfig["hostname"] = m_smtpHost; |
| 148 | smtpconfig["port"] = m_smtpPort; |
| 149 | smtpconfig["method"] = networkMethodToString(m_smtpMethod); |
| 150 | QVariantMap map; |
| 151 | map["imap"] = imapconfig; |
| 152 | map["smtp"] = smtpconfig; |
| 153 | return map; |
| 154 | } |
| 155 | |
| 156 | bool ServerConfiguration::operator ==(const ServerConfiguration *config) const |
| 157 | { |
| 158 | return (config->imapHost() == m_imapHost |
| 159 | && config->imapPort() == m_imapPort |
| 160 | && config->imapMethod() == m_imapMethod |
| 161 | && config->smtpHost() == m_smtpHost |
| 162 | && config->smtpPort() == m_smtpPort |
| 163 | && config->smtpMethod() == m_smtpMethod); |
| 164 | } |