File indexing completed on 2024-04-28 08:47:33
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "passwordmanager.h" 0010 0011 #include <QApplication> 0012 0013 #include <KMessageBox> 0014 #include <KWallet> 0015 0016 #include "choqokbehaviorsettings.h" 0017 #include "choqokuiglobal.h" 0018 #include "libchoqokdebug.h" 0019 0020 namespace Choqok 0021 { 0022 class PasswordManager::Private 0023 { 0024 public: 0025 Private() 0026 : wallet(nullptr), conf(nullptr), cfg(nullptr) 0027 {} 0028 0029 ~Private() 0030 { 0031 if (cfg) { 0032 cfg->sync(); 0033 } 0034 delete wallet; 0035 delete conf; 0036 delete cfg; 0037 } 0038 0039 bool openWallet() 0040 { 0041 qCDebug(CHOQOK); 0042 #pragma message("This segfaults on KF5") 0043 // if(kapp->sessionSaving()) 0044 // return false; 0045 if ((wallet && wallet->isOpen())) { 0046 return true; 0047 } 0048 WId id = 0; 0049 if (Choqok::UI::Global::mainWindow()) { 0050 id = Choqok::UI::Global::mainWindow()->winId(); 0051 } 0052 wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), id); 0053 if (wallet) { 0054 if (!wallet->setFolder(QCoreApplication::applicationName())) { 0055 wallet->createFolder(QCoreApplication::applicationName()); 0056 wallet->setFolder(QCoreApplication::applicationName()); 0057 } 0058 qCDebug(CHOQOK) << "Wallet successfully opened."; 0059 return true; 0060 } else if (!conf) { 0061 cfg = new KConfig(QLatin1String("choqok/secretsrc"), KConfig::NoGlobals, QStandardPaths::DataLocation); 0062 conf = new KConfigGroup(cfg, QLatin1String("Secrets")); 0063 KMessageBox::information(Choqok::UI::Global::mainWindow(), 0064 i18n("Cannot open KDE Wallet manager, your secrets will be stored as plain text. You can install KWallet to fix this."), QString(), QLatin1String("DontShowKWalletProblem"), 0065 KMessageBox::Dangerous); 0066 } 0067 return false; 0068 } 0069 void syncConfigs() 0070 { 0071 cfg->sync(); 0072 } 0073 KWallet::Wallet *wallet; 0074 KConfigGroup *conf; 0075 0076 private: 0077 KConfig *cfg; 0078 }; 0079 0080 PasswordManager::PasswordManager() 0081 : QObject(qApp), d(new Private) 0082 { 0083 qCDebug(CHOQOK); 0084 } 0085 0086 PasswordManager::~PasswordManager() 0087 { 0088 delete d; 0089 } 0090 0091 PasswordManager *PasswordManager::mSelf = nullptr; 0092 0093 PasswordManager *PasswordManager::self() 0094 { 0095 if (!mSelf) { 0096 mSelf = new PasswordManager; 0097 } 0098 return mSelf; 0099 } 0100 0101 QString PasswordManager::readPassword(const QString &alias) 0102 { 0103 if (d->openWallet()) { 0104 QString pass; 0105 if (d->wallet->readPassword(alias, pass) == 0) { 0106 qCDebug(CHOQOK) << "Read password from wallet"; 0107 return pass; 0108 } else { 0109 qCDebug(CHOQOK) << "Error on reading password from wallet"; 0110 return QString(); 0111 } 0112 } else { 0113 QByteArray pass = QByteArray::fromBase64(d->conf->readEntry(alias, QByteArray())); 0114 return QString::fromUtf8(pass.data(), pass.size()); 0115 } 0116 } 0117 0118 bool PasswordManager::writePassword(const QString &alias, const QString &password) 0119 { 0120 if (d->openWallet()) { 0121 if (d->wallet->writePassword(alias, password) == 0) { 0122 qCDebug(CHOQOK) << "Password wrote to wallet successfuly"; 0123 return true; 0124 } else { 0125 qCDebug(CHOQOK) << "Error on writing password to wallet"; 0126 return false; 0127 } 0128 } else { 0129 d->conf->writeEntry(alias, password.toUtf8().toBase64()); 0130 d->syncConfigs(); 0131 return true; 0132 } 0133 return false; 0134 } 0135 0136 bool PasswordManager::removePassword(const QString &alias) 0137 { 0138 if (d->openWallet()) { 0139 if (!d->wallet->removeEntry(alias)) { 0140 return true; 0141 } 0142 } else { 0143 d->conf->deleteEntry(alias); 0144 return true; 0145 } 0146 return false; 0147 } 0148 0149 } 0150 0151 #include "moc_passwordmanager.cpp"