| 1 | // Copyright (C) 2024 Ahmad Samir <a.samirh78@gmail.com> | 
|---|---|
| 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 QDIRENTRYINFO_P_H | 
| 5 | #define QDIRENTRYINFO_P_H | 
| 6 | |
| 7 | // | 
| 8 | // W A R N I N G | 
| 9 | // ------------- | 
| 10 | // | 
| 11 | // This file is not part of the Qt API. It exists purely as an | 
| 12 | // implementation detail. This header file may change from version to | 
| 13 | // version without notice, or even be removed. | 
| 14 | // | 
| 15 | // We mean it. | 
| 16 | // | 
| 17 | |
| 18 | #include <QtCore/private/qfileinfo_p.h> | 
| 19 | #include <QtCore/private/qfilesystementry_p.h> | 
| 20 | #include <QtCore/private/qfilesystemmetadata_p.h> | 
| 21 | |
| 22 | QT_BEGIN_NAMESPACE | 
| 23 | |
| 24 | class QDirEntryInfo | 
| 25 | { | 
| 26 | const QFileSystemMetaData &ensureFilled(QFileSystemMetaData::MetaDataFlags what) | 
| 27 | { | 
| 28 | if (!metaData.hasFlags(flags: what)) | 
| 29 | QFileSystemEngine::fillMetaData(entry, data&: metaData, what); | 
| 30 | return metaData; | 
| 31 | } | 
| 32 | |
| 33 | public: | 
| 34 | const QFileInfo &fileInfo() | 
| 35 | { | 
| 36 | if (!fileInfoOpt) { | 
| 37 | fileInfoOpt.emplace(args: new QFileInfoPrivate(entry, metaData)); | 
| 38 | metaData.clear(); | 
| 39 | } | 
| 40 | return *fileInfoOpt; | 
| 41 | } | 
| 42 | |
| 43 | QString fileName() | 
| 44 | { return fileInfoOpt ? fileInfoOpt->fileName() : entry.fileName(); } | 
| 45 | QString baseName() | 
| 46 | { return fileInfoOpt ? fileInfoOpt->baseName() : entry.baseName(); } | 
| 47 | QString completeBaseName() const | 
| 48 | { return fileInfoOpt ? fileInfoOpt->completeBaseName() : entry.completeBaseName(); } | 
| 49 | QString suffix() const | 
| 50 | { return fileInfoOpt ? fileInfoOpt->suffix() : entry.suffix(); } | 
| 51 | QString completeSuffix() const | 
| 52 | { return fileInfoOpt ? fileInfoOpt->completeSuffix() : entry.completeSuffix(); } | 
| 53 | QString filePath() | 
| 54 | { return fileInfoOpt ? fileInfoOpt->filePath() : entry.filePath(); } | 
| 55 | |
| 56 | QString bundleName() { return fileInfo().bundleName(); } | 
| 57 | |
| 58 | QString canonicalFilePath() | 
| 59 | { | 
| 60 | // QFileInfo caches these strings | 
| 61 | return fileInfo().canonicalFilePath(); | 
| 62 | } | 
| 63 | |
| 64 | QString absoluteFilePath() { | 
| 65 | // QFileInfo caches these strings | 
| 66 | return fileInfo().absoluteFilePath(); | 
| 67 | } | 
| 68 | |
| 69 | QString absolutePath() { | 
| 70 | // QFileInfo caches these strings | 
| 71 | return fileInfo().absolutePath(); | 
| 72 | } | 
| 73 | |
| 74 | |
| 75 | bool isDir() { | 
| 76 | if (fileInfoOpt) | 
| 77 | return fileInfoOpt->isDir(); | 
| 78 | |
| 79 | return ensureFilled(what: QFileSystemMetaData::DirectoryType).isDirectory(); | 
| 80 | } | 
| 81 | |
| 82 | bool isFile() { | 
| 83 | if (fileInfoOpt) | 
| 84 | return fileInfoOpt->isFile(); | 
| 85 | |
| 86 | return ensureFilled(what: QFileSystemMetaData::FileType).isFile(); | 
| 87 | } | 
| 88 | |
| 89 | bool isSymLink() { | 
| 90 | if (fileInfoOpt) | 
| 91 | return fileInfoOpt->isSymLink(); | 
| 92 | |
| 93 | return ensureFilled(what: QFileSystemMetaData::LegacyLinkType).isLegacyLink(); | 
| 94 | } | 
| 95 | |
| 96 | bool isSymbolicLink() { | 
| 97 | if (fileInfoOpt) | 
| 98 | return fileInfoOpt->isSymbolicLink(); | 
| 99 | |
| 100 | return ensureFilled(what: QFileSystemMetaData::LinkType).isLink(); | 
| 101 | } | 
| 102 | |
| 103 | bool exists() { | 
| 104 | if (fileInfoOpt) | 
| 105 | return fileInfoOpt->exists(); | 
| 106 | |
| 107 | return ensureFilled(what: QFileSystemMetaData::ExistsAttribute).exists(); | 
| 108 | } | 
| 109 | |
| 110 | bool isHidden() { | 
| 111 | if (fileInfoOpt) | 
| 112 | return fileInfoOpt->isHidden(); | 
| 113 | |
| 114 | return ensureFilled(what: QFileSystemMetaData::HiddenAttribute).isHidden(); | 
| 115 | } | 
| 116 | |
| 117 | bool isReadable() { | 
| 118 | if (fileInfoOpt) | 
| 119 | return fileInfoOpt->isReadable(); | 
| 120 | |
| 121 | return ensureFilled(what: QFileSystemMetaData::UserReadPermission).isReadable(); | 
| 122 | } | 
| 123 | |
| 124 | bool isWritable() { | 
| 125 | if (fileInfoOpt) | 
| 126 | return fileInfoOpt->isWritable(); | 
| 127 | |
| 128 | return ensureFilled(what: QFileSystemMetaData::UserWritePermission).isWritable(); | 
| 129 | } | 
| 130 | |
| 131 | bool isExecutable() { | 
| 132 | if (fileInfoOpt) | 
| 133 | return fileInfoOpt->isExecutable(); | 
| 134 | |
| 135 | return ensureFilled(what: QFileSystemMetaData::UserExecutePermission).isExecutable(); | 
| 136 | } | 
| 137 | |
| 138 | qint64 size() { return fileInfo().size(); } | 
| 139 | |
| 140 | QDateTime fileTime(QFile::FileTime type, const QTimeZone &tz) | 
| 141 | { | 
| 142 | return fileInfo().fileTime(time: type, tz); | 
| 143 | } | 
| 144 | |
| 145 | private: | 
| 146 | friend class QDirListingPrivate; | 
| 147 | friend class QDirListing; | 
| 148 | |
| 149 | QFileSystemEntry entry; | 
| 150 | QFileSystemMetaData metaData; | 
| 151 | std::optional<QFileInfo> fileInfoOpt; | 
| 152 | }; | 
| 153 | |
| 154 | QT_END_NAMESPACE | 
| 155 | |
| 156 | #endif // QDIRENTRYINFO_P_H | 
| 157 | 
