From 6dde6928f6f40153713b1b6afec0d2c78df1fd03 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 22 Sep 2017 11:23:19 +0200 Subject: [PATCH] Added persistent saving of inoreader accounts. --- resources/sql/db_init_mysql.sql | 10 ++++++++++ resources/sql/db_init_sqlite.sql | 10 ++++++++++ resources/sql/db_update_mysql_9_10.sql | 11 ++++++++++ resources/sql/db_update_sqlite_9_10.sql | 11 ++++++++++ src/definitions/definitions.h | 2 +- src/miscellaneous/databasequeries.cpp | 4 ++-- src/services/inoreader/definitions.h | 20 +++++++++---------- .../gui/formeditinoreaderaccount.cpp | 15 +++++++++++--- .../inoreader/gui/formeditinoreaderaccount.ui | 3 +++ .../inoreader/inoreaderserviceroot.cpp | 10 +++++++++- src/services/inoreader/inoreaderserviceroot.h | 2 +- .../network/inoreadernetworkfactory.cpp | 8 ++++++++ .../network/inoreadernetworkfactory.h | 2 ++ 13 files changed, 90 insertions(+), 18 deletions(-) create mode 100755 resources/sql/db_update_mysql_9_10.sql create mode 100755 resources/sql/db_update_sqlite_9_10.sql diff --git a/resources/sql/db_init_mysql.sql b/resources/sql/db_init_mysql.sql index 48e81ac81..b8c688c4e 100644 --- a/resources/sql/db_init_mysql.sql +++ b/resources/sql/db_init_mysql.sql @@ -42,6 +42,16 @@ CREATE TABLE IF NOT EXISTS OwnCloudAccounts ( FOREIGN KEY (id) REFERENCES Accounts (id) ); +-- ! +CREATE TABLE IF NOT EXISTS InoreaderAccounts ( + id INTEGER, + username TEXT NOT NULL, + access_token TEXT, + refresh_token TEXT, + msg_limit INTEGER NOT NULL DEFAULT -1 CHECK (msg_limit >= -1), + + FOREIGN KEY (id) REFERENCES Accounts (id) +); DROP TABLE IF EXISTS Categories; -- ! CREATE TABLE IF NOT EXISTS Categories ( diff --git a/resources/sql/db_init_sqlite.sql b/resources/sql/db_init_sqlite.sql index 6bfdfc404..ea0bd781d 100644 --- a/resources/sql/db_init_sqlite.sql +++ b/resources/sql/db_init_sqlite.sql @@ -37,6 +37,16 @@ CREATE TABLE IF NOT EXISTS OwnCloudAccounts ( FOREIGN KEY (id) REFERENCES Accounts (id) ); -- ! +CREATE TABLE IF NOT EXISTS InoreaderAccounts ( + id INTEGER, + username TEXT NOT NULL, + access_token TEXT, + refresh_token TEXT, + msg_limit INTEGER NOT NULL DEFAULT -1 CHECK (msg_limit >= -1), + + FOREIGN KEY (id) REFERENCES Accounts (id) +); +-- ! DROP TABLE IF EXISTS Categories; -- ! CREATE TABLE IF NOT EXISTS Categories ( diff --git a/resources/sql/db_update_mysql_9_10.sql b/resources/sql/db_update_mysql_9_10.sql new file mode 100755 index 000000000..f781743b5 --- /dev/null +++ b/resources/sql/db_update_mysql_9_10.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS InoreaderAccounts ( + id INTEGER, + username TEXT NOT NULL, + access_token TEXT, + refresh_token TEXT, + msg_limit INTEGER NOT NULL DEFAULT -1 CHECK (msg_limit >= -1), + + FOREIGN KEY (id) REFERENCES Accounts (id) +); +-- ! +UPDATE Information SET inf_value = '10' WHERE inf_key = 'schema_version'; \ No newline at end of file diff --git a/resources/sql/db_update_sqlite_9_10.sql b/resources/sql/db_update_sqlite_9_10.sql new file mode 100755 index 000000000..da852c8c2 --- /dev/null +++ b/resources/sql/db_update_sqlite_9_10.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS InoreaderAccounts ( + id INTEGER, + username TEXT NOT NULL, + access_token TEXT, + refresh_token TEXT, + msg_limit INTEGER NOT NULL DEFAULT -1 CHECK (msg_limit >= -1), + + FOREIGN KEY (id) REFERENCES Accounts (id) +); +-- ! +UPDATE Information SET inf_value = '10' WHERE inf_key = 'schema_version'; \ No newline at end of file diff --git a/src/definitions/definitions.h b/src/definitions/definitions.h index f44f0a896..25da22181 100755 --- a/src/definitions/definitions.h +++ b/src/definitions/definitions.h @@ -124,7 +124,7 @@ #define APP_DB_SQLITE_FILE "database.db" // Keep this in sync with schema versions declared in SQL initialization code. -#define APP_DB_SCHEMA_VERSION "9" +#define APP_DB_SCHEMA_VERSION "10" #define APP_DB_UPDATE_FILE_PATTERN "db_update_%1_%2_%3.sql" #define APP_DB_COMMENT_SPLIT "-- !\n" #define APP_DB_NAME_PLACEHOLDER "##" diff --git a/src/miscellaneous/databasequeries.cpp b/src/miscellaneous/databasequeries.cpp index 7d30fcb2e..428a12ad3 100755 --- a/src/miscellaneous/databasequeries.cpp +++ b/src/miscellaneous/databasequeries.cpp @@ -1491,7 +1491,7 @@ bool DatabaseQueries::overwriteInoreaderAccount(QSqlDatabase db, const QString& query.bindValue(QSL(":access_token"), access_token); query.bindValue(QSL(":refresh_token"), refresh_token); query.bindValue(QSL(":id"), account_id); - query.bindValue(QSL(":msg_limit"), batch_size); + query.bindValue(QSL(":msg_limit"), batch_size <= 0 ? UNLIMITED_BATCH_SIZE : batch_size); if (query.exec()) { return true; @@ -1512,7 +1512,7 @@ bool DatabaseQueries::createInoreaderAccount(QSqlDatabase db, int id_to_assign, q.bindValue(QSL(":username"), username); q.bindValue(QSL(":access_token"), access_token); q.bindValue(QSL(":refresh_token"), refresh_token); - q.bindValue(QSL(":msg_limit"), batch_size); + q.bindValue(QSL(":msg_limit"), batch_size <= 0 ? UNLIMITED_BATCH_SIZE : batch_size); if (q.exec()) { return true; diff --git a/src/services/inoreader/definitions.h b/src/services/inoreader/definitions.h index 059aaf86a..5fbc92718 100755 --- a/src/services/inoreader/definitions.h +++ b/src/services/inoreader/definitions.h @@ -19,15 +19,15 @@ #ifndef INOREADER_DEFINITIONS_H #define INOREADER_DEFINITIONS_H -#define INOREADER_OAUTH_PORT 12885 -#define INOREADER_OAUTH_SCOPE "read write" -#define INOREADER_OAUTH_TOKEN_URL "https://www.inoreader.com/oauth2/token" -#define INOREADER_OAUTH_AUTH_URL "https://www.inoreader.com/oauth2/auth" -#define INOREADER_OAUTH_CLI_ID "1000000604" -#define INOREADER_OAUTH_CLI_KEY "gsStoZ3aAoQJCgQxoFSuXkWI7Sly87yK" -#define INOREADER_REFRESH_TOKEN_KEY "refresh_token" -#define INOREADER_ACCESS_TOKEN_KEY "access_token" -#define INOREADER_DEFAULT_BATCH_SIZE 900 -#define INOREADER_MAX_BATCH_SIZE 999 +#define INOREADER_OAUTH_PORT 12885 +#define INOREADER_OAUTH_SCOPE "read write" +#define INOREADER_OAUTH_TOKEN_URL "https://www.inoreader.com/oauth2/token" +#define INOREADER_OAUTH_AUTH_URL "https://www.inoreader.com/oauth2/auth" +#define INOREADER_OAUTH_CLI_ID "1000000604" +#define INOREADER_OAUTH_CLI_KEY "gsStoZ3aAoQJCgQxoFSuXkWI7Sly87yK" +#define INOREADER_REFRESH_TOKEN_KEY "refresh_token" +#define INOREADER_ACCESS_TOKEN_KEY "access_token" +#define INOREADER_DEFAULT_BATCH_SIZE -1 +#define INOREADER_UNLIMITED_BATCH_SIZE -1 #endif // INOREADER_DEFINITIONS_H diff --git a/src/services/inoreader/gui/formeditinoreaderaccount.cpp b/src/services/inoreader/gui/formeditinoreaderaccount.cpp index 09078f452..93a49317c 100755 --- a/src/services/inoreader/gui/formeditinoreaderaccount.cpp +++ b/src/services/inoreader/gui/formeditinoreaderaccount.cpp @@ -32,9 +32,18 @@ FormEditInoreaderAccount::FormEditInoreaderAccount(QWidget* parent) : QDialog(pa tr("Not tested yet."), tr("Not tested yet.")); m_ui.m_lblTestResult->label()->setWordWrap(true); + + connect(m_ui.m_spinLimitMessages, static_cast(&QSpinBox::valueChanged), [=](int value) { + if (value <= 0) { + m_ui.m_spinLimitMessages->setSuffix(QSL(" ") + tr("= unlimited")); + } + else { + m_ui.m_spinLimitMessages->setSuffix(QSL(" ") + tr("messages")); + } + }); + m_ui.m_spinLimitMessages->setValue(INOREADER_DEFAULT_BATCH_SIZE); - m_ui.m_spinLimitMessages->setMinimum(1); - m_ui.m_spinLimitMessages->setMaximum(INOREADER_MAX_BATCH_SIZE); + m_ui.m_spinLimitMessages->setMinimum(INOREADER_UNLIMITED_BATCH_SIZE); connect(m_ui.m_btnTestSetup, &QPushButton::clicked, this, &FormEditInoreaderAccount::testSetup); connect(m_ui.m_buttonBox, &QDialogButtonBox::accepted, this, &FormEditInoreaderAccount::onClickedOk); @@ -78,7 +87,7 @@ void FormEditInoreaderAccount::onClickedOk() { if (m_editableRoot == nullptr) { // We want to confirm newly created account. // So save new account into DB, setup its properties. - m_editableRoot = new InoreaderServiceRoot(); + m_editableRoot = new InoreaderServiceRoot(m_network); editing_account = false; } diff --git a/src/services/inoreader/gui/formeditinoreaderaccount.ui b/src/services/inoreader/gui/formeditinoreaderaccount.ui index 8b5bd81de..b624e4acb 100755 --- a/src/services/inoreader/gui/formeditinoreaderaccount.ui +++ b/src/services/inoreader/gui/formeditinoreaderaccount.ui @@ -29,6 +29,9 @@ 16777215 + + = unlimited + -1 diff --git a/src/services/inoreader/inoreaderserviceroot.cpp b/src/services/inoreader/inoreaderserviceroot.cpp index 5f686a4c4..554565151 100755 --- a/src/services/inoreader/inoreaderserviceroot.cpp +++ b/src/services/inoreader/inoreaderserviceroot.cpp @@ -24,7 +24,15 @@ #include "services/inoreader/inoreaderentrypoint.h" #include "services/inoreader/network/inoreadernetworkfactory.h" -InoreaderServiceRoot::InoreaderServiceRoot(RootItem* parent) : ServiceRoot(parent) {} +InoreaderServiceRoot::InoreaderServiceRoot(InoreaderNetworkFactory* network, RootItem* parent) : ServiceRoot(parent), + m_network(network) { + if (m_network == nullptr) { + m_network = new InoreaderNetworkFactory(this); + } + else { + m_network->setParent(this); + } +} InoreaderServiceRoot::~InoreaderServiceRoot() {} diff --git a/src/services/inoreader/inoreaderserviceroot.h b/src/services/inoreader/inoreaderserviceroot.h index 529a5ed95..c7d5a4f8e 100755 --- a/src/services/inoreader/inoreaderserviceroot.h +++ b/src/services/inoreader/inoreaderserviceroot.h @@ -28,7 +28,7 @@ class InoreaderServiceRoot : public ServiceRoot { Q_OBJECT public: - explicit InoreaderServiceRoot(RootItem* parent = nullptr); + explicit InoreaderServiceRoot(InoreaderNetworkFactory* network, RootItem* parent = nullptr); virtual ~InoreaderServiceRoot(); void saveAccountDataToDatabase(); diff --git a/src/services/inoreader/network/inoreadernetworkfactory.cpp b/src/services/inoreader/network/inoreadernetworkfactory.cpp index a51b18aaf..c6c5470b6 100755 --- a/src/services/inoreader/network/inoreadernetworkfactory.cpp +++ b/src/services/inoreader/network/inoreadernetworkfactory.cpp @@ -122,6 +122,14 @@ void InoreaderNetworkFactory::initializeOauth() { }); } +void InoreaderNetworkFactory::setRefreshToken(const QString& refreshToken) { + m_refreshToken = refreshToken; +} + +void InoreaderNetworkFactory::setAccessToken(const QString& accessToken) { + m_accessToken = accessToken; +} + QString InoreaderNetworkFactory::refreshToken() const { return m_refreshToken; } diff --git a/src/services/inoreader/network/inoreadernetworkfactory.h b/src/services/inoreader/network/inoreadernetworkfactory.h index 078f2d7bd..e0ecff6c1 100755 --- a/src/services/inoreader/network/inoreadernetworkfactory.h +++ b/src/services/inoreader/network/inoreadernetworkfactory.h @@ -39,6 +39,8 @@ class InoreaderNetworkFactory : public QObject { QString accessToken() const; QString refreshToken() const; + void setAccessToken(const QString& accessToken); + void setRefreshToken(const QString& refreshToken); public slots: void logIn();