mirror of
https://github.com/martinrotter/rssguard.git
synced 2025-01-30 09:04:52 +01:00
Refactores some of TT-RSS network stuff.
This commit is contained in:
parent
7b7fe704d2
commit
1213c2e742
@ -19,6 +19,8 @@
|
||||
#define API_STATUS_ERR 1
|
||||
#define STATUS_OK "OK"
|
||||
|
||||
#define CONTENT_NOT_LOADED -1
|
||||
|
||||
// Login.
|
||||
#define API_DISABLED "API_DISABLED" // API is not enabled.
|
||||
#define LOGIN_ERROR "LOGIN_ERROR" // Incorrect password/username.
|
||||
|
@ -90,15 +90,14 @@ void FormEditAccount::displayPassword(bool display) {
|
||||
|
||||
void FormEditAccount::performTest() {
|
||||
TtRssNetworkFactory factory;
|
||||
QNetworkReply::NetworkError err;
|
||||
|
||||
factory.setUsername(m_ui->m_txtUsername->lineEdit()->text());
|
||||
factory.setPassword(m_ui->m_txtPassword->lineEdit()->text());
|
||||
factory.setUrl(m_ui->m_txtUrl->lineEdit()->text());
|
||||
|
||||
TtRssLoginResponse result = factory.login(err);
|
||||
TtRssLoginResponse result = factory.login();
|
||||
|
||||
if (err == QNetworkReply::NoError) {
|
||||
if (factory.lastError() == QNetworkReply::NoError) {
|
||||
if (result.hasError()) {
|
||||
QString error = result.error();
|
||||
|
||||
@ -154,9 +153,7 @@ void FormEditAccount::onClickedOk() {
|
||||
m_editableRoot->saveAccountDataToDatabase();
|
||||
|
||||
if (editing_account) {
|
||||
QNetworkReply::NetworkError error;
|
||||
|
||||
m_editableRoot->network()->logout(error);
|
||||
m_editableRoot->network()->logout();
|
||||
m_editableRoot->completelyRemoveAllData();
|
||||
m_editableRoot->syncIn();
|
||||
}
|
||||
|
@ -32,7 +32,7 @@
|
||||
|
||||
TtRssNetworkFactory::TtRssNetworkFactory()
|
||||
: m_url(QString()), m_username(QString()), m_password(QString()), m_sessionId(QString()),
|
||||
m_lastLoginTime(QDateTime()) {
|
||||
m_lastLoginTime(QDateTime()), m_lastError(QNetworkReply::NoError) {
|
||||
}
|
||||
|
||||
TtRssNetworkFactory::~TtRssNetworkFactory() {
|
||||
@ -66,9 +66,13 @@ QDateTime TtRssNetworkFactory::lastLoginTime() const {
|
||||
return m_lastLoginTime;
|
||||
}
|
||||
|
||||
TtRssLoginResponse TtRssNetworkFactory::login(QNetworkReply::NetworkError &error) {
|
||||
QNetworkReply::NetworkError TtRssNetworkFactory::lastError() const {
|
||||
return m_lastError;
|
||||
}
|
||||
|
||||
TtRssLoginResponse TtRssNetworkFactory::login() {
|
||||
if (!m_sessionId.isEmpty()) {
|
||||
logout(error);
|
||||
logout();
|
||||
}
|
||||
|
||||
QtJson::JsonObject json;
|
||||
@ -85,11 +89,11 @@ TtRssLoginResponse TtRssNetworkFactory::login(QNetworkReply::NetworkError &error
|
||||
m_lastLoginTime = QDateTime::currentDateTime();
|
||||
}
|
||||
|
||||
error = network_reply.first;
|
||||
m_lastError = network_reply.first;
|
||||
return login_response;
|
||||
}
|
||||
|
||||
TtRssResponse TtRssNetworkFactory::logout(QNetworkReply::NetworkError &error) {
|
||||
TtRssResponse TtRssNetworkFactory::logout() {
|
||||
if (!m_sessionId.isEmpty()) {
|
||||
|
||||
QtJson::JsonObject json;
|
||||
@ -99,21 +103,21 @@ TtRssResponse TtRssNetworkFactory::logout(QNetworkReply::NetworkError &error) {
|
||||
QByteArray result_raw;
|
||||
NetworkResult network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw);
|
||||
|
||||
error = network_reply.first;
|
||||
m_lastError = network_reply.first;
|
||||
|
||||
if (error == QNetworkReply::NoError) {
|
||||
if (m_lastError == QNetworkReply::NoError) {
|
||||
m_sessionId.clear();
|
||||
}
|
||||
|
||||
return TtRssResponse(QString::fromUtf8(result_raw));
|
||||
}
|
||||
else {
|
||||
error = QNetworkReply::NoError;
|
||||
m_lastError = QNetworkReply::NoError;
|
||||
return TtRssResponse();
|
||||
}
|
||||
}
|
||||
|
||||
TtRssGetFeedsCategoriesResponse TtRssNetworkFactory::getFeedsCategories(QNetworkReply::NetworkError &error) {
|
||||
TtRssGetFeedsCategoriesResponse TtRssNetworkFactory::getFeedsCategories() {
|
||||
QtJson::JsonObject json;
|
||||
json["op"] = "getFeedTree";
|
||||
json["sid"] = m_sessionId;
|
||||
@ -125,20 +129,20 @@ TtRssGetFeedsCategoriesResponse TtRssNetworkFactory::getFeedsCategories(QNetwork
|
||||
|
||||
if (result.isNotLoggedIn()) {
|
||||
// We are not logged in.
|
||||
login(error);
|
||||
login();
|
||||
json["sid"] = m_sessionId;
|
||||
|
||||
network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw);
|
||||
result = TtRssGetFeedsCategoriesResponse(QString::fromUtf8(result_raw));
|
||||
}
|
||||
|
||||
error = network_reply.first;
|
||||
m_lastError = network_reply.first;
|
||||
return result;
|
||||
}
|
||||
|
||||
TtRssGetHeadlinesResponse TtRssNetworkFactory::getHeadlines(int feed_id, bool force_update, int limit, int skip,
|
||||
bool show_content, bool include_attachments,
|
||||
bool sanitize, QNetworkReply::NetworkError &error) {
|
||||
bool sanitize) {
|
||||
QtJson::JsonObject json;
|
||||
json["op"] = "getHeadlines";
|
||||
json["sid"] = m_sessionId;
|
||||
@ -156,21 +160,20 @@ TtRssGetHeadlinesResponse TtRssNetworkFactory::getHeadlines(int feed_id, bool fo
|
||||
|
||||
if (result.isNotLoggedIn()) {
|
||||
// We are not logged in.
|
||||
login(error);
|
||||
login();
|
||||
json["sid"] = m_sessionId;
|
||||
|
||||
network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw);
|
||||
result = TtRssGetHeadlinesResponse(QString::fromUtf8(result_raw));
|
||||
}
|
||||
|
||||
error = network_reply.first;
|
||||
m_lastError = network_reply.first;
|
||||
return result;
|
||||
}
|
||||
|
||||
TtRssUpdateArticleResponse TtRssNetworkFactory::updateArticles(const QStringList &ids,
|
||||
UpdateArticle::OperatingField field,
|
||||
UpdateArticle::Mode mode,
|
||||
QNetworkReply::NetworkError &error) {
|
||||
UpdateArticle::Mode mode) {
|
||||
QtJson::JsonObject json;
|
||||
json["op"] = "updateArticle";
|
||||
json["sid"] = m_sessionId;
|
||||
@ -184,14 +187,14 @@ TtRssUpdateArticleResponse TtRssNetworkFactory::updateArticles(const QStringList
|
||||
|
||||
if (result.isNotLoggedIn()) {
|
||||
// We are not logged in.
|
||||
login(error);
|
||||
login();
|
||||
json["sid"] = m_sessionId;
|
||||
|
||||
network_reply = NetworkFactory::uploadData(m_url, DOWNLOAD_TIMEOUT, QtJson::serialize(json), CONTENT_TYPE, result_raw);
|
||||
result = TtRssUpdateArticleResponse(QString::fromUtf8(result_raw));
|
||||
}
|
||||
|
||||
error = network_reply.first;
|
||||
m_lastError = network_reply.first;
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -208,7 +211,7 @@ bool TtRssResponse::isLoaded() const {
|
||||
|
||||
int TtRssResponse::seq() const {
|
||||
if (!isLoaded()) {
|
||||
return -1;
|
||||
return CONTENT_NOT_LOADED;
|
||||
}
|
||||
else {
|
||||
return m_rawContent["seq"].toInt();
|
||||
@ -217,7 +220,7 @@ int TtRssResponse::seq() const {
|
||||
|
||||
int TtRssResponse::status() const {
|
||||
if (!isLoaded()) {
|
||||
return -1;
|
||||
return CONTENT_NOT_LOADED;
|
||||
}
|
||||
else {
|
||||
return m_rawContent["status"].toInt();
|
||||
@ -237,7 +240,7 @@ TtRssLoginResponse::~TtRssLoginResponse() {
|
||||
|
||||
int TtRssLoginResponse::apiLevel() const {
|
||||
if (!isLoaded()) {
|
||||
return -1;
|
||||
return CONTENT_NOT_LOADED;
|
||||
}
|
||||
else {
|
||||
return m_rawContent["content"].toMap()["api_level"].toInt();
|
||||
|
@ -114,24 +114,26 @@ class TtRssNetworkFactory {
|
||||
// Metadata.
|
||||
QDateTime lastLoginTime() const;
|
||||
|
||||
QNetworkReply::NetworkError lastError() const;
|
||||
|
||||
// Operations.
|
||||
|
||||
// Logs user in.
|
||||
TtRssLoginResponse login(QNetworkReply::NetworkError &error);
|
||||
TtRssLoginResponse login();
|
||||
|
||||
// Logs user out.
|
||||
TtRssResponse logout(QNetworkReply::NetworkError &error);
|
||||
TtRssResponse logout();
|
||||
|
||||
// Gets feeds from the server.
|
||||
TtRssGetFeedsCategoriesResponse getFeedsCategories(QNetworkReply::NetworkError &error);
|
||||
TtRssGetFeedsCategoriesResponse getFeedsCategories();
|
||||
|
||||
// Gets headlines (messages) from the server.
|
||||
TtRssGetHeadlinesResponse getHeadlines(int feed_id, bool force_update, int limit, int skip,
|
||||
bool show_content, bool include_attachments,
|
||||
bool sanitize, QNetworkReply::NetworkError &error);
|
||||
bool sanitize);
|
||||
|
||||
TtRssUpdateArticleResponse updateArticles(const QStringList &ids, UpdateArticle::OperatingField field,
|
||||
UpdateArticle::Mode mode, QNetworkReply::NetworkError &error);
|
||||
UpdateArticle::Mode mode);
|
||||
|
||||
private:
|
||||
QString m_url;
|
||||
@ -139,6 +141,7 @@ class TtRssNetworkFactory {
|
||||
QString m_password;
|
||||
QString m_sessionId;
|
||||
QDateTime m_lastLoginTime;
|
||||
QNetworkReply::NetworkError m_lastError;
|
||||
};
|
||||
|
||||
#endif // TTRSSNETWORKFACTORY_H
|
||||
|
@ -46,15 +46,13 @@ TtRssServiceRoot *TtRssCategory::serviceRoot() {
|
||||
}
|
||||
|
||||
bool TtRssCategory::markAsReadUnread(RootItem::ReadStatus status) {
|
||||
QNetworkReply::NetworkError error;
|
||||
QStringList ids = serviceRoot()->customIDSOfMessagesForItem(this);
|
||||
TtRssUpdateArticleResponse response = serviceRoot()->network()->updateArticles(ids, UpdateArticle::Unread,
|
||||
status == RootItem::Unread ?
|
||||
UpdateArticle::SetToTrue :
|
||||
UpdateArticle::SetToFalse,
|
||||
error);
|
||||
UpdateArticle::SetToFalse);
|
||||
|
||||
if (error != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
if (serviceRoot()->network()->lastError() != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
|
@ -85,7 +85,6 @@ int TtRssFeed::countOfUnreadMessages() const {
|
||||
}
|
||||
|
||||
int TtRssFeed::update() {
|
||||
QNetworkReply::NetworkError error;
|
||||
QList<Message> messages;
|
||||
int newly_added_messages = 0;
|
||||
int limit = MAX_MESSAGES;
|
||||
@ -93,9 +92,9 @@ int TtRssFeed::update() {
|
||||
|
||||
do {
|
||||
TtRssGetHeadlinesResponse headlines = serviceRoot()->network()->getHeadlines(customId(), true, limit, skip,
|
||||
true, true, false, error);
|
||||
true, true, false);
|
||||
|
||||
if (error != QNetworkReply::NoError) {
|
||||
if (serviceRoot()->network()->lastError() != QNetworkReply::NoError) {
|
||||
setStatus(Feed::Error);
|
||||
return 0;
|
||||
}
|
||||
@ -145,15 +144,13 @@ QList<Message> TtRssFeed::undeletedMessages() const {
|
||||
}
|
||||
|
||||
bool TtRssFeed::markAsReadUnread(RootItem::ReadStatus status) {
|
||||
QNetworkReply::NetworkError error;
|
||||
QStringList ids = serviceRoot()->customIDSOfMessagesForItem(this);
|
||||
TtRssUpdateArticleResponse response = serviceRoot()->network()->updateArticles(ids, UpdateArticle::Unread,
|
||||
status == RootItem::Unread ?
|
||||
UpdateArticle::SetToTrue :
|
||||
UpdateArticle::SetToFalse,
|
||||
error);
|
||||
UpdateArticle::SetToFalse);
|
||||
|
||||
if (error != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
if (serviceRoot()->network()->lastError() != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
|
@ -34,15 +34,13 @@ TtRssServiceRoot *TtRssRecycleBin::serviceRoot() {
|
||||
}
|
||||
|
||||
bool TtRssRecycleBin::markAsReadUnread(RootItem::ReadStatus status) {
|
||||
QNetworkReply::NetworkError error;
|
||||
QStringList ids = serviceRoot()->customIDSOfMessagesForItem(this);
|
||||
TtRssUpdateArticleResponse response = serviceRoot()->network()->updateArticles(ids, UpdateArticle::Unread,
|
||||
status == RootItem::Unread ?
|
||||
UpdateArticle::SetToTrue :
|
||||
UpdateArticle::SetToFalse,
|
||||
error);
|
||||
UpdateArticle::SetToFalse);
|
||||
|
||||
if (error != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
if (serviceRoot()->network()->lastError() != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
|
@ -55,10 +55,9 @@ void TtRssServiceRoot::start() {
|
||||
}
|
||||
|
||||
void TtRssServiceRoot::stop() {
|
||||
QNetworkReply::NetworkError error;
|
||||
m_network->logout(error);
|
||||
m_network->logout();
|
||||
|
||||
qDebug("Stopping Tiny Tiny RSS account, logging out with result '%d'.", (int) error);
|
||||
qDebug("Stopping Tiny Tiny RSS account, logging out with result '%d'.", (int) m_network->lastError());
|
||||
}
|
||||
|
||||
QString TtRssServiceRoot::code() {
|
||||
@ -86,15 +85,13 @@ bool TtRssServiceRoot::deleteViaGui() {
|
||||
}
|
||||
|
||||
bool TtRssServiceRoot::markAsReadUnread(RootItem::ReadStatus status) {
|
||||
QNetworkReply::NetworkError error;
|
||||
QStringList ids = customIDSOfMessagesForItem(this);
|
||||
TtRssUpdateArticleResponse response = m_network->updateArticles(ids, UpdateArticle::Unread,
|
||||
status == RootItem::Unread ?
|
||||
UpdateArticle::SetToTrue :
|
||||
UpdateArticle::SetToFalse,
|
||||
error);
|
||||
UpdateArticle::SetToFalse);
|
||||
|
||||
if (error != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
if (m_network->lastError() != QNetworkReply::NoError || response.updateStatus() != STATUS_OK) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
@ -166,16 +163,17 @@ QList<QAction*> TtRssServiceRoot::contextMenu() {
|
||||
return serviceMenu();
|
||||
}
|
||||
|
||||
bool TtRssServiceRoot::onBeforeSetMessagesRead(RootItem *selected_item, const QList<Message> &messages, RootItem::ReadStatus read) {
|
||||
bool TtRssServiceRoot::onBeforeSetMessagesRead(RootItem *selected_item, const QList<Message> &messages,
|
||||
RootItem::ReadStatus read) {
|
||||
Q_UNUSED(selected_item)
|
||||
|
||||
QNetworkReply::NetworkError error;
|
||||
TtRssUpdateArticleResponse response = m_network->updateArticles(customIDsOfMessages(messages),
|
||||
UpdateArticle::Unread,
|
||||
read == RootItem::Unread ? UpdateArticle::SetToTrue : UpdateArticle::SetToFalse,
|
||||
error);
|
||||
read == RootItem::Unread ?
|
||||
UpdateArticle::SetToTrue :
|
||||
UpdateArticle::SetToFalse);
|
||||
|
||||
if (error == QNetworkReply::NoError && response.updateStatus() == STATUS_OK) {
|
||||
if (m_network->lastError() == QNetworkReply::NoError && response.updateStatus() == STATUS_OK) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
@ -196,17 +194,14 @@ bool TtRssServiceRoot::onAfterSetMessagesRead(RootItem *selected_item, const QLi
|
||||
bool TtRssServiceRoot::onBeforeSwitchMessageImportance(RootItem *selected_item, const QList<QPair<Message,Importance> > &changes) {
|
||||
Q_UNUSED(selected_item)
|
||||
|
||||
QNetworkReply::NetworkError error;
|
||||
|
||||
// NOTE: We just toggle it here, because we know, that there is only
|
||||
// toggling of starred status supported by RSS Guard right now and
|
||||
// Tiny Tiny RSS API allows it, which is greate.
|
||||
TtRssUpdateArticleResponse response = m_network->updateArticles(customIDsOfMessages(changes),
|
||||
UpdateArticle::Starred,
|
||||
UpdateArticle::Togggle,
|
||||
error);
|
||||
UpdateArticle::Togggle);
|
||||
|
||||
if (error == QNetworkReply::NoError && response.updateStatus() == STATUS_OK) {
|
||||
if (m_network->lastError() == QNetworkReply::NoError && response.updateStatus() == STATUS_OK) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
@ -536,10 +531,9 @@ void TtRssServiceRoot::syncIn() {
|
||||
setIcon(qApp->icons()->fromTheme(QSL("item-sync")));
|
||||
itemChanged(QList<RootItem*>() << this);
|
||||
|
||||
QNetworkReply::NetworkError err;
|
||||
TtRssGetFeedsCategoriesResponse feed_cats_response = m_network->getFeedsCategories(err);
|
||||
TtRssGetFeedsCategoriesResponse feed_cats_response = m_network->getFeedsCategories();
|
||||
|
||||
if (err == QNetworkReply::NoError) {
|
||||
if (m_network->lastError() == QNetworkReply::NoError) {
|
||||
RootItem *new_tree = feed_cats_response.feedsCategories(true, m_network->url());
|
||||
|
||||
// Purge old data from SQL and clean all model items.
|
||||
|
Loading…
x
Reference in New Issue
Block a user