LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
transactionalfilesystem.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_TRANSACTIONALFILESYSTEM_H
21 #define LIBREPCB_TRANSACTIONALFILESYSTEM_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "directorylock.h"
27 #include "filesystem.h"
28 
29 #include <QtCore>
30 
31 #include <memory>
32 
33 /*******************************************************************************
34  * Namespace / Forward Declarations
35  ******************************************************************************/
36 
37 class QuaZipFile;
38 
39 namespace librepcb {
40 
41 /*******************************************************************************
42  * Class TransactionalFileSystem
43  ******************************************************************************/
44 
63 class TransactionalFileSystem final : public FileSystem {
64  Q_OBJECT
65 
66 public:
67  enum class RestoreMode {
68  NO,
69  YES,
70  ASK,
71  ABORT,
72  };
73 
74  // Constructors / Destructor
75  TransactionalFileSystem() = delete;
76  TransactionalFileSystem(const FilePath& filepath, bool writable = false,
77  RestoreMode restoreMode = RestoreMode::NO,
78  QObject* parent = nullptr);
79  TransactionalFileSystem(const TransactionalFileSystem& other) = delete;
80  virtual ~TransactionalFileSystem() noexcept;
81 
82  // Getters
83  bool isWritable() const noexcept { return mIsWritable; }
84  bool isRestoredFromAutosave() const noexcept { return mRestoredFromAutosave; }
85 
86  // Inherited from FileSystem
87  virtual FilePath getAbsPath(const QString& path = "") const noexcept override;
88  virtual QStringList getDirs(const QString& path = "") const noexcept override;
89  virtual QStringList getFiles(const QString& path = "") const
90  noexcept override;
91  virtual bool fileExists(const QString& path) const noexcept override;
92  virtual QByteArray read(const QString& path) const override;
93  virtual void write(const QString& path, const QByteArray& content) override;
94  virtual void removeFile(const QString& path) override;
95  virtual void removeDirRecursively(const QString& path = "") override;
96 
97  // General Methods
98  void loadFromZip(const FilePath& fp);
99  void exportToZip(const FilePath& fp) const;
100  void autosave();
101  void save();
102 
103  // Static Methods
104  static std::shared_ptr<TransactionalFileSystem> open(
105  const FilePath& filepath, bool writable,
106  RestoreMode restoreMode = RestoreMode::NO, QObject* parent = nullptr) {
107  return std::make_shared<TransactionalFileSystem>(filepath, writable,
108  restoreMode, parent);
109  }
110  static std::shared_ptr<TransactionalFileSystem> openRO(
111  const FilePath& filepath, RestoreMode restoreMode = RestoreMode::NO,
112  QObject* parent = nullptr) {
113  return open(filepath, false, restoreMode, parent);
114  }
115  static std::shared_ptr<TransactionalFileSystem> openRW(
116  const FilePath& filepath, RestoreMode restoreMode = RestoreMode::NO,
117  QObject* parent = nullptr) {
118  return open(filepath, true, restoreMode, parent);
119  }
120  static QString cleanPath(QString path) noexcept;
121 
122 private: // Methods
123  bool isRemoved(const QString& path) const noexcept;
124  void exportDirToZip(QuaZipFile& file, const FilePath& zipFp,
125  const QString& dir) const;
126  void saveDiff(const QString& type) const;
127  void loadDiff(const FilePath& fp);
128  void removeDiff(const QString& type);
129  void discardChanges() noexcept;
130 
131 private: // Data
136 
137  // File system modifications
138  QHash<QString, QByteArray> mModifiedFiles;
139  QSet<QString> mRemovedFiles;
140  QSet<QString> mRemovedDirs;
141 };
142 
143 /*******************************************************************************
144  * End of File
145  ******************************************************************************/
146 
147 } // namespace librepcb
148 
149 #endif // LIBREPCB_TRANSACTIONALFILESYSTEM_H
void exportToZip(const FilePath &fp) const
Definition: transactionalfilesystem.cpp:251
Special FileSystem implementation for libraries and projects.
Definition: transactionalfilesystem.h:63
void discardChanges() noexcept
Definition: transactionalfilesystem.cpp:452
void loadDiff(const FilePath &fp)
Definition: transactionalfilesystem.cpp:416
void removeDiff(const QString &type)
Definition: transactionalfilesystem.cpp:439
virtual void write(const QString &path, const QByteArray &content) override
Definition: transactionalfilesystem.cpp:202
virtual FilePath getAbsPath(const QString &path="") const noexceptoverride
Definition: transactionalfilesystem.cpp:118
static std::shared_ptr< TransactionalFileSystem > openRW(const FilePath &filepath, RestoreMode restoreMode=RestoreMode::NO, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:115
bool isWritable() const noexcept
Definition: transactionalfilesystem.h:83
void exportDirToZip(QuaZipFile &file, const FilePath &zipFp, const QString &dir) const
Definition: transactionalfilesystem.cpp:345
Ask (with QMessageBox) whether to restore or not.
virtual ~TransactionalFileSystem() noexcept
Definition: transactionalfilesystem.cpp:99
void loadFromZip(const FilePath &fp)
Definition: transactionalfilesystem.cpp:235
virtual QByteArray read(const QString &path) const override
Definition: transactionalfilesystem.cpp:189
static std::shared_ptr< TransactionalFileSystem > open(const FilePath &filepath, bool writable, RestoreMode restoreMode=RestoreMode::NO, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:104
void save()
Definition: transactionalfilesystem.cpp:274
RestoreMode
Definition: transactionalfilesystem.h:67
DirectoryLock mLock
Definition: transactionalfilesystem.h:134
virtual bool fileExists(const QString &path) const noexceptoverride
Definition: transactionalfilesystem.cpp:178
QHash< QString, QByteArray > mModifiedFiles
Definition: transactionalfilesystem.h:138
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
FilePath mFilePath
Definition: transactionalfilesystem.h:132
virtual QStringList getFiles(const QString &path="") const noexceptoverride
Definition: transactionalfilesystem.cpp:151
bool mRestoredFromAutosave
Definition: transactionalfilesystem.h:135
virtual void removeFile(const QString &path) override
Definition: transactionalfilesystem.cpp:209
QSet< QString > mRemovedFiles
Definition: transactionalfilesystem.h:139
QSet< QString > mRemovedDirs
Definition: transactionalfilesystem.h:140
Base class / interface for all file system implementations.
Definition: filesystem.h:43
bool isRestoredFromAutosave() const noexcept
Definition: transactionalfilesystem.h:84
void saveDiff(const QString &type) const
Definition: transactionalfilesystem.cpp:386
bool isRemoved(const QString &path) const noexcept
Definition: transactionalfilesystem.cpp:331
static QString cleanPath(QString path) noexcept
Definition: transactionalfilesystem.cpp:319
virtual QStringList getDirs(const QString &path="") const noexceptoverride
Definition: transactionalfilesystem.cpp:123
Throw a RuntimeError if a backup exists.
bool mIsWritable
Definition: transactionalfilesystem.h:133
virtual void removeDirRecursively(const QString &path="") override
Definition: transactionalfilesystem.cpp:215
This class can be used to implement file-based directory locks.
Definition: directorylock.h:159
void autosave()
Definition: transactionalfilesystem.cpp:270
static std::shared_ptr< TransactionalFileSystem > openRO(const FilePath &filepath, RestoreMode restoreMode=RestoreMode::NO, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:110