| 1 |
// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#ifndef DTK_NO_PROJECT |
| 8 |
#include <dtkcore_global.h> |
| 9 |
#else |
| 10 |
#define DCORE_BEGIN_NAMESPACE |
| 11 |
#define DCORE_END_NAMESPACE |
| 12 |
#endif |
| 13 |
#include <private/qabstractfileengine_p.h> |
| 14 |
|
| 15 |
#include <QSharedPointer> |
| 16 |
#include <QDateTime> |
| 17 |
|
| 18 |
QT_BEGIN_NAMESPACE |
| 19 |
class QBuffer; |
| 20 |
QT_END_NAMESPACE |
| 21 |
|
| 22 |
DCORE_BEGIN_NAMESPACE |
| 23 |
|
| 24 |
class DDciFileEngineHandler : public QAbstractFileEngineHandler |
| 25 |
{ |
| 26 |
public: |
| 27 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) |
| 28 |
std::unique_ptr<QAbstractFileEngine> create(const QString &fileName) const override; |
| 29 |
#else |
| 30 |
QAbstractFileEngine *create(const QString &fileName) const override; |
| 31 |
#endif |
| 32 |
}; |
| 33 |
|
| 34 |
class DDciFile; |
| 35 |
using DDciFileShared = QSharedPointer<DDciFile>; |
| 36 |
class DDciFileEngineIterator : public QAbstractFileEngineIterator |
| 37 |
{ |
| 38 |
friend class DDciFileEngine; |
| 39 |
public: |
| 40 |
DDciFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters); |
| 41 |
|
| 42 |
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) |
| 43 |
QString next() override; |
| 44 |
bool hasNext() const override; |
| 45 |
#else |
| 46 |
DDciFileEngineIterator(QDirListing::IteratorFlags filters, const QStringList &nameFilters); |
| 47 |
bool advance() override; |
| 48 |
#endif |
| 49 |
|
| 50 |
QString currentFileName() const override; |
| 51 |
|
| 52 |
private: |
| 53 |
mutable DDciFileShared file; |
| 54 |
mutable QStringList list; |
| 55 |
mutable int nextValid = -1; |
| 56 |
int current = -1; |
| 57 |
}; |
| 58 |
|
| 59 |
class DDciFileEngine : public QAbstractFileEngine |
| 60 |
{ |
| 61 |
friend class DDciFileEngineIterator; |
| 62 |
public: |
| 63 |
explicit DDciFileEngine(const QString &fullPath); |
| 64 |
~DDciFileEngine(); |
| 65 |
|
| 66 |
bool isValid() const; |
| 67 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) |
| 68 |
bool open(QIODevice::OpenMode openMode, std::optional<QFile::Permissions> permissions = std::nullopt) override; |
| 69 |
#else |
| 70 |
bool open(QIODevice::OpenMode openMode) override; |
| 71 |
#endif |
| 72 |
bool close() override; |
| 73 |
bool flushToFile(QFile *target, bool writeFile) const; |
| 74 |
bool flush() override; |
| 75 |
bool syncToDisk() override; |
| 76 |
|
| 77 |
qint64 size() const override; |
| 78 |
qint64 pos() const override; |
| 79 |
bool seek(qint64 pos) override; |
| 80 |
bool isSequential() const override; |
| 81 |
bool remove() override; |
| 82 |
bool copy(const QString &newName) override; |
| 83 |
bool rename(const QString &newName) override; |
| 84 |
bool renameOverwrite(const QString &newName) override; |
| 85 |
bool link(const QString &newName) override; |
| 86 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) |
| 87 |
bool mkdir(const QString &dirName, |
| 88 |
bool createParentDirectories, |
| 89 |
std::optional<QFile::Permissions> permissions = std::nullopt) const override; |
| 90 |
#else |
| 91 |
bool mkdir(const QString &dirName, bool createParentDirectories) const override; |
| 92 |
#endif |
| 93 |
bool rmdir(const QString &dirName, bool recurseParentDirectories) const override; |
| 94 |
bool setSize(qint64 size) override; |
| 95 |
bool caseSensitive() const override; |
| 96 |
bool isRelativePath() const override; |
| 97 |
|
| 98 |
QByteArray id() const override; |
| 99 |
uint ownerId(FileOwner owner) const override; |
| 100 |
QString owner(FileOwner owner) const override; |
| 101 |
|
| 102 |
FileFlags fileFlags(FileFlags type = FileInfoAll) const override; |
| 103 |
QString fileName(FileName file = DefaultName) const override; |
| 104 |
|
| 105 |
void setFileName(const QString &fullPath) override; |
| 106 |
|
| 107 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 1) |
| 108 |
QDateTime fileTime(QFile::FileTime time) const override; |
| 109 |
#else |
| 110 |
QDateTime fileTime(FileTime time) const override; |
| 111 |
#endif |
| 112 |
|
| 113 |
typedef DDciFileEngineIterator Iterator; |
| 114 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 1) |
| 115 |
IteratorUniquePtr beginEntryList(const QString &path, QDirListing::IteratorFlags filters, const QStringList &filterNames) override; |
| 116 |
IteratorUniquePtr endEntryList() override; |
| 117 |
#elif QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) |
| 118 |
IteratorUniquePtr beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames) override; |
| 119 |
IteratorUniquePtr endEntryList() override; |
| 120 |
#else |
| 121 |
Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames) override; |
| 122 |
Iterator *endEntryList() override; |
| 123 |
#endif |
| 124 |
|
| 125 |
qint64 read(char *data, qint64 maxlen) override; |
| 126 |
qint64 write(const char *data, qint64 len) override; |
| 127 |
|
| 128 |
bool extension(Extension extension, const ExtensionOption *option = 0, |
| 129 |
ExtensionReturn *output = 0) override; |
| 130 |
bool supportsExtension(Extension extension) const override; |
| 131 |
|
| 132 |
bool cloneTo(QAbstractFileEngine *target) override; |
| 133 |
|
| 134 |
private: |
| 135 |
bool forceSave(bool writeFile = false) const; |
| 136 |
|
| 137 |
/* |
| 138 |
* fullPath 格式:"dci:" + "真实文件路径" + "DCI 内部文件的路径" |
| 139 |
* 例如:"dci:/home/user/test.dci/subfile.png" |
| 140 |
* 其中 "/home/user/test.dci" 为真实文件路径,"/subfile.png" |
| 141 |
* 是 DCI 文件的内部路径。 |
| 142 |
* 函数返回的第一个数据是"真实文件路径"。 |
| 143 |
*/ |
| 144 |
static QPair<QString, QString> resolvePath(const QString &fullPath, |
| 145 |
const QString &realFilePath = QString(), |
| 146 |
bool needRealFileExists = true); |
| 147 |
|
| 148 |
DDciFileShared file; |
| 149 |
QString dciFilePath; |
| 150 |
QFile realDciFile; |
| 151 |
QString subfilePath; |
| 152 |
|
| 153 |
QByteArray fileData; |
| 154 |
QBuffer *fileBuffer = nullptr; |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
DCORE_END_NAMESPACE |