From 39f73a0d02d81e3d1f8f338f1b2ea9097f3d78be Mon Sep 17 00:00:00 2001
From: Martin Rotter <rotter@praktik.cz>
Date: Wed, 20 Sep 2017 11:58:30 +0200
Subject: [PATCH] Refactoring, common code moved to superclasses.

---
 src/services/abstract/category.cpp             |  4 ++++
 src/services/abstract/category.h               |  1 +
 src/services/abstract/feed.cpp                 |  4 ++++
 src/services/abstract/feed.h                   |  2 ++
 .../owncloud/network/owncloudnetworkfactory.h  |  3 +--
 src/services/owncloud/owncloudfeed.cpp         |  4 ----
 src/services/owncloud/owncloudfeed.h           |  1 -
 src/services/standard/standardcategory.cpp     |  4 ----
 src/services/standard/standardcategory.h       |  1 -
 src/services/standard/standardfeed.cpp         |  4 ----
 src/services/standard/standardfeed.h           |  1 -
 src/services/tt-rss/ttrsscategory.cpp          | 18 ++----------------
 src/services/tt-rss/ttrsscategory.h            |  1 -
 src/services/tt-rss/ttrssfeed.cpp              |  4 ----
 src/services/tt-rss/ttrssfeed.h                |  1 -
 15 files changed, 14 insertions(+), 39 deletions(-)

diff --git a/src/services/abstract/category.cpp b/src/services/abstract/category.cpp
index 6592a1678..1cd2627a0 100755
--- a/src/services/abstract/category.cpp
+++ b/src/services/abstract/category.cpp
@@ -66,3 +66,7 @@ void Category::updateCounts(bool including_total_count) {
     }
   }
 }
+
+bool Category::cleanMessages(bool clean_read_only) {
+  return getParentServiceRoot()->cleanFeeds(getSubTreeFeeds(), clean_read_only);
+}
diff --git a/src/services/abstract/category.h b/src/services/abstract/category.h
index 2031e40a5..c3030c976 100755
--- a/src/services/abstract/category.h
+++ b/src/services/abstract/category.h
@@ -29,6 +29,7 @@ class Category : public RootItem {
     virtual ~Category();
 
     void updateCounts(bool including_total_count);
+    bool cleanMessages(bool clean_read_only);
 };
 
 #endif // CATEGORY_H
diff --git a/src/services/abstract/feed.cpp b/src/services/abstract/feed.cpp
index 7eede996d..d2da94f65 100755
--- a/src/services/abstract/feed.cpp
+++ b/src/services/abstract/feed.cpp
@@ -214,6 +214,10 @@ void Feed::run() {
   emit messagesObtained(msgs, error_during_obtaining);
 }
 
+bool Feed::cleanMessages(bool clean_read_only) {
+  return getParentServiceRoot()->cleanFeeds(QList<Feed*>() << this, clean_read_only);
+}
+
 int Feed::updateMessages(const QList<Message>& messages, bool error_during_obtaining) {
   QList<RootItem*> items_to_update;
   int updated_messages = 0;
diff --git a/src/services/abstract/feed.h b/src/services/abstract/feed.h
index a18175d24..23abc2c35 100755
--- a/src/services/abstract/feed.h
+++ b/src/services/abstract/feed.h
@@ -82,6 +82,8 @@ class Feed : public RootItem, public QRunnable {
     // Runs update in thread (thread pooled).
     void run();
 
+    bool cleanMessages(bool clean_read_only);
+
   public slots:
     void updateCounts(bool including_total_count);
     int updateMessages(const QList<Message>& messages, bool error_during_obtaining);
diff --git a/src/services/owncloud/network/owncloudnetworkfactory.h b/src/services/owncloud/network/owncloudnetworkfactory.h
index 6faed2656..aa4a768d5 100755
--- a/src/services/owncloud/network/owncloudnetworkfactory.h
+++ b/src/services/owncloud/network/owncloudnetworkfactory.h
@@ -73,8 +73,7 @@ class RootItem;
 
 class OwnCloudGetFeedsCategoriesResponse {
   public:
-    explicit OwnCloudGetFeedsCategoriesResponse(const QString& raw_categories = QString(),
-                                                const QString& raw_feeds = QString());
+    explicit OwnCloudGetFeedsCategoriesResponse(const QString& raw_categories = QString(), const QString& raw_feeds = QString());
     virtual ~OwnCloudGetFeedsCategoriesResponse();
 
     // Returns tree of feeds/categories.
diff --git a/src/services/owncloud/owncloudfeed.cpp b/src/services/owncloud/owncloudfeed.cpp
index 6850fe206..6c38d3413 100755
--- a/src/services/owncloud/owncloudfeed.cpp
+++ b/src/services/owncloud/owncloudfeed.cpp
@@ -91,10 +91,6 @@ bool OwnCloudFeed::markAsReadUnread(RootItem::ReadStatus status) {
   return getParentServiceRoot()->markFeedsReadUnread(QList<Feed*>() << this, status);
 }
 
-bool OwnCloudFeed::cleanMessages(bool clear_only_read) {
-  return getParentServiceRoot()->cleanFeeds(QList<Feed*>() << this, clear_only_read);
-}
-
 OwnCloudServiceRoot* OwnCloudFeed::serviceRoot() const {
   return qobject_cast<OwnCloudServiceRoot*>(getParentServiceRoot());
 }
diff --git a/src/services/owncloud/owncloudfeed.h b/src/services/owncloud/owncloudfeed.h
index 12e23af01..f0c86f312 100755
--- a/src/services/owncloud/owncloudfeed.h
+++ b/src/services/owncloud/owncloudfeed.h
@@ -40,7 +40,6 @@ class OwnCloudFeed : public Feed {
     bool removeItself();
 
     bool markAsReadUnread(ReadStatus status);
-    bool cleanMessages(bool clear_only_read);
 
     OwnCloudServiceRoot* serviceRoot() const;
 
diff --git a/src/services/standard/standardcategory.cpp b/src/services/standard/standardcategory.cpp
index 758585a34..ea19e16d5 100755
--- a/src/services/standard/standardcategory.cpp
+++ b/src/services/standard/standardcategory.cpp
@@ -116,10 +116,6 @@ bool StandardCategory::markAsReadUnread(ReadStatus status) {
   return serviceRoot()->markFeedsReadUnread(getSubTreeFeeds(), status);
 }
 
-bool StandardCategory::cleanMessages(bool clean_read_only) {
-  return serviceRoot()->cleanFeeds(getSubTreeFeeds(), clean_read_only);
-}
-
 bool StandardCategory::removeItself() {
   bool children_removed = true;
 
diff --git a/src/services/standard/standardcategory.h b/src/services/standard/standardcategory.h
index 519dd1508..1c7a69db4 100755
--- a/src/services/standard/standardcategory.h
+++ b/src/services/standard/standardcategory.h
@@ -60,7 +60,6 @@ class StandardCategory : public Category {
     bool deleteViaGui();
 
     bool markAsReadUnread(ReadStatus status);
-    bool cleanMessages(bool clean_read_only);
 
     // Removes category and all its children from persistent
     // database.
diff --git a/src/services/standard/standardfeed.cpp b/src/services/standard/standardfeed.cpp
index 63e8120d4..3777bbc71 100755
--- a/src/services/standard/standardfeed.cpp
+++ b/src/services/standard/standardfeed.cpp
@@ -110,10 +110,6 @@ bool StandardFeed::markAsReadUnread(ReadStatus status) {
   return serviceRoot()->markFeedsReadUnread(QList<Feed*>() << this, status);
 }
 
-bool StandardFeed::cleanMessages(bool clean_read_only) {
-  return serviceRoot()->cleanFeeds(QList<Feed*>() << this, clean_read_only);
-}
-
 QVariant StandardFeed::data(int column, int role) const {
   switch (role) {
     case Qt::ToolTipRole:
diff --git a/src/services/standard/standardfeed.h b/src/services/standard/standardfeed.h
index be9d610dd..2adf2e632 100755
--- a/src/services/standard/standardfeed.h
+++ b/src/services/standard/standardfeed.h
@@ -70,7 +70,6 @@ class StandardFeed : public Feed {
     bool deleteViaGui();
 
     bool markAsReadUnread(ReadStatus status);
-    bool cleanMessages(bool clean_read_only);
 
     QVariant data(int column, int role) const;
 
diff --git a/src/services/tt-rss/ttrsscategory.cpp b/src/services/tt-rss/ttrsscategory.cpp
index 604ba0f11..7fb14861c 100755
--- a/src/services/tt-rss/ttrsscategory.cpp
+++ b/src/services/tt-rss/ttrsscategory.cpp
@@ -45,20 +45,6 @@ TtRssServiceRoot* TtRssCategory::serviceRoot() const {
 }
 
 bool TtRssCategory::markAsReadUnread(RootItem::ReadStatus status) {
-  const QStringList ids = serviceRoot()->customIDSOfMessagesForItem(this);
-  TtRssUpdateArticleResponse response = serviceRoot()->network()->updateArticles(ids, UpdateArticle::Unread,
-                                                                                 status == RootItem::Unread ?
-                                                                                 UpdateArticle::SetToTrue :
-                                                                                 UpdateArticle::SetToFalse);
-
-  if (serviceRoot()->network()->lastError() != QNetworkReply::NoError || response.updateStatus()  != STATUS_OK) {
-    return false;
-  }
-  else {
-    return serviceRoot()->markFeedsReadUnread(getSubTreeFeeds(), status);
-  }
-}
-
-bool TtRssCategory::cleanMessages(bool clear_only_read) {
-  return serviceRoot()->cleanFeeds(getSubTreeFeeds(), clear_only_read);
+  serviceRoot()->addMessageStatesToCache(serviceRoot()->customIDSOfMessagesForItem(this), status);
+  return true;
 }
diff --git a/src/services/tt-rss/ttrsscategory.h b/src/services/tt-rss/ttrsscategory.h
index 629f7c9b2..675eaab40 100755
--- a/src/services/tt-rss/ttrsscategory.h
+++ b/src/services/tt-rss/ttrsscategory.h
@@ -36,7 +36,6 @@ class TtRssCategory : public Category {
     TtRssServiceRoot* serviceRoot() const;
 
     bool markAsReadUnread(ReadStatus status);
-    bool cleanMessages(bool clear_only_read);
 };
 
 #endif // TTRSSCATEGORY_H
diff --git a/src/services/tt-rss/ttrssfeed.cpp b/src/services/tt-rss/ttrssfeed.cpp
index f397e24c5..c52135171 100755
--- a/src/services/tt-rss/ttrssfeed.cpp
+++ b/src/services/tt-rss/ttrssfeed.cpp
@@ -83,10 +83,6 @@ bool TtRssFeed::markAsReadUnread(RootItem::ReadStatus status) {
   return getParentServiceRoot()->markFeedsReadUnread(QList<Feed*>() << this, status);
 }
 
-bool TtRssFeed::cleanMessages(bool clear_only_read) {
-  return getParentServiceRoot()->cleanFeeds(QList<Feed*>() << this, clear_only_read);
-}
-
 bool TtRssFeed::editItself(TtRssFeed* new_feed_data) {
   QSqlDatabase database = qApp->database()->connection("aa", DatabaseFactory::FromSettings);
 
diff --git a/src/services/tt-rss/ttrssfeed.h b/src/services/tt-rss/ttrssfeed.h
index 025a54499..50ef9b747 100755
--- a/src/services/tt-rss/ttrssfeed.h
+++ b/src/services/tt-rss/ttrssfeed.h
@@ -41,7 +41,6 @@ class TtRssFeed : public Feed {
     bool deleteViaGui();
 
     bool markAsReadUnread(ReadStatus status);
-    bool cleanMessages(bool clear_only_read);
 
     bool editItself(TtRssFeed* new_feed_data);
     bool removeItself();