| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef CACHEFORSERVICEROOT_H |
| 4 | #define CACHEFORSERVICEROOT_H |
| 5 | |
| 6 | #include "services/abstract/serviceroot.h" |
| 7 | |
| 8 | #include <QMap> |
| 9 | #include <QMutex> |
| 10 | #include <QPair> |
| 11 | #include <QStringList> |
| 12 | |
| 13 | struct RSSGUARD_DLLSPEC CacheSnapshot { |
| 14 | QMap<QString, QStringList> m_cachedLabelAssignments; |
| 15 | QMap<QString, QStringList> m_cachedLabelDeassignments; |
| 16 | QMap<RootItem::ReadStatus, QStringList> m_cachedStatesRead; |
| 17 | QMap<RootItem::Importance, QList<Message>> m_cachedStatesImportant; |
| 18 | }; |
| 19 | |
| 20 | class RSSGUARD_DLLSPEC CacheForServiceRoot { |
| 21 | public: |
| 22 | explicit CacheForServiceRoot(); |
| 23 | virtual ~CacheForServiceRoot(); |
| 24 | |
| 25 | virtual void saveAllCachedData(bool ignore_errors) = 0; |
| 26 | |
| 27 | void addLabelsAssignmentsToCache(const QStringList& ids_of_messages, const QString& lbl_custom_id, bool assign); |
| 28 | void addLabelsAssignmentsToCache(const QList<Message>& ids_of_messages, Label* lbl, bool assign); |
| 29 | void addMessageStatesToCache(const QList<Message>& ids_of_messages, RootItem::Importance importance); |
| 30 | void addMessageStatesToCache(const QStringList& ids_of_messages, RootItem::ReadStatus read); |
| 31 | |
| 32 | void loadCacheFromFile(); |
| 33 | void setUniqueId(int unique_id); |
| 34 | bool isEmpty() const; |
| 35 | |
| 36 | protected: |
| 37 | // Returns all cached data and clears the cache. |
| 38 | // NOTE: If returned data are not successfuly passed back to |
| 39 | // server then caller needs to re-add the data back to cache. |
| 40 | CacheSnapshot takeMessageCache(); |
| 41 | |
| 42 | private: |
| 43 | void clearCache(); |
| 44 | void saveCacheToFile(); |
| 45 | |
| 46 | int m_uniqueId; |
| 47 | QScopedPointer<QMutex> m_cacheSaveMutex; |
| 48 | |
| 49 | // Map where key is label's custom ID and value is list of message custom IDs |
| 50 | // which we want to assign to the label. |
| 51 | QMap<QString, QStringList> m_cachedLabelAssignments; |
| 52 | |
| 53 | // Map where key is label's custom ID and value is list of message custom IDs |
| 54 | // which we want to remove from the label assignment. |
| 55 | QMap<QString, QStringList> m_cachedLabelDeassignments; |
| 56 | |
| 57 | // Map of cached read/unread changes. |
| 58 | QMap<RootItem::ReadStatus, QStringList> m_cachedStatesRead; |
| 59 | |
| 60 | // Map of cached important/unimportant changes. |
| 61 | QMap<RootItem::Importance, QList<Message>> m_cachedStatesImportant; |
| 62 | }; |
| 63 | |
| 64 | #endif // CACHEFORSERVICEROOT_H |