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
48QT_BEGIN_NAMESPACE
49
50
51class QDirIterator;
52class QDirPrivate;
53
54class Q_CORE_EXPORT QDir
55{
56public:
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#ifdef Q_COMPILER_RVALUE_REFS
113 QDir &operator=(QDir &&other) Q_DECL_NOTHROW { swap(other); return *this; }
114#endif
115
116 void swap(QDir &other) Q_DECL_NOTHROW
117 { qSwap(d_ptr, other.d_ptr); }
118
119 void setPath(const QString &path);
120 QString path() const;
121 QString absolutePath() const;
122 QString canonicalPath() const;
123
124#if QT_DEPRECATED_SINCE(5, 13)
125 QT_DEPRECATED_X("Use QDir::addSearchPath() instead")
126 static void addResourceSearchPath(const QString &path);
127#endif
128
129 static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
130 static void addSearchPath(const QString &prefix, const QString &path);
131 static QStringList searchPaths(const QString &prefix);
132
133 QString dirName() const;
134 QString filePath(const QString &fileName) const;
135 QString absoluteFilePath(const QString &fileName) const;
136 QString relativeFilePath(const QString &fileName) const;
137
138 static QString toNativeSeparators(const QString &pathName);
139 static QString fromNativeSeparators(const QString &pathName);
140
141 bool cd(const QString &dirName);
142 bool cdUp();
143
144 QStringList nameFilters() const;
145 void setNameFilters(const QStringList &nameFilters);
146
147 Filters filter() const;
148 void setFilter(Filters filter);
149 SortFlags sorting() const;
150 void setSorting(SortFlags sort);
151
152 uint count() const;
153 bool isEmpty(Filters filters = Filters(AllEntries | NoDotAndDotDot)) const;
154
155 QString operator[](int) const;
156
157 static QStringList nameFiltersFromString(const QString &nameFilter);
158
159 QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
160 QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
161 SortFlags sort = NoSort) const;
162
163 QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
164 QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
165 SortFlags sort = NoSort) const;
166
167 bool mkdir(const QString &dirName) const;
168 bool rmdir(const QString &dirName) const;
169 bool mkpath(const QString &dirPath) const;
170 bool rmpath(const QString &dirPath) const;
171
172 bool removeRecursively();
173
174 bool isReadable() const;
175 bool exists() const;
176 bool isRoot() const;
177
178 static bool isRelativePath(const QString &path);
179 inline static bool isAbsolutePath(const QString &path) { return !isRelativePath(path); }
180 bool isRelative() const;
181 inline bool isAbsolute() const { return !isRelative(); }
182 bool makeAbsolute();
183
184 bool operator==(const QDir &dir) const;
185 inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
186
187 bool remove(const QString &fileName);
188 bool rename(const QString &oldName, const QString &newName);
189 bool exists(const QString &name) const;
190
191 static QFileInfoList drives();
192
193 Q_DECL_CONSTEXPR static inline QChar listSeparator() Q_DECL_NOTHROW
194 {
195#if defined(Q_OS_WIN)
196 return QLatin1Char(';');
197#else
198 return QLatin1Char(':');
199#endif
200 }
201
202 static QChar separator(); // ### Qt6: Make it inline
203
204 static bool setCurrent(const QString &path);
205 static inline QDir current() { return QDir(currentPath()); }
206 static QString currentPath();
207
208 static inline QDir home() { return QDir(homePath()); }
209 static QString homePath();
210 static inline QDir root() { return QDir(rootPath()); }
211 static QString rootPath();
212 static inline QDir temp() { return QDir(tempPath()); }
213 static QString tempPath();
214
215#if QT_CONFIG(regularexpression)
216 static bool match(const QStringList &filters, const QString &fileName);
217 static bool match(const QString &filter, const QString &fileName);
218#endif
219
220 static QString cleanPath(const QString &path);
221 void refresh() const;
222
223protected:
224 explicit QDir(QDirPrivate &d);
225
226 QSharedDataPointer<QDirPrivate> d_ptr;
227
228private:
229 friend class QDirIterator;
230 // Q_DECLARE_PRIVATE equivalent for shared data pointers
231 QDirPrivate* d_func();
232 inline const QDirPrivate* d_func() const
233 {
234 return d_ptr.constData();
235 }
236
237};
238
239Q_DECLARE_SHARED(QDir)
240Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::Filters)
241Q_DECLARE_OPERATORS_FOR_FLAGS(QDir::SortFlags)
242
243#ifndef QT_NO_DEBUG_STREAM
244class QDebug;
245Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters);
246Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QDir &dir);
247#endif
248
249QT_END_NAMESPACE
250
251#endif // QDIR_H
252