This commit is contained in:
Martin Rotter 2021-02-12 15:04:01 +01:00
parent ca364b9688
commit c6c84daaed
11 changed files with 73 additions and 47 deletions

View File

@ -10,6 +10,8 @@
#include "network-web/oauth2service.h"
#include "services/abstract/category.h"
#include "services/feedly/definitions.h"
#include "services/feedly/feedlynetwork.h"
#include "services/feedly/feedlyserviceroot.h"
#include "services/gmail/definitions.h"
#include "services/gmail/gmailfeed.h"
#include "services/gmail/gmailserviceroot.h"
@ -1848,7 +1850,7 @@ bool DatabaseQueries::createFeedlyAccount(const QSqlDatabase& db, const QString&
QSqlQuery q(db);
q.prepare("INSERT INTO FeedlyAccounts (id, username, developer_access_token, refresh_token, msg_limit) "
"VALUES (:id, :service, :username, :developer_access_token, :refresh_token, :msg_limit);");
"VALUES (:id, :username, :developer_access_token, :refresh_token, :msg_limit);");
q.bindValue(QSL(":id"), id_to_assign);
q.bindValue(QSL(":username"), username);
q.bindValue(QSL(":developer_access_token"), developer_access_token);
@ -2590,6 +2592,45 @@ QStringList DatabaseQueries::getAllRecipients(const QSqlDatabase& db, int accoun
return rec;
}
QList<ServiceRoot*> DatabaseQueries::getFeedlyAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery query(db);
QList<ServiceRoot*> roots;
if (query.exec("SELECT * FROM FeedlyAccounts;")) {
while (query.next()) {
auto* root = new FeedlyServiceRoot();
root->setId(query.value(0).toInt());
root->setAccountId(query.value(0).toInt());
root->network()->setUsername(query.value(1).toString());
root->network()->setDeveloperAccessToken(query.value(2).toString());
root->network()->oauth()->setRefreshToken(query.value(3).toString());
root->network()->setBatchSize(query.value(4).toInt());
root->updateTitle();
fillBaseAccountData(db, root);
roots.append(root);
}
if (ok != nullptr) {
*ok = true;
}
}
else {
qWarningNN << LOGSEC_GMAIL
<< "Getting list of activated accounts failed: '"
<< query.lastError().text()
<< "'.";
if (ok != nullptr) {
*ok = false;
}
}
return roots;
}
QList<ServiceRoot*> DatabaseQueries::getGmailAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery query(db);
QList<ServiceRoot*> roots;
@ -2640,27 +2681,6 @@ bool DatabaseQueries::deleteGmailAccount(const QSqlDatabase& db, int account_id)
return q.exec();
}
bool DatabaseQueries::storeNewGmailTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id) {
QSqlQuery query(db);
query.prepare("UPDATE GmailAccounts "
"SET refresh_token = :refresh_token "
"WHERE id = :id;");
query.bindValue(QSL(":refresh_token"), refresh_token);
query.bindValue(QSL(":id"), account_id);
if (query.exec()) {
return true;
}
else {
qWarningNN << LOGSEC_GMAIL
<< "Updating tokens in DB failed: '"
<< query.lastError().text()
<< "'.";
return false;
}
}
bool DatabaseQueries::deleteInoreaderAccount(const QSqlDatabase& db, int account_id) {
QSqlQuery q(db);
@ -2670,23 +2690,22 @@ bool DatabaseQueries::deleteInoreaderAccount(const QSqlDatabase& db, int account
return q.exec();
}
bool DatabaseQueries::storeNewInoreaderTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id) {
bool DatabaseQueries::storeNewOauthTokens(const QSqlDatabase& db, const QString& table_name,
const QString& refresh_token, int account_id) {
QSqlQuery query(db);
query.prepare("UPDATE InoreaderAccounts "
"SET refresh_token = :refresh_token "
"WHERE id = :id;");
query.prepare(QSL("UPDATE %1 "
"SET refresh_token = :refresh_token "
"WHERE id = :id;").arg(table_name));
query.bindValue(QSL(":refresh_token"), refresh_token);
query.bindValue(QSL(":id"), account_id);
if (query.exec()) {
qDebugNN << LOGSEC_DB << "Stored new refresh token into table" << QUOTE_W_SPACE_DOT(table_name);
return true;
}
else {
qWarningNN << LOGSEC_INOREADER
<< "Updating tokens in DB failed: '"
<< query.lastError().text()
<< "'.";
qWarningNN << LOGSEC_DB << "Updating tokens in DB failed:" << QUOTE_W_SPACE_DOT(query.lastError().text());
return false;
}
}

View File

@ -89,6 +89,8 @@ class DatabaseQueries {
bool* ok = nullptr);
// Common account methods.
static bool storeNewOauthTokens(const QSqlDatabase& db, const QString& table_name,
const QString& refresh_token, int account_id);
static void fillBaseAccountData(const QSqlDatabase& db, ServiceRoot* account, bool* ok = nullptr);
static int createBaseAccount(const QSqlDatabase& db, const QString& code, bool* ok = nullptr);
static void editBaseAccount(const QSqlDatabase& db, ServiceRoot* account, bool* ok = nullptr);
@ -151,6 +153,7 @@ class DatabaseQueries {
static void fillFeedData(T* feed, const QSqlRecord& sql_record);
// Feedly account.
static QList<ServiceRoot*> getFeedlyAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static bool createFeedlyAccount(const QSqlDatabase& db,
const QString& username,
const QString& developer_access_token,
@ -199,7 +202,6 @@ class DatabaseQueries {
// Gmail account.
static QStringList getAllRecipients(const QSqlDatabase& db, int account_id);
static bool deleteGmailAccount(const QSqlDatabase& db, int account_id);
static bool storeNewGmailTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id);
static QList<ServiceRoot*> getGmailAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static bool overwriteGmailAccount(const QSqlDatabase& db, const QString& username, const QString& app_id,
const QString& app_key, const QString& redirect_url, const QString& refresh_token,
@ -210,7 +212,6 @@ class DatabaseQueries {
// Inoreader account.
static bool deleteInoreaderAccount(const QSqlDatabase& db, int account_id);
static bool storeNewInoreaderTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id);
static QList<ServiceRoot*> getInoreaderAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static bool overwriteInoreaderAccount(const QSqlDatabase& db, const QString& username, const QString& app_id,
const QString& app_key, const QString& redirect_url, const QString& refresh_token,

View File

@ -2,6 +2,8 @@
#define FEEDLY_DEFINITIONS_H
#define FEEDLY_UNLIMITED_BATCH_SIZE -1
#define FEEDLY_MAX_BATCH_SIZE 999
#define FEEDLY_GENERATE_DAT "https://feedly.com/v3/auth/dev"
#define FEEDLY_API_REDIRECT_URI_PORT 8080

View File

@ -19,9 +19,7 @@ ServiceRoot* FeedlyEntryPoint::createNewRoot() const {
QList<ServiceRoot*> FeedlyEntryPoint::initializeSubtree() const {
QSqlDatabase database = qApp->database()->connection(QSL("FeedlyEntryPoint"));
return {};
//return DatabaseQueries::getGreaderAccounts(database);
return DatabaseQueries::getFeedlyAccounts(database);
}
QString FeedlyEntryPoint::name() const {

View File

@ -6,6 +6,7 @@
#include "miscellaneous/application.h"
#include "network-web/networkfactory.h"
#include "miscellaneous/databasequeries.h"
#include "network-web/webfactory.h"
#include "services/abstract/category.h"
#include "services/abstract/label.h"
@ -39,7 +40,7 @@ FeedlyNetwork::FeedlyNetwork(QObject* parent)
connect(m_oauth, &OAuth2Service::tokensRetrieveError, this, &FeedlyNetwork::onTokensError);
connect(m_oauth, &OAuth2Service::authFailed, this, &FeedlyNetwork::onAuthFailed);
connect(m_oauth, &OAuth2Service::tokensRetrieved, this, &FeedlyNetwork::ontokensRetrieved);
connect(m_oauth, &OAuth2Service::tokensRetrieved, this, &FeedlyNetwork::onTokensRetrieved);
#endif
}
@ -107,8 +108,7 @@ void FeedlyNetwork::onTokensError(const QString& error, const QString& error_des
QSystemTrayIcon::MessageIcon::Critical,
nullptr, false,
[this]() {
m_oauth->setAccessToken({});
m_oauth->setRefreshToken({});
m_oauth->logout(false);
m_oauth->login();
});
}
@ -119,19 +119,19 @@ void FeedlyNetwork::onAuthFailed() {
QSystemTrayIcon::MessageIcon::Critical,
nullptr, false,
[this]() {
m_oauth->logout(false);
m_oauth->login();
});
}
void FeedlyNetwork::ontokensRetrieved(const QString& access_token, const QString& refresh_token, int expires_in) {
void FeedlyNetwork::onTokensRetrieved(const QString& access_token, const QString& refresh_token, int expires_in) {
Q_UNUSED(expires_in)
Q_UNUSED(access_token)
if (m_service != nullptr && !refresh_token.isEmpty()) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
//DatabaseQueries::storeNewInoreaderTokens(database, refresh_token, m_service->accountId());
DatabaseQueries::storeNewOauthTokens(database, QSL("FeedlyAccounts"), refresh_token, m_service->accountId());
qApp->showGuiMessage(tr("Logged in successfully"),
tr("Your login to Feedly was authorized."),
QSystemTrayIcon::MessageIcon::Information);

View File

@ -42,7 +42,7 @@ class FeedlyNetwork : public QObject {
private slots:
void onTokensError(const QString& error, const QString& error_description);
void onAuthFailed();
void ontokensRetrieved(const QString& access_token, const QString& refresh_token, int expires_in);
void onTokensRetrieved(const QString& access_token, const QString& refresh_token, int expires_in);
#endif
private:

View File

@ -66,7 +66,7 @@ void FeedlyServiceRoot::start(bool freshly_activated) {
}
QString FeedlyServiceRoot::code() const {
return FeedlyServiceRoot().code();
return FeedlyEntryPoint().code();
}
void FeedlyServiceRoot::saveAllCachedData(bool ignore_errors) {

View File

@ -60,6 +60,10 @@ FeedlyAccountDetails::FeedlyAccountDetails(QWidget* parent) : QWidget(parent) {
connect(m_ui.m_txtDeveloperAccessToken->lineEdit(), &BaseLineEdit::textChanged,
this, &FeedlyAccountDetails::onDeveloperAccessTokenChanged);
m_ui.m_spinLimitMessages->setMinimum(FEEDLY_UNLIMITED_BATCH_SIZE);
m_ui.m_spinLimitMessages->setMaximum(FEEDLY_MAX_BATCH_SIZE);
m_ui.m_spinLimitMessages->setValue(FEEDLY_UNLIMITED_BATCH_SIZE);
setTabOrder(m_ui.m_txtUsername->lineEdit(), m_ui.m_btnGetToken);
setTabOrder(m_ui.m_btnGetToken, m_ui.m_txtDeveloperAccessToken->lineEdit());
setTabOrder(m_ui.m_txtDeveloperAccessToken->lineEdit(), m_ui.m_spinLimitMessages);

View File

@ -36,8 +36,10 @@ void FormEditFeedlyAccount::apply() {
m_details->m_oauth->logout();
m_details->m_oauth->deleteLater();
// Force live OAuth object to re-start it's
account<FeedlyServiceRoot>()->network()->oauth()->setRedirectUrl(account<FeedlyServiceRoot>()->network()->oauth()->redirectUrl());
// Force live OAuth object to re-start it's redirection handler.
account<FeedlyServiceRoot>()->network()->oauth()->setRedirectUrl(QString(OAUTH_REDIRECT_URI) +
QL1C(':') +
QString::number(FEEDLY_API_REDIRECT_URI_PORT));
}
#endif

View File

@ -126,8 +126,8 @@ void GmailNetworkFactory::initializeOauth() {
if (m_service != nullptr && !refresh_token.isEmpty()) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
DatabaseQueries::storeNewGmailTokens(database, refresh_token, m_service->accountId());
DatabaseQueries::storeNewOauthTokens(database, QSL("GmailAccounts"), refresh_token, m_service->accountId());
qApp->showGuiMessage(tr("Logged in successfully"),
tr("Your login to Gmail was authorized."),
QSystemTrayIcon::MessageIcon::Information);

View File

@ -61,8 +61,8 @@ void InoreaderNetworkFactory::initializeOauth() {
if (m_service != nullptr && !refresh_token.isEmpty()) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
DatabaseQueries::storeNewInoreaderTokens(database, refresh_token, m_service->accountId());
DatabaseQueries::storeNewOauthTokens(database, QSL("InoreaderAccounts"), refresh_token, m_service->accountId());
qApp->showGuiMessage(tr("Logged in successfully"),
tr("Your login to Inoreader was authorized."),
QSystemTrayIcon::MessageIcon::Information);