File indexing completed on 2024-05-19 05:42:15

0001 // ct_lvtprj_project_file.h                                         -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #ifndef CT_LVTPRJ_PROJECT_FILE_H
0021 #define CT_LVTPRJ_PROJECT_FILE_H
0022 
0023 #include <filesystem>
0024 #include <memory>
0025 #include <optional>
0026 #include <string>
0027 
0028 #include <lvtprj_export.h>
0029 
0030 #include <result/result.hpp>
0031 
0032 #include <QObject>
0033 
0034 namespace Codethink::lvtprj {
0035 
0036 struct ProjectFileError {
0037     std::string errorMessage;
0038 };
0039 
0040 class LVTPRJ_EXPORT ProjectFile : public QObject {
0041     // Represents a Project on disk.
0042     // the project is a tar file, it needs to be
0043     // uncompressed to be used. When open() is called,
0044     // it will uncompress in $TEMP, to get the open location
0045     // call openLocation(). All changes done in the folder
0046     // will be compressed again when the project is saved,
0047     // or closed.
0048     Q_OBJECT
0049 
0050   public:
0051     ProjectFile();
0052     // Create an empty project file.
0053 
0054     ~ProjectFile();
0055     // Closes the project file.
0056 
0057     [[nodiscard]] static std::filesystem::path backupFolder();
0058 
0059     [[nodiscard]] bool isOpen() const;
0060 
0061     [[nodiscard]] auto createEmpty() -> cpp::result<void, ProjectFileError>;
0062     // creates a new project that can be saved later.
0063 
0064     [[nodiscard]] auto open(const std::filesystem::path& path) -> cpp::result<void, ProjectFileError>;
0065     // Extract the contents of path into $TEMP, and sets up the project workspace.
0066 
0067     [[nodiscard]] auto save() -> cpp::result<void, ProjectFileError>;
0068     // Compresses the contents of $TEMP into the project file.
0069 
0070     // As soon as we have a new code database that doesn't matches our current
0071     // CAD database, we need to copy the contents over, and add the missing
0072     // CAD tables.
0073     cpp::result<void, ProjectFileError> resetCadDatabaseFromCodeDatabase();
0074 
0075     enum class BackupFileBehavior { Keep, Discard };
0076 
0077     [[nodiscard]] auto saveAs(const std::filesystem::path& path, BackupFileBehavior behavior)
0078         -> cpp::result<void, ProjectFileError>;
0079     // save a copy of this project on the new folder.
0080 
0081     [[nodiscard]] auto close() -> cpp::result<void, ProjectFileError>;
0082 
0083     auto setProjectName(std::string name) -> void;
0084     // sets the name of the project
0085 
0086     [[nodiscard]] std::string projectName() const;
0087     // returns the name of the project
0088 
0089     void setProjectInformation(std::string projectInfo);
0090     // detailed explanation of what the project is.
0091 
0092     [[nodiscard]] std::string projectInformation() const;
0093     // returns the detailed information of the project
0094 
0095     [[nodiscard]] std::filesystem::path location() const;
0096     // return the path of where this is saved on disk.
0097 
0098     [[nodiscard]] std::filesystem::path openLocation() const;
0099     // return the full path of the workspace in $TEMP
0100 
0101     [[nodiscard]] std::filesystem::path codeDatabasePath() const;
0102     // return the path of the code database on the open workspace.
0103 
0104     [[nodiscard]] std::filesystem::path cadDatabasePath() const;
0105     // return the path of the cad database on the open workspace.
0106 
0107     [[nodiscard]] bool hasCadDatabase() const;
0108     // does this project has a cad database yet?
0109 
0110     [[nodiscard]] bool hasCodeDatabase() const;
0111     // does this project have a code database yet?
0112 
0113     [[nodiscard]] bool isDirty() const;
0114     // does the project have changes that are not saved in disk yet?
0115 
0116     void setDirty();
0117 
0118     [[nodiscard]] static std::string_view codebaseDbFilename();
0119     // return the string for the codebase db file.
0120 
0121     [[nodiscard]] cpp::result<void, ProjectFileError> saveBackup();
0122     [[nodiscard]] std::filesystem::path backupPath() const;
0123 
0124     void requestAutosave(int msec);
0125 
0126     void setSourceCodePath(std::filesystem::path sourceCodePath);
0127     [[nodiscard]] virtual std::filesystem::path sourceCodePath() const;
0128 
0129     Q_SIGNAL void communicateError(const Codethink::lvtprj::ProjectFileError& error);
0130     // This "feeds" the application with error codes when something happen during the Qt event loop.
0131 
0132     [[nodiscard]] std::vector<QJsonDocument> leftPanelTab();
0133     [[nodiscard]] std::vector<QJsonDocument> rightPanelTab();
0134 
0135     enum BookmarkType { LeftPane, RightPane, Bookmark };
0136     [[nodiscard]] cpp::result<void, ProjectFileError> saveBookmark(const QJsonDocument& doc, BookmarkType bookmarkType);
0137 
0138     QList<QString> bookmarks() const;
0139     QJsonDocument getBookmark(const QString& name) const;
0140     void removeBookmark(const QString& name);
0141     Q_SIGNAL void bookmarksChanged();
0142 
0143     void prepareSave();
0144 
0145   private:
0146     auto dumpProjectMetadata() const -> cpp::result<void, ProjectFileError>;
0147     void loadProjectMetadata();
0148 
0149     struct Private;
0150     std::unique_ptr<Private> d;
0151 };
0152 
0153 } // namespace Codethink::lvtprj
0154 
0155 #endif