From 34e0e709a396144a773151435c9e84753e657c5a Mon Sep 17 00:00:00 2001 From: martinrotter Date: Thu, 27 Apr 2017 07:41:27 +0200 Subject: [PATCH] Separate cache functionality into own class. --- rssguard.pro | 6 +- src/services/abstract/cacheforserviceroot.cpp | 77 +++++++++++++++++++ src/services/abstract/cacheforserviceroot.h | 45 +++++++++++ src/services/owncloud/owncloudrecyclebin.cpp | 1 + src/services/owncloud/owncloudserviceroot.cpp | 52 ++----------- src/services/owncloud/owncloudserviceroot.h | 12 +-- 6 files changed, 137 insertions(+), 56 deletions(-) create mode 100755 src/services/abstract/cacheforserviceroot.cpp create mode 100755 src/services/abstract/cacheforserviceroot.h diff --git a/rssguard.pro b/rssguard.pro index 672a69c89..5ea644670 100755 --- a/rssguard.pro +++ b/rssguard.pro @@ -327,7 +327,8 @@ HEADERS += src/core/feeddownloader.h \ src/services/standard/feedparser.h \ src/services/standard/rdfparser.h \ src/services/standard/rssparser.h \ - src/miscellaneous/serviceoperator.h + src/miscellaneous/serviceoperator.h \ + src/services/abstract/cacheforserviceroot.h SOURCES += src/core/feeddownloader.cpp \ src/core/feedsmodel.cpp \ @@ -446,7 +447,8 @@ SOURCES += src/core/feeddownloader.cpp \ src/services/standard/feedparser.cpp \ src/services/standard/rdfparser.cpp \ src/services/standard/rssparser.cpp \ - src/miscellaneous/serviceoperator.cpp + src/miscellaneous/serviceoperator.cpp \ + src/services/abstract/cacheforserviceroot.cpp FORMS += src/gui/toolbareditor.ui \ src/network-web/downloaditem.ui \ diff --git a/src/services/abstract/cacheforserviceroot.cpp b/src/services/abstract/cacheforserviceroot.cpp new file mode 100755 index 000000000..eb812c4d1 --- /dev/null +++ b/src/services/abstract/cacheforserviceroot.cpp @@ -0,0 +1,77 @@ +// This file is part of RSS Guard. +// +// Copyright (C) 2011-2017 by Martin Rotter +// +// RSS Guard is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// RSS Guard is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with RSS Guard. If not, see . + +#include "services/abstract/cacheforserviceroot.h" + +#include "miscellaneous/mutex.h" + +#include + + +CacheForServiceRoot::CacheForServiceRoot() : m_cacheSaveMutex(new Mutex(QMutex::NonRecursive, nullptr)), + m_cachedStatesRead(QMap()), + m_cachedStatesImportant(QMap()) { +} + +CacheForServiceRoot::~CacheForServiceRoot() { + m_cacheSaveMutex->deleteLater(); +} + +void CacheForServiceRoot::addMessageStatesToCache(const QStringList &ids_of_messages, RootItem::ReadStatus read) { + m_cacheSaveMutex->lock(); + + QStringList &list_act = m_cachedStatesRead[read]; + QStringList &list_other = m_cachedStatesRead[read == RootItem::Read ? RootItem::Unread : RootItem::Read]; + + // Store changes, they will be sent to server later. + list_act.append(ids_of_messages); + + QSet set_act = list_act.toSet(); + QSet set_other = list_other.toSet(); + + // Now, we want to remove all IDS from list_other, which are contained in list. + set_other -= set_act; + + list_act.clear(); list_act.append(set_act.toList()); + list_other.clear(); list_other.append(set_other.toList()); + + m_cacheSaveMutex->unlock(); +} + +QPair, QMap > CacheForServiceRoot::takeMessageCache() { + m_cacheSaveMutex->lock(); + + if (m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty()) { + // No cached changes. + m_cacheSaveMutex->unlock(); + return QPair, QMap >(); + } + + // Make copy of changes. + QMap cached_data_read = m_cachedStatesRead; + cached_data_read.detach(); + + QMap cached_data_imp = m_cachedStatesImportant; + cached_data_imp.detach(); + + m_cachedStatesRead.clear(); + m_cachedStatesImportant.clear(); + + m_cacheSaveMutex->unlock(); + + return QPair, QMap >(cached_data_read, cached_data_imp); +} diff --git a/src/services/abstract/cacheforserviceroot.h b/src/services/abstract/cacheforserviceroot.h new file mode 100755 index 000000000..67829fd54 --- /dev/null +++ b/src/services/abstract/cacheforserviceroot.h @@ -0,0 +1,45 @@ +// This file is part of RSS Guard. +// +// Copyright (C) 2011-2017 by Martin Rotter +// +// RSS Guard is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// RSS Guard is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with RSS Guard. If not, see . + +#ifndef CACHEFORSERVICEROOT_H +#define CACHEFORSERVICEROOT_H + +#include "services/abstract/serviceroot.h" + +#include +#include +#include + + +class Mutex; + +class CacheForServiceRoot { + public: + explicit CacheForServiceRoot(); + virtual ~CacheForServiceRoot(); + + void addMessageStatesToCache(const QStringList &ids_of_messages, RootItem::ReadStatus read); + + protected: + QPair, QMap> takeMessageCache(); + + Mutex *m_cacheSaveMutex; + QMap m_cachedStatesRead; + QMap m_cachedStatesImportant; +}; + +#endif // CACHEFORSERVICEROOT_H diff --git a/src/services/owncloud/owncloudrecyclebin.cpp b/src/services/owncloud/owncloudrecyclebin.cpp index 5e9cb715d..3926a3fbf 100755 --- a/src/services/owncloud/owncloudrecyclebin.cpp +++ b/src/services/owncloud/owncloudrecyclebin.cpp @@ -18,6 +18,7 @@ #include "services/owncloud/owncloudrecyclebin.h" #include "services/owncloud/owncloudserviceroot.h" +#include "services/abstract/cacheforserviceroot.h" #include "services/owncloud/network/owncloudnetworkfactory.h" #include diff --git a/src/services/owncloud/owncloudserviceroot.cpp b/src/services/owncloud/owncloudserviceroot.cpp index 68c0654e5..1a800b446 100755 --- a/src/services/owncloud/owncloudserviceroot.cpp +++ b/src/services/owncloud/owncloudserviceroot.cpp @@ -33,8 +33,7 @@ OwnCloudServiceRoot::OwnCloudServiceRoot(RootItem *parent) - : ServiceRoot(parent), m_cacheSaveMutex(new Mutex(QMutex::NonRecursive, this)), m_cachedStatesRead(QMap()), - m_cachedStatesImportant(QMap()), m_recycleBin(new OwnCloudRecycleBin(this)), + : ServiceRoot(parent), CacheForServiceRoot(), m_recycleBin(new OwnCloudRecycleBin(this)), m_actionSyncIn(nullptr), m_serviceMenu(QList()), m_network(new OwnCloudNetworkFactory()) { setIcon(OwnCloudServiceEntryPoint().icon()); } @@ -113,51 +112,11 @@ OwnCloudNetworkFactory *OwnCloudServiceRoot::network() const { return m_network; } -void OwnCloudServiceRoot::addMessageStatesToCache(const QStringList &ids_of_messages, RootItem::ReadStatus read) { - m_cacheSaveMutex->lock(); - - QStringList &list_act = m_cachedStatesRead[read]; - QStringList &list_other = m_cachedStatesRead[read == RootItem::Read ? RootItem::Unread : RootItem::Read]; - - // Store changes, they will be sent to server later. - list_act.append(ids_of_messages); - - QSet set_act = list_act.toSet(); - QSet set_other = list_other.toSet(); - - // Now, we want to remove all IDS from list_other, which are contained in list. - set_other -= set_act; - - list_act.clear(); list_act.append(set_act.toList()); - list_other.clear(); list_other.append(set_other.toList()); - - m_cacheSaveMutex->unlock(); -} - void OwnCloudServiceRoot::saveAllCachedData() { - m_cacheSaveMutex->lock(); + QPair, QMap> msgCache = takeMessageCache(); + QMapIterator i(msgCache.first); - if (m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty()) { - // No cached changes. - m_cacheSaveMutex->unlock(); - return; - } - - // Make copy of changes. - QMap cached_data_read = m_cachedStatesRead; - cached_data_read.detach(); - - QMap cached_data_imp = m_cachedStatesImportant; - cached_data_imp.detach(); - - m_cachedStatesRead.clear(); - m_cachedStatesImportant.clear(); - - m_cacheSaveMutex->unlock(); - - QMapIterator i(cached_data_read); - - // Save the actual data. + // Save the actual data read/unread. while (i.hasNext()) { i.next(); auto key = i.key(); @@ -169,7 +128,8 @@ void OwnCloudServiceRoot::saveAllCachedData() { } } -bool OwnCloudServiceRoot::onBeforeSetMessagesRead(RootItem *selected_item, const QList &messages, +bool OwnCloudServiceRoot::onBeforeSetMessagesRead(RootItem *selected_item, + const QList &messages, RootItem::ReadStatus read) { Q_UNUSED(selected_item) diff --git a/src/services/owncloud/owncloudserviceroot.h b/src/services/owncloud/owncloudserviceroot.h index a99fb4049..c3609b4e2 100755 --- a/src/services/owncloud/owncloudserviceroot.h +++ b/src/services/owncloud/owncloudserviceroot.h @@ -19,6 +19,7 @@ #define OWNCLOUDSERVICEROOT_H #include "services/abstract/serviceroot.h" +#include "services/abstract/cacheforserviceroot.h" #include @@ -27,11 +28,11 @@ class OwnCloudNetworkFactory; class OwnCloudRecycleBin; class Mutex; -class OwnCloudServiceRoot : public ServiceRoot { +class OwnCloudServiceRoot : public ServiceRoot, public CacheForServiceRoot { Q_OBJECT public: - explicit OwnCloudServiceRoot(RootItem *parent = NULL); + explicit OwnCloudServiceRoot(RootItem *parent = nullptr); virtual ~OwnCloudServiceRoot(); bool canBeEdited() const; @@ -48,14 +49,13 @@ class OwnCloudServiceRoot : public ServiceRoot { OwnCloudNetworkFactory *network() const; - void addMessageStatesToCache(const QStringList &ids_of_messages, ReadStatus read); - bool onBeforeSetMessagesRead(RootItem *selected_item, const QList &messages, ReadStatus read); bool onBeforeSwitchMessageImportance(RootItem *selected_item, const QList &changes); void updateTitle(); void saveAccountDataToDatabase(); + void saveAllCachedData(); public slots: @@ -63,10 +63,6 @@ class OwnCloudServiceRoot : public ServiceRoot { void addNewCategory(); private: - Mutex *m_cacheSaveMutex; - QMap m_cachedStatesRead; - QMap m_cachedStatesImportant; - QMap storeCustomFeedsData(); void restoreCustomFeedsData(const QMap &data, const QHash &feeds); RootItem *obtainNewTreeForSyncIn() const;