File indexing completed on 2024-05-26 05:28:39

0001 /* Copyright (C) 2014 Dan Chapman <dpniel@ubuntu.com>
0002    Copyright (C) 2006 - 2016 Jan Kundrát <jkt@kde.org>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 #include <QDebug>
0024 #include <QObject>
0025 #include <QSettings>
0026 #include "Common/InvokeMethod.h"
0027 #include "Common/PortNumbers.h"
0028 #include "Common/SettingsNames.h"
0029 #include "MSA/Account.h"
0030 
0031 namespace MSA {
0032 
0033 Account::Account(QObject *parent, QSettings *settings, const QString &accountName) :
0034     QObject(parent), m_settings(settings), m_accountName(accountName),
0035     m_port(0), m_authenticateEnabled(false), m_reuseImapAuth(false), m_saveToImap(false), m_useBurl(false)
0036 {
0037     restoreSettings();
0038 }
0039 
0040 QString Account::server() const
0041 {
0042     return m_server;
0043 }
0044 
0045 void Account::setServer(const QString &server)
0046 {
0047     if (server == m_server) {
0048         return;
0049     }
0050     m_server = server;
0051     emit serverChanged();
0052 }
0053 
0054 int Account::port() const
0055 {
0056     return m_port;
0057 }
0058 
0059 void Account::setPort(const quint16 port)
0060 {
0061     if (m_port == port) {
0062         return;
0063     }
0064     m_port = port;
0065     maybeShowPortWarning();
0066     emit portChanged();
0067 
0068 }
0069 
0070 QString Account::username() const
0071 {
0072     return m_username;
0073 }
0074 
0075 void Account::setUsername(const QString &username)
0076 {
0077     if (username == m_username) {
0078         return;
0079     }
0080     m_username = username;
0081     emit usernameChanged();
0082 }
0083 
0084 bool Account::authenticateEnabled() const
0085 {
0086     return m_authenticateEnabled;
0087 }
0088 
0089 void Account::setAuthenticateEnabled(const bool auth)
0090 {
0091     if (auth == m_authenticateEnabled) {
0092         return;
0093     }
0094     m_authenticateEnabled = auth;
0095     emit authenticateEnabledChanged();
0096 }
0097 
0098 bool Account::reuseImapAuthentication() const
0099 {
0100     return m_reuseImapAuth;
0101 }
0102 
0103 void Account::setReuseImapAuthentication(const bool reuseImapAuth)
0104 {
0105     if (reuseImapAuth == m_reuseImapAuth) {
0106         return;
0107     }
0108     m_reuseImapAuth = reuseImapAuth;
0109     emit reuseImapAuthenticationChanged();
0110 }
0111 
0112 QString Account::pathToSendmail() const
0113 {
0114     return m_pathToSendmail;
0115 }
0116 
0117 void Account::setPathToSendmail(const QString &sendmail)
0118 {
0119     if (sendmail == m_pathToSendmail) {
0120         return;
0121     }
0122     m_pathToSendmail = sendmail;
0123     emit pathToSendmailChanged();
0124 }
0125 
0126 bool Account::saveToImap() const
0127 {
0128     return m_saveToImap;
0129 }
0130 
0131 void Account::setSaveToImap(const bool selected)
0132 {
0133     if (selected == m_saveToImap) {
0134         return;
0135     }
0136     m_saveToImap = selected;
0137     emit saveToImapChanged();
0138 }
0139 
0140 QString Account::sentMailboxName() const
0141 {
0142     return m_sentMailboxName;
0143 }
0144 
0145 void Account::setSentMailboxName(const QString &location)
0146 {
0147     if (location == m_sentMailboxName) {
0148         return;
0149     }
0150     m_sentMailboxName = location;
0151     emit sentMailboxNameChanged();
0152 }
0153 
0154 bool Account::useBurl() const
0155 {
0156     return m_useBurl;
0157 }
0158 
0159 void Account::setUseBurl(const bool selected)
0160 {
0161     if (selected == m_useBurl) {
0162         return;
0163     }
0164     m_useBurl = selected;
0165     emit useBurlChanged();
0166 }
0167 
0168 quint16 Account::defaultPort(const Method method)
0169 {
0170     switch (method) {
0171     case Method::SMTP:
0172     case Method::SMTP_STARTTLS:
0173         return Common::PORT_SMTP_SUBMISSION;
0174         break;
0175     case Method::SSMTP:
0176         return Common::PORT_SMTP_SSL;
0177         break;
0178     case Method::SENDMAIL:
0179     case Method::IMAP_SENDMAIL:
0180         break;
0181     }
0182     Q_ASSERT(false);
0183     return 0;
0184 }
0185 
0186 Account::Method Account::submissionMethod() const
0187 {
0188     return m_msaSubmissionMethod;
0189 }
0190 
0191 void Account::setSubmissionMethod(const Method method)
0192 {
0193     if (method == m_msaSubmissionMethod) {
0194         return;
0195     }
0196     switch (method) {
0197     case Method::SMTP:
0198     case Method::SSMTP:
0199     case Method::SMTP_STARTTLS:
0200         m_msaSubmissionMethod = method;
0201         setPort(defaultPort(m_msaSubmissionMethod));
0202         break;
0203     case Method::SENDMAIL:
0204     case Method::IMAP_SENDMAIL:
0205         m_msaSubmissionMethod = method;
0206         break;
0207     }
0208     emit submissionMethodChanged();
0209 
0210 }
0211 
0212 void Account::maybeShowPortWarning()
0213 {
0214     switch (m_msaSubmissionMethod) {
0215     case Method::SENDMAIL:
0216     case Method::IMAP_SENDMAIL:
0217         // this isn't a direct network connection -> ignore this
0218         return;
0219     case Method::SMTP:
0220     case Method::SMTP_STARTTLS:
0221     case Method::SSMTP:
0222         // we're connecting through the network -> let's check the port
0223         break;
0224     }
0225     QString portWarn;
0226     const int defPort = defaultPort(m_msaSubmissionMethod);
0227     if (m_port != defPort) {
0228         switch (m_msaSubmissionMethod) {
0229         case Method::SENDMAIL:
0230         case Method::IMAP_SENDMAIL:
0231             break;
0232         case Method::SMTP:
0233             portWarn = tr("This port is nonstandard. The default port for cleartext SMTP is %1.").arg(defPort);
0234             break;
0235         case Method::SMTP_STARTTLS:
0236             portWarn = tr("This port is nonstandard. The default port for SMTP submission secured via STARTTLS is %1.").arg(defPort);
0237             break;
0238         case Method::SSMTP:
0239             portWarn = tr("This port is nonstandard. The default port for SMTP over encrypted SSL/TLS connection is %1.").arg(defPort);
0240             break;
0241         }
0242     }
0243 
0244     emit showPortWarning(portWarn);
0245 }
0246 
0247 /** @short Persistently save the SMTP settings
0248 
0249 This method should be called by the UI when the user wishes to save current settings
0250 */
0251 void Account::saveSettings()
0252 {
0253     switch (m_msaSubmissionMethod) {
0254     case Method::SMTP:
0255     case Method::SMTP_STARTTLS:
0256         m_settings->setValue(Common::SettingsNames::msaMethodKey, Common::SettingsNames::methodSMTP);
0257         m_settings->setValue(Common::SettingsNames::smtpStartTlsKey,
0258                              m_msaSubmissionMethod == Method::SMTP_STARTTLS); // unconditionally
0259         m_settings->setValue(Common::SettingsNames::smtpAuthKey, m_authenticateEnabled);
0260         m_settings->setValue(Common::SettingsNames::smtpAuthReuseImapCredsKey, m_reuseImapAuth);
0261         m_settings->setValue(Common::SettingsNames::smtpUserKey, m_username);
0262         m_settings->setValue(Common::SettingsNames::smtpHostKey, m_server);
0263         m_settings->setValue(Common::SettingsNames::smtpPortKey, m_port);
0264         break;
0265     case Method::SSMTP:
0266         m_settings->setValue(Common::SettingsNames::msaMethodKey, Common::SettingsNames::methodSSMTP);
0267         m_settings->setValue(Common::SettingsNames::smtpAuthKey, m_authenticateEnabled);
0268         m_settings->setValue(Common::SettingsNames::smtpAuthReuseImapCredsKey, m_reuseImapAuth);
0269         m_settings->setValue(Common::SettingsNames::smtpUserKey, m_username);
0270         m_settings->setValue(Common::SettingsNames::smtpHostKey, m_server);
0271         m_settings->setValue(Common::SettingsNames::smtpPortKey, m_port);
0272         break;
0273     case Method::SENDMAIL:
0274         m_settings->setValue(Common::SettingsNames::msaMethodKey, Common::SettingsNames::methodSENDMAIL);
0275         m_settings->setValue(Common::SettingsNames::sendmailKey, m_pathToSendmail);
0276         break;
0277     case Method::IMAP_SENDMAIL:
0278         m_settings->setValue(Common::SettingsNames::msaMethodKey, Common::SettingsNames::methodImapSendmail);
0279         break;
0280     }
0281 
0282     m_settings->setValue(Common::SettingsNames::composerSaveToImapKey, m_saveToImap);
0283     if (m_saveToImap) {
0284         m_settings->setValue(Common::SettingsNames::composerImapSentKey, m_sentMailboxName);
0285         m_settings->setValue(Common::SettingsNames::smtpUseBurlKey, m_useBurl);
0286     } else {
0287         m_settings->setValue(Common::SettingsNames::smtpUseBurlKey, false);
0288     }
0289 
0290     emit settingsSaved();
0291 }
0292 
0293 /** @short Restore SMTP settings from file
0294 
0295 This method restores a users persistently stored settings. This should be called from the UI,
0296 usually when an action to cancel any changes that may have been made.
0297 */
0298 void Account::restoreSettings()
0299 {
0300     m_server = m_settings->value(Common::SettingsNames::smtpHostKey).toString();
0301     m_username = m_settings->value(Common::SettingsNames::smtpUserKey).toString();
0302     QString connMethod = m_settings->value(Common::SettingsNames::msaMethodKey,
0303                                            Common::SettingsNames::methodSMTP).toString();
0304     if (connMethod == Common::SettingsNames::methodSMTP) {
0305         m_msaSubmissionMethod = m_settings->value(Common::SettingsNames::smtpStartTlsKey).toBool() ?
0306                             Method::SMTP_STARTTLS : Method::SMTP;
0307     } else if (connMethod == Common::SettingsNames::methodSSMTP) {
0308         m_msaSubmissionMethod = Method::SSMTP;
0309     } else if (connMethod == Common::SettingsNames::methodSENDMAIL) {
0310         m_msaSubmissionMethod = Method::SENDMAIL;
0311     } else if (connMethod == Common::SettingsNames::methodImapSendmail) {
0312         m_msaSubmissionMethod = Method::IMAP_SENDMAIL;
0313     } else {
0314         // unknown submission type, lets default to SMTP
0315         m_msaSubmissionMethod = Method::SMTP;
0316     }
0317     m_port = m_settings->value(Common::SettingsNames::smtpPortKey, QVariant(0)).toInt();
0318     if (!m_port) {
0319         switch (m_msaSubmissionMethod) {
0320         case Method::SMTP:
0321         case Method::SMTP_STARTTLS:
0322         case Method::SSMTP:
0323             m_port = defaultPort(m_msaSubmissionMethod);
0324             break;
0325         case Method::SENDMAIL:
0326         case Method::IMAP_SENDMAIL:
0327             break;
0328         }
0329     }
0330     m_authenticateEnabled = m_settings->value(Common::SettingsNames::smtpAuthKey, false).toBool();
0331     m_reuseImapAuth = m_settings->value(Common::SettingsNames::smtpAuthReuseImapCredsKey, false).toBool();
0332     m_pathToSendmail = m_settings->value(Common::SettingsNames::sendmailKey,
0333                                          Common::SettingsNames::sendmailDefaultCmd).toString();
0334     m_saveToImap = m_settings->value(Common::SettingsNames::composerSaveToImapKey, true).toBool();
0335     m_sentMailboxName = m_settings->value(Common::SettingsNames::composerImapSentKey, QLatin1String("Sent")).toString();
0336     m_useBurl = m_settings->value(Common::SettingsNames::smtpUseBurlKey, false).toBool();
0337 
0338     // Be sure the GUI has time to react to the port warning status
0339     EMIT_LATER_NOARG(this, maybeShowPortWarning);
0340 }
0341 
0342 }