| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/cacheforserviceroot.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "services/abstract/label.h" |
| 7 | |
| 8 | #include <QDir> |
| 9 | #include <QSet> |
| 10 | |
| 11 | CacheForServiceRoot::CacheForServiceRoot() : m_uniqueId(NO_PARENT_CATEGORY), m_cacheSaveMutex(new QMutex()) {} |
| 12 | |
| 13 | CacheForServiceRoot::~CacheForServiceRoot() {} |
| 14 | |
| 15 | void CacheForServiceRoot::addLabelsAssignmentsToCache(const QStringList& ids_of_messages, |
| 16 | const QString& lbl_custom_id, |
| 17 | bool assign) { |
| 18 | if (ids_of_messages.isEmpty()) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (assign) { |
| 23 | for (const QString& custom_id : ids_of_messages) { |
| 24 | if (m_cachedLabelDeassignments[lbl_custom_id].contains(custom_id)) { |
| 25 | // We want to assign this ID but it was marked for deassignment, remove from deassignment. |
| 26 | m_cachedLabelDeassignments[lbl_custom_id].removeAll(custom_id); |
| 27 | } |
| 28 | else { |
| 29 | m_cachedLabelAssignments[lbl_custom_id].append(custom_id); |
| 30 | m_cachedLabelAssignments[lbl_custom_id].removeDuplicates(); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | else { |
| 35 | for (const QString& custom_id : ids_of_messages) { |
| 36 | if (m_cachedLabelAssignments[lbl_custom_id].contains(custom_id)) { |
| 37 | // We want to deassign this ID but it was marked for assignment, remove from assignment. |
| 38 | m_cachedLabelAssignments[lbl_custom_id].removeAll(custom_id); |
| 39 | } |
| 40 | else { |
| 41 | m_cachedLabelDeassignments[lbl_custom_id].append(custom_id); |
| 42 | m_cachedLabelDeassignments[lbl_custom_id].removeDuplicates(); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | saveCacheToFile(); |
| 48 | } |
| 49 | |
| 50 | void CacheForServiceRoot::addLabelsAssignmentsToCache(const QList<Message>& ids_of_messages, Label* lbl, bool assign) { |
| 51 | auto custom_ids = lbl->getParentServiceRoot()->customIDsOfMessages(ids_of_messages); |
| 52 | |
| 53 | addLabelsAssignmentsToCache(custom_ids, lbl->customId(), assign); |
| 54 | } |
| 55 | |
| 56 | void CacheForServiceRoot::addMessageStatesToCache(const QList<Message>& ids_of_messages, |
| 57 | RootItem::Importance importance) { |
| 58 | if (ids_of_messages.isEmpty()) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | QMutexLocker lck(m_cacheSaveMutex.data()); |
| 63 | QList<Message>& list_act = m_cachedStatesImportant[importance]; |
| 64 | QList<Message>& list_other = |
| 65 | m_cachedStatesImportant[importance == RootItem::Importance::Important ? RootItem::Importance::NotImportant |
| 66 | : RootItem::Importance::Important]; |
| 67 | |
| 68 | // Store changes, they will be sent to server later. |
| 69 | list_act.append(ids_of_messages); |
| 70 | |
| 71 | #if QT_VERSION >= 0x050E00 // Qt >= 5.14.0 |
| 72 | QSet<Message> set_act(list_act.begin(), list_act.end()); |
| 73 | QSet<Message> set_other(list_other.begin(), list_other.end()); |
| 74 | #else |
| 75 | QSet<Message> set_act = list_act.toSet(); |
| 76 | QSet<Message> set_other = list_other.toSet(); |
| 77 | #endif |
| 78 | |
| 79 | // Now, we want to remove all IDS from list_other, which are contained in list. |
| 80 | set_other -= set_act; |
| 81 | list_act.clear(); |
| 82 | list_act.append(set_act.values()); |
| 83 | list_other.clear(); |
| 84 | list_other.append(set_other.values()); |
| 85 | |
| 86 | saveCacheToFile(); |
| 87 | } |
| 88 | |
| 89 | void CacheForServiceRoot::addMessageStatesToCache(const QStringList& ids_of_messages, RootItem::ReadStatus read) { |
| 90 | if (ids_of_messages.isEmpty()) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | QMutexLocker lck(m_cacheSaveMutex.data()); |
| 95 | QStringList& list_act = m_cachedStatesRead[read]; |
| 96 | QStringList& list_other = |
| 97 | m_cachedStatesRead[read == RootItem::ReadStatus::Read ? RootItem::ReadStatus::Unread : RootItem::ReadStatus::Read]; |
| 98 | |
| 99 | // Store changes, they will be sent to server later. |
| 100 | list_act.append(ids_of_messages); |
| 101 | |
| 102 | #if QT_VERSION >= 0x050E00 // Qt >= 5.14.0 |
| 103 | QSet<QString> set_act(list_act.begin(), list_act.end()); |
| 104 | QSet<QString> set_other(list_other.begin(), list_other.end()); |
| 105 | #else |
| 106 | QSet<QString> set_act = list_act.toSet(); |
| 107 | QSet<QString> set_other = list_other.toSet(); |
| 108 | #endif |
| 109 | |
| 110 | // Now, we want to remove all IDS from list_other, which are contained in list. |
| 111 | set_other -= set_act; |
| 112 | list_act.clear(); |
| 113 | list_act.append(set_act.values()); |
| 114 | list_other.clear(); |
| 115 | list_other.append(set_other.values()); |
| 116 | |
| 117 | saveCacheToFile(); |
| 118 | } |
| 119 | |
| 120 | void CacheForServiceRoot::saveCacheToFile() { |
| 121 | // Save to file. |
| 122 | const QString file_cache = |
| 123 | qApp->userDataFolder() + QDir::separator() + QString::number(m_uniqueId) + "-cached-msgs.dat"; |
| 124 | |
| 125 | if (isEmpty()) { |
| 126 | QFile::remove(file_cache); |
| 127 | } |
| 128 | else { |
| 129 | QFile file(file_cache); |
| 130 | |
| 131 | if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
| 132 | QDataStream stream(&file); |
| 133 | |
| 134 | stream << m_cachedStatesImportant << m_cachedStatesRead << m_cachedLabelAssignments << m_cachedLabelDeassignments; |
| 135 | file.flush(); |
| 136 | file.close(); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void CacheForServiceRoot::clearCache() { |
| 142 | m_cachedStatesRead.clear(); |
| 143 | m_cachedStatesImportant.clear(); |
| 144 | m_cachedLabelAssignments.clear(); |
| 145 | m_cachedLabelDeassignments.clear(); |
| 146 | } |
| 147 | |
| 148 | void CacheForServiceRoot::loadCacheFromFile() { |
| 149 | QMutexLocker lck(m_cacheSaveMutex.data()); |
| 150 | |
| 151 | clearCache(); |
| 152 | |
| 153 | // Load from file. |
| 154 | const QString file_cache = |
| 155 | qApp->userDataFolder() + QDir::separator() + QString::number(m_uniqueId) + "-cached-msgs.dat"; |
| 156 | QFile file(file_cache); |
| 157 | |
| 158 | if (file.exists()) { |
| 159 | if (file.open(QIODevice::OpenModeFlag::ReadOnly)) { |
| 160 | QDataStream stream(&file); |
| 161 | |
| 162 | stream >> m_cachedStatesImportant >> m_cachedStatesRead >> m_cachedLabelAssignments >> m_cachedLabelDeassignments; |
| 163 | file.close(); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void CacheForServiceRoot::setUniqueId(int unique_id) { |
| 169 | m_uniqueId = unique_id; |
| 170 | } |
| 171 | |
| 172 | CacheSnapshot CacheForServiceRoot::takeMessageCache() { |
| 173 | QMutexLocker lck(m_cacheSaveMutex.data()); |
| 174 | |
| 175 | if (isEmpty()) { |
| 176 | return CacheSnapshot(); |
| 177 | } |
| 178 | |
| 179 | // Make copy of changes. |
| 180 | auto cached_data_read = m_cachedStatesRead; |
| 181 | auto cached_data_imp = m_cachedStatesImportant; |
| 182 | auto cached_ass_lbl = m_cachedLabelAssignments; |
| 183 | auto cached_deass_lbl = m_cachedLabelDeassignments; |
| 184 | |
| 185 | cached_data_read.detach(); |
| 186 | cached_data_imp.detach(); |
| 187 | cached_ass_lbl.detach(); |
| 188 | cached_deass_lbl.detach(); |
| 189 | |
| 190 | clearCache(); |
| 191 | saveCacheToFile(); |
| 192 | |
| 193 | CacheSnapshot c; |
| 194 | |
| 195 | c.m_cachedLabelAssignments = cached_ass_lbl; |
| 196 | c.m_cachedLabelDeassignments = cached_deass_lbl; |
| 197 | c.m_cachedStatesImportant = cached_data_imp; |
| 198 | c.m_cachedStatesRead = cached_data_read; |
| 199 | |
| 200 | return c; |
| 201 | } |
| 202 | |
| 203 | bool CacheForServiceRoot::isEmpty() const { |
| 204 | return m_cachedStatesRead.isEmpty() && m_cachedStatesImportant.isEmpty() && m_cachedLabelAssignments.isEmpty() && |
| 205 | m_cachedLabelDeassignments.isEmpty(); |
| 206 | } |