Auto status change to "Under Review"
@@ -1,394 +1,389 | |||||
1 | #include <Catalogue/CatalogueController.h> |
|
1 | #include <Catalogue/CatalogueController.h> | |
2 |
|
2 | |||
3 | #include <Variable/Variable.h> |
|
3 | #include <Variable/Variable.h> | |
4 |
|
4 | |||
5 | #include <CatalogueDao.h> |
|
5 | #include <CatalogueDao.h> | |
6 |
|
6 | |||
7 | #include <ComparaisonPredicate.h> |
|
7 | #include <ComparaisonPredicate.h> | |
8 | #include <CompoundPredicate.h> |
|
8 | #include <CompoundPredicate.h> | |
9 | #include <DBCatalogue.h> |
|
9 | #include <DBCatalogue.h> | |
10 | #include <DBEvent.h> |
|
10 | #include <DBEvent.h> | |
11 | #include <DBEventProduct.h> |
|
11 | #include <DBEventProduct.h> | |
12 | #include <DBTag.h> |
|
12 | #include <DBTag.h> | |
13 | #include <IRequestPredicate.h> |
|
13 | #include <IRequestPredicate.h> | |
14 |
|
14 | |||
15 | #include <QDataStream> |
|
15 | #include <QDataStream> | |
16 | #include <QMutex> |
|
16 | #include <QMutex> | |
17 | #include <QThread> |
|
17 | #include <QThread> | |
18 |
|
18 | |||
19 | #include <QDir> |
|
19 | #include <QDir> | |
20 | #include <QStandardPaths> |
|
20 | #include <QStandardPaths> | |
21 |
|
21 | |||
22 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") |
|
22 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") | |
23 |
|
23 | |||
24 | namespace { |
|
24 | namespace { | |
25 |
|
25 | |||
26 | static QString REPOSITORY_WORK_SUFFIX = QString{"_work"}; |
|
26 | static QString REPOSITORY_WORK_SUFFIX = QString{"_work"}; | |
27 | static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"}; |
|
27 | static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"}; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | class CatalogueController::CatalogueControllerPrivate { |
|
30 | class CatalogueController::CatalogueControllerPrivate { | |
31 |
|
31 | |||
32 | public: |
|
32 | public: | |
33 | explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {} |
|
33 | explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {} | |
34 |
|
34 | |||
35 | CatalogueDao m_CatalogueDao; |
|
35 | CatalogueDao m_CatalogueDao; | |
36 |
|
36 | |||
37 | QStringList m_RepositoryList; |
|
37 | QStringList m_RepositoryList; | |
38 | CatalogueController *m_Q; |
|
38 | CatalogueController *m_Q; | |
39 |
|
39 | |||
40 | QSet<QString> m_EventKeysWithChanges; |
|
40 | QSet<QString> m_EventKeysWithChanges; | |
41 |
|
41 | |||
42 | QString eventUniqueKey(const std::shared_ptr<DBEvent> &event) const; |
|
42 | QString eventUniqueKey(const std::shared_ptr<DBEvent> &event) const; | |
43 |
|
43 | |||
44 | void copyDBtoDB(const QString &dbFrom, const QString &dbTo); |
|
44 | void copyDBtoDB(const QString &dbFrom, const QString &dbTo); | |
45 | QString toWorkRepository(QString repository); |
|
45 | QString toWorkRepository(QString repository); | |
46 | QString toSyncRepository(QString repository); |
|
46 | QString toSyncRepository(QString repository); | |
47 | void savAllDB(); |
|
47 | void savAllDB(); | |
48 |
|
48 | |||
49 | void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true); |
|
49 | void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true); | |
50 | void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true); |
|
50 | void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true); | |
51 | }; |
|
51 | }; | |
52 |
|
52 | |||
53 | CatalogueController::CatalogueController(QObject *parent) |
|
53 | CatalogueController::CatalogueController(QObject *parent) | |
54 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)} |
|
54 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)} | |
55 | { |
|
55 | { | |
56 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") |
|
56 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") | |
57 | << QThread::currentThread(); |
|
57 | << QThread::currentThread(); | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | CatalogueController::~CatalogueController() |
|
60 | CatalogueController::~CatalogueController() | |
61 | { |
|
61 | { | |
62 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") |
|
62 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") | |
63 | << QThread::currentThread(); |
|
63 | << QThread::currentThread(); | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | QStringList CatalogueController::getRepositories() const |
|
66 | QStringList CatalogueController::getRepositories() const | |
67 | { |
|
67 | { | |
68 | return impl->m_RepositoryList; |
|
68 | return impl->m_RepositoryList; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | void CatalogueController::addDB(const QString &dbPath) |
|
71 | void CatalogueController::addDB(const QString &dbPath) | |
72 | { |
|
72 | { | |
73 | QDir dbDir(dbPath); |
|
73 | QDir dbDir(dbPath); | |
74 | if (dbDir.exists()) { |
|
74 | if (dbDir.exists()) { | |
75 | auto dirName = dbDir.dirName(); |
|
75 | auto dirName = dbDir.dirName(); | |
76 |
|
76 | |||
77 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) |
|
77 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) | |
78 | != impl->m_RepositoryList.cend()) { |
|
78 | != impl->m_RepositoryList.cend()) { | |
79 | qCCritical(LOG_CatalogueController()) |
|
79 | qCCritical(LOG_CatalogueController()) | |
80 | << tr("Impossible to addDB that is already loaded"); |
|
80 | << tr("Impossible to addDB that is already loaded"); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { |
|
83 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { | |
84 | qCCritical(LOG_CatalogueController()) |
|
84 | qCCritical(LOG_CatalogueController()) | |
85 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); |
|
85 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); | |
86 | } |
|
86 | } | |
87 | else { |
|
87 | else { | |
88 | impl->m_RepositoryList << dirName; |
|
88 | impl->m_RepositoryList << dirName; | |
89 | impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName)); |
|
89 | impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName)); | |
90 | } |
|
90 | } | |
91 | } |
|
91 | } | |
92 | else { |
|
92 | else { | |
93 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") |
|
93 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") | |
94 | << dbPath; |
|
94 | << dbPath; | |
95 | } |
|
95 | } | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) |
|
98 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) | |
99 | { |
|
99 | { | |
100 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { |
|
100 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { | |
101 | qCCritical(LOG_CatalogueController()) |
|
101 | qCCritical(LOG_CatalogueController()) | |
102 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); |
|
102 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); | |
103 | } |
|
103 | } | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | std::list<std::shared_ptr<DBEvent> > |
|
106 | std::list<std::shared_ptr<DBEvent> > | |
107 | CatalogueController::retrieveEvents(const QString &repository) const |
|
107 | CatalogueController::retrieveEvents(const QString &repository) const | |
108 | { |
|
108 | { | |
109 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; |
|
109 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; | |
110 |
|
110 | |||
111 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
111 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; | |
112 | auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName)); |
|
112 | auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName)); | |
113 | for (auto event : events) { |
|
113 | for (auto event : events) { | |
114 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
114 | eventsShared.push_back(std::make_shared<DBEvent>(event)); | |
115 | } |
|
115 | } | |
116 | return eventsShared; |
|
116 | return eventsShared; | |
117 | } |
|
117 | } | |
118 |
|
118 | |||
119 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const |
|
119 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const | |
120 | { |
|
120 | { | |
121 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
121 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; | |
122 | for (auto repository : impl->m_RepositoryList) { |
|
122 | for (auto repository : impl->m_RepositoryList) { | |
123 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); |
|
123 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | return eventsShared; |
|
126 | return eventsShared; | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | std::list<std::shared_ptr<DBEvent> > |
|
129 | std::list<std::shared_ptr<DBEvent> > | |
130 | CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const |
|
130 | CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const | |
131 | { |
|
131 | { | |
132 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
132 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; | |
133 | auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue); |
|
133 | auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue); | |
134 | for (auto event : events) { |
|
134 | for (auto event : events) { | |
135 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
135 | eventsShared.push_back(std::make_shared<DBEvent>(event)); | |
136 | } |
|
136 | } | |
137 | return eventsShared; |
|
137 | return eventsShared; | |
138 | } |
|
138 | } | |
139 |
|
139 | |||
140 | void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event) |
|
140 | void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event) | |
141 | { |
|
141 | { | |
142 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
142 | event->setRepository(impl->toWorkRepository(event->getRepository())); | |
143 |
|
143 | |||
144 | auto uniqueId = impl->eventUniqueKey(event); |
|
144 | auto uniqueId = impl->eventUniqueKey(event); | |
145 | impl->m_EventKeysWithChanges.insert(uniqueId); |
|
145 | impl->m_EventKeysWithChanges.insert(uniqueId); | |
146 |
|
146 | |||
147 | impl->m_CatalogueDao.updateEvent(*event); |
|
147 | impl->m_CatalogueDao.updateEvent(*event); | |
148 | } |
|
148 | } | |
149 |
|
149 | |||
150 | void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct) |
|
150 | void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct) | |
151 | { |
|
151 | { | |
152 | impl->m_CatalogueDao.updateEventProduct(*eventProduct); |
|
152 | impl->m_CatalogueDao.updateEventProduct(*eventProduct); | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event) |
|
155 | void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event) | |
156 | { |
|
156 | { | |
157 | // Remove it from both repository and repository_work |
|
157 | // Remove it from both repository and repository_work | |
158 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
158 | event->setRepository(impl->toWorkRepository(event->getRepository())); | |
159 | impl->m_CatalogueDao.removeEvent(*event); |
|
159 | impl->m_CatalogueDao.removeEvent(*event); | |
160 | event->setRepository(impl->toSyncRepository(event->getRepository())); |
|
160 | event->setRepository(impl->toSyncRepository(event->getRepository())); | |
161 | impl->m_CatalogueDao.removeEvent(*event); |
|
161 | impl->m_CatalogueDao.removeEvent(*event); | |
162 | impl->savAllDB(); |
|
162 | impl->savAllDB(); | |
163 | } |
|
163 | } | |
164 |
|
164 | |||
165 | void CatalogueController::addEvent(std::shared_ptr<DBEvent> event) |
|
165 | void CatalogueController::addEvent(std::shared_ptr<DBEvent> event) | |
166 | { |
|
166 | { | |
167 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
167 | event->setRepository(impl->toWorkRepository(event->getRepository())); | |
168 |
|
168 | |||
169 | auto eventTemp = *event; |
|
169 | auto eventTemp = *event; | |
170 | impl->m_CatalogueDao.addEvent(eventTemp); |
|
170 | impl->m_CatalogueDao.addEvent(eventTemp); | |
171 |
|
171 | |||
172 | // Call update is necessary at the creation of add Event if it has some tags or some event |
|
172 | // Call update is necessary at the creation of add Event if it has some tags or some event | |
173 | // products |
|
173 | // products | |
174 | if (!event->getEventProducts().empty() || !event->getTags().empty()) { |
|
174 | if (!event->getEventProducts().empty() || !event->getTags().empty()) { | |
175 |
|
175 | |||
176 | auto eventProductsTemp = eventTemp.getEventProducts(); |
|
176 | auto eventProductsTemp = eventTemp.getEventProducts(); | |
177 | auto eventProductTempUpdated = std::list<DBEventProduct>{}; |
|
177 | auto eventProductTempUpdated = std::list<DBEventProduct>{}; | |
178 | for (auto eventProductTemp : eventProductsTemp) { |
|
178 | for (auto eventProductTemp : eventProductsTemp) { | |
179 | eventProductTemp.setEvent(eventTemp); |
|
179 | eventProductTemp.setEvent(eventTemp); | |
180 | eventProductTempUpdated.push_back(eventProductTemp); |
|
180 | eventProductTempUpdated.push_back(eventProductTemp); | |
181 | } |
|
181 | } | |
182 | eventTemp.setEventProducts(eventProductTempUpdated); |
|
182 | eventTemp.setEventProducts(eventProductTempUpdated); | |
183 |
|
183 | |||
184 | impl->m_CatalogueDao.updateEvent(eventTemp); |
|
184 | impl->m_CatalogueDao.updateEvent(eventTemp); | |
185 | } |
|
185 | } | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event) |
|
188 | void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event) | |
189 | { |
|
189 | { | |
190 | impl->saveEvent(event, true); |
|
190 | impl->saveEvent(event, true); | |
191 | impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event)); |
|
191 | impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event)); | |
192 | } |
|
192 | } | |
193 |
|
193 | |||
194 | bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const |
|
194 | bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const | |
195 | { |
|
195 | { | |
196 | return impl->m_EventKeysWithChanges.contains(impl->eventUniqueKey(event)); |
|
196 | return impl->m_EventKeysWithChanges.contains(impl->eventUniqueKey(event)); | |
197 | } |
|
197 | } | |
198 |
|
198 | |||
199 | std::list<std::shared_ptr<DBCatalogue> > |
|
199 | std::list<std::shared_ptr<DBCatalogue> > | |
200 | CatalogueController::retrieveCatalogues(const QString &repository) const |
|
200 | CatalogueController::retrieveCatalogues(const QString &repository) const | |
201 | { |
|
201 | { | |
202 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; |
|
202 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; | |
203 |
|
203 | |||
204 | auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; |
|
204 | auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; | |
205 | auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName)); |
|
205 | auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName)); | |
206 | for (auto catalogue : catalogues) { |
|
206 | for (auto catalogue : catalogues) { | |
207 | cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue)); |
|
207 | cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue)); | |
208 | } |
|
208 | } | |
209 | return cataloguesShared; |
|
209 | return cataloguesShared; | |
210 | } |
|
210 | } | |
211 |
|
211 | |||
212 | void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
212 | void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue) | |
213 | { |
|
213 | { | |
214 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); |
|
214 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); | |
215 |
|
215 | |||
216 | impl->m_CatalogueDao.updateCatalogue(*catalogue); |
|
216 | impl->m_CatalogueDao.updateCatalogue(*catalogue); | |
217 | } |
|
217 | } | |
218 |
|
218 | |||
219 | void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
219 | void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue) | |
220 | { |
|
220 | { | |
221 | // Remove it from both repository and repository_work |
|
221 | // Remove it from both repository and repository_work | |
222 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); |
|
222 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); | |
223 | impl->m_CatalogueDao.removeCatalogue(*catalogue); |
|
223 | impl->m_CatalogueDao.removeCatalogue(*catalogue); | |
224 | catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository())); |
|
224 | catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository())); | |
225 | impl->m_CatalogueDao.removeCatalogue(*catalogue); |
|
225 | impl->m_CatalogueDao.removeCatalogue(*catalogue); | |
226 | } |
|
226 | } | |
227 |
|
227 | |||
228 | void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
228 | void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue) | |
229 | { |
|
229 | { | |
230 | impl->saveCatalogue(catalogue, true); |
|
230 | impl->saveCatalogue(catalogue, true); | |
231 | } |
|
231 | } | |
232 |
|
232 | |||
233 | void CatalogueController::saveAll() |
|
233 | void CatalogueController::saveAll() | |
234 | { |
|
234 | { | |
235 | for (auto repository : impl->m_RepositoryList) { |
|
235 | for (auto repository : impl->m_RepositoryList) { | |
236 | // Save Event |
|
236 | // Save Event | |
237 | auto events = this->retrieveEvents(repository); |
|
237 | auto events = this->retrieveEvents(repository); | |
238 | for (auto event : events) { |
|
238 | for (auto event : events) { | |
239 | impl->saveEvent(event, false); |
|
239 | impl->saveEvent(event, false); | |
240 | } |
|
240 | } | |
241 |
|
241 | |||
242 | // Save Catalogue |
|
242 | // Save Catalogue | |
243 | auto catalogues = this->retrieveCatalogues(repository); |
|
243 | auto catalogues = this->retrieveCatalogues(repository); | |
244 | for (auto catalogue : catalogues) { |
|
244 | for (auto catalogue : catalogues) { | |
245 | impl->saveCatalogue(catalogue, false); |
|
245 | impl->saveCatalogue(catalogue, false); | |
246 | } |
|
246 | } | |
247 | } |
|
247 | } | |
248 |
|
248 | |||
249 | impl->savAllDB(); |
|
249 | impl->savAllDB(); | |
250 | impl->m_EventKeysWithChanges.clear(); |
|
250 | impl->m_EventKeysWithChanges.clear(); | |
251 | } |
|
251 | } | |
252 |
|
252 | |||
253 | bool CatalogueController::hasChanges() const |
|
253 | bool CatalogueController::hasChanges() const | |
254 | { |
|
254 | { | |
255 | return !impl->m_EventKeysWithChanges.isEmpty(); // TODO: catalogues |
|
255 | return !impl->m_EventKeysWithChanges.isEmpty(); // TODO: catalogues | |
256 | } |
|
256 | } | |
257 |
|
257 | |||
258 | QByteArray |
|
258 | QByteArray | |
259 | CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const |
|
259 | CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const | |
260 | { |
|
260 | { | |
261 | auto encodedData = QByteArray{}; |
|
261 | auto encodedData = QByteArray{}; | |
262 |
|
262 | |||
263 | QMap<QString, QVariantList> idsPerRepository; |
|
263 | QMap<QString, QVariantList> idsPerRepository; | |
264 | for (auto event : events) { |
|
264 | for (auto event : events) { | |
265 | idsPerRepository[event->getRepository()] << event->getUniqId(); |
|
265 | idsPerRepository[event->getRepository()] << event->getUniqId(); | |
266 | } |
|
266 | } | |
267 |
|
267 | |||
268 | QDataStream stream{&encodedData, QIODevice::WriteOnly}; |
|
268 | QDataStream stream{&encodedData, QIODevice::WriteOnly}; | |
269 | stream << idsPerRepository; |
|
269 | stream << idsPerRepository; | |
270 |
|
270 | |||
271 | return encodedData; |
|
271 | return encodedData; | |
272 | } |
|
272 | } | |
273 |
|
273 | |||
274 | QVector<std::shared_ptr<DBEvent> > |
|
274 | QVector<std::shared_ptr<DBEvent> > | |
275 | CatalogueController::eventsForMimeData(const QByteArray &mimeData) const |
|
275 | CatalogueController::eventsForMimeData(const QByteArray &mimeData) const | |
276 | { |
|
276 | { | |
277 | auto events = QVector<std::shared_ptr<DBEvent> >{}; |
|
277 | auto events = QVector<std::shared_ptr<DBEvent> >{}; | |
278 | QDataStream stream{mimeData}; |
|
278 | QDataStream stream{mimeData}; | |
279 |
|
279 | |||
280 | QMap<QString, QVariantList> idsPerRepository; |
|
280 | QMap<QString, QVariantList> idsPerRepository; | |
281 | stream >> idsPerRepository; |
|
281 | stream >> idsPerRepository; | |
282 |
|
282 | |||
283 | for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) { |
|
283 | for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) { | |
284 | auto repository = it.key(); |
|
284 | auto repository = it.key(); | |
285 | auto allRepositoryEvent = retrieveEvents(repository); |
|
285 | auto allRepositoryEvent = retrieveEvents(repository); | |
286 | for (auto uuid : it.value()) { |
|
286 | for (auto uuid : it.value()) { | |
287 | for (auto repositoryEvent : allRepositoryEvent) { |
|
287 | for (auto repositoryEvent : allRepositoryEvent) { | |
288 | if (uuid.toUuid() == repositoryEvent->getUniqId()) { |
|
288 | if (uuid.toUuid() == repositoryEvent->getUniqId()) { | |
289 | events << repositoryEvent; |
|
289 | events << repositoryEvent; | |
290 | } |
|
290 | } | |
291 | } |
|
291 | } | |
292 | } |
|
292 | } | |
293 | } |
|
293 | } | |
294 |
|
294 | |||
295 | return events; |
|
295 | return events; | |
296 | } |
|
296 | } | |
297 |
|
297 | |||
298 | void CatalogueController::initialize() |
|
298 | void CatalogueController::initialize() | |
299 | { |
|
299 | { | |
300 | <<<<<<< HEAD |
|
|||
301 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") |
|
|||
302 | << QThread::currentThread(); |
|
|||
303 | impl->m_WorkingMutex.lock(); |
|
|||
304 | ======= |
|
|||
305 | qCDebug(LOG_CatalogueController()) |
|
300 | qCDebug(LOG_CatalogueController()) | |
306 | << tr("CatalogueController init") << QThread::currentThread(); |
|
301 | << tr("CatalogueController init") << QThread::currentThread(); | |
307 | >>>>>>> 286decc... unthread the catalogue controller |
|
302 | ||
308 | impl->m_CatalogueDao.initialize(); |
|
303 | impl->m_CatalogueDao.initialize(); | |
309 | auto defaultRepositoryLocation |
|
304 | auto defaultRepositoryLocation | |
310 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
|
305 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); | |
311 |
|
306 | |||
312 | QDir defaultRepositoryLocationDir; |
|
307 | QDir defaultRepositoryLocationDir; | |
313 | if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) { |
|
308 | if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) { | |
314 | defaultRepositoryLocationDir.cd(defaultRepositoryLocation); |
|
309 | defaultRepositoryLocationDir.cd(defaultRepositoryLocation); | |
315 | auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT); |
|
310 | auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT); | |
316 | qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") |
|
311 | qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") | |
317 | << defaultRepository; |
|
312 | << defaultRepository; | |
318 | this->addDB(defaultRepository); |
|
313 | this->addDB(defaultRepository); | |
319 | } |
|
314 | } | |
320 | else { |
|
315 | else { | |
321 | qCWarning(LOG_CatalogueController()) |
|
316 | qCWarning(LOG_CatalogueController()) | |
322 | << tr("Cannot load the persistent default repository from ") |
|
317 | << tr("Cannot load the persistent default repository from ") | |
323 | << defaultRepositoryLocation; |
|
318 | << defaultRepositoryLocation; | |
324 | } |
|
319 | } | |
325 |
|
320 | |||
326 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); |
|
321 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); | |
327 | } |
|
322 | } | |
328 |
|
323 | |||
329 | QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey( |
|
324 | QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey( | |
330 | const std::shared_ptr<DBEvent> &event) const |
|
325 | const std::shared_ptr<DBEvent> &event) const | |
331 | { |
|
326 | { | |
332 | return event->getUniqId().toString().append(event->getRepository()); |
|
327 | return event->getUniqId().toString().append(event->getRepository()); | |
333 | } |
|
328 | } | |
334 |
|
329 | |||
335 | void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom, |
|
330 | void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom, | |
336 | const QString &dbTo) |
|
331 | const QString &dbTo) | |
337 | { |
|
332 | { | |
338 | // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; |
|
333 | // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; | |
339 | auto catalogues = m_CatalogueDao.getCatalogues(dbFrom); |
|
334 | auto catalogues = m_CatalogueDao.getCatalogues(dbFrom); | |
340 | auto events = m_CatalogueDao.getEvents(dbFrom); |
|
335 | auto events = m_CatalogueDao.getEvents(dbFrom); | |
341 | for (auto catalogue : catalogues) { |
|
336 | for (auto catalogue : catalogues) { | |
342 | m_CatalogueDao.copyCatalogue(catalogue, dbTo, true); |
|
337 | m_CatalogueDao.copyCatalogue(catalogue, dbTo, true); | |
343 | } |
|
338 | } | |
344 |
|
339 | |||
345 | for (auto event : events) { |
|
340 | for (auto event : events) { | |
346 | m_CatalogueDao.copyEvent(event, dbTo, true); |
|
341 | m_CatalogueDao.copyEvent(event, dbTo, true); | |
347 | } |
|
342 | } | |
348 | } |
|
343 | } | |
349 |
|
344 | |||
350 | QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository) |
|
345 | QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository) | |
351 | { |
|
346 | { | |
352 | auto syncRepository = toSyncRepository(repository); |
|
347 | auto syncRepository = toSyncRepository(repository); | |
353 |
|
348 | |||
354 | return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX); |
|
349 | return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX); | |
355 | } |
|
350 | } | |
356 |
|
351 | |||
357 | QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository) |
|
352 | QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository) | |
358 | { |
|
353 | { | |
359 | auto syncRepository = repository; |
|
354 | auto syncRepository = repository; | |
360 | if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) { |
|
355 | if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) { | |
361 | syncRepository.remove(REPOSITORY_WORK_SUFFIX); |
|
356 | syncRepository.remove(REPOSITORY_WORK_SUFFIX); | |
362 | } |
|
357 | } | |
363 | else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) { |
|
358 | else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) { | |
364 | syncRepository.remove(REPOSITORY_TRASH_SUFFIX); |
|
359 | syncRepository.remove(REPOSITORY_TRASH_SUFFIX); | |
365 | } |
|
360 | } | |
366 | return syncRepository; |
|
361 | return syncRepository; | |
367 | } |
|
362 | } | |
368 |
|
363 | |||
369 | void CatalogueController::CatalogueControllerPrivate::savAllDB() |
|
364 | void CatalogueController::CatalogueControllerPrivate::savAllDB() | |
370 | { |
|
365 | { | |
371 | for (auto repository : m_RepositoryList) { |
|
366 | for (auto repository : m_RepositoryList) { | |
372 | auto defaultRepositoryLocation |
|
367 | auto defaultRepositoryLocation | |
373 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
|
368 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); | |
374 | m_CatalogueDao.saveDB(defaultRepositoryLocation, repository); |
|
369 | m_CatalogueDao.saveDB(defaultRepositoryLocation, repository); | |
375 | } |
|
370 | } | |
376 | } |
|
371 | } | |
377 |
|
372 | |||
378 | void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event, |
|
373 | void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event, | |
379 | bool persist) |
|
374 | bool persist) | |
380 | { |
|
375 | { | |
381 | m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true); |
|
376 | m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true); | |
382 | if (persist) { |
|
377 | if (persist) { | |
383 | savAllDB(); |
|
378 | savAllDB(); | |
384 | } |
|
379 | } | |
385 | } |
|
380 | } | |
386 |
|
381 | |||
387 | void CatalogueController::CatalogueControllerPrivate::saveCatalogue( |
|
382 | void CatalogueController::CatalogueControllerPrivate::saveCatalogue( | |
388 | std::shared_ptr<DBCatalogue> catalogue, bool persist) |
|
383 | std::shared_ptr<DBCatalogue> catalogue, bool persist) | |
389 | { |
|
384 | { | |
390 | m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true); |
|
385 | m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true); | |
391 | if (persist) { |
|
386 | if (persist) { | |
392 | savAllDB(); |
|
387 | savAllDB(); | |
393 | } |
|
388 | } | |
394 | } |
|
389 | } |
General Comments 3
Status change > Approved
You need to be logged in to leave comments.
Login now