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