1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QFILEINFO_H
5#define QFILEINFO_H
6
7#include <QtCore/qcompare.h>
8#include <QtCore/qfile.h>
9#include <QtCore/qlist.h>
10#include <QtCore/qshareddata.h>
11#include <QtCore/qmetatype.h>
12#include <QtCore/qdatetime.h>
13#include <QtCore/qtimezone.h>
14
15QT_BEGIN_NAMESPACE
16
17
18class QDir;
19class QDirIteratorPrivate;
20class QFileInfoPrivate;
21
22class Q_CORE_EXPORT QFileInfo
23{
24 friend class QDirIteratorPrivate;
25 friend class QDirListingPrivate;
26 friend class QFileInfoPrivate;
27public:
28 explicit QFileInfo(QFileInfoPrivate *d);
29
30#ifdef QT_IMPLICIT_QFILEINFO_CONSTRUCTION
31#define QFILEINFO_MAYBE_EXPLICIT Q_IMPLICIT
32#else
33#define QFILEINFO_MAYBE_EXPLICIT explicit
34#endif
35
36 QFileInfo();
37 QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QString &file);
38 QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QFileDevice &file);
39 QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QDir &dir, const QString &file);
40 QFileInfo(const QFileInfo &fileinfo);
41#ifdef Q_QDOC
42 QFileInfo(const std::filesystem::path &file);
43 QFileInfo(const QDir &dir, const std::filesystem::path &file);
44#elif QT_CONFIG(cxx17_filesystem)
45 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
46 QFILEINFO_MAYBE_EXPLICIT QFileInfo(const T &file) : QFileInfo(QtPrivate::fromFilesystemPath(path: file)) { }
47
48 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
49 QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QDir &dir, const T &file) : QFileInfo(dir, QtPrivate::fromFilesystemPath(path: file))
50 {
51 }
52#endif // QT_CONFIG(cxx17_filesystem)
53
54#undef QFILEINFO_MAYBE_EXPLICIT
55
56 ~QFileInfo();
57
58 QFileInfo &operator=(const QFileInfo &fileinfo);
59 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QFileInfo)
60
61 void swap(QFileInfo &other) noexcept
62 { d_ptr.swap(other&: other.d_ptr); }
63
64#if QT_CORE_REMOVED_SINCE(6, 8)
65 bool operator==(const QFileInfo &fileinfo) const;
66 inline bool operator!=(const QFileInfo &fileinfo) const { return !(operator==(fileinfo)); }
67#endif
68
69 void setFile(const QString &file);
70 void setFile(const QFileDevice &file);
71 void setFile(const QDir &dir, const QString &file);
72#ifdef Q_QDOC
73 void setFile(const std::filesystem::path &file);
74#elif QT_CONFIG(cxx17_filesystem)
75 template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
76 void setFile(const T &file) { setFile(QtPrivate::fromFilesystemPath(path: file)); }
77#endif // QT_CONFIG(cxx17_filesystem)
78
79 bool exists() const;
80 static bool exists(const QString &file);
81 void refresh();
82
83 QString filePath() const;
84 QString absoluteFilePath() const;
85 QString canonicalFilePath() const;
86#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
87 std::filesystem::path filesystemFilePath() const
88 { return QtPrivate::toFilesystemPath(path: filePath()); }
89 std::filesystem::path filesystemAbsoluteFilePath() const
90 { return QtPrivate::toFilesystemPath(path: absoluteFilePath()); }
91 std::filesystem::path filesystemCanonicalFilePath() const
92 { return QtPrivate::toFilesystemPath(path: canonicalFilePath()); }
93#endif // QT_CONFIG(cxx17_filesystem)
94 QString fileName() const;
95 QString baseName() const;
96 QString completeBaseName() const;
97 QString suffix() const;
98 QString bundleName() const;
99 QString completeSuffix() const;
100
101 QString path() const;
102 QString absolutePath() const;
103 QString canonicalPath() const;
104#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
105 std::filesystem::path filesystemPath() const { return QtPrivate::toFilesystemPath(path: path()); }
106 std::filesystem::path filesystemAbsolutePath() const
107 { return QtPrivate::toFilesystemPath(path: absolutePath()); }
108 std::filesystem::path filesystemCanonicalPath() const
109 { return QtPrivate::toFilesystemPath(path: canonicalPath()); }
110#endif // QT_CONFIG(cxx17_filesystem)
111 QDir dir() const;
112 QDir absoluteDir() const;
113
114 bool isReadable() const;
115 bool isWritable() const;
116 bool isExecutable() const;
117 bool isHidden() const;
118 bool isNativePath() const;
119
120 bool isRelative() const;
121 inline bool isAbsolute() const { return !isRelative(); }
122 bool makeAbsolute();
123
124 bool isFile() const;
125 bool isDir() const;
126 bool isSymLink() const;
127 bool isSymbolicLink() const;
128 bool isShortcut() const;
129 bool isAlias() const;
130 bool isJunction() const;
131 bool isRoot() const;
132 bool isBundle() const;
133
134 QString symLinkTarget() const;
135 QString readSymLink() const;
136 QString junctionTarget() const;
137
138#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
139 std::filesystem::path filesystemSymLinkTarget() const
140 { return QtPrivate::toFilesystemPath(path: symLinkTarget()); }
141
142 std::filesystem::path filesystemReadSymLink() const
143 { return QtPrivate::toFilesystemPath(path: readSymLink()); }
144
145 std::filesystem::path filesystemJunctionTarget() const
146 { return QtPrivate::toFilesystemPath(path: junctionTarget()); }
147#endif // QT_CONFIG(cxx17_filesystem)
148
149 QString owner() const;
150 uint ownerId() const;
151 QString group() const;
152 uint groupId() const;
153
154 bool permission(QFile::Permissions permissions) const;
155 QFile::Permissions permissions() const;
156
157 qint64 size() const;
158
159 QDateTime birthTime() const { return fileTime(time: QFile::FileBirthTime); }
160 QDateTime metadataChangeTime() const { return fileTime(time: QFile::FileMetadataChangeTime); }
161 QDateTime lastModified() const { return fileTime(time: QFile::FileModificationTime); }
162 QDateTime lastRead() const { return fileTime(time: QFile::FileAccessTime); }
163 QDateTime fileTime(QFile::FileTime time) const;
164
165 QDateTime birthTime(const QTimeZone &tz) const { return fileTime(time: QFile::FileBirthTime, tz); }
166 QDateTime metadataChangeTime(const QTimeZone &tz) const { return fileTime(time: QFile::FileMetadataChangeTime, tz); }
167 QDateTime lastModified(const QTimeZone &tz) const { return fileTime(time: QFile::FileModificationTime, tz); }
168 QDateTime lastRead(const QTimeZone &tz) const { return fileTime(time: QFile::FileAccessTime, tz); }
169 QDateTime fileTime(QFile::FileTime time, const QTimeZone &tz) const;
170
171 bool caching() const;
172 void setCaching(bool on);
173 void stat();
174
175protected:
176 QSharedDataPointer<QFileInfoPrivate> d_ptr;
177
178private:
179 friend Q_CORE_EXPORT bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs);
180 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QFileInfo)
181 QFileInfoPrivate* d_func();
182 inline const QFileInfoPrivate* d_func() const
183 {
184 return d_ptr.constData();
185 }
186};
187
188Q_DECLARE_SHARED(QFileInfo)
189
190typedef QList<QFileInfo> QFileInfoList;
191
192#ifndef QT_NO_DEBUG_STREAM
193Q_CORE_EXPORT QDebug operator<<(QDebug, const QFileInfo &);
194#endif
195
196QT_END_NAMESPACE
197
198QT_DECL_METATYPE_EXTERN(QFileInfo, Q_CORE_EXPORT)
199
200#endif // QFILEINFO_H
201

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/corelib/io/qfileinfo.h