Separate cache functionality into own class.
This commit is contained in:
		
							parent
							
								
									803d7bc1a7
								
							
						
					
					
						commit
						34e0e709a3
					
				
					 6 changed files with 137 additions and 56 deletions
				
			
		|  | @ -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 \ | ||||
|  |  | |||
							
								
								
									
										77
									
								
								src/services/abstract/cacheforserviceroot.cpp
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										77
									
								
								src/services/abstract/cacheforserviceroot.cpp
									
										
									
									
									
										Executable file
									
								
							|  | @ -0,0 +1,77 @@ | |||
| // This file is part of RSS Guard.
 | ||||
| //
 | ||||
| // Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
 | ||||
| //
 | ||||
| // 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 <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| #include "services/abstract/cacheforserviceroot.h" | ||||
| 
 | ||||
| #include "miscellaneous/mutex.h" | ||||
| 
 | ||||
| #include <QSet> | ||||
| 
 | ||||
| 
 | ||||
| CacheForServiceRoot::CacheForServiceRoot() : m_cacheSaveMutex(new Mutex(QMutex::NonRecursive, nullptr)), | ||||
|   m_cachedStatesRead(QMap<RootItem::ReadStatus, QStringList>()), | ||||
|   m_cachedStatesImportant(QMap<RootItem::Importance, QStringList>()) { | ||||
| } | ||||
| 
 | ||||
| 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<QString> set_act = list_act.toSet(); | ||||
|   QSet<QString> 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<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QStringList> > CacheForServiceRoot::takeMessageCache() { | ||||
|   m_cacheSaveMutex->lock(); | ||||
| 
 | ||||
|   if (m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty()) { | ||||
|     // No cached changes.
 | ||||
|     m_cacheSaveMutex->unlock(); | ||||
|     return QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QStringList> >(); | ||||
|   } | ||||
| 
 | ||||
|   // Make copy of changes.
 | ||||
|   QMap<RootItem::ReadStatus, QStringList> cached_data_read = m_cachedStatesRead; | ||||
|   cached_data_read.detach(); | ||||
| 
 | ||||
|   QMap<RootItem::Importance, QStringList> cached_data_imp = m_cachedStatesImportant; | ||||
|   cached_data_imp.detach(); | ||||
| 
 | ||||
|   m_cachedStatesRead.clear(); | ||||
|   m_cachedStatesImportant.clear(); | ||||
| 
 | ||||
|   m_cacheSaveMutex->unlock(); | ||||
| 
 | ||||
|   return QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QStringList> >(cached_data_read, cached_data_imp); | ||||
| } | ||||
							
								
								
									
										45
									
								
								src/services/abstract/cacheforserviceroot.h
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										45
									
								
								src/services/abstract/cacheforserviceroot.h
									
										
									
									
									
										Executable file
									
								
							|  | @ -0,0 +1,45 @@ | |||
| // This file is part of RSS Guard.
 | ||||
| //
 | ||||
| // Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
 | ||||
| //
 | ||||
| // 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 <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| #ifndef CACHEFORSERVICEROOT_H | ||||
| #define CACHEFORSERVICEROOT_H | ||||
| 
 | ||||
| #include "services/abstract/serviceroot.h" | ||||
| 
 | ||||
| #include <QStringList> | ||||
| #include <QPair> | ||||
| #include <QMap> | ||||
| 
 | ||||
| 
 | ||||
| class Mutex; | ||||
| 
 | ||||
| class CacheForServiceRoot { | ||||
|   public: | ||||
|     explicit CacheForServiceRoot(); | ||||
|     virtual ~CacheForServiceRoot(); | ||||
| 
 | ||||
|     void addMessageStatesToCache(const QStringList &ids_of_messages, RootItem::ReadStatus read); | ||||
| 
 | ||||
|   protected: | ||||
|     QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QStringList>> takeMessageCache(); | ||||
| 
 | ||||
|     Mutex *m_cacheSaveMutex; | ||||
|     QMap<RootItem::ReadStatus, QStringList> m_cachedStatesRead; | ||||
|     QMap<RootItem::Importance, QStringList> m_cachedStatesImportant; | ||||
| }; | ||||
| 
 | ||||
| #endif // CACHEFORSERVICEROOT_H
 | ||||
|  | @ -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 <QNetworkReply> | ||||
|  |  | |||
|  | @ -33,8 +33,7 @@ | |||
| 
 | ||||
| 
 | ||||
| OwnCloudServiceRoot::OwnCloudServiceRoot(RootItem *parent) | ||||
|   : ServiceRoot(parent), m_cacheSaveMutex(new Mutex(QMutex::NonRecursive, this)), m_cachedStatesRead(QMap<RootItem::ReadStatus, QStringList>()), | ||||
|     m_cachedStatesImportant(QMap<RootItem::Importance, QStringList>()), m_recycleBin(new OwnCloudRecycleBin(this)), | ||||
|   : ServiceRoot(parent), CacheForServiceRoot(), m_recycleBin(new OwnCloudRecycleBin(this)), | ||||
|     m_actionSyncIn(nullptr), m_serviceMenu(QList<QAction*>()), 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<QString> set_act = list_act.toSet(); | ||||
|   QSet<QString> 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<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QStringList>> msgCache = takeMessageCache(); | ||||
|   QMapIterator<RootItem::ReadStatus, QStringList> i(msgCache.first); | ||||
| 
 | ||||
|   if (m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty()) { | ||||
|     // No cached changes.
 | ||||
|     m_cacheSaveMutex->unlock(); | ||||
|     return; | ||||
|   } | ||||
| 
 | ||||
|   // Make copy of changes.
 | ||||
|   QMap<RootItem::ReadStatus, QStringList> cached_data_read = m_cachedStatesRead; | ||||
|   cached_data_read.detach(); | ||||
| 
 | ||||
|   QMap<RootItem::Importance, QStringList> cached_data_imp = m_cachedStatesImportant; | ||||
|   cached_data_imp.detach(); | ||||
| 
 | ||||
|   m_cachedStatesRead.clear(); | ||||
|   m_cachedStatesImportant.clear(); | ||||
| 
 | ||||
|   m_cacheSaveMutex->unlock(); | ||||
| 
 | ||||
|   QMapIterator<RootItem::ReadStatus, QStringList> 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<Message> &messages, | ||||
| bool OwnCloudServiceRoot::onBeforeSetMessagesRead(RootItem *selected_item, | ||||
|                                                   const QList<Message> &messages, | ||||
|                                                   RootItem::ReadStatus read) { | ||||
|   Q_UNUSED(selected_item) | ||||
| 
 | ||||
|  |  | |||
|  | @ -19,6 +19,7 @@ | |||
| #define OWNCLOUDSERVICEROOT_H | ||||
| 
 | ||||
| #include "services/abstract/serviceroot.h" | ||||
| #include "services/abstract/cacheforserviceroot.h" | ||||
| 
 | ||||
| #include <QMap> | ||||
| 
 | ||||
|  | @ -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<Message> &messages, ReadStatus read); | ||||
|     bool onBeforeSwitchMessageImportance(RootItem *selected_item, const QList<ImportanceChange> &changes); | ||||
| 
 | ||||
|     void updateTitle(); | ||||
|     void saveAccountDataToDatabase(); | ||||
| 
 | ||||
| 
 | ||||
|     void saveAllCachedData(); | ||||
| 
 | ||||
|   public slots: | ||||
|  | @ -63,10 +63,6 @@ class OwnCloudServiceRoot : public ServiceRoot { | |||
|     void addNewCategory(); | ||||
| 
 | ||||
|   private: | ||||
|     Mutex *m_cacheSaveMutex; | ||||
|     QMap<RootItem::ReadStatus, QStringList> m_cachedStatesRead; | ||||
|     QMap<RootItem::Importance, QStringList> m_cachedStatesImportant; | ||||
| 
 | ||||
|     QMap<int,QVariant> storeCustomFeedsData(); | ||||
|     void restoreCustomFeedsData(const QMap<int,QVariant> &data, const QHash<int,Feed*> &feeds); | ||||
|     RootItem *obtainNewTreeForSyncIn() const; | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue