diff --git a/PB/include/pb/persistence/ProjectPersistence.h b/PB/include/pb/persistence/ProjectPersistence.h new file mode 100644 index 000000000..56eb7fbf2 --- /dev/null +++ b/PB/include/pb/persistence/ProjectPersistence.h @@ -0,0 +1,53 @@ +#pragma once +#include + +namespace PB { +class ProjectPersistenceListener { +public: + virtual ~ProjectPersistenceListener() = default; + virtual void onMetadataUpdated() = 0; + virtual void onProjectRead() = 0; + virtual void onPersistenceError(PBDev::Error) = 0; +}; + +class ProjectPersistence final : public PersistenceMetadataListener, + public PersistenceProjectListener { +public: + ProjectPersistence() = default; + ~ProjectPersistence() = default; + + void configure(Path localStatePath); + void configure(ProjectPersistenceListener *listener); + + std::shared_ptr currentProject(); + + void recallProject(boost::uuids::uuid const &uuid); + + void recallProject(std::string name); + + void recallMetadata(); + + boost::uuids::uuid uuid(std::string name); + + bool hasUUID(std::string name) const; + + std::vector projectsNames() const; + + void onProjectRead(std::shared_ptr project) override; + void onProjectPersistenceError(PBDev::Error) override; + + void onMetadataRead( + boost::bimaps::bimap metadata) override; + void onMetadataPersistenceError(PBDev::Error) override; + +private: + std::string name(boost::uuids::uuid uuid); + + ProjectPersistenceListener *mListener = nullptr; + Path mLocalStatePath; + Persistence mPersistence; + std::shared_ptr mProject = nullptr; + + boost::bimaps::bimap mMetadata; +}; +} // namespace PB \ No newline at end of file diff --git a/PB/src/ProjectPersistence.cpp b/PB/src/ProjectPersistence.cpp new file mode 100644 index 000000000..cbc1a5d00 --- /dev/null +++ b/PB/src/ProjectPersistence.cpp @@ -0,0 +1,78 @@ +#include + +namespace PB { +void ProjectPersistence::configure(Path localStatePath) +{ + mLocalStatePath = localStatePath; +} + +void ProjectPersistence::configure(ProjectPersistenceListener *listener) +{ + mListener = listener; +} + +std::shared_ptr ProjectPersistence::currentProject() +{ + return mProject; +} + +void ProjectPersistence::recallProject(boost::uuids::uuid const &uuid) +{ + mPersistence.recallProject(name(uuid)); +} + +void ProjectPersistence::recallProject(std::string name) +{ + mPersistence.recallProject(name); +} + +void ProjectPersistence::recallMetadata() { mPersistence.recallMetadata(); } + +boost::uuids::uuid ProjectPersistence::uuid(std::string name) +{ + return mMetadata.right.at(name); +} + +bool ProjectPersistence::hasUUID(std::string name) const +{ + return mMetadata.right.find(name) != mMetadata.right.end(); +} + +std::vector ProjectPersistence::projectsNames() const +{ + std::vector projects; + for (auto &it : mMetadata) { + projects.push_back(it.right); + } + return projects; +} + +std::string ProjectPersistence::name(boost::uuids::uuid uuid) +{ + return mMetadata.left.at(uuid); +} + +void ProjectPersistence::onProjectRead(std::shared_ptr project) +{ + mProject = project; + mListener->onProjectRead(); +} + +void ProjectPersistence::onProjectPersistenceError(PBDev::Error error) +{ + mListener->onPersistenceError(error); +} + +void ProjectPersistence::onMetadataRead( + boost::bimaps::bimap metadata) +{ + mMetadata = metadata; + mListener->onMetadataUpdated(); +} + +void ProjectPersistence::onMetadataPersistenceError(PBDev::Error error) +{ + mListener->onPersistenceError(error); +} + +} // namespace PB \ No newline at end of file