1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QDIR_H
41 #define QDIR_H
42 
43 #include <QtCore/qstring.h>
44 #include <QtCore/qfileinfo.h>
45 #include <QtCore/qstringlist.h>
46 #include <QtCore/qshareddata.h>
47 
48 QT_BEGIN_NAMESPACE
49 
50 
51 class QDirIterator;
52 class QDirPrivate;
53 
54 class Q_CORE_EXPORT QDir
55 {
56 public:
57     enum Filter { Dirs        = 0x001,
58                   Files       = 0x002,
59                   Drives      = 0x004,
60                   NoSymLinks  = 0x008,
61                   AllEntries  = Dirs | Files | Drives,
62                   TypeMask    = 0x00f,
63 
64                   Readable    = 0x010,
65                   Writable    = 0x020,
66                   Executable  = 0x040,
67                   PermissionMask    = 0x070,
68 
69                   Modified    = 0x080,
70                   Hidden      = 0x100,
71                   System      = 0x200,
72 
73                   AccessMask  = 0x3F0,
74 
75                   AllDirs       = 0x400,
76                   CaseSensitive = 0x800,
77                   NoDot         = 0x2000,
78                   NoDotDot      = 0x4000,
79                   NoDotAndDotDot = NoDot | NoDotDot,
80 
81                   NoFilter = -1
82     };
83     Q_DECLARE_FLAGS(Filters, Filter)
84 
85     enum SortFlag { Name        = 0x00,
86                     Time        = 0x01,
87                     Size        = 0x02,
88                     Unsorted    = 0x03,
89                     SortByMask  = 0x03,
90 
91                     DirsFirst   = 0x04,
92                     Reversed    = 0x08,
93                     IgnoreCase  = 0x10,
94                     DirsLast    = 0x20,
95                     LocaleAware = 0x40,
96                     Type        = 0x80,
97                     NoSort = -1
98     };
99     Q_DECLARE_FLAGS(SortFlags, SortFlag)
100 
101     QDir(const QDir &);
102     QDir(const QString &path = QString());
103     QDir(const QString &path, const QString &nameFilter,
104          SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
105     ~QDir();
106 
107     QDir &operator=(const QDir &);
108 #if QT_DEPRECATED_SINCE(5, 13)
109     QT_DEPRECATED_X("Use QDir::setPath() instead")
110     QDir &operator=(const QString &path);
111 #endif
112     QDir &operator=(QDir &&other) noexcept { swap(other); return *this; }
113 
swap(QDir & other)114     void swap(QDir &other) noexcept
115     { qSwap(d_ptr, other.d_ptr); }
116 
117     void setPath(const QString &path);
118     QString path() const;
119     QString absolutePath() const;
120     QString canonicalPath() const;
121 
122 #if QT_DEPRECATED_SINCE(5, 13)
123     QT_DEPRECATED_X("Use QDir::addSearchPath() instead")
124     static void addResourceSearchPath(const QString &path);
125 #endif
126 
127     static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
128     static void addSearchPath(const QString &prefix, const QString &path);
129     static QStringList searchPaths(const QString &prefix);
130 
131     QString dirName() const;
132     QString filePath(const QString &fileName) const;
133     QString absoluteFilePath(const QString &fileName) const;
134     QString relativeFilePath(const QString &fileName) const;
135 
136     static QString toNativeSeparators(const QString &pathName);
137     static QString fromNativeSeparators(const QString &pathName);
138 
139     bool cd(const QString &dirName);
140     bool cdUp();
141 
142     QStringList nameFilters() const;
143     void setNameFilters(const QStringList &nameFilters);
144 
145     Filters filter() const;
146     void setFilter(Filters filter);
147     SortFlags sorting() const;
148     void setSorting(SortFlags sort);
149 
150     uint count() const;
151     bool isEmpty(Filters filters = Filters(AllEntries | NoDotAndDotDot)) const;
152 
153     QString operator[](int) const;
154 
155     static QStringList nameFiltersFromString(const QString &nameFilter);
156 
157     QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
158     QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
159                           SortFlags sort = NoSort) const;
160 
161     QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
162     QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
163                                 SortFlags sort = NoSort) const;
164 
165     bool mkdir(const QString &dirName) const;
166     bool rmdir(const QString &dirName) const;
167     bool mkpath(const QString &dirPath) const;
168     bool rmpath(const QString &dirPath) const;
169 
170     bool removeRecursively();
171 
172     bool isReadable() const;
173     bool exists() const;
174     bool isRoot() const;
175 
176     static bool isRelativePath(const QString &path);
isAbsolutePath(const QString & path)177     inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
178     bool isRelative() const;
isAbsolute()179     inline bool isAbsolute() const { return !isRelative(); }
180     bool makeAbsolute();
181 
182     bool operator==(const QDir &dir) const;
183     inline bool operator!=(const QDir &dir) const {  return !operator==(dir); }
184 
185     bool remove(const QString &fileName);
186     bool rename(const QString &oldName, const QString &newName);
187     bool exists(const QString &name) const;
188 
189     static QFileInfoList drives();
190 
listSeparator()191     Q_DECL_CONSTEXPR static inline QChar listSeparator() noexcept
192     {
193 #if defined(Q_OS_WIN)
194         return QLatin1Char(';');
195 #else
196         return QLatin1Char(':');
197 #endif
198     }
199 
200     static QChar separator(); // ### Qt6: Make it inline
201 
202     static bool setCurrent(const QString &path);
current()203     static inline QDir current() { return QDir(currentPath()); }
204     static QString currentPath();
205 
home()206     static inline QDir home() { return QDir(homePath()); }
207     static QString homePath();
root()208     static inline QDir root() { return QDir(rootPath()); }
209     static QString rootPath();
temp()210     static inline QDir temp() { return QDir(tempPath()); }
211     static QString tempPath();
212 
213 #if QT_CONFIG(regularexpression)
214     static bool match(const QStringList &filters, const QString &fileName);
215     static bool match(const QString &filter, const QString &fileName);
216 #endif
217 
218     static QString cleanPath(const QString &path);
219     void refresh() const;
220 
221 protected:
222     explicit QDir(QDirPrivate &d);
223 
224     QSharedDataPointer<QDirPrivate> d_ptr;
225 
226 private:
227     friend class QDirIterator;
228     // Q_DECLARE_PRIVATE equivalent for shared data pointers
229     QDirPrivate* d_func();
d_func()230     inline const QDirPrivate* d_func() const
231     {
232         return d_ptr.constData();
233     }
234 
235 };
236 
237 Q_DECLARE_SHARED(QDir)
238 Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters)
239 Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags)
240 
241 #ifndef QT_NO_DEBUG_STREAM
242 class QDebug;
243 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters);
244 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir);
245 #endif
246 
247 QT_END_NAMESPACE
248 
249 #endif // QDIR_H
250