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