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  const FilePath& getPath() const noexcept { return mFilePath; }
84  bool isWritable() const noexcept { return mIsWritable; }
85  bool isRestoredFromAutosave() const noexcept { return mRestoredFromAutosave; }
86 
87  // Inherited from FileSystem
88  virtual FilePath getAbsPath(const QString& path = "") const noexcept override;
89  virtual QStringList getDirs(const QString& path = "") const noexcept override;
90  virtual QStringList getFiles(const QString& path = "") const
91  noexcept override;
92  virtual bool fileExists(const QString& path) const noexcept override;
93  virtual QByteArray read(const QString& path) const override;
94  virtual void write(const QString& path, const QByteArray& content) override;
95  virtual void removeFile(const QString& path) override;
96  virtual void removeDirRecursively(const QString& path = "") override;
97 
98  // General Methods
99  void loadFromZip(const FilePath& fp);
100  void exportToZip(const FilePath& fp) const;
101  void discardChanges() noexcept;
102  QStringList checkForModifications() const;
103  void autosave();
104  void save();
105 
106  // Static Methods
107  static std::shared_ptr<TransactionalFileSystem> open(
108  const FilePath& filepath, bool writable,
109  RestoreMode restoreMode = RestoreMode::NO, QObject* parent = nullptr) {
110  return std::make_shared<TransactionalFileSystem>(filepath, writable,
111  restoreMode, parent);
112  }
113  static std::shared_ptr<TransactionalFileSystem> openRO(
114  const FilePath& filepath, RestoreMode restoreMode = RestoreMode::NO,
115  QObject* parent = nullptr) {
116  return open(filepath, false, restoreMode, parent);
117  }
118  static std::shared_ptr<TransactionalFileSystem> openRW(
119  const FilePath& filepath, RestoreMode restoreMode = RestoreMode::NO,
120  QObject* parent = nullptr) {
121  return open(filepath, true, restoreMode, parent);
122  }
123  static QString cleanPath(QString path) noexcept;
124 
125 private: // Methods
126  bool isRemoved(const QString& path) const noexcept;
127  void exportDirToZip(QuaZipFile& file, const FilePath& zipFp,
128  const QString& dir) const;
129  void saveDiff(const QString& type) const;
130  void loadDiff(const FilePath& fp);
131  void removeDiff(const QString& type);
132 
133 private: // Data
138 
139  // File system modifications
140  QHash<QString, QByteArray> mModifiedFiles;
141  QSet<QString> mRemovedFiles;
142  QSet<QString> mRemovedDirs;
143 };
144 
145 /*******************************************************************************
146  * End of File
147  ******************************************************************************/
148 
149 } // namespace librepcb
150 
151 #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:270
void loadDiff(const FilePath &fp)
Definition: transactionalfilesystem.cpp:454
void removeDiff(const QString &type)
Definition: transactionalfilesystem.cpp:477
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:118
bool isWritable() const noexcept
Definition: transactionalfilesystem.h:84
void exportDirToZip(QuaZipFile &file, const FilePath &zipFp, const QString &dir) const
Definition: transactionalfilesystem.cpp:383
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:107
void save()
Definition: transactionalfilesystem.cpp:312
RestoreMode
Definition: transactionalfilesystem.h:67
const FilePath & getPath() const noexcept
Definition: transactionalfilesystem.h:83
DirectoryLock mLock
Definition: transactionalfilesystem.h:136
QStringList checkForModifications() const
Definition: transactionalfilesystem.cpp:276
virtual bool fileExists(const QString &path) const noexceptoverride
Definition: transactionalfilesystem.cpp:178
QHash< QString, QByteArray > mModifiedFiles
Definition: transactionalfilesystem.h:140
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
FilePath mFilePath
Definition: transactionalfilesystem.h:134
virtual QStringList getFiles(const QString &path="") const noexceptoverride
Definition: transactionalfilesystem.cpp:151
bool mRestoredFromAutosave
Definition: transactionalfilesystem.h:137
virtual void removeFile(const QString &path) override
Definition: transactionalfilesystem.cpp:209
QSet< QString > mRemovedFiles
Definition: transactionalfilesystem.h:141
QSet< QString > mRemovedDirs
Definition: transactionalfilesystem.h:142
Base class / interface for all file system implementations.
Definition: filesystem.h:43
bool isRestoredFromAutosave() const noexcept
Definition: transactionalfilesystem.h:85
void saveDiff(const QString &type) const
Definition: transactionalfilesystem.cpp:424
bool isRemoved(const QString &path) const noexcept
Definition: transactionalfilesystem.cpp:369
static QString cleanPath(QString path) noexcept
Definition: transactionalfilesystem.cpp:357
virtual QStringList getDirs(const QString &path="") const noexceptoverride
Definition: transactionalfilesystem.cpp:123
Throw a RuntimeError if a backup exists.
bool mIsWritable
Definition: transactionalfilesystem.h:135
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:308
static std::shared_ptr< TransactionalFileSystem > openRO(const FilePath &filepath, RestoreMode restoreMode=RestoreMode::NO, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:113