#include #include #include #include #include #include #include #include #include #include #include #include #include Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") namespace { static QString REPOSITORY_WORK_SUFFIX = QString{"Work"}; } class CatalogueController::CatalogueControllerPrivate { public: QMutex m_WorkingMutex; CatalogueDao m_CatalogueDao; std::list m_RepositoryList; }; CatalogueController::CatalogueController(QObject *parent) : impl{spimpl::make_unique_impl()} { qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") << QThread::currentThread(); } CatalogueController::~CatalogueController() { qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") << QThread::currentThread(); this->waitForFinish(); } void CatalogueController::addDB(const QString &dbPath) { QDir dbDir(dbPath); if (dbDir.exists()) { auto dirName = dbDir.dirName(); if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) != impl->m_RepositoryList.cend()) { qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that is already loaded"); } if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); } else { impl->m_RepositoryList << dirName; } } else { qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") << dbPath; } } void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) { if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { qCCritical(LOG_CatalogueController()) << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); } } std::list > CatalogueController::retrieveEvents(const QString &repository) const { auto eventsShared = std::list >{}; auto events = impl->m_CatalogueDao.getEvents(repository); for (auto event : events) { eventsShared.push_back(std::make_shared(event)); } return eventsShared; } std::list > CatalogueController::retrieveAllEvents() const { auto eventsShared = std::list >{}; for (auto repository : impl->m_RepositoryList) { eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); } return eventsShared; } void CatalogueController::initialize() { qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") << QThread::currentThread(); impl->m_WorkingMutex.lock(); impl->m_CatalogueDao.initialize(); auto defaultRepository = QString("%1/%2").arg( QStandardPaths::displayName(QStandardPaths::AppDataLocation), REPOSITORY_DEFAULT); QDir defaultRepositoryDir(defaultRepository); if (defaultRepositoryDir.exists()) { qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") << defaultRepository; this->addDB(defaultRepository); qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); } } void CatalogueController::finalize() { impl->m_WorkingMutex.unlock(); } void CatalogueController::waitForFinish() { QMutexLocker locker{&impl->m_WorkingMutex}; }