LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
smartfile.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_SMARTFILE_H
21 #define LIBREPCB_SMARTFILE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../exceptions.h"
27 #include "filepath.h"
28 
29 #include <QtCore>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 /*******************************************************************************
37  * Class SmartFile
38  ******************************************************************************/
39 
59 class SmartFile {
60  Q_DECLARE_TR_FUNCTIONS(SmartFile)
61 
62 public:
63  // Constructors / Destructor
64  SmartFile() = delete;
65  SmartFile(const SmartFile& other) = delete;
66 
83  SmartFile(const FilePath& filepath, bool restore, bool readOnly, bool create);
84 
90  virtual ~SmartFile() noexcept;
91 
92  // Getters
93 
99  const FilePath& getFilepath() const noexcept { return mFilePath; }
100 
108  bool isRestored() const noexcept { return mIsRestored; }
109 
117  bool isReadOnly() const noexcept { return mIsReadOnly; }
118 
127  bool isCreated() const noexcept { return mIsCreated; }
128 
129  // General Methods
130 
139  void removeFile(bool original);
140 
141  // Operator Overloadings
142  SmartFile& operator=(const SmartFile& rhs) = delete;
143 
144 protected:
145  // Protected Methods
146 
165  const FilePath& prepareSaveAndReturnFilePath(bool toOriginal);
166 
177  void updateMembersAfterSaving(bool toOriginal) noexcept;
178 
179  // General Attributes
180 
185 
190 
198 
209 
217 
223 };
224 
225 /*******************************************************************************
226  * End of File
227  ******************************************************************************/
228 
229 } // namespace librepcb
230 
231 #endif // LIBREPCB_SMARTFILE_H
bool mIsReadOnly
If true, the file is opened as read-only.
Definition: smartfile.h:216
bool isCreated() const noexcept
Check if this file is created and not yet saved to the harddisc.
Definition: smartfile.h:127
SmartFile & operator=(const SmartFile &rhs)=delete
const FilePath & getFilepath() const noexcept
Get the filepath to the file which was passed to the constructor.
Definition: smartfile.h:99
FilePath mTmpFilePath
The filepath to the temporary file (mFilePath + '~')
Definition: smartfile.h:189
bool isRestored() const noexcept
Check if this file was restored from a backup.
Definition: smartfile.h:108
void updateMembersAfterSaving(bool toOriginal) noexcept
Update the member variables mIsRestored and mIsCreated after saving.
Definition: smartfile.cpp:110
void removeFile(bool original)
Remove the file from the file system.
Definition: smartfile.cpp:87
virtual ~SmartFile() noexcept
The destructor.
Definition: smartfile.cpp:72
The abstract SmartFile class represents a file and provides some useful methods to work with that fil...
Definition: smartfile.h:59
const FilePath & prepareSaveAndReturnFilePath(bool toOriginal)
Prepare to save the file and return the filepath to the file.
Definition: smartfile.cpp:102
bool isReadOnly() const noexcept
Check if this file was opened in read-only mode.
Definition: smartfile.h:117
FilePath mFilePath
The filepath which was passed to the constructor.
Definition: smartfile.h:184
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:130
bool mIsRestored
This variable determines whether the file was restored or not.
Definition: smartfile.h:208
FilePath mOpenedFilePath
The filepath from where the content was loaded.
Definition: smartfile.h:197
bool mIsCreated
If true, the file was created and not yet written to the filesystem (so the file mFilePath does not y...
Definition: smartfile.h:222