clazy: fix QString(QLatin1String(...))
[trojita.git] / src / Plugins / QtKeyChain / QtKeyChainPassword.cpp
blobc4997c43ebeca46170a7113b7933ac93d06e8622
1 /* Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <QString>
24 #include <qt5keychain/keychain.h>
25 #include "QtKeyChainPassword.h"
27 namespace Plugins {
29 QtKeyChainPasswordJob::QtKeyChainPasswordJob(const QString &accountId, const QString &accountType, const QString &password,
30 enum Type type, QObject *parent) :
31 PasswordJob(parent), m_accountId(accountId), m_accountType(accountType), m_password(password), m_type(type), m_job(0)
35 void QtKeyChainPasswordJob::doStart()
37 switch (m_type) {
38 case Request:
39 m_job = new QKeychain::ReadPasswordJob(QStringLiteral("Trojita"), this);
40 static_cast<QKeychain::ReadPasswordJob *>(m_job)->setKey(m_accountId + QLatin1Char('-') + m_accountType);
41 break;
42 case Store:
43 m_job = new QKeychain::WritePasswordJob(QStringLiteral("Trojita"), this);
44 static_cast<QKeychain::WritePasswordJob *>(m_job)->setKey(m_accountId + QLatin1Char('-') + m_accountType);
45 static_cast<QKeychain::WritePasswordJob *>(m_job)->setTextData(m_password);
46 break;
47 case Delete:
48 m_job = new QKeychain::DeletePasswordJob(QStringLiteral("Trojita"), this);
49 static_cast<QKeychain::DeletePasswordJob *>(m_job)->setKey(m_accountId + QLatin1Char('-') + m_accountType);
50 break;
52 m_job->setAutoDelete(true);
53 connect(m_job, &QKeychain::Job::finished, this, &QtKeyChainPasswordJob::result);
54 m_job->start();
57 void QtKeyChainPasswordJob::result()
59 switch (m_job->error()) {
60 case QKeychain::NoError:
61 break;
62 case QKeychain::EntryNotFound:
63 emit error(PasswordJob::NoSuchPassword, QString());
64 return;
65 default:
66 emit error(PasswordJob::UnknownError, m_job->errorString());
67 return;
70 switch (m_type) {
71 case Request:
72 m_password = qobject_cast<QKeychain::ReadPasswordJob *>(m_job)->textData();
73 emit passwordAvailable(m_password);
74 return;
75 case Store:
76 emit passwordStored();
77 return;
78 case Delete:
79 emit passwordDeleted();
80 return;
84 void QtKeyChainPasswordJob::doStop()
86 if (m_job) {
87 disconnect(m_job, nullptr, this, nullptr);
88 m_job->deleteLater();
89 m_job = 0;
91 emit error(PasswordJob::Stopped, QString());
94 QtKeyChainPassword::QtKeyChainPassword(QObject *parent) : PasswordPlugin(parent)
98 PasswordPlugin::Features QtKeyChainPassword::features() const
100 return PasswordPlugin::FeatureEncryptedStorage;
103 PasswordJob *QtKeyChainPassword::requestPassword(const QString &accountId, const QString &accountType)
105 return new QtKeyChainPasswordJob(accountId, accountType, QString(), QtKeyChainPasswordJob::Request, this);
108 PasswordJob *QtKeyChainPassword::storePassword(const QString &accountId, const QString &accountType, const QString &password)
110 return new QtKeyChainPasswordJob(accountId, accountType, password, QtKeyChainPasswordJob::Store, this);
113 PasswordJob *QtKeyChainPassword::deletePassword(const QString &accountId, const QString &accountType)
115 return new QtKeyChainPasswordJob(accountId, accountType, QString(), QtKeyChainPasswordJob::Delete, this);
120 QString trojita_plugin_QtKeyChainPasswordPlugin::name() const
122 return QStringLiteral("qtkeychainpassword");
125 QString trojita_plugin_QtKeyChainPasswordPlugin::description() const
127 return tr("Secure storage via QtKeychain");
130 Plugins::PasswordPlugin *trojita_plugin_QtKeyChainPasswordPlugin::create(QObject *parent, QSettings *)
132 return new Plugins::QtKeyChainPassword(parent);
135 // vim: set et ts=4 sts=4 sw=4