#include "FormatRegistry.h" #include "EntryImageProvider.h" #include "backend/FormatReader.h" #include "backend/formats/comic/ComicReader.h" #include "backend/formats/EpubReader.h" #include "backend/formats/Fb2Reader.h" #include "backend/formats/TxtReader.h" #include #include #include FormatSpec FormatSpec::comic(const QString &ext, bool matchesDirectory, const QString &scheme, SourceFactory makeSource) { return FormatSpec{ ext, matchesDirectory, scheme, [makeSource, scheme] { return std::make_unique(makeSource, scheme); }, makeSource }; } FormatSpec FormatSpec::epub(SourceFactory makeSource) { return FormatSpec{ QStringLiteral("epub"), false, QStringLiteral("epub"), [makeSource] { return std::make_unique(makeSource, QStringLiteral("epub")); }, makeSource }; } FormatSpec FormatSpec::fb2(const QString &ext, SourceFactory makeSource) { return FormatSpec{ ext, false, QStringLiteral("fb2"), [makeSource] { return std::make_unique(makeSource, QStringLiteral("fb2")); }, makeSource }; } FormatSpec FormatSpec::txt(const QString &ext) { return FormatSpec{ ext, false, QString(), [] { return std::make_unique(); }, nullptr }; } FormatRegistry::FormatRegistry(QVector specs) : m_specs(std::move(specs)) { } const FormatSpec *FormatRegistry::specFor(const QString &filePath) const { const QFileInfo fi(filePath); const bool isDir = fi.isDir(); const QString lowerPath = filePath.toLower(); for (const FormatSpec &spec : m_specs) { if (isDir) { if (spec.matchesDirectory) return &spec; } else if (!spec.matchesDirectory) { if (lowerPath.endsWith(QLatin1Char('.') + spec.extension.toLower())) return &spec; } } return nullptr; } std::unique_ptr FormatRegistry::createReader(const QString &filePath) const { const FormatSpec *spec = specFor(filePath); return spec ? spec->makeReader() : nullptr; } std::unique_ptr FormatRegistry::createByteSourceForScheme(const QString &scheme) const { for (const FormatSpec &spec : m_specs) { if (spec.scheme == scheme && spec.makeSource) return spec.makeSource(); } return nullptr; } QStringList FormatRegistry::supportedExtensions() const { QStringList out; for (const FormatSpec &spec : m_specs) if (!spec.matchesDirectory && !spec.extension.isEmpty()) out << spec.extension; return out; } void FormatRegistry::registerImageProviders(QQmlEngine &engine) const { QSet registeredSchemes; for (const FormatSpec &spec : m_specs) { if (!spec.scheme.isEmpty() && spec.makeSource && !registeredSchemes.contains(spec.scheme)) { registeredSchemes.insert(spec.scheme); engine.addImageProvider(spec.scheme, new EntryImageProvider(spec.makeSource, spec.scheme)); } } }