LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
filepath.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_FILEPATH_H
21 #define LIBREPCB_FILEPATH_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 /*******************************************************************************
29  * Namespace / Forward Declarations
30  ******************************************************************************/
31 namespace librepcb {
32 
33 /*******************************************************************************
34  * Class FilePath
35  ******************************************************************************/
36 
130 class FilePath final {
131 public: // Types
133  // spaces
135  ReplaceSpaces = 1 << 1,
136  // case
137  KeepCase = 0,
138  ToLowerCase = 1 << 2,
139  ToUpperCase = 1 << 3,
140  // default
142  };
143  Q_DECLARE_FLAGS(CleanFileNameOptions, CleanFileNameOption);
144 
145 public: // Methods
146  // Constructors / Destructor
147 
151  FilePath() noexcept;
152 
158  explicit FilePath(const QString& filepath) noexcept;
159 
165  FilePath(const FilePath& other) noexcept;
166 
167  // Setters
168 
180  bool setPath(const QString& filepath) noexcept;
181 
182  // Getters
183 
189  bool isValid() const noexcept { return mIsValid; }
190 
196  bool isExistingFile() const noexcept;
197 
203  bool isExistingDir() const noexcept;
204 
211  bool isEmptyDir() const noexcept;
212 
218  bool isRoot() const noexcept;
219 
226  bool isLocatedInDir(const FilePath& dir) const noexcept;
227 
234  QString toStr() const noexcept;
235 
243  QString toNative() const noexcept;
244 
260  FilePath toUnique() const noexcept;
261 
277  QString toRelative(const FilePath& base) const noexcept;
278 
284  QUrl toQUrl() const noexcept { return QUrl::fromLocalFile(toStr()); }
285 
291  QString getBasename() const noexcept;
292 
298  QString getCompleteBasename() const noexcept;
299 
305  QString getSuffix() const noexcept;
306 
312  QString getCompleteSuffix() const noexcept;
313 
319  QString getFilename() const noexcept;
320 
327  FilePath getParentDir() const noexcept;
328 
343  FilePath getPathTo(const QString& filename) const noexcept;
344 
345  // Static Methods
346 
362  static FilePath fromRelative(const FilePath& base,
363  const QString& relative) noexcept;
364 
372  static FilePath getTempPath() noexcept;
373 
382  static FilePath getApplicationTempPath() noexcept;
383 
389  static FilePath getRandomTempPath() noexcept;
390 
414  static QString cleanFileName(const QString& userInput,
415  CleanFileNameOptions options) noexcept;
416 
417  // Operator Overloadings
418 
422  FilePath& operator=(const FilePath& rhs) noexcept;
423 
431  bool operator==(const FilePath& rhs) const noexcept;
432 
440  bool operator!=(const FilePath& rhs) const noexcept;
441 
442 private:
443  // Private Methods
444 
454  static QString makeWellFormatted(const QString& filepath) noexcept;
455 
456  // Attributes
457 
458  bool mIsValid;
459  QFileInfo
461 };
462 
463 // Non-Member Functions
464 QDataStream& operator<<(QDataStream& stream, const FilePath& filepath);
465 QDebug& operator<<(QDebug& stream, const FilePath& filepath);
466 inline uint qHash(const FilePath& key, uint seed) noexcept {
467  return qHash(key.toStr(), seed);
468 }
469 
470 /*******************************************************************************
471  * End of File
472  ******************************************************************************/
473 
474 } // namespace librepcb
475 
476 Q_DECLARE_METATYPE(librepcb::FilePath)
477 Q_DECLARE_OPERATORS_FOR_FLAGS(librepcb::FilePath::CleanFileNameOptions)
478 
479 #endif // LIBREPCB_FILEPATH_H
Definition: filepath.h:139
bool isExistingFile() const noexcept
Check if the specified filepath is an existing file.
Definition: filepath.cpp:65
FilePath() noexcept
The default constructor (this will create an invalid object!)
Definition: filepath.cpp:36
bool isLocatedInDir(const FilePath &dir) const noexcept
Check if the filepath is located inside another directory.
Definition: filepath.cpp:91
static QString cleanFileName(const QString &userInput, CleanFileNameOptions options) noexcept
Clean a given string so that it becomes a valid filename.
Definition: filepath.cpp:221
QString getCompleteBasename() const noexcept
Get the complete basename of the file or directory.
Definition: filepath.cpp:133
QString getFilename() const noexcept
Get the whole filename (without the path) of the file or directory.
Definition: filepath.cpp:154
QString getBasename() const noexcept
Get the basename of the file or directory.
Definition: filepath.cpp:126
bool isRoot() const noexcept
Check if the specified filepath is the root directory.
Definition: filepath.cpp:85
Definition: filepath.h:137
static FilePath fromRelative(const FilePath &base, const QString &relative) noexcept
Build an absolute and well-formatted filepath from a relative filepath.
Definition: filepath.cpp:195
bool mIsValid
Definition: filepath.h:458
static FilePath getRandomTempPath() noexcept
Get a random temporary directory path (e.g. "/tmp/librepcb/42")
Definition: filepath.cpp:215
CleanFileNameOption
Definition: filepath.h:132
QString toRelative(const FilePath &base) const noexcept
Convert an absolute filepath to a relative filepath (relative to another filepath) ...
Definition: filepath.cpp:119
FilePath toUnique() const noexcept
Get a unique version of the filepath (resolve symbolic links if possible)
Definition: filepath.cpp:109
QFileInfo mFileInfo
the absolute and well-formatted filepath in a QFileInfo
Definition: filepath.h:460
bool isExistingDir() const noexcept
Check if the specified filepath is an existing directory.
Definition: filepath.cpp:71
FilePath getParentDir() const noexcept
Get the filepath of the parent directory of the file or directory.
Definition: filepath.cpp:161
bool isValid() const noexcept
Check whether this object contains a valid filepath or not.
Definition: filepath.h:189
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:130
QString toNative() const noexcept
Get the absolute filepath with native directory separators ("/" resp. "\")
Definition: filepath.cpp:103
bool isEmptyDir() const noexcept
Check if the specified filepath is an existing, empty directory.
Definition: filepath.cpp:77
Definition: filepath.h:134
FilePath getPathTo(const QString &filename) const noexcept
Get the filepath to a file or directory which is relative to this filepath.
Definition: filepath.cpp:167
static FilePath getApplicationTempPath() noexcept
Get the path to the temporary application directory (e.g. "/tmp/librepcb")
Definition: filepath.cpp:211
bool setPath(const QString &filepath) noexcept
Set a new filepath.
Definition: filepath.cpp:55
QString getCompleteSuffix() const noexcept
Get the complete suffix of the file or directory.
Definition: filepath.cpp:147
Q_DECLARE_FLAGS(CleanFileNameOptions, CleanFileNameOption)
static FilePath getTempPath() noexcept
Get the path to the temporary directory (e.g. "/tmp" on Unix/Linux)
Definition: filepath.cpp:202
Definition: filepath.h:141
QString getSuffix() const noexcept
Get the suffix of the file or directory.
Definition: filepath.cpp:140
static QString makeWellFormatted(const QString &filepath) noexcept
Make a filepath well-formatted (except making it absolute!)
Definition: filepath.cpp:240
Definition: filepath.h:135
QString toStr() const noexcept
Get the absolute and well-formatted filepath as a QString.
Definition: filepath.cpp:97
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119
Definition: filepath.h:138
QUrl toQUrl() const noexcept
Create and return a QUrl object with this filepath.
Definition: filepath.h:284