File indexing completed on 2024-06-23 05:21:19

0001 /* Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include <QString>
0024 #include <QSettings>
0025 
0026 #include "ClearTextPassword.h"
0027 
0028 struct Settings
0029 {
0030 static QString imapPassKey, smtpPassKey;
0031 };
0032 QString Settings::imapPassKey = QStringLiteral("imap.auth.pass");
0033 QString Settings::smtpPassKey = QStringLiteral("msa.smtp.auth.pass");
0034 
0035 ClearTextPasswordJob::ClearTextPasswordJob(const QString &accountId, const QString &accountType, const QString &password,
0036     enum Type type, QObject *parent, QSettings *settings) :
0037     PasswordJob(parent), m_accountId(accountId), m_accountType(accountType), m_password(password), m_type(type), m_settings(settings)
0038 {
0039 }
0040 
0041 void ClearTextPasswordJob::doStart()
0042 {
0043     QVariant password;
0044     switch (m_type) {
0045     case Request:
0046         if (m_accountType == QLatin1String("imap")) {
0047             password = m_settings->value(Settings::imapPassKey);
0048         } else if (m_accountType == QLatin1String("smtp")) {
0049             password = m_settings->value(Settings::smtpPassKey);
0050         } else {
0051             emit error(PasswordJob::UnknownError, tr("This plugin only supports storing of IMAP and SMTP passwords"));
0052             break;
0053         }
0054         if (password.type() != QVariant::String || password.toString().isEmpty()) {
0055             emit error(PasswordJob::NoSuchPassword, QString());
0056         } else {
0057             emit passwordAvailable(password.toString());
0058         }
0059         break;
0060 
0061     case Store:
0062         if (m_accountType == QLatin1String("imap")) {
0063             m_settings->setValue(Settings::imapPassKey, m_password);
0064         } else if (m_accountType == QLatin1String("smtp")) {
0065             m_settings->setValue(Settings::smtpPassKey, m_password);
0066         } else {
0067             emit error(PasswordJob::UnknownError, tr("This plugin only supports storing of IMAP and SMTP passwords"));
0068             break;
0069         }
0070         emit passwordStored();
0071         break;
0072 
0073     case Delete:
0074         if (m_accountType == QLatin1String("imap")) {
0075             m_settings->remove(Settings::imapPassKey);
0076         } else if (m_accountType == QLatin1String("smtp")) {
0077             m_settings->remove(Settings::smtpPassKey);
0078         } else {
0079             emit error(PasswordJob::UnknownError, tr("This plugin only supports storing of IMAP and SMTP passwords"));
0080             break;
0081         }
0082         emit passwordDeleted();
0083         break;
0084     }
0085 }
0086 
0087 void ClearTextPasswordJob::doStop()
0088 {
0089     emit error(PasswordJob::Stopped, QString());
0090 }
0091 
0092 ClearTextPassword::ClearTextPassword(QObject *parent, QSettings *settings) : PasswordPlugin(parent), m_settings(settings)
0093 {
0094 }
0095 
0096 PasswordPlugin::Features ClearTextPassword::features() const
0097 {
0098     return PasswordPlugin::Feature();
0099 }
0100 
0101 PasswordJob *ClearTextPassword::requestPassword(const QString &accountId, const QString &accountType)
0102 {
0103     return new ClearTextPasswordJob(accountId, accountType, QString(), ClearTextPasswordJob::Request, this, m_settings);
0104 }
0105 
0106 PasswordJob *ClearTextPassword::storePassword(const QString &accountId, const QString &accountType, const QString &password)
0107 {
0108     return new ClearTextPasswordJob(accountId, accountType, password, ClearTextPasswordJob::Store, this, m_settings);
0109 }
0110 
0111 PasswordJob *ClearTextPassword::deletePassword(const QString &accountId, const QString &accountType)
0112 {
0113     return new ClearTextPasswordJob(accountId, accountType, QString(), ClearTextPasswordJob::Delete, this, m_settings);
0114 }
0115 
0116 QString trojita_plugin_ClearTextPasswordPlugin::name() const
0117 {
0118     return QStringLiteral("cleartextpassword");
0119 }
0120 
0121 QString trojita_plugin_ClearTextPasswordPlugin::description() const
0122 {
0123     return tr("Trojitá's settings");
0124 }
0125 
0126 Plugins::PasswordPlugin *trojita_plugin_ClearTextPasswordPlugin::create(QObject *parent, QSettings *settings)
0127 {
0128     Q_ASSERT(settings);
0129     return new ClearTextPassword(parent, settings);
0130 }
0131 
0132 // vim: set et ts=4 sts=4 sw=4