Refactored common code.

This commit is contained in:
Martin Rotter 2017-10-03 10:26:17 +02:00
parent 27faa7dfc5
commit a643307508
9 changed files with 67 additions and 89 deletions

View file

@ -24,10 +24,6 @@
#include "services/inoreader/inoreaderentrypoint.h"
#endif
#include "services/owncloud/owncloudserviceentrypoint.h"
#include "services/standard/standardserviceentrypoint.h"
#include "services/tt-rss/ttrssserviceentrypoint.h"
#include "core/feeddownloader.h"
#include "core/feedsmodel.h"
#include "core/feedsproxymodel.h"
@ -36,6 +32,10 @@
#include "miscellaneous/application.h"
#include "miscellaneous/databasecleaner.h"
#include "miscellaneous/mutex.h"
#include "services/abstract/cacheforserviceroot.h"
#include "services/owncloud/owncloudserviceentrypoint.h"
#include "services/standard/standardserviceentrypoint.h"
#include "services/tt-rss/ttrssserviceentrypoint.h"
#include <QtConcurrent/QtConcurrentRun>
#include <QThread>
@ -224,8 +224,11 @@ void FeedReader::executeNextAutoUpdate() {
void FeedReader::checkServicesForAsyncOperations() {
foreach (ServiceRoot* service, m_feedsModel->serviceRoots()) {
// Store any cached data.
service->saveAllCachedData();
auto cache = dynamic_cast<CacheForServiceRoot*>(service);
if (cache != nullptr) {
cache->saveAllCachedData();
}
}
asyncCacheSaveFinished();

View file

@ -196,7 +196,12 @@ void Feed::run() {
<< QThread::currentThreadId() << "\'.";
// Save all cached data first.
getParentServiceRoot()->saveAllCachedData();
auto cache = dynamic_cast<CacheForServiceRoot*>(getParentServiceRoot());
if (cache != nullptr) {
cache->saveAllCachedData();
}
bool error_during_obtaining;
QList<Message> msgs = obtainNewMessages(&error_during_obtaining);

View file

@ -203,8 +203,6 @@ QList<Message> ServiceRoot::undeletedMessages() const {
return DatabaseQueries::getUndeletedMessagesForAccount(database, accountId());
}
void ServiceRoot::saveAllCachedData() {}
void ServiceRoot::itemChanged(const QList<RootItem*>& items) {
emit dataChanged(items);
}
@ -447,9 +445,14 @@ bool ServiceRoot::loadMessagesForItem(RootItem* item, MessagesModel* model) {
}
bool ServiceRoot::onBeforeSetMessagesRead(RootItem* selected_item, const QList<Message>& messages, RootItem::ReadStatus read) {
Q_UNUSED(messages)
Q_UNUSED(read)
Q_UNUSED(selected_item)
auto cache = dynamic_cast<CacheForServiceRoot*>(this);
if (cache != nullptr) {
cache->addMessageStatesToCache(customIDsOfMessages(messages), read);
}
return true;
}
@ -463,7 +466,32 @@ bool ServiceRoot::onAfterSetMessagesRead(RootItem* selected_item, const QList<Me
bool ServiceRoot::onBeforeSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes) {
Q_UNUSED(selected_item)
Q_UNUSED(changes)
auto cache = dynamic_cast<CacheForServiceRoot*>(this);
if (cache != nullptr) {
// Now, we need to separate the changes because of ownCloud API limitations.
QList<Message> mark_starred_msgs;
QList<Message> mark_unstarred_msgs;
foreach (const ImportanceChange& pair, changes) {
if (pair.second == RootItem::Important) {
mark_starred_msgs.append(pair.first);
}
else {
mark_unstarred_msgs.append(pair.first);
}
}
if (!mark_starred_msgs.isEmpty()) {
cache->addMessageStatesToCache(mark_starred_msgs, RootItem::Important);
}
if (!mark_unstarred_msgs.isEmpty()) {
cache->addMessageStatesToCache(mark_unstarred_msgs, RootItem::NotImportant);
}
}
return true;
}

View file

@ -79,7 +79,6 @@ class ServiceRoot : public RootItem {
// user explicitly deletes existing service instance.
virtual void start(bool freshly_activated) = 0;
virtual void stop() = 0;
virtual void saveAllCachedData();
// Account ID corresponds with DB attribute Accounts (id).
int accountId() const;

View file

@ -151,7 +151,25 @@ QList<Message> InoreaderNetworkFactory::messages(const QString& stream_id, bool*
}
}
void InoreaderNetworkFactory::markMessagesRead(RootItem::ReadStatus status, const QStringList& custom_ids) {}
void InoreaderNetworkFactory::markMessagesRead(RootItem::ReadStatus status, const QStringList& custom_ids) {
QString target_url = INOREADER_API_EDIT_TAG;
if (status == RootItem::ReadStatus::Read) {
target_url += QString("?a=user/-/") + INOREADER_STATE_READ + "&";
}
else {
target_url += QString("?r=user/-/") + INOREADER_STATE_READ + "&";
}
QStringList trimmed_ids;
foreach (const QString& id, custom_ids) {
trimmed_ids.append(QString("i=") + id);
}
QString full_url = target_url + trimmed_ids.join(QL1C('&'));
}
void InoreaderNetworkFactory::markMessagesStarred(RootItem::Importance importance, const QStringList& custom_ids) {}

View file

@ -143,42 +143,6 @@ void OwnCloudServiceRoot::saveAllCachedData() {
}
}
bool OwnCloudServiceRoot::onBeforeSetMessagesRead(RootItem* selected_item,
const QList<Message>& messages,
RootItem::ReadStatus read) {
Q_UNUSED(selected_item)
addMessageStatesToCache(customIDsOfMessages(messages), read);
return true;
}
bool OwnCloudServiceRoot::onBeforeSwitchMessageImportance(RootItem* selected_item,
const QList<ImportanceChange>& changes) {
Q_UNUSED(selected_item)
// Now, we need to separate the changes because of ownCloud API limitations.
QList<Message> mark_starred_msgs;
QList<Message> mark_unstarred_msgs;
foreach (const ImportanceChange& pair, changes) {
if (pair.second == RootItem::Important) {
mark_starred_msgs.append(pair.first);
}
else {
mark_unstarred_msgs.append(pair.first);
}
}
if (!mark_starred_msgs.isEmpty()) {
addMessageStatesToCache(mark_starred_msgs, RootItem::Important);
}
if (!mark_unstarred_msgs.isEmpty()) {
addMessageStatesToCache(mark_unstarred_msgs, RootItem::NotImportant);
}
return true;
}
void OwnCloudServiceRoot::updateTitle() {
setTitle(m_network->authUsername() + QSL(" (Nextcloud News)"));
}

View file

@ -47,9 +47,6 @@ class OwnCloudServiceRoot : public ServiceRoot, public CacheForServiceRoot {
QString code() const;
OwnCloudNetworkFactory* network() const;
bool onBeforeSetMessagesRead(RootItem* selected_item, const QList<Message>& messages, ReadStatus read);
bool onBeforeSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes);
void updateTitle();
void saveAccountDataToDatabase();

View file

@ -193,39 +193,6 @@ QList<QAction*> TtRssServiceRoot::serviceMenu() {
return m_serviceMenu;
}
bool TtRssServiceRoot::onBeforeSetMessagesRead(RootItem* selected_item, const QList<Message>& messages, RootItem::ReadStatus read) {
Q_UNUSED(selected_item)
addMessageStatesToCache(customIDsOfMessages(messages), read);
return true;
}
bool TtRssServiceRoot::onBeforeSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes) {
Q_UNUSED(selected_item)
// Now, we need to separate the changes because of ownCloud API limitations.
QList<Message> mark_starred_msgs;
QList<Message> mark_unstarred_msgs;
foreach (const ImportanceChange& pair, changes) {
if (pair.second == RootItem::Important) {
mark_starred_msgs.append(pair.first);
}
else {
mark_unstarred_msgs.append(pair.first);
}
}
if (!mark_starred_msgs.isEmpty()) {
addMessageStatesToCache(mark_starred_msgs, RootItem::Important);
}
if (!mark_unstarred_msgs.isEmpty()) {
addMessageStatesToCache(mark_unstarred_msgs, RootItem::NotImportant);
}
return true;
}
TtRssNetworkFactory* TtRssServiceRoot::network() const {
return m_network;
}

View file

@ -49,9 +49,6 @@ class TtRssServiceRoot : public ServiceRoot, public CacheForServiceRoot {
void saveAllCachedData();
bool onBeforeSetMessagesRead(RootItem* selected_item, const QList<Message>& messages, ReadStatus read);
bool onBeforeSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes);
// Access to network.
TtRssNetworkFactory* network() const;