File indexing completed on 2024-04-21 04:55:25

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 "accountmanager.h"
0010 
0011 #include <QUrl>
0012 #include <QStandardPaths>
0013 #include <QApplication>
0014 
0015 #include <KConfig>
0016 #include <KConfigGroup>
0017 #include <KIO/DeleteJob>
0018 #include <KIO/StatJob>
0019 #include <KJobWidgets>
0020 #include <KLocalizedString>
0021 #include <KSharedConfig>
0022 #include <KWallet>
0023 
0024 #include "libchoqokdebug.h"
0025 #include "microblog.h"
0026 #include "passwordmanager.h"
0027 #include "pluginmanager.h"
0028 
0029 namespace Choqok
0030 {
0031 
0032 static QList<Account *> sortAccountsByPriority(QList<Account *> &accounts)
0033 {
0034     qCDebug(CHOQOK) << accounts.count();
0035     QList<Account *> result;
0036     for (Account *ac: accounts) {
0037         bool inserted = false;
0038         int i = 0;
0039         while (i < result.count()) {
0040             if (ac->priority() < result[i]->priority()) {
0041                 result.insert(i, ac);
0042                 inserted = true;
0043                 break;
0044             }
0045             ++i;
0046         }
0047         if (!inserted) {
0048             result.insert(i, ac);
0049         }
0050     }
0051     return result;
0052 }
0053 
0054 class AccountManager::Private
0055 {
0056 public:
0057     Private()
0058         : conf(nullptr)
0059     {}
0060     QList<Account *> accounts;
0061     KSharedConfig::Ptr conf;
0062     QString lastError;
0063 };
0064 
0065 AccountManager::AccountManager()
0066     : QObject(qApp), d(new Private)
0067 {
0068     qCDebug(CHOQOK);
0069     d->conf = KSharedConfig::openConfig();
0070 }
0071 
0072 AccountManager::~AccountManager()
0073 {
0074     qCDebug(CHOQOK);
0075     mSelf = nullptr;
0076     d->conf->sync();
0077     delete d;
0078 }
0079 
0080 AccountManager *AccountManager::mSelf = nullptr;
0081 
0082 AccountManager *AccountManager::self()
0083 {
0084     if (!mSelf) {
0085         mSelf = new AccountManager;
0086     }
0087     return mSelf;
0088 }
0089 
0090 const QList< Account * > &AccountManager::accounts() const
0091 {
0092     return d->accounts;
0093 }
0094 
0095 Account *AccountManager::findAccount(const QString &alias)
0096 {
0097     qCDebug(CHOQOK) << "Finding:" << alias;
0098     for (Account *ac: d->accounts) {
0099         if (ac->alias().compare(alias) == 0) {
0100             return ac;
0101         }
0102     }
0103 
0104     d->lastError = i18n("There is no account with alias %1.", alias);
0105     return nullptr;
0106 }
0107 
0108 bool AccountManager::removeAccount(const QString &alias)
0109 {
0110     qCDebug(CHOQOK) << "Removing" << alias;
0111     int count = d->accounts.count();
0112     d->conf->deleteGroup(QStringLiteral("Account_%1").arg(alias));
0113     d->conf->sync();
0114     for (int i = 0; i < count; ++i) {
0115         if (d->accounts[i]->alias() == alias) {
0116             Choqok::Account *a = d->accounts.takeAt(i);
0117             if (!a) {
0118                 return false;
0119             }
0120             QStringList names = a->timelineNames();
0121             while (!names.isEmpty()) {
0122                 const QString tmpFile = QStandardPaths::locate(QStandardPaths::DataLocation,
0123                                                                generatePostBackupFileName(a->alias(), names.takeFirst()));
0124                 qCDebug(CHOQOK) << "Will remove" << tmpFile;
0125                 const QUrl path = QUrl::fromLocalFile(tmpFile);
0126 
0127                 if (path.isValid()) {
0128                     KIO::StatJob *job = KIO::stat(path, KIO::StatJob::SourceSide, 1);
0129                     KJobWidgets::setWindow(job, UI::Global::mainWindow());
0130                     job->exec();
0131                     KIO::DeleteJob *delJob = KIO::del(path);
0132                     KJobWidgets::setWindow(delJob, UI::Global::mainWindow());
0133                     delJob->exec();
0134                 }
0135             }
0136             a->deleteLater();
0137             PasswordManager::self()->removePassword(alias);
0138             Q_EMIT accountRemoved(alias);
0139             return true;
0140         }
0141     }
0142     d->lastError = i18n("There is no account with alias %1.", alias);
0143     return false;
0144 }
0145 
0146 Account *AccountManager::registerAccount(Account *account)
0147 {
0148     qCDebug(CHOQOK) << "Adding:" << account->alias();
0149 
0150     if (!account || d->accounts.contains(account) || account->alias().isEmpty()) {
0151         return nullptr;
0152     }
0153 
0154     // If this account already exists, do nothing
0155     for (Choqok::Account *curracc: d->accounts) {
0156         if (account->alias() == curracc->alias()) {
0157             d->lastError = i18n("An account with this alias already exists: a unique alias has to be specified.");
0158             qCDebug(CHOQOK) << "An account with this alias already exists: a unique alias has to be specified.";
0159             return nullptr;
0160         }
0161     }
0162 
0163     d->accounts.append(account);
0164     d->accounts = sortAccountsByPriority(d->accounts);
0165 
0166     Q_EMIT accountAdded(account);
0167     return account;
0168 }
0169 
0170 void AccountManager::loadAllAccounts()
0171 {
0172     qCDebug(CHOQOK);
0173     for (Account *ac: d->accounts) {
0174         ac->deleteLater();
0175     }
0176     d->accounts.clear();
0177     const QStringList accountGroups = d->conf->groupList().filter(QRegExp(QLatin1String("^Account_")));
0178     qCDebug(CHOQOK) << accountGroups;
0179     for (const QString &grp: accountGroups) {
0180         qCDebug(CHOQOK) << grp;
0181         KConfigGroup cg(d->conf, grp);
0182 //         KConfigGroup pluginConfig( d->conf, QLatin1String("Plugins") );
0183 
0184         QString blog = cg.readEntry("MicroBlog", QString());
0185         Choqok::MicroBlog *mBlog = nullptr;
0186         if (!blog.isEmpty() && cg.readEntry("Enabled", true)) {
0187             mBlog = qobject_cast<MicroBlog *>(PluginManager::self()->loadPlugin(blog));
0188         }
0189         if (mBlog) {
0190             const QString alias = cg.readEntry("Alias", QString());
0191             if (alias.isEmpty()) {
0192                 continue;    ///Unknown alias
0193             }
0194             Account *acc = mBlog->createNewAccount(alias);
0195             if (acc) {
0196                 d->accounts.append(acc);
0197             }
0198         }
0199     }
0200     qCDebug(CHOQOK) << d->accounts.count() << "accounts loaded.";
0201     d->accounts = sortAccountsByPriority(d->accounts);
0202     Q_EMIT allAccountsLoaded();
0203 }
0204 
0205 QString AccountManager::generatePostBackupFileName(const QString &alias, const QString &name)
0206 {
0207     return QString(alias + QLatin1Char('_') + name + QLatin1String("_backuprc"));
0208 }
0209 
0210 QString AccountManager::lastError() const
0211 {
0212     return d->lastError;
0213 }
0214 
0215 }
0216 
0217 #include "moc_accountmanager.cpp"