| 1 | // Copyright (C) 2018 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 QQMLPREVIEWFILEENGINE_H |
| 5 | #define QQMLPREVIEWFILEENGINE_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 "qqmlpreviewfileloader.h" |
| 19 | |
| 20 | #include <QtCore/qpointer.h> |
| 21 | #include <private/qabstractfileengine_p.h> |
| 22 | #include <private/qfsfileengine_p.h> |
| 23 | #include <QtCore/qbuffer.h> |
| 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class QQmlPreviewFileEngine : public QAbstractFileEngine |
| 30 | { |
| 31 | public: |
| 32 | QQmlPreviewFileEngine(const QString &file, const QString &absolute, |
| 33 | QQmlPreviewFileLoader *loader); |
| 34 | |
| 35 | void setFileName(const QString &file) override; |
| 36 | |
| 37 | bool open(QIODevice::OpenMode flags, std::optional<QFile::Permissions> permissions) override; |
| 38 | bool close() override; |
| 39 | qint64 size() const override; |
| 40 | qint64 pos() const override; |
| 41 | bool seek(qint64) override; |
| 42 | qint64 read(char *data, qint64 maxlen) override; |
| 43 | |
| 44 | FileFlags fileFlags(FileFlags type) const override; |
| 45 | QString fileName(QAbstractFileEngine::FileName file) const override; |
| 46 | uint ownerId(FileOwner) const override; |
| 47 | |
| 48 | IteratorUniquePtr beginEntryList(const QString &path, QDirListing::IteratorFlags filters, |
| 49 | const QStringList &filterNames) override; |
| 50 | IteratorUniquePtr endEntryList() override; |
| 51 | |
| 52 | // Forwarding to fallback if exists |
| 53 | bool flush() override; |
| 54 | bool syncToDisk() override; |
| 55 | bool isSequential() const override; |
| 56 | bool remove() override; |
| 57 | bool copy(const QString &newName) override; |
| 58 | bool rename(const QString &newName) override; |
| 59 | bool renameOverwrite(const QString &newName) override; |
| 60 | bool link(const QString &newName) override; |
| 61 | bool mkdir(const QString &dirName, bool createParentDirectories, |
| 62 | std::optional<QFile::Permissions> permissions = std::nullopt) const override; |
| 63 | bool rmdir(const QString &dirName, bool recurseParentDirectories) const override; |
| 64 | bool setSize(qint64 size) override; |
| 65 | bool caseSensitive() const override; |
| 66 | bool isRelativePath() const override; |
| 67 | QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const override; |
| 68 | bool setPermissions(uint perms) override; |
| 69 | QByteArray id() const override; |
| 70 | QString owner(FileOwner) const override; |
| 71 | QDateTime fileTime(QFile::FileTime time) const override; |
| 72 | int handle() const override; |
| 73 | qint64 readLine(char *data, qint64 maxlen) override; |
| 74 | qint64 write(const char *data, qint64 len) override; |
| 75 | bool extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output) override; |
| 76 | bool supportsExtension(Extension extension) const override; |
| 77 | |
| 78 | private: |
| 79 | void load() const; |
| 80 | |
| 81 | QString m_name; |
| 82 | QString m_absolute; |
| 83 | QPointer<QQmlPreviewFileLoader> m_loader; |
| 84 | |
| 85 | mutable QBuffer m_contents; |
| 86 | mutable QStringList m_entries; |
| 87 | mutable std::unique_ptr<QAbstractFileEngine> m_fallback; |
| 88 | mutable QQmlPreviewFileLoader::Result m_result = QQmlPreviewFileLoader::Unknown; |
| 89 | }; |
| 90 | |
| 91 | class QQmlPreviewFileEngineHandler : public QAbstractFileEngineHandler |
| 92 | { |
| 93 | Q_DISABLE_COPY_MOVE(QQmlPreviewFileEngineHandler) |
| 94 | public: |
| 95 | QQmlPreviewFileEngineHandler(QQmlPreviewFileLoader *loader); |
| 96 | std::unique_ptr<QAbstractFileEngine> create(const QString &fileName) const override; |
| 97 | |
| 98 | private: |
| 99 | QPointer<QQmlPreviewFileLoader> m_loader; |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | |
| 104 | QT_END_NAMESPACE |
| 105 | |
| 106 | #endif // QQMLPREVIEWFILEENGINE_H |
| 107 | |