File indexing completed on 2024-05-05 04:57:28
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 "twittereditaccount.h" 0010 0011 #include <QCheckBox> 0012 #include <QInputDialog> 0013 #include <QPushButton> 0014 0015 #include <KIO/AccessManager> 0016 #include <KMessageBox> 0017 0018 #include "accountmanager.h" 0019 #include "choqoktools.h" 0020 0021 #include "twitteraccount.h" 0022 #include "twitterdebug.h" 0023 #include "twittermicroblog.h" 0024 #include "twitterapioauth.h" 0025 0026 TwitterEditAccountWidget::TwitterEditAccountWidget(TwitterMicroBlog *microblog, 0027 TwitterAccount *account, QWidget *parent) 0028 : ChoqokEditAccountWidget(account, parent), mAccount(account) 0029 { 0030 setupUi(this); 0031 kcfg_basicAuth->hide(); 0032 connect(kcfg_authorize, &QPushButton::clicked, this, &TwitterEditAccountWidget::authorizeUser); 0033 if (mAccount) { 0034 kcfg_alias->setText(mAccount->alias()); 0035 setAuthenticated(!mAccount->oauthToken().isEmpty() && !mAccount->oauthTokenSecret().isEmpty()); 0036 } else { 0037 setAuthenticated(false); 0038 QString newAccountAlias = microblog->serviceName(); 0039 QString servName = newAccountAlias; 0040 int counter = 1; 0041 while (Choqok::AccountManager::self()->findAccount(newAccountAlias)) { 0042 newAccountAlias = QStringLiteral("%1%2").arg(servName).arg(counter); 0043 counter++; 0044 } 0045 setAccount(mAccount = new TwitterAccount(microblog, newAccountAlias)); 0046 kcfg_alias->setText(newAccountAlias); 0047 } 0048 loadTimelinesTableState(); 0049 kcfg_alias->setFocus(Qt::OtherFocusReason); 0050 } 0051 0052 TwitterEditAccountWidget::~TwitterEditAccountWidget() 0053 { 0054 0055 } 0056 0057 bool TwitterEditAccountWidget::validateData() 0058 { 0059 if (kcfg_alias->text().isEmpty() || !isAuthenticated) { 0060 return false; 0061 } else { 0062 return true; 0063 } 0064 } 0065 0066 Choqok::Account *TwitterEditAccountWidget::apply() 0067 { 0068 qCDebug(CHOQOK); 0069 mAccount->setAlias(kcfg_alias->text()); 0070 mAccount->setOauthToken(mAccount->oauthInterface()->token().toLatin1()); 0071 mAccount->setOauthTokenSecret(mAccount->oauthInterface()->tokenSecret().toLatin1()); 0072 saveTimelinesTableState(); 0073 mAccount->writeConfig(); 0074 return mAccount; 0075 } 0076 0077 void TwitterEditAccountWidget::authorizeUser() 0078 { 0079 qCDebug(CHOQOK); 0080 0081 mAccount->oauthInterface()->grant(); 0082 0083 connect(mAccount->oauthInterface(), &QAbstractOAuth::authorizeWithBrowser, &Choqok::openUrl); 0084 connect(mAccount->oauthInterface(), &QAbstractOAuth::statusChanged, this, &TwitterEditAccountWidget::getPinCode); 0085 } 0086 0087 void TwitterEditAccountWidget::getPinCode() 0088 { 0089 isAuthenticated = false; 0090 if (mAccount->oauthInterface()->status() == QAbstractOAuth::Status::TemporaryCredentialsReceived) { 0091 QString verifier = QInputDialog::getText(this, i18n("PIN"), 0092 i18n("Enter the PIN received from Twitter:")); 0093 if (verifier.isEmpty()) { 0094 return; 0095 } 0096 0097 mAccount->oauthInterface()->continueGrantWithVerifier(verifier); 0098 } else if (mAccount->oauthInterface()->status() == QAbstractOAuth::Status::Granted) { 0099 setAuthenticated(true); 0100 KMessageBox::information(this, i18n("Choqok is authorized successfully."), i18n("Authorized")); 0101 } else { 0102 KMessageBox::detailedError(this, i18n("Authorization Error"), i18n("OAuth authorization error")); 0103 } 0104 } 0105 0106 void TwitterEditAccountWidget::setAuthenticated(bool authenticated) 0107 { 0108 isAuthenticated = authenticated; 0109 if (authenticated) { 0110 kcfg_authorize->setIcon(QIcon::fromTheme(QLatin1String("object-unlocked"))); 0111 kcfg_authenticateLed->on(); 0112 kcfg_authenticateStatus->setText(i18n("Authenticated")); 0113 } else { 0114 kcfg_authorize->setIcon(QIcon::fromTheme(QLatin1String("object-locked"))); 0115 kcfg_authenticateLed->off(); 0116 kcfg_authenticateStatus->setText(i18n("Not Authenticated")); 0117 } 0118 } 0119 0120 void TwitterEditAccountWidget::loadTimelinesTableState() 0121 { 0122 for (const QString &timeline: mAccount->microblog()->timelineNames()) { 0123 int newRow = timelinesTable->rowCount(); 0124 timelinesTable->insertRow(newRow); 0125 timelinesTable->setItem(newRow, 0, new QTableWidgetItem(timeline)); 0126 0127 QCheckBox *enable = new QCheckBox(timelinesTable); 0128 enable->setChecked(mAccount->timelineNames().contains(timeline)); 0129 timelinesTable->setCellWidget(newRow, 1, enable); 0130 } 0131 } 0132 0133 void TwitterEditAccountWidget::saveTimelinesTableState() 0134 { 0135 QStringList timelines; 0136 int rowCount = timelinesTable->rowCount(); 0137 for (int i = 0; i < rowCount; ++i) { 0138 QCheckBox *enable = qobject_cast<QCheckBox *>(timelinesTable->cellWidget(i, 1)); 0139 if (enable && enable->isChecked()) { 0140 timelines << timelinesTable->item(i, 0)->text(); 0141 } 0142 } 0143 timelines.removeDuplicates(); 0144 mAccount->setTimelineNames(timelines); 0145 } 0146 0147 #include "moc_twittereditaccount.cpp"