summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qdirentryinfo_p.h
blob: 7ed5391ff04a65cebb7f4221d78ac3010fa5c058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2024 Ahmad Samir <a.samirh78@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QDIRENTRYINFO_P_H
#define QDIRENTRYINFO_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/private/qfileinfo_p.h>
#include <QtCore/private/qfilesystementry_p.h>
#include <QtCore/private/qfilesystemmetadata_p.h>

QT_BEGIN_NAMESPACE

class QDirEntryInfo
{
    const QFileSystemMetaData &ensureFilled(QFileSystemMetaData::MetaDataFlags what)
    {
        if (!metaData.hasFlags(what))
            QFileSystemEngine::fillMetaData(entry, metaData, what);
        return metaData;
    }

public:
    const QFileInfo &fileInfo()
    {
        if (!fileInfoOpt) {
            fileInfoOpt.emplace(new QFileInfoPrivate(entry, metaData));
            metaData.clear();
        }
        return *fileInfoOpt;
    }

    QString fileName()
    { return fileInfoOpt ? fileInfoOpt->fileName() : entry.fileName(); }
    QString baseName()
    { return fileInfoOpt ? fileInfoOpt->baseName() : entry.baseName(); }
    QString completeBaseName() const
    { return fileInfoOpt ? fileInfoOpt->completeBaseName() : entry.completeBaseName(); }
    QString suffix() const
    { return fileInfoOpt ? fileInfoOpt->suffix() : entry.suffix(); }
    QString completeSuffix() const
    { return fileInfoOpt ? fileInfoOpt->completeSuffix() : entry.completeSuffix(); }
    QString filePath()
    { return fileInfoOpt ? fileInfoOpt->filePath() : entry.filePath(); }

    QString bundleName() { return fileInfo().bundleName(); }

    QString canonicalFilePath()
    {
        // QFileInfo caches these strings
        return fileInfo().canonicalFilePath();
    }

    QString absoluteFilePath()  {
        // QFileInfo caches these strings
        return fileInfo().absoluteFilePath();
    }

    QString absolutePath()  {
        // QFileInfo caches these strings
        return fileInfo().absolutePath();
    }


    bool isDir() {
        if (fileInfoOpt)
            return fileInfoOpt->isDir();

        return ensureFilled(QFileSystemMetaData::DirectoryType).isDirectory();
    }

    bool isFile() {
        if (fileInfoOpt)
            return fileInfoOpt->isFile();

        return ensureFilled(QFileSystemMetaData::FileType).isFile();
    }

    bool isSymLink() {
        if (fileInfoOpt)
            return fileInfoOpt->isSymLink();

        return ensureFilled(QFileSystemMetaData::LegacyLinkType).isLegacyLink();
    }

    bool isSymbolicLink() {
        if (fileInfoOpt)
            return fileInfoOpt->isSymbolicLink();

        return ensureFilled(QFileSystemMetaData::LinkType).isLink();
    }

    bool exists() {
        if (fileInfoOpt)
            return fileInfoOpt->exists();

        return ensureFilled(QFileSystemMetaData::ExistsAttribute).exists();
    }

    bool isHidden() {
        if (fileInfoOpt)
            return fileInfoOpt->isHidden();

        return ensureFilled(QFileSystemMetaData::HiddenAttribute).isHidden();
    }

    bool isReadable() {
        if (fileInfoOpt)
            return fileInfoOpt->isReadable();

        return ensureFilled(QFileSystemMetaData::UserReadPermission).isReadable();
    }

    bool isWritable() {
        if (fileInfoOpt)
            return fileInfoOpt->isWritable();

        return ensureFilled(QFileSystemMetaData::UserWritePermission).isWritable();
    }

    bool isExecutable() {
        if (fileInfoOpt)
            return fileInfoOpt->isExecutable();

        return ensureFilled(QFileSystemMetaData::UserExecutePermission).isExecutable();
    }

    qint64 size() { return fileInfo().size(); }

    QDateTime fileTime(QFile::FileTime type, const QTimeZone &tz)
    {
        return fileInfo().fileTime(type, tz);
    }

private:
    friend class QDirListingPrivate;
    friend class QDirListing;

    QFileSystemEntry entry;
    QFileSystemMetaData metaData;
    std::optional<QFileInfo> fileInfoOpt;
};

QT_END_NAMESPACE

#endif // QDIRENTRYINFO_P_H