@@ -1,131 +1,139 | |||
|
1 | 1 | #include <Catalogue/CatalogueController.h> |
|
2 | 2 | |
|
3 | 3 | #include <Variable/Variable.h> |
|
4 | 4 | |
|
5 | 5 | #include <CatalogueDao.h> |
|
6 | 6 | |
|
7 | 7 | #include <ComparaisonPredicate.h> |
|
8 | 8 | #include <CompoundPredicate.h> |
|
9 | 9 | #include <DBCatalogue.h> |
|
10 | 10 | #include <DBEvent.h> |
|
11 | 11 | #include <DBTag.h> |
|
12 | 12 | #include <IRequestPredicate.h> |
|
13 | 13 | |
|
14 | 14 | #include <QMutex> |
|
15 | 15 | #include <QThread> |
|
16 | 16 | |
|
17 | 17 | #include <QDir> |
|
18 | 18 | #include <QStandardPaths> |
|
19 | 19 | |
|
20 | 20 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") |
|
21 | 21 | |
|
22 |
namespace |
|
|
22 | namespace { | |
|
23 | 23 | |
|
24 | 24 | static QString REPOSITORY_WORK_SUFFIX = QString{"Work"}; |
|
25 | 25 | |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | class CatalogueController::CatalogueControllerPrivate { |
|
29 | 29 | public: |
|
30 | 30 | QMutex m_WorkingMutex; |
|
31 | 31 | CatalogueDao m_CatalogueDao; |
|
32 | 32 | |
|
33 | 33 | std::list<QString> m_RepositoryList; |
|
34 | 34 | }; |
|
35 | 35 | |
|
36 | 36 | CatalogueController::CatalogueController(QObject *parent) |
|
37 | 37 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>()} |
|
38 | 38 | { |
|
39 | 39 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") |
|
40 | 40 | << QThread::currentThread(); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | CatalogueController::~CatalogueController() |
|
44 | 44 | { |
|
45 | 45 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") |
|
46 | 46 | << QThread::currentThread(); |
|
47 | 47 | this->waitForFinish(); |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | void CatalogueController::addDB(const QString &dbPath) |
|
51 | 51 | { |
|
52 | 52 | QDir dbDir(dbPath); |
|
53 | 53 | if (dbDir.exists()) { |
|
54 | 54 | auto dirName = dbDir.dirName(); |
|
55 | 55 | |
|
56 | 56 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) |
|
57 | 57 | != impl->m_RepositoryList.cend()) { |
|
58 | 58 | qCCritical(LOG_CatalogueController()) |
|
59 | 59 | << tr("Impossible to addDB that is already loaded"); |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { |
|
63 | 63 | qCCritical(LOG_CatalogueController()) |
|
64 | 64 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); |
|
65 | 65 | } |
|
66 | else | |
|
67 | { | |
|
68 | impl->m_RepositoryList << dirName; | |
|
66 | else { | |
|
67 | impl->m_RepositoryList.push_back(dirName); | |
|
69 | 68 | } |
|
70 | 69 | } |
|
71 | 70 | else { |
|
72 | 71 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") |
|
73 | 72 | << dbPath; |
|
74 | 73 | } |
|
75 | 74 | } |
|
76 | 75 | |
|
77 | 76 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) |
|
78 | 77 | { |
|
79 | 78 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { |
|
80 | 79 | qCCritical(LOG_CatalogueController()) |
|
81 | 80 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); |
|
82 | 81 | } |
|
83 | 82 | } |
|
84 | 83 | |
|
85 | 84 | std::list<std::shared_ptr<DBEvent> > |
|
86 | 85 | CatalogueController::retrieveEvents(const QString &repository) const |
|
87 | 86 | { |
|
88 | 87 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
89 | 88 | auto events = impl->m_CatalogueDao.getEvents(repository); |
|
90 | 89 | for (auto event : events) { |
|
91 | 90 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
92 | 91 | } |
|
93 | 92 | return eventsShared; |
|
94 | 93 | } |
|
95 | 94 | |
|
96 | 95 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const |
|
97 | 96 | { |
|
98 | 97 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
99 | 98 | for (auto repository : impl->m_RepositoryList) { |
|
100 | 99 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); |
|
101 | 100 | } |
|
102 | 101 | |
|
103 | 102 | return eventsShared; |
|
104 | 103 | } |
|
105 | 104 | |
|
106 | 105 | void CatalogueController::initialize() |
|
107 | 106 | { |
|
108 | 107 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") |
|
109 | 108 | << QThread::currentThread(); |
|
110 | 109 | impl->m_WorkingMutex.lock(); |
|
111 | 110 | impl->m_CatalogueDao.initialize(); |
|
112 |
auto defaultRepository |
|
|
113 |
QStandardPaths:: |
|
|
114 | QDir defaultRepositoryDir(defaultRepository); | |
|
115 |
|
|
|
116 | qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") | |
|
117 | << defaultRepository; | |
|
111 | auto defaultRepositoryLocation | |
|
112 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); | |
|
113 | ||
|
114 | QDir defaultRepositoryLocationDir; | |
|
115 | if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) { | |
|
116 | defaultRepositoryLocationDir.cd(defaultRepositoryLocation); | |
|
117 | auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT); | |
|
118 | qCInfo(LOG_CatalogueController()) | |
|
119 | << tr("Persistant data loading from: ") << defaultRepository; | |
|
118 | 120 | this->addDB(defaultRepository); |
|
119 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); | |
|
120 | 121 | } |
|
122 | else { | |
|
123 | qCWarning(LOG_CatalogueController()) | |
|
124 | << tr("Cannot load the persistent default repository from ") | |
|
125 | << defaultRepositoryLocation; | |
|
126 | } | |
|
127 | ||
|
128 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); | |
|
121 | 129 | } |
|
122 | 130 | |
|
123 | 131 | void CatalogueController::finalize() |
|
124 | 132 | { |
|
125 | 133 | impl->m_WorkingMutex.unlock(); |
|
126 | 134 | } |
|
127 | 135 | |
|
128 | 136 | void CatalogueController::waitForFinish() |
|
129 | 137 | { |
|
130 | 138 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
131 | 139 | } |
General Comments 0
You need to be logged in to leave comments.
Login now