LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
transactionaldirectory.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_TRANSACTIONALDIRECTORY_H
21 #define LIBREPCB_TRANSACTIONALDIRECTORY_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "filesystem.h"
27 
28 #include <QtCore>
29 
30 #include <memory>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 
36 namespace librepcb {
37 
38 class TransactionalFileSystem;
39 
40 /*******************************************************************************
41  * Class TransactionalDirectory
42  ******************************************************************************/
43 
52 class TransactionalDirectory final : public FileSystem {
53  Q_OBJECT
54 
55 public:
56  // Constructors / Destructor
57  TransactionalDirectory(QObject* parent = nullptr);
58  explicit TransactionalDirectory(std::shared_ptr<TransactionalFileSystem> fs,
59  const QString& dir = "",
60  QObject* parent = nullptr) noexcept;
61  // Note: The "other" parameter must not be a const reference because of
62  // const correctness reasons! A const TransactionalDirectory must
63  // not be convertible to a non-const TransactionalDirectory because
64  // it would allow writing to a file system which should actually be
65  // const.
67  const QString& subdir = "",
68  QObject* parent = nullptr) noexcept;
69  virtual ~TransactionalDirectory() noexcept;
70 
71  // Getters
72  std::shared_ptr<const TransactionalFileSystem> getFileSystem() const
73  noexcept {
74  return mFileSystem;
75  }
76  std::shared_ptr<TransactionalFileSystem> getFileSystem() noexcept {
77  return mFileSystem;
78  }
79  const QString& getPath() const noexcept { return mPath; }
80  bool isWritable() const noexcept;
81  bool isRestoredFromAutosave() const noexcept;
82 
83  // Inherited from FileSystem
84  virtual FilePath getAbsPath(const QString& path = "") const noexcept override;
85  virtual QStringList getDirs(const QString& path = "") const noexcept override;
86  virtual QStringList getFiles(const QString& path = "") const
87  noexcept override;
88  virtual bool fileExists(const QString& path) const noexcept override;
89  virtual QByteArray read(const QString& path) const override;
90  virtual void write(const QString& path, const QByteArray& content) override;
91  virtual void removeFile(const QString& path) override;
92  virtual void removeDirRecursively(const QString& path = "") override;
93 
94  // General Methods
95  void saveTo(TransactionalDirectory& dest);
96  void moveTo(TransactionalDirectory& dest);
97 
98 private: // Methods
100  const QString& srcDir,
102  const QString& dstDir);
103 
104 private: // Data
106  QString mPath;
107 };
108 
109 /*******************************************************************************
110  * End of File
111  ******************************************************************************/
112 
113 } // namespace librepcb
114 
115 #endif // LIBREPCB_TRANSACTIONALDIRECTORY_H
Special FileSystem implementation for libraries and projects.
Definition: transactionalfilesystem.h:63
virtual bool fileExists(const QString &path) const noexceptoverride
Definition: transactionaldirectory.cpp:94
TransactionalDirectory(QObject *parent=nullptr)
Definition: transactionaldirectory.cpp:36
virtual void removeDirRecursively(const QString &path="") override
Definition: transactionaldirectory.cpp:111
Helper class to access a subdirectory of TransactionalFileSystem.
Definition: transactionaldirectory.h:52
bool isWritable() const noexcept
Definition: transactionaldirectory.cpp:67
QString mPath
Definition: transactionaldirectory.h:106
virtual QByteArray read(const QString &path) const override
Definition: transactionaldirectory.cpp:98
virtual FilePath getAbsPath(const QString &path="") const noexceptoverride
Definition: transactionaldirectory.cpp:79
static void copyDirRecursively(TransactionalFileSystem &srcFs, const QString &srcDir, TransactionalFileSystem &dstFs, const QString &dstDir)
Definition: transactionaldirectory.cpp:150
std::shared_ptr< const TransactionalFileSystem > getFileSystem() const noexcept
Definition: transactionaldirectory.h:72
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
virtual ~TransactionalDirectory() noexcept
Definition: transactionaldirectory.cpp:60
bool isRestoredFromAutosave() const noexcept
Definition: transactionaldirectory.cpp:71
std::shared_ptr< TransactionalFileSystem > mFileSystem
Definition: transactionaldirectory.h:105
virtual QStringList getFiles(const QString &path="") const noexceptoverride
Definition: transactionaldirectory.cpp:89
virtual QStringList getDirs(const QString &path="") const noexceptoverride
Definition: transactionaldirectory.cpp:84
Base class / interface for all file system implementations.
Definition: filesystem.h:43
void moveTo(TransactionalDirectory &dest)
Definition: transactionaldirectory.cpp:131
virtual void removeFile(const QString &path) override
Definition: transactionaldirectory.cpp:107
void saveTo(TransactionalDirectory &dest)
Definition: transactionaldirectory.cpp:119
std::shared_ptr< TransactionalFileSystem > getFileSystem() noexcept
Definition: transactionaldirectory.h:76
virtual void write(const QString &path, const QByteArray &content) override
Definition: transactionaldirectory.cpp:102
const QString & getPath() const noexcept
Definition: transactionaldirectory.h:79