File indexing completed on 2024-04-28 04:55:44

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2017 Andrea Scarpino <scarpino@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "mastodonaccount.h"
0010 
0011 #include <KIO/AccessManager>
0012 
0013 #include "passwordmanager.h"
0014 
0015 #include "mastodonmicroblog.h"
0016 
0017 class MastodonAccount::Private
0018 {
0019 public:
0020     QString consumerKey;
0021     QString consumerSecret;
0022     QString host;
0023     uint id;
0024     QString tokenSecret;
0025     QStringList followers;
0026     QStringList following;
0027     QVariantList lists;
0028     MastodonOAuth *oAuth;
0029     QStringList timelineNames;
0030 };
0031 
0032 MastodonAccount::MastodonAccount(MastodonMicroBlog *parent, const QString &alias):
0033     Account(parent, alias), d(new Private)
0034 {
0035     d->host = configGroup()->readEntry("Host", QString());
0036     d->id = configGroup()->readEntry("Id", uint());
0037     d->followers = configGroup()->readEntry("Followers", QStringList());
0038     d->following = configGroup()->readEntry("Following", QStringList());
0039     d->lists = configGroup()->readEntry("Lists", QVariantList());
0040     d->tokenSecret = Choqok::PasswordManager::self()->readPassword(QStringLiteral("%1_tokenSecret").arg(alias));
0041     d->consumerKey = configGroup()->readEntry("ConsumerKey", QString());
0042     d->consumerSecret = Choqok::PasswordManager::self()->readPassword(QStringLiteral("%1_consumerSecret").arg(alias));
0043     d->oAuth = new MastodonOAuth(this);
0044     d->oAuth->setToken(d->tokenSecret);
0045 
0046     setPostCharLimit(500);
0047 
0048     parent->fetchFollowers(this, false);
0049     parent->fetchFollowing(this, false);
0050 }
0051 
0052 MastodonAccount::~MastodonAccount()
0053 {
0054     d->oAuth->deleteLater();
0055     delete d;
0056 }
0057 
0058 void MastodonAccount::writeConfig()
0059 {
0060     configGroup()->writeEntry("Host", d->host);
0061     configGroup()->writeEntry("Id", d->id);
0062     configGroup()->writeEntry("ConsumerKey", d->consumerKey);
0063     configGroup()->writeEntry("Followers", d->followers);
0064     configGroup()->writeEntry("Following", d->following);
0065     configGroup()->writeEntry("Lists", d->lists);
0066 
0067     Choqok::PasswordManager::self()->writePassword(QStringLiteral("%1_consumerSecret").arg(alias()),
0068             d->consumerSecret);
0069     Choqok::PasswordManager::self()->writePassword(QStringLiteral("%1_tokenSecret").arg(alias()),
0070             d->tokenSecret);
0071     Choqok::Account::writeConfig();
0072 }
0073 
0074 QString MastodonAccount::host()
0075 {
0076     return d->host;
0077 }
0078 
0079 void MastodonAccount::setHost(const QString &host)
0080 {
0081     d->host = host;
0082 }
0083 
0084 uint MastodonAccount::id()
0085 {
0086     return d->id;
0087 }
0088 
0089 void MastodonAccount::setId(const uint id)
0090 {
0091     d->id = id;
0092 }
0093 
0094 QString MastodonAccount::consumerKey()
0095 {
0096     return d->consumerKey;
0097 }
0098 
0099 void MastodonAccount::setConsumerKey(const QString &consumerKey)
0100 {
0101     d->consumerKey = consumerKey;
0102 }
0103 
0104 QString MastodonAccount::consumerSecret()
0105 {
0106     return d->consumerSecret;
0107 }
0108 
0109 void MastodonAccount::setConsumerSecret(const QString &consumerSecret)
0110 {
0111     d->consumerSecret = consumerSecret;
0112 }
0113 
0114 QString MastodonAccount::tokenSecret()
0115 {
0116     return d->tokenSecret;
0117 }
0118 
0119 void MastodonAccount::setTokenSecret(const QString &tokenSecret)
0120 {
0121     d->tokenSecret = tokenSecret;
0122 }
0123 
0124 MastodonOAuth *MastodonAccount::oAuth()
0125 {
0126     return d->oAuth;
0127 }
0128 
0129 QStringList MastodonAccount::followers() {
0130     return d->followers;
0131 }
0132 
0133 void MastodonAccount::setFollowers(const QStringList &followers) {
0134     d->followers = followers;
0135     writeConfig();
0136 }
0137 
0138 QStringList MastodonAccount::following() {
0139     return d->following;
0140 }
0141 
0142 void MastodonAccount::setFollowing(const QStringList &following) {
0143     d->following = following;
0144     writeConfig();
0145 }
0146 
0147 QVariantList MastodonAccount::lists() {
0148     return d->lists;
0149 }
0150 
0151 void MastodonAccount::setLists(const QVariantList &lists) {
0152     d->lists = lists;
0153     writeConfig();
0154 }
0155 
0156 #include "moc_mastodonaccount.cpp"