diff --git a/microblogs/pumpio/pumpioaccount.cpp b/microblogs/pumpio/pumpioaccount.cpp index c8d0bc34..0043c5de 100644 --- a/microblogs/pumpio/pumpioaccount.cpp +++ b/microblogs/pumpio/pumpioaccount.cpp @@ -1,90 +1,111 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #include "pumpioaccount.h" #include "pumpiomicroblog.h" +class PumpIOAccount::Private +{ +public: + QString clientID; + QString clientSecret; + QString host; + QString token; + QString tokenSecret; +}; + PumpIOAccount::PumpIOAccount(PumpIOMicroBlog* parent, const QString& alias): - Account(parent, alias) - , m_clientID() - , m_clientSecret() - , m_host() - , m_token() - , m_tokenSecret() + Account(parent, alias), d(new Private) { } PumpIOAccount::~PumpIOAccount() { + delete d; } QString PumpIOAccount::host() { - return m_host; + return d->host; } void PumpIOAccount::setHost(const QString& host) { - m_host = host; + d->host = host; } QString PumpIOAccount::clientID() { - return m_clientID; + return d->clientID; } void PumpIOAccount::setClientID(const QString& clientID) { - m_clientID = clientID; + d->clientID = clientID; } QString PumpIOAccount::clientSecret() { - return m_clientSecret; + return d->clientSecret; } void PumpIOAccount::setClientSecret(const QString& clientSecret) { - m_clientSecret = clientSecret; + d->clientSecret = clientSecret; } QString PumpIOAccount::token() { - return m_token; + return d->token; } void PumpIOAccount::setToken(const QString& token) { - m_token = token; + d->token = token; } QString PumpIOAccount::tokenSecret() { - return m_tokenSecret; + return d->tokenSecret; } void PumpIOAccount::setTokenSecret(const QString& tokenSecret) { - m_tokenSecret = tokenSecret; + d->tokenSecret = tokenSecret; +} + +QString PumpIOAccount::webfingerID() +{ + return username() + '@' + d->host; +} + +void PumpIOAccount::writeConfig() +{ + configGroup()->writeEntry("host", d->host); + configGroup()->writeEntry("clientID", d->clientID); + configGroup()->writeEntry("clientSecret", d->clientSecret); + configGroup()->writeEntry("token", d->token); + configGroup()->writeEntry("tokenSecret", d->tokenSecret); + Choqok::Account::writeConfig(); } diff --git a/microblogs/pumpio/pumpioaccount.h b/microblogs/pumpio/pumpioaccount.h index 15943273..6f7a3c3d 100644 --- a/microblogs/pumpio/pumpioaccount.h +++ b/microblogs/pumpio/pumpioaccount.h @@ -1,62 +1,63 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #ifndef PUMPIOACCOUNT_H #define PUMPIOACCOUNT_H #include "account.h" class PumpIOMicroBlog; class PumpIOAccount : public Choqok::Account { Q_OBJECT public: explicit PumpIOAccount(PumpIOMicroBlog* parent, const QString& alias); virtual ~PumpIOAccount(); QString host(); void setHost(const QString& host); QString clientID(); void setClientID(const QString& clientID); QString clientSecret(); void setClientSecret(const QString& clientSecret); QString token(); void setToken(const QString& token); QString tokenSecret(); void setTokenSecret(const QString& tokenSecret); + QString webfingerID(); + + virtual void writeConfig(); + private: - QString m_clientID; - QString m_clientSecret; - QString m_host; - QString m_token; - QString m_tokenSecret; + class Private; + Private *d; }; #endif // PUMPIOACCOUNT_H diff --git a/microblogs/pumpio/pumpioeditaccountwidget.cpp b/microblogs/pumpio/pumpioeditaccountwidget.cpp index 54ad7087..d72317be 100644 --- a/microblogs/pumpio/pumpioeditaccountwidget.cpp +++ b/microblogs/pumpio/pumpioeditaccountwidget.cpp @@ -1,153 +1,178 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #include "pumpioeditaccountwidget.h" #include #include #include #include #include #include #include #include #include #include "choqoktools.h" #include "accountmanager.h" #include "pumpioaccount.h" #include "pumpiomicroblog.h" PumpIOEditAccountWidget::PumpIOEditAccountWidget(PumpIOMicroBlog* microblog, PumpIOAccount* account, QWidget* parent): ChoqokEditAccountWidget(account, parent) , m_account(account) { setupUi(this); connect(kcfg_authorize, SIGNAL(clicked(bool)), SLOT(authorizeUser())); if (m_account) { kcfg_alias->setText(m_account->alias()); + kcfg_webfingerid->setText(m_account->webfingerID()); + isAuthenticated(); } else { QString newAccountAlias = microblog->serviceName(); const QString servName = newAccountAlias; int counter = 1; while (Choqok::AccountManager::self()->findAccount(newAccountAlias)) { newAccountAlias = QString("%1%2").arg(servName).arg(counter); counter++; } m_account = new PumpIOAccount(microblog, newAccountAlias); setAccount(m_account); kcfg_alias->setText(newAccountAlias); } } PumpIOEditAccountWidget::~PumpIOEditAccountWidget() { } Choqok::Account* PumpIOEditAccountWidget::apply() { m_account->setAlias(kcfg_alias->text()); m_account->setUsername(kcfg_webfingerid->text().split('@')[0]); m_account->writeConfig(); return m_account; } void PumpIOEditAccountWidget::authorizeUser() { m_qoauth = new QOAuth::Interface(new KIO::Integration::AccessManager(this), this); if (m_account->clientID().isEmpty() || m_account->clientSecret().isEmpty()) { registerClient(); } m_qoauth->setConsumerKey(m_account->clientID().toLocal8Bit()); m_qoauth->setConsumerSecret(m_account->clientSecret().toLocal8Bit()); QOAuth::ParamMap oAuthParams; oAuthParams.insert("oauth_callback", "oob"); QOAuth::ParamMap oAuthRequest = m_qoauth->requestToken("https://" + m_account->host() + "/oauth/request_token", QOAuth::GET, QOAuth::HMAC_SHA1, oAuthParams); if (m_qoauth->error() == QOAuth::NoError) { - m_account->setToken(oAuthRequest.value(QOAuth::tokenParameterName())); - m_account->setTokenSecret(oAuthRequest.value(QOAuth::tokenSecretParameterName())); + const QString token = oAuthRequest.value(QOAuth::tokenParameterName()); + const QString tokenSecret = oAuthRequest.value(QOAuth::tokenSecretParameterName()); KUrl oAuthAuthorizeURL("https://" + m_account->host() + "/oauth/authorize"); - oAuthAuthorizeURL.addQueryItem("oauth_token", m_account->token()); + oAuthAuthorizeURL.addQueryItem("oauth_token", token); Choqok::openUrl(oAuthAuthorizeURL); QString verifier = KInputDialog::getText( i18n("PIN"), i18n("Enter the verifier code received from Pump.io:")); QOAuth::ParamMap oAuthVerifierParams; oAuthVerifierParams.insert("oauth_verifier", verifier.toUtf8()); QOAuth::ParamMap oAuthVerifierRequest = m_qoauth->accessToken("https://" + m_account->host() + "/oauth/access_token", - QOAuth::POST, m_account->token().toLocal8Bit(), - m_account->tokenSecret().toLocal8Bit(), + QOAuth::POST, token.toLocal8Bit(), + tokenSecret.toLocal8Bit(), QOAuth::HMAC_SHA1, oAuthVerifierParams); if (m_qoauth->error() == QOAuth::NoError) { m_account->setToken(oAuthVerifierRequest.value(QOAuth::tokenParameterName())); m_account->setTokenSecret(oAuthVerifierRequest.value(QOAuth::tokenSecretParameterName())); - KMessageBox::information(this, i18n("Choqok is authorized successfully."), - i18n("Authorized")); + if (isAuthenticated()) { + KMessageBox::information(this, i18n("Choqok is authorized successfully."), + i18n("Authorized")); + } } else { kDebug() << "QOAuth error: " + Choqok::qoauthErrorText(m_qoauth->error()); } } else { kDebug() << "QOAuth error: " + Choqok::qoauthErrorText(m_qoauth->error()); } } void PumpIOEditAccountWidget::registerClient() { m_account->setHost(kcfg_webfingerid->text().split('@')[1]); KUrl url("https://" + m_account->host() + "/api/client/register"); QByteArray data("{" " \"type\": \"client_associate\", " " \"application_type\": \"native\", " " \"application_name\": \"Choqok\" " "}"); KIO::StoredTransferJob *job = KIO::storedHttpPost(data, url, KIO::HideProgressInfo); if ( !job ) { kDebug() << "Cannot create an http POST request!"; return; } job->addMetaData("content-type", "Content-Type: application/json"); QEventLoop loop; connect(job, SIGNAL(result(KJob*)), &loop, SLOT(quit())); job->start(); loop.exec(); if (job->error()) { kDebug() << "An error occurred in Job"; return; } else { KIO::StoredTransferJob *stj = qobject_cast(job); QJson::Parser parser; QVariantMap result = parser.parse(stj->data()).toMap(); m_account->setClientID(result.value("client_id").toString()); m_account->setClientSecret(result.value("client_secret").toString()); } } + +bool PumpIOEditAccountWidget::validateData() +{ + if (kcfg_webfingerid->text().isEmpty() || !kcfg_webfingerid->text().contains('@')) { + return false; + } else { + return true; + } +} + +bool PumpIOEditAccountWidget::isAuthenticated() +{ + if (m_account->token().isEmpty() || m_account->tokenSecret().isEmpty()) { + return false; + } else { + kcfg_authorize->setIcon(KIcon("object-unlocked")); + kcfg_authenticateLed->setState(KLed::On); + kcfg_authenticateStatus->setText(i18n("Authenticated")); + return true; + } +} diff --git a/microblogs/pumpio/pumpioeditaccountwidget.h b/microblogs/pumpio/pumpioeditaccountwidget.h index 09333c6e..be8b7a80 100644 --- a/microblogs/pumpio/pumpioeditaccountwidget.h +++ b/microblogs/pumpio/pumpioeditaccountwidget.h @@ -1,57 +1,59 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #ifndef PUMPIOEDITACCOUNTWIDGET_H #define PUMPIOEDITACCOUNTWIDGET_H #include "editaccountwidget.h" #include #include "ui_pumpioeditaccountwidget.h" class PumpIOAccount; class PumpIOMicroBlog; class KJob; class PumpIOEditAccountWidget : public ChoqokEditAccountWidget, Ui::PumpIOEditAccountWidget { Q_OBJECT public: explicit PumpIOEditAccountWidget(PumpIOMicroBlog* microblog, PumpIOAccount* account, QWidget* parent); virtual ~PumpIOEditAccountWidget(); virtual Choqok::Account* apply(); + virtual bool validateData(); private Q_SLOTS: void authorizeUser(); private: void registerClient(); + bool isAuthenticated(); PumpIOAccount* m_account; QOAuth::Interface *m_qoauth; }; #endif // PUMPIOEDITACCOUNTWIDGET_H diff --git a/microblogs/pumpio/pumpioeditaccountwidget.ui b/microblogs/pumpio/pumpioeditaccountwidget.ui index b5cad6af..2b5291d0 100644 --- a/microblogs/pumpio/pumpioeditaccountwidget.ui +++ b/microblogs/pumpio/pumpioeditaccountwidget.ui @@ -1,197 +1,206 @@ PumpIOEditAccountWidget 0 0 387 347 0 PumpIO Account &Alias: Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter kcfg_alias The account alias The alias is the name you want to give to your account. It should be unique. You can have several connections to the same service so the alias lets you give them names. 0 0 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'DejaVu Sans'; font-size:9pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt; font-weight:600;">Note:</span><span style=" font-size:8pt;"> The alias must be unique.</span></p></body></html> Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop true Credentials QFormLayout::ExpandingFieldsGrow Webfinger ID: Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter OAuth Authentication 0 0 Click the below button; if everything goes well, you will pointed to Pump.io website to allow access to Choqok. true + + true + 0 0 + + KLed::Off + - + Not Authenticated Verify Credentials &Authenticate with Pump.io service + + + Qt::Vertical 20 40 KLineEdit QLineEdit
klineedit.h
KPushButton QPushButton
kpushbutton.h
KLed QWidget
kled.h
KTabWidget QTabWidget
ktabwidget.h
1
kcfg_alias kcfg_webfingerid kcfg_authorize tabwidget
diff --git a/microblogs/pumpio/pumpiomicroblog.cpp b/microblogs/pumpio/pumpiomicroblog.cpp index 8fc85824..ff88f46c 100644 --- a/microblogs/pumpio/pumpiomicroblog.cpp +++ b/microblogs/pumpio/pumpiomicroblog.cpp @@ -1,54 +1,66 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #include "pumpiomicroblog.h" #include +#include "accountmanager.h" #include "pumpioaccount.h" #include "pumpioeditaccountwidget.h" K_PLUGIN_FACTORY( MyPluginFactory, registerPlugin < PumpIOMicroBlog > (); ) K_EXPORT_PLUGIN( MyPluginFactory( "choqok_pumpio" ) ) PumpIOMicroBlog::PumpIOMicroBlog(QObject* parent, const QVariantList& args): MicroBlog(MyPluginFactory::componentData(), parent) { setServiceName("Pump.io"); setServiceHomepageUrl("http://pump.io"); } PumpIOMicroBlog::~PumpIOMicroBlog() { } ChoqokEditAccountWidget* PumpIOMicroBlog::createEditAccountWidget(Choqok::Account* account, QWidget* parent) { PumpIOAccount *acc = qobject_cast(account); if (acc || !account) { return new PumpIOEditAccountWidget(this, acc, parent); } else { kDebug() << "Account passed here was not a valid PumpIOAccount!"; - return 0L; + return 0; + } +} + +Choqok::Account* PumpIOMicroBlog::createNewAccount(const QString& alias) +{ + PumpIOAccount *acc = qobject_cast( Choqok::AccountManager::self()->findAccount(alias) ); + if (!acc) { + return new PumpIOAccount(this, alias); + } else { + kDebug() << "Cannot create a new PumpIOAccount!"; + return 0; } } diff --git a/microblogs/pumpio/pumpiomicroblog.h b/microblogs/pumpio/pumpiomicroblog.h index 4ff63de1..5d40316f 100644 --- a/microblogs/pumpio/pumpiomicroblog.h +++ b/microblogs/pumpio/pumpiomicroblog.h @@ -1,39 +1,40 @@ /* This file is part of Choqok, the KDE micro-blogging client Copyright (C) 2013 Andrea Scarpino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see http://www.gnu.org/licenses/ */ #ifndef PUMPIOMICROBLOG_H #define PUMPIOMICROBLOG_H #include "microblog.h" class PumpIOMicroBlog : public Choqok::MicroBlog { Q_OBJECT public: explicit PumpIOMicroBlog(QObject* parent, const QVariantList& args); virtual ~PumpIOMicroBlog(); virtual ChoqokEditAccountWidget* createEditAccountWidget(Choqok::Account* account, QWidget* parent); + virtual Choqok::Account* createNewAccount(const QString& alias); }; #endif // PUMPIOMICROBLOG_H \ No newline at end of file