File indexing completed on 2024-04-14 04:50:21

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 "account.h"
0010 
0011 #include <KSharedConfig>
0012 
0013 #include "libchoqokdebug.h"
0014 #include "microblog.h"
0015 #include "passwordmanager.h"
0016 
0017 namespace Choqok
0018 {
0019 
0020 class Account::Private
0021 {
0022 public:
0023     Private(Choqok::MicroBlog *parent, const QString &mAlias)
0024         : alias(mAlias), blog(parent)
0025     {
0026         configGroup = new KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("Account_%1").arg(alias));
0027         username = configGroup->readEntry("Username", QString());
0028         priority = configGroup->readEntry("Priority", static_cast<uint>(0));
0029         readonly = configGroup->readEntry("ReadOnly", false);
0030         showInQuickPost = configGroup->readEntry("ShowInQuickPost", true);
0031         enable = configGroup->readEntry("Enable", true);
0032         postCharLimit = configGroup->readEntry("PostCharLimit", static_cast<uint>(140));
0033         password = PasswordManager::self()->readPassword(alias);
0034     }
0035     QString username;
0036     QString password;
0037     QString alias;
0038     MicroBlog *blog;
0039     KConfigGroup *configGroup;
0040     uint priority;
0041     bool readonly;
0042     bool enable;
0043     bool showInQuickPost;
0044     uint postCharLimit;
0045 };
0046 
0047 Account::Account(Choqok::MicroBlog *parent, const QString &alias)
0048     : QObject(parent), d(new Private(parent, alias))
0049 {
0050     qCDebug(CHOQOK);
0051 }
0052 
0053 Account::~Account()
0054 {
0055     qCDebug(CHOQOK) << alias();
0056 //     writeConfig();
0057     delete d->configGroup;
0058 }
0059 
0060 void Account::writeConfig()
0061 {
0062     d->configGroup->writeEntry("Alias", d->alias);
0063     d->configGroup->writeEntry("Username", d->username);
0064     d->configGroup->writeEntry("Priority", d->priority);
0065     d->configGroup->writeEntry("ReadOnly", d->readonly);
0066     d->configGroup->writeEntry("Enable", d->enable);
0067     d->configGroup->writeEntry("ShowInQuickPost", d->showInQuickPost);
0068     d->configGroup->writeEntry("MicroBlog", microblog()->pluginId());
0069     d->configGroup->writeEntry("PostCharLimit", d->postCharLimit);
0070     if (!password().isEmpty()) {
0071         PasswordManager::self()->writePassword(d->alias, password());
0072     }
0073     d->configGroup->sync();
0074     Q_EMIT modified(this);
0075 }
0076 
0077 QString Account::username() const
0078 {
0079     return d->username;
0080 }
0081 
0082 void Account::setUsername(const QString &name)
0083 {
0084     d->username = name;
0085 }
0086 
0087 QString Account::password() const
0088 {
0089     return d->password;
0090 }
0091 
0092 void Account::setPassword(const QString &pass)
0093 {
0094     d->password = pass;
0095 }
0096 
0097 QString Account::alias() const
0098 {
0099     return d->alias;
0100 }
0101 
0102 void Account::setAlias(const QString &alias)
0103 {
0104     d->alias = alias;
0105     d->configGroup->deleteGroup();
0106     delete d->configGroup;
0107     d->configGroup = new KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("Account_%1").arg(d->alias));
0108     writeConfig();
0109 }
0110 
0111 bool Account::isReadOnly() const
0112 {
0113     return d->readonly;
0114 }
0115 void Account::setReadOnly(bool readonly /*= true*/)
0116 {
0117     d->readonly = readonly;
0118 }
0119 
0120 MicroBlog *Account::microblog() const
0121 {
0122     return d->blog;
0123 }
0124 
0125 void Account::setPriority(uint priority)
0126 {
0127     d->priority = priority;
0128 //     d->configGroup->writeEntry( "Priority", d->priority );
0129 }
0130 
0131 uint Account::priority() const
0132 {
0133     return d->priority;
0134 }
0135 
0136 bool Account::isEnabled() const
0137 {
0138     return d->enable;
0139 }
0140 
0141 void Account::setEnabled(bool enabled)
0142 {
0143     d->enable = enabled;
0144     Q_EMIT status(this, enabled);
0145 }
0146 
0147 uint Account::postCharLimit() const
0148 {
0149     return d->postCharLimit;
0150 }
0151 
0152 void Account::setPostCharLimit(const uint limit)
0153 {
0154     d->postCharLimit = limit;
0155 }
0156 
0157 bool Account::showInQuickPost() const
0158 {
0159     return d->showInQuickPost;
0160 }
0161 
0162 void Account::setShowInQuickPost(bool show)
0163 {
0164     d->showInQuickPost = show;
0165 }
0166 
0167 KConfigGroup *Account::configGroup() const
0168 {
0169     return d->configGroup;
0170 }
0171 
0172 QStringList Account::timelineNames() const
0173 {
0174     return d->blog->timelineNames();
0175 }
0176 
0177 }
0178 
0179 #include "moc_account.cpp"