##// END OF EJS Templates
Several additions for Catalogue GUI...
jeandet -
r55:6b6bb3a15bf8
parent child
Show More
@@ -1,142 +1,148
1 1 #ifndef SCIQLOP_CATALOGUECONTROLLER_H
2 2 #define SCIQLOP_CATALOGUECONTROLLER_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Common/spimpl.h>
7 7 #include <Data/DateTimeRange.h>
8 8 #include <QLoggingCategory>
9 9 #include <QObject>
10 10 #include <QUuid>
11 11 #include <Repository.hpp>
12 12 #include <map>
13 13 #include <memory>
14 14
15 15 class DBCatalogue;
16 16 class DBEvent;
17 17 class DBEventProduct;
18 18
19 19 Q_DECLARE_LOGGING_CATEGORY(LOG_CatalogueController)
20 20
21 21 class DataSourceItem;
22 22 class Variable;
23 23
24 24 constexpr auto default_repo = "default";
25 25
26 26 /**
27 27 * @brief The CatalogueController class aims to handle catalogues and event
28 28 * using the CatalogueAPI library.
29 29 */
30 30 class SCIQLOP_CORE_EXPORT CatalogueController : public QObject
31 31 {
32 32 Q_OBJECT
33 33 public:
34 34 using time_t = double;
35 35 using Product_t = CatalogiCpp::Product<time_t>;
36 36 using Repository_t = CatalogiCpp::Repository<time_t>;
37 37 using Event_t = Repository_t::Event_t;
38 38 using Event_ptr = Repository_t::Event_ptr;
39 39 using Catalogue_t = Repository_t::Catalogue_t;
40 40 using Catalogue_ptr = typename Repository_t::Catalogue_ptr;
41 41 using uuid_t = Repository_t::uuid_t;
42 42 template<typename... _Args>
43 43 static inline Catalogue_ptr make_catalogue_ptr(_Args&&... __args)
44 44 {
45 45 return Repository_t::make_catalogue_ptr(std::forward<_Args>(__args)...);
46 46 }
47 47
48 48 template<typename... _Args>
49 49 static inline Event_ptr make_event_ptr(_Args&&... __args)
50 50 {
51 51 return Catalogue_t::make_event_ptr(std::forward<_Args>(__args)...);
52 52 }
53 53
54 54 explicit CatalogueController(QObject* parent = nullptr);
55 55 virtual ~CatalogueController();
56 56
57 57 QStringList repositories() const;
58 58 void loadRepository(const QString& path, const QString& name);
59 59 void saveRepository(const QString& path, const QString& name);
60 60
61 61 std::vector<Event_ptr> events();
62 62 std::vector<Event_ptr> events(const QString& repository);
63 std::vector<Event_ptr> events(const Catalogue_ptr& catalogue);
63 64
64 65 std::vector<Catalogue_ptr> catalogues();
65 66 std::vector<Catalogue_ptr> catalogues(const QString& repository);
66 67
67 68 bool hasUnsavedChanges(Event_ptr event);
68 69
69 70 std::optional<QString> repository(Event_ptr event);
70 71 std::optional<QString> repository(Catalogue_ptr catalogue);
71 72
72 73 void save(Event_ptr event);
73 74 void save(Catalogue_ptr catalogue);
74 75 void save(const QString& repository);
75 76
76 77 void add(const QString& repository);
77 void add(const QString& catalogue, const QString& repository);
78 Catalogue_ptr add(const QString& catalogue, const QString& repository);
78 79 void add(Event_ptr event, Catalogue_ptr catalogue);
79 80 void add(Event_ptr event, const QString& repository = default_repo);
80 81
82 signals:
83 void repositoryAdded(const QString& repository);
84 void catalogueAdded(const Catalogue_ptr& catalogue, const QString& repository);
85 void catalogueChanged(const Catalogue_ptr& catalogue);
86
81 87 // // Event
82 88 // /// retrieveEvents with empty repository retrieve them from the default
83 89 // repository std::list<std::shared_ptr<DBEvent>> retrieveEvents(const
84 90 // QString& repository) const; std::list<std::shared_ptr<DBEvent>>
85 91 // retrieveAllEvents() const;
86 92
87 93 // void addEvent(std::shared_ptr<DBEvent> event);
88 94 // void updateEvent(std::shared_ptr<DBEvent> event);
89 95 // void updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct);
90 96 // void removeEvent(std::shared_ptr<DBEvent> event);
91 97 // // void trashEvent(std::shared_ptr<DBEvent> event);
92 98 // // void restore(std::shared_ptr<DBEvent> event);
93 99 // void saveEvent(std::shared_ptr<DBEvent> event);
94 100 // void discardEvent(std::shared_ptr<DBEvent> event, bool& removed);
95 101 // bool eventHasChanges(std::shared_ptr<DBEvent> event) const;
96 102
97 103 // // Catalogue
98 104 // std::list<std::shared_ptr<DBEvent>> retrieveEventsFromCatalogue(
99 105 // std::shared_ptr<DBCatalogue> catalogue) const;
100 106
101 107 // /// retrieveEvents with empty repository retrieve them from the default
102 108 // repository std::list<std::shared_ptr<DBCatalogue>> retrieveCatalogues(
103 109 // const QString& repository = QString()) const;
104 110 // void addCatalogue(std::shared_ptr<DBCatalogue> catalogue);
105 111 // void updateCatalogue(std::shared_ptr<DBCatalogue> catalogue);
106 112 // void removeCatalogue(std::shared_ptr<DBCatalogue> catalogue);
107 113 // void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue);
108 114 // void discardCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool&
109 115 // removed);
110 116
111 117 // void saveAll();
112 118 // bool hasChanges() const;
113 119
114 120 // /// Returns the MIME data associated to a list of variables
115 121 // QByteArray mimeDataForEvents(const QVector<std::shared_ptr<DBEvent>>&
116 122 // events) const;
117 123
118 124 // /// Returns the list of variables contained in a MIME data
119 125 // QVector<std::shared_ptr<DBEvent>> eventsForMimeData(const QByteArray&
120 126 // mimeData) const;
121 127
122 128 // /// Returns the MIME data associated to a list of variables
123 129 // QByteArray mimeDataForCatalogues(const
124 130 // QVector<std::shared_ptr<DBCatalogue>>& catalogues) const;
125 131
126 132 // /// Returns the list of variables contained in a MIME data
127 133 // QVector<std::shared_ptr<DBCatalogue>> cataloguesForMimeData(const
128 134 // QByteArray& mimeData) const;
129 135
130 136 // public slots:
131 137 // /// Manage init/end of the controller
132 138 // void initialize();
133 139
134 140 private:
135 141 std::map<QString, CatalogiCpp::Repository<time_t>> _lastSavedRepos;
136 142 std::map<QString, CatalogiCpp::Repository<time_t>> _currentRepos;
137 143
138 144 // class CatalogueControllerPrivate;
139 145 // spimpl::unique_impl_ptr<CatalogueControllerPrivate> impl;
140 146 };
141 147
142 148 #endif // SCIQLOP_CATALOGUECONTROLLER_H
@@ -1,35 +1,90
1 1 #ifndef SCIQLOP_STRINGUTILS_H
2 2 #define SCIQLOP_STRINGUTILS_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 #include <QRegExp>
7 #include <QString>
8 #include <QStringList>
9 #include <functional>
10 #include <set>
6 11 #include <vector>
7 12
8 class QString;
9
10 13 /**
11 14 * Utility class with methods for strings
12 15 */
13 struct SCIQLOP_CORE_EXPORT StringUtils {
14 /**
15 * Generates a unique name from a default name and a set of forbidden names.
16 *
17 * Generating the unique name is done by adding an index to the default name and stopping at the
18 * first index for which the generated name is not in the forbidden names.
19 *
20 * Examples (defaultName, forbiddenNames -> result):
21 * - "FGM", {"FGM"} -> "FGM1"
22 * - "FGM", {"ABC"} -> "FGM"
23 * - "FGM", {"FGM", "FGM1"} -> "FGM2"
24 * - "FGM", {"FGM", "FGM2"} -> "FGM1"
25 * - "", {"ABC"} -> "1"
26 *
27 * @param defaultName the default name
28 * @param forbiddenNames the set of forbidden names
29 * @return the unique name generated
30 */
31 static QString uniqueName(const QString &defaultName,
32 const std::vector<QString> &forbiddenNames) noexcept;
33 };
16 namespace StringUtils
17 {
18 /**
19 * Generates a unique name from a default name and a set of forbidden names.
20 *
21 * Generating the unique name is done by adding an index to the default name
22 * and stopping at the first index for which the generated name is not in the
23 * forbidden names.
24 *
25 * Examples (defaultName, forbiddenNames -> result):
26 * - "FGM", {"FGM"} -> "FGM1"
27 * - "FGM", {"ABC"} -> "FGM"
28 * - "FGM", {"FGM", "FGM1"} -> "FGM2"
29 * - "FGM", {"FGM", "FGM2"} -> "FGM1"
30 * - "", {"ABC"} -> "1"
31 *
32 * @param defaultName the default name
33 * @param forbiddenNames the set of forbidden names
34 * @return the unique name generated
35 */
36 static QString uniqueName(const QString& defaultName,
37 const std::vector<QString>& forbiddenNames) noexcept
38 {
39 // Gets the base of the unique name to generate, by removing trailing number
40 // (for example, base name of "FGM12" is "FGM")
41 auto baseName = defaultName;
42 baseName.remove(QRegExp{QStringLiteral("\\d*$")});
43
44 // Finds the unique name by adding an index to the base name and stops when
45 // the generated name isn't forbidden
46 QString newName{};
47 auto forbidden = true;
48 for(auto i = 0; forbidden; ++i)
49 {
50 newName = (i == 0) ? baseName : baseName + QString::number(i);
51 forbidden =
52 newName.isEmpty() ||
53 std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(),
54 [&newName](const auto& name) {
55 return name.compare(newName, Qt::CaseInsensitive) == 0;
56 });
57 }
58
59 return newName;
60 }
61
62 template<typename container>
63 QString join(const container& input, const char* sep)
64 {
65 QStringList list;
66 if constexpr(std::is_same_v<typename container::value_type, std::string>)
67 {
68 std::transform(
69 std::cbegin(input), std::cend(input), std::back_inserter(list),
70 [](const auto& item) { return QString::fromStdString(item); });
71 }
72 else if constexpr(std::is_same_v<typename container::value_type, QString>)
73 {
74 std::copy(std::cbegin(input), std::cend(input), std::back_inserter(list));
75 }
76 return list.join(sep);
77 }
78
79 template<typename container>
80 QString join(const container& input, const char* sep,
81 std::function<QString(typename container::value_type)> op)
82 {
83 QStringList list;
84 std::transform(std::cbegin(input), std::cend(input),
85 std::back_inserter(list), op);
86 return list.join(sep);
87 }
88 } // namespace StringUtils
34 89
35 90 #endif // SCIQLOP_STRINGUTILS_H
@@ -1,780 +1,800
1 1 #include <Catalogue/CatalogueController.h>
2 2 #include <CatalogueIO.hpp>
3 3 #include <Common/containers.h>
4 4 #include <Common/debug.h>
5 5 #include <QDataStream>
6 6 #include <QDir>
7 7 #include <QMutex>
8 8 #include <QStandardPaths>
9 9 #include <QThread>
10 10
11 11 using namespace SciQLop::containers;
12 12
13 13 // class CatalogueController::CatalogueControllerPrivate
14 14 //{
15 15
16 16 // public:
17 17 // explicit CatalogueControllerPrivate(CatalogueController* parent) : m_Q {
18 18 // parent } {}
19 19
20 20 // CatalogueDao m_CatalogueDao;
21 21
22 22 // QStringList m_RepositoryList;
23 23 // CatalogueController* m_Q;
24 24
25 25 // QSet<QString> m_KeysWithChanges;
26 26
27 27 // QString eventUniqueKey(const std::shared_ptr<DBEvent>& event) const;
28 28 // QString catalogueUniqueKey(const std::shared_ptr<DBCatalogue>& catalogue)
29 29 // const;
30 30
31 31 // void copyDBtoDB(const QString& dbFrom, const QString& dbTo);
32 32 // QString toWorkRepository(QString repository);
33 33 // QString toSyncRepository(QString repository);
34 34 // void savAllDB();
35 35
36 36 // void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true);
37 37 // void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist =
38 38 // true);
39 39
40 40 // std::shared_ptr<IRequestPredicate> createFinder(
41 41 // const QUuid& uniqId, const QString& repository, DBType type);
42 42 //};
43 43
44 44 CatalogueController::CatalogueController(QObject* parent)
45 45 //: impl { spimpl::make_unique_impl<CatalogueControllerPrivate>(this) }
46 46 {}
47 47
48 48 CatalogueController::~CatalogueController() {}
49 49
50 50 QStringList CatalogueController::repositories() const
51 51 {
52 52 QStringList repos;
53 53 std::transform(std::begin(_currentRepos), std::end(_currentRepos),
54 54 std::back_inserter(repos),
55 55 [](auto& pair) { return pair.first; });
56 56 return repos;
57 57 }
58 58
59 59 void CatalogueController::loadRepository(const QString& path,
60 60 const QString& name)
61 61 {
62 62 if(QFile::exists(path))
63 63 {
64 64 auto repo = CatalogiCpp::load_repository<time_t>(path.toStdString());
65 65 _lastSavedRepos[name] = repo;
66 66 _currentRepos[name] = repo;
67 67 }
68 68 }
69 69
70 70 void CatalogueController::saveRepository(const QString& path,
71 71 const QString& name)
72 72 {
73 73 if(contains(_currentRepos, name))
74 74 {
75 75 CatalogiCpp::save_repository(_currentRepos[name], path.toStdString());
76 76 _lastSavedRepos[name] = _currentRepos[name];
77 77 }
78 78 }
79 79
80 80 std::vector<CatalogueController::Event_ptr> CatalogueController::events()
81 81 {
82 82 std::vector<CatalogueController::Event_ptr> e_list;
83 83 for(auto& [_, repo] : _currentRepos)
84 84 {
85 85 for(auto& [_, event] : repo.events())
86 86 e_list.push_back(event);
87 87 }
88 88 return e_list;
89 89 }
90 90
91 std::vector<CatalogueController::Event_ptr>
92 CatalogueController::events(const CatalogueController::Catalogue_ptr& catalogue)
93 {
94 std::vector<CatalogueController::Event_ptr> e_list;
95 for(auto& [_, event] : catalogue->events())
96 e_list.push_back(event);
97 return e_list;
98 }
99
91 100 std::vector<std::shared_ptr<CatalogueController::Event_t>>
92 101 CatalogueController::events(const QString& repository)
93 102 {
94 103 std::vector<std::shared_ptr<CatalogueController::Event_t>> e_list;
95 104 if(contains(_currentRepos, repository))
96 105 {
97 106 auto repo = _currentRepos[repository];
98 107 for(auto& [_, event] : repo.events())
99 108 e_list.push_back(event);
100 109 }
101 110 return e_list;
102 111 }
103 112
104 113 std::vector<CatalogueController::Catalogue_ptr>
105 114 CatalogueController::catalogues()
106 115 {
107 116 std::vector<CatalogueController::Catalogue_ptr> c_list;
108 117 for(auto& [_, repo] : _currentRepos)
109 118 {
110 119 for(auto& [_, catalogue] : repo.catalogues())
111 120 c_list.push_back(catalogue);
112 121 }
113 122 return c_list;
114 123 }
115 124
116 125 std::vector<CatalogueController::Catalogue_ptr>
117 126 CatalogueController::catalogues(const QString& repository)
118 127 {
119 128 std::vector<CatalogueController::Catalogue_ptr> c_list;
120 129 if(contains(_currentRepos, repository))
121 130 {
122 131 auto repo = _currentRepos[repository];
123 132 for(auto& [_, catalogue] : repo.catalogues())
124 133 c_list.push_back(catalogue);
125 134 }
126 135 return c_list;
127 136 }
128 137
129 138 bool CatalogueController::hasUnsavedChanges(
130 139 CatalogueController::Event_ptr event)
131 140 {
132 141 if(auto repo = repository(event))
133 142 {
134 143 if(contains(_lastSavedRepos, *repo))
135 144 { return *event != *(_lastSavedRepos[*repo].event(event->uuid)); }
136 145 }
137 146 return true;
138 147 }
139 148
140 149 std::optional<QString>
141 150 CatalogueController::repository(CatalogueController::Event_ptr event)
142 151 {
143 152 for(auto& [repoName, repo] : _currentRepos)
144 153 {
145 154 if(repo.event(event->uuid)) return repoName;
146 155 }
147 156 return std::nullopt;
148 157 }
149 158
150 159 std::optional<QString>
151 160 CatalogueController::repository(CatalogueController::Catalogue_ptr catalogue)
152 161 {
153 162 for(auto& [repoName, repo] : _currentRepos)
154 163 {
155 164 if(repo.catalogue(catalogue->uuid)) return repoName;
156 165 }
157 166 return std::nullopt;
158 167 }
159 168
160 169 void CatalogueController::save(CatalogueController::Event_ptr event)
161 170 {
162 171 auto repo_name = repository(event);
163 172 if(repo_name && contains(_lastSavedRepos, *repo_name))
164 173 {
165 174 auto repo = _lastSavedRepos[*repo_name];
166 175 if(contains(repo.events(), event->uuid))
167 176 {
168 177 auto saved_evemt = _lastSavedRepos[*repo_name].event(event->uuid);
169 178 *saved_evemt = *event;
170 179 }
171 180 else
172 181 {
173 182 // TODO/Question should we also take care of which catalogue has it?
174 183 // if an event is created and added to a catalogue, what should we do if
175 184 // the user clicks the event save button?
176 185 // - Only save event value in all events list?
177 186 // - Also save the fact that this event is added to a catalogue and save
178 187 // the catalogue?
179 188 repo.add(event);
180 189 }
181 190 }
182 191 }
183 192
184 193 void CatalogueController::save(CatalogueController::Catalogue_ptr catalogue)
185 194 {
186 195 auto repo_name = repository(catalogue);
187 196 if(repo_name && contains(_lastSavedRepos, *repo_name))
188 197 {
189 198 auto repo = _lastSavedRepos[*repo_name];
190 199 if(contains(repo.catalogues(), catalogue->uuid))
191 200 {
192 201 auto saved_catalogue = repo.catalogue(catalogue->uuid);
193 202 *saved_catalogue = *catalogue;
194 203 }
195 204 else
196 205 {
197 206 repo.add(catalogue);
198 207 }
199 208 }
200 209 }
201 210
202 211 void CatalogueController::save(const QString& repository)
203 212 {
204 213 if(contains(_currentRepos, repository))
205 214 { _lastSavedRepos[repository] = _currentRepos[repository]; }
206 215 else
207 216 {
208 217 SCIQLOP_ERROR(CatalogueController, "Trying to save an unknown repository");
209 218 }
210 219 }
211 220
212 221 void CatalogueController::add(const QString& repository)
213 222 {
214 223 if(!contains(_currentRepos, repository))
215 { _currentRepos[repository] = Repository_t{}; }
224 {
225 _currentRepos[repository] = Repository_t{};
226 emit repositoryAdded(repository);
227 }
216 228 }
217 229
218 void CatalogueController::add(const QString& catalogue,
219 const QString& repository)
230 CatalogueController::Catalogue_ptr
231 CatalogueController::add(const QString& catalogue, const QString& repository)
220 232 {
221 if(!contains(_currentRepos, repository))
222 { _currentRepos[repository] = Repository_t{}; }
233 if(!contains(_currentRepos, repository)) { add(repository); }
223 234 auto new_catalogue = make_catalogue_ptr();
224 235 new_catalogue->name = catalogue.toStdString();
225 _currentRepos[repository].add(std::move(new_catalogue));
236 _currentRepos[repository].add(new_catalogue);
237 emit catalogueAdded(new_catalogue, repository);
238 return new_catalogue;
239 }
240
241 void CatalogueController::add(CatalogueController::Event_ptr event,
242 CatalogueController::Catalogue_ptr catalogue)
243 {
244 catalogue->add(event);
245 emit this->catalogueChanged(catalogue);
226 246 }
227 247
228 248 void CatalogueController::add(CatalogueController::Event_ptr event,
229 249 const QString& repository)
230 250 {}
231 251
232 252 // void CatalogueController::saveDB(const QString& destinationPath, const
233 253 // QString& repositoryName)
234 254 //{
235 255 // if (!impl->m_CatalogueDao.saveDB(destinationPath, repositoryName))
236 256 // {
237 257 // qCCritical(LOG_CatalogueController())
238 258 // << tr("Impossible to saveDB %1 from %2 ").arg(repositoryName,
239 259 // destinationPath);
240 260 // }
241 261 //}
242 262
243 263 // std::list<std::shared_ptr<DBEvent>> CatalogueController::retrieveEvents(
244 264 // const QString& repository) const
245 265 //{
246 266 // QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT :
247 267 // repository;
248 268
249 269 // auto eventsShared = std::list<std::shared_ptr<DBEvent>> {};
250 270 // auto events =
251 271 // impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName)); for
252 272 // (auto event : events)
253 273 // {
254 274 // eventsShared.push_back(std::make_shared<DBEvent>(event));
255 275 // }
256 276 // return eventsShared;
257 277 //}
258 278
259 279 // std::list<std::shared_ptr<DBEvent>> CatalogueController::retrieveAllEvents()
260 280 // const
261 281 //{
262 282 // auto eventsShared = std::list<std::shared_ptr<DBEvent>> {};
263 283 // for (auto repository : impl->m_RepositoryList)
264 284 // {
265 285 // eventsShared.splice(eventsShared.end(), retrieveEvents(repository));
266 286 // }
267 287
268 288 // return eventsShared;
269 289 //}
270 290
271 291 // std::list<std::shared_ptr<DBEvent>>
272 292 // CatalogueController::retrieveEventsFromCatalogue(
273 293 // std::shared_ptr<DBCatalogue> catalogue) const
274 294 //{
275 295 // auto eventsShared = std::list<std::shared_ptr<DBEvent>> {};
276 296 // auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue);
277 297 // for (auto event : events)
278 298 // {
279 299 // eventsShared.push_back(std::make_shared<DBEvent>(event));
280 300 // }
281 301 // return eventsShared;
282 302 //}
283 303
284 304 // void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event)
285 305 //{
286 306 // event->setRepository(impl->toWorkRepository(event->getRepository()));
287 307
288 308 // auto uniqueId = impl->eventUniqueKey(event);
289 309 // impl->m_KeysWithChanges.insert(uniqueId);
290 310
291 311 // impl->m_CatalogueDao.updateEvent(*event);
292 312 //}
293 313
294 314 // void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct>
295 315 // eventProduct)
296 316 //{
297 317 // impl->m_CatalogueDao.updateEventProduct(*eventProduct);
298 318 //}
299 319
300 320 // void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event)
301 321 //{
302 322 // // Remove it from both repository and repository_work
303 323 // event->setRepository(impl->toWorkRepository(event->getRepository()));
304 324 // impl->m_CatalogueDao.removeEvent(*event);
305 325 // event->setRepository(impl->toSyncRepository(event->getRepository()));
306 326 // impl->m_CatalogueDao.removeEvent(*event);
307 327 // impl->savAllDB();
308 328 //}
309 329
310 330 // void CatalogueController::addEvent(std::shared_ptr<DBEvent> event)
311 331 //{
312 332 // event->setRepository(impl->toWorkRepository(event->getRepository()));
313 333
314 334 // auto eventTemp = *event;
315 335 // impl->m_CatalogueDao.addEvent(eventTemp);
316 336
317 337 // // Call update is necessary at the creation of add Event if it has some
318 338 // tags or some event
319 339 // // products
320 340 // if (!event->getEventProducts().empty() || !event->getTags().empty())
321 341 // {
322 342
323 343 // auto eventProductsTemp = eventTemp.getEventProducts();
324 344 // auto eventProductTempUpdated = std::list<DBEventProduct> {};
325 345 // for (auto eventProductTemp : eventProductsTemp)
326 346 // {
327 347 // eventProductTemp.setEvent(eventTemp);
328 348 // eventProductTempUpdated.push_back(eventProductTemp);
329 349 // }
330 350 // eventTemp.setEventProducts(eventProductTempUpdated);
331 351
332 352 // impl->m_CatalogueDao.updateEvent(eventTemp);
333 353 // }
334 354
335 355 // auto workPred = impl->createFinder(event->getUniqId(),
336 356 // event->getRepository(), DBType::WORK);
337 357
338 358 // auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
339 359 // *event = workEvent;
340 360
341 361 // auto uniqueId = impl->eventUniqueKey(event);
342 362 // impl->m_KeysWithChanges.insert(uniqueId);
343 363 //}
344 364
345 365 // void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
346 366 //{
347 367 // impl->saveEvent(event, true);
348 368 // impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
349 369 //}
350 370
351 371 // void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool&
352 372 // removed)
353 373 //{
354 374 // auto syncPred = impl->createFinder(event->getUniqId(),
355 375 // event->getRepository(), DBType::SYNC); auto workPred =
356 376 // impl->createFinder(event->getUniqId(), event->getRepository(),
357 377 // DBType::WORK);
358 378
359 379 // auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
360 380 // if (!syncEvent.getUniqId().isNull())
361 381 // {
362 382 // removed = false;
363 383 // impl->m_CatalogueDao.copyEvent(
364 384 // syncEvent, impl->toWorkRepository(event->getRepository()), true);
365 385
366 386 // auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
367 387 // *event = workEvent;
368 388 // impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
369 389 // }
370 390 // else
371 391 // {
372 392 // removed = true;
373 393 // // Since the element wasn't in sync repository. Discard it means
374 394 // remove it
375 395 // event->setRepository(impl->toWorkRepository(event->getRepository()));
376 396 // impl->m_CatalogueDao.removeEvent(*event);
377 397 // }
378 398 //}
379 399
380 400 // bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event)
381 401 // const
382 402 //{
383 403 // return impl->m_KeysWithChanges.contains(impl->eventUniqueKey(event));
384 404 //}
385 405
386 406 // std::list<std::shared_ptr<DBCatalogue>>
387 407 // CatalogueController::retrieveCatalogues(
388 408 // const QString& repository) const
389 409 //{
390 410 // QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT :
391 411 // repository;
392 412
393 413 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue>> {};
394 414 // auto catalogues =
395 415 // impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
396 416 // for (auto catalogue : catalogues)
397 417 // {
398 418 // cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
399 419 // }
400 420 // return cataloguesShared;
401 421 //}
402 422
403 423 // void CatalogueController::addCatalogue(std::shared_ptr<DBCatalogue>
404 424 // catalogue)
405 425 //{
406 426 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
407 427
408 428 // auto catalogueTemp = *catalogue;
409 429 // impl->m_CatalogueDao.addCatalogue(catalogueTemp);
410 430
411 431 // auto workPred
412 432 // = impl->createFinder(catalogue->getUniqId(),
413 433 // catalogue->getRepository(), DBType::WORK);
414 434
415 435 // auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
416 436 // *catalogue = workCatalogue;
417 437
418 438 // auto uniqueId = impl->catalogueUniqueKey(catalogue);
419 439 // impl->m_KeysWithChanges.insert(uniqueId);
420 440 //}
421 441
422 442 // void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue>
423 443 // catalogue)
424 444 //{
425 445 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
426 446
427 447 // auto uniqueId = impl->catalogueUniqueKey(catalogue);
428 448 // impl->m_KeysWithChanges.insert(uniqueId);
429 449
430 450 // impl->m_CatalogueDao.updateCatalogue(*catalogue);
431 451 //}
432 452
433 453 // void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue>
434 454 // catalogue)
435 455 //{
436 456 // // Remove it from both repository and repository_work
437 457 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
438 458 // impl->m_CatalogueDao.removeCatalogue(*catalogue);
439 459 // catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
440 460 // impl->m_CatalogueDao.removeCatalogue(*catalogue);
441 461 // impl->savAllDB();
442 462 //}
443 463
444 464 // void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue>
445 465 // catalogue)
446 466 //{
447 467 // impl->saveCatalogue(catalogue, true);
448 468 // impl->m_KeysWithChanges.remove(impl->catalogueUniqueKey(catalogue));
449 469
450 470 // // remove key of events of the catalogue
451 471 // if (catalogue->getType() == CatalogueType::STATIC)
452 472 // {
453 473 // auto events = this->retrieveEventsFromCatalogue(catalogue);
454 474 // for (auto event : events)
455 475 // {
456 476 // impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
457 477 // }
458 478 // }
459 479 //}
460 480
461 481 // void CatalogueController::discardCatalogue(std::shared_ptr<DBCatalogue>
462 482 // catalogue, bool& removed)
463 483 //{
464 484 // auto syncPred
465 485 // = impl->createFinder(catalogue->getUniqId(),
466 486 // catalogue->getRepository(), DBType::SYNC);
467 487 // auto workPred
468 488 // = impl->createFinder(catalogue->getUniqId(),
469 489 // catalogue->getRepository(), DBType::WORK);
470 490
471 491 // auto syncCatalogue = impl->m_CatalogueDao.getCatalogue(syncPred);
472 492 // if (!syncCatalogue.getUniqId().isNull())
473 493 // {
474 494 // removed = false;
475 495 // impl->m_CatalogueDao.copyCatalogue(
476 496 // syncCatalogue, impl->toWorkRepository(catalogue->getRepository()),
477 497 // true);
478 498
479 499 // auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
480 500 // *catalogue = workCatalogue;
481 501 // impl->m_KeysWithChanges.remove(impl->catalogueUniqueKey(catalogue));
482 502 // }
483 503 // else
484 504 // {
485 505 // removed = true;
486 506 // // Since the element wasn't in sync repository. Discard it means
487 507 // remove it
488 508 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
489 509 // impl->m_CatalogueDao.removeCatalogue(*catalogue);
490 510 // }
491 511 //}
492 512
493 513 // void CatalogueController::saveAll()
494 514 //{
495 515 // for (auto repository : impl->m_RepositoryList)
496 516 // {
497 517 // // Save Event
498 518 // auto events = this->retrieveEvents(repository);
499 519 // for (auto event : events)
500 520 // {
501 521 // impl->saveEvent(event, false);
502 522 // }
503 523
504 524 // // Save Catalogue
505 525 // auto catalogues = this->retrieveCatalogues(repository);
506 526 // for (auto catalogue : catalogues)
507 527 // {
508 528 // impl->saveCatalogue(catalogue, false);
509 529 // }
510 530 // }
511 531
512 532 // impl->savAllDB();
513 533 // impl->m_KeysWithChanges.clear();
514 534 //}
515 535
516 536 // bool CatalogueController::hasChanges() const
517 537 //{
518 538 // return !impl->m_KeysWithChanges.isEmpty();
519 539 //}
520 540
521 541 // QByteArray CatalogueController::mimeDataForEvents(
522 542 // const QVector<std::shared_ptr<DBEvent>>& events) const
523 543 //{
524 544 // auto encodedData = QByteArray {};
525 545
526 546 // QMap<QString, QVariantList> idsPerRepository;
527 547 // for (auto event : events)
528 548 // {
529 549 // idsPerRepository[event->getRepository()] << event->getUniqId();
530 550 // }
531 551
532 552 // QDataStream stream { &encodedData, QIODevice::WriteOnly };
533 553 // stream << idsPerRepository;
534 554
535 555 // return encodedData;
536 556 //}
537 557
538 558 // QVector<std::shared_ptr<DBEvent>> CatalogueController::eventsForMimeData(
539 559 // const QByteArray& mimeData) const
540 560 //{
541 561 // auto events = QVector<std::shared_ptr<DBEvent>> {};
542 562 // QDataStream stream { mimeData };
543 563
544 564 // QMap<QString, QVariantList> idsPerRepository;
545 565 // stream >> idsPerRepository;
546 566
547 567 // for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend();
548 568 // ++it)
549 569 // {
550 570 // auto repository = it.key();
551 571 // auto allRepositoryEvent = retrieveEvents(repository);
552 572 // for (auto uuid : it.value())
553 573 // {
554 574 // for (auto repositoryEvent : allRepositoryEvent)
555 575 // {
556 576 // if (uuid.toUuid() == repositoryEvent->getUniqId())
557 577 // {
558 578 // events << repositoryEvent;
559 579 // }
560 580 // }
561 581 // }
562 582 // }
563 583
564 584 // return events;
565 585 //}
566 586
567 587 // QByteArray CatalogueController::mimeDataForCatalogues(
568 588 // const QVector<std::shared_ptr<DBCatalogue>>& catalogues) const
569 589 //{
570 590 // auto encodedData = QByteArray {};
571 591
572 592 // QMap<QString, QVariantList> idsPerRepository;
573 593 // for (auto catalogue : catalogues)
574 594 // {
575 595 // idsPerRepository[catalogue->getRepository()] <<
576 596 // catalogue->getUniqId();
577 597 // }
578 598
579 599 // QDataStream stream { &encodedData, QIODevice::WriteOnly };
580 600 // stream << idsPerRepository;
581 601
582 602 // return encodedData;
583 603 //}
584 604
585 605 // QVector<std::shared_ptr<DBCatalogue>>
586 606 // CatalogueController::cataloguesForMimeData(
587 607 // const QByteArray& mimeData) const
588 608 //{
589 609 // auto catalogues = QVector<std::shared_ptr<DBCatalogue>> {};
590 610 // QDataStream stream { mimeData };
591 611
592 612 // QMap<QString, QVariantList> idsPerRepository;
593 613 // stream >> idsPerRepository;
594 614
595 615 // for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend();
596 616 // ++it)
597 617 // {
598 618 // auto repository = it.key();
599 619 // auto allRepositoryCatalogues = retrieveCatalogues(repository);
600 620 // for (auto uuid : it.value())
601 621 // {
602 622 // for (auto repositoryCatalogues : allRepositoryCatalogues)
603 623 // {
604 624 // if (uuid.toUuid() == repositoryCatalogues->getUniqId())
605 625 // {
606 626 // catalogues << repositoryCatalogues;
607 627 // }
608 628 // }
609 629 // }
610 630 // }
611 631
612 632 // return catalogues;
613 633 //}
614 634
615 635 // void CatalogueController::initialize()
616 636 //{
617 637 // qCDebug(LOG_CatalogueController())
618 638 // << tr("CatalogueController init") << QThread::currentThread();
619 639
620 640 // impl->m_CatalogueDao.initialize();
621 641 // auto defaultRepositoryLocation
622 642 // = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
623 643
624 644 // QDir defaultRepositoryLocationDir;
625 645 // if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation))
626 646 // {
627 647 // defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
628 648 // auto defaultRepository =
629 649 // defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
630 650
631 651 // qCInfo(LOG_CatalogueController())
632 652 // << tr("Persistant data loading from: ") << defaultRepository;
633 653
634 654 // QDir dbDir(defaultRepository);
635 655 // impl->m_RepositoryList << REPOSITORY_DEFAULT;
636 656 // if (dbDir.exists())
637 657 // {
638 658 // auto dirName = dbDir.dirName();
639 659
640 660 // if (impl->m_CatalogueDao.addDB(defaultRepository, dirName))
641 661 // {
642 662 // impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
643 663 // }
644 664 // }
645 665 // else
646 666 // {
647 667 // qCInfo(LOG_CatalogueController())
648 668 // << tr("Initialisation of Default repository detected") <<
649 669 // defaultRepository;
650 670 // }
651 671 // }
652 672 // else
653 673 // {
654 674 // qCWarning(LOG_CatalogueController())
655 675 // << tr("Cannot load the persistent default repository from ")
656 676 // << defaultRepositoryLocation;
657 677 // }
658 678
659 679 // qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
660 680 //}
661 681
662 682 // QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
663 683 // const std::shared_ptr<DBEvent>& event) const
664 684 //{
665 685 // return event->getUniqId().toString().append(event->getRepository());
666 686 //}
667 687
668 688 // QString CatalogueController::CatalogueControllerPrivate::catalogueUniqueKey(
669 689 // const std::shared_ptr<DBCatalogue>& catalogue) const
670 690 //{
671 691 // return
672 692 // catalogue->getUniqId().toString().append(catalogue->getRepository());
673 693 //}
674 694
675 695 // void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(
676 696 // const QString& dbFrom, const QString& dbTo)
677 697 //{
678 698 // // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
679 699 // auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
680 700 // auto events = m_CatalogueDao.getEvents(dbFrom);
681 701 // for (auto catalogue : catalogues)
682 702 // {
683 703 // m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
684 704 // }
685 705
686 706 // for (auto event : events)
687 707 // {
688 708 // m_CatalogueDao.copyEvent(event, dbTo, true);
689 709 // }
690 710 //}
691 711
692 712 // QString
693 713 // CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString
694 714 // repository)
695 715 //{
696 716 // auto syncRepository = toSyncRepository(repository);
697 717
698 718 // return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
699 719 //}
700 720
701 721 // QString
702 722 // CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString
703 723 // repository)
704 724 //{
705 725 // auto syncRepository = repository;
706 726 // if (repository.endsWith(REPOSITORY_WORK_SUFFIX))
707 727 // {
708 728 // syncRepository.remove(REPOSITORY_WORK_SUFFIX);
709 729 // }
710 730 // else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX))
711 731 // {
712 732 // syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
713 733 // }
714 734 // return syncRepository;
715 735 //}
716 736
717 737 // void CatalogueController::CatalogueControllerPrivate::savAllDB()
718 738 //{
719 739 // for (auto repository : m_RepositoryList)
720 740 // {
721 741 // auto defaultRepositoryLocation
722 742 // =
723 743 // QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
724 744 // m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
725 745 // }
726 746 //}
727 747
728 748 // void CatalogueController::CatalogueControllerPrivate::saveEvent(
729 749 // std::shared_ptr<DBEvent> event, bool persist)
730 750 //{
731 751 // m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()),
732 752 // true); if (persist)
733 753 // {
734 754 // savAllDB();
735 755 // }
736 756 //}
737 757
738 758 // void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
739 759 // std::shared_ptr<DBCatalogue> catalogue, bool persist)
740 760 //{
741 761 // m_CatalogueDao.copyCatalogue(*catalogue,
742 762 // toSyncRepository(catalogue->getRepository()), true); if (persist)
743 763 // {
744 764 // savAllDB();
745 765 // }
746 766 //}
747 767
748 768 // std::shared_ptr<IRequestPredicate>
749 769 // CatalogueController::CatalogueControllerPrivate::createFinder(
750 770 // const QUuid& uniqId, const QString& repository, DBType type)
751 771 //{
752 772 // // update catalogue parameter
753 773 // auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
754 774 // QString { "uniqId" }, uniqId, ComparaisonOperation::EQUALEQUAL);
755 775
756 776 // auto repositoryType = repository;
757 777 // switch (type)
758 778 // {
759 779 // case DBType::SYNC:
760 780 // repositoryType = toSyncRepository(repositoryType);
761 781 // break;
762 782 // case DBType::WORK:
763 783 // repositoryType = toWorkRepository(repositoryType);
764 784 // break;
765 785 // case DBType::TRASH:
766 786 // default:
767 787 // break;
768 788 // }
769 789
770 790 // auto repositoryPredicate = std::make_shared<ComparaisonPredicate>(
771 791 // QString { "repository" }, repositoryType,
772 792 // ComparaisonOperation::EQUALEQUAL);
773 793
774 794 // auto finderPred =
775 795 // std::make_shared<CompoundPredicate>(CompoundOperation::AND);
776 796 // finderPred->AddRequestPredicate(uniqIdPredicate);
777 797 // finderPred->AddRequestPredicate(repositoryPredicate);
778 798
779 799 // return finderPred;
780 800 //}
@@ -1,30 +1,5
1 1 #include "Common/StringUtils.h"
2 2
3 #include <QRegExp>
4 #include <QString>
5 3
6 #include <set>
7 4
8 QString StringUtils::uniqueName(const QString &defaultName,
9 const std::vector<QString> &forbiddenNames) noexcept
10 {
11 // Gets the base of the unique name to generate, by removing trailing number (for example, base
12 // name of "FGM12" is "FGM")
13 auto baseName = defaultName;
14 baseName.remove(QRegExp{QStringLiteral("\\d*$")});
15 5
16 // Finds the unique name by adding an index to the base name and stops when the generated name
17 // isn't forbidden
18 QString newName{};
19 auto forbidden = true;
20 for (auto i = 0; forbidden; ++i) {
21 newName = (i == 0) ? baseName : baseName + QString::number(i);
22 forbidden = newName.isEmpty()
23 || std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(),
24 [&newName](const auto &name) {
25 return name.compare(newName, Qt::CaseInsensitive) == 0;
26 });
27 }
28
29 return newName;
30 }
General Comments 0
You need to be logged in to leave comments. Login now