63 lines
1.7 KiB
C++
63 lines
1.7 KiB
C++
#include "FormatRegistry.h"
|
|
|
|
#include "EntryImageProvider.h"
|
|
#include "backend/FormatReader.h"
|
|
#include "backend/formats/comic/ComicReader.h"
|
|
|
|
#include <QFileInfo>
|
|
#include <QQmlEngine>
|
|
|
|
FormatSpec FormatSpec::comic(const QString &ext, bool matchesDirectory, const QString &scheme,
|
|
SourceFactory makeSource)
|
|
{
|
|
return FormatSpec{
|
|
ext,
|
|
matchesDirectory,
|
|
scheme,
|
|
[makeSource, scheme] { return std::make_unique<ComicReader>(makeSource, scheme); },
|
|
makeSource
|
|
};
|
|
}
|
|
|
|
FormatRegistry::FormatRegistry(QVector<FormatSpec> 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 ext = fi.suffix().toLower();
|
|
|
|
for (const FormatSpec &spec : m_specs) {
|
|
if (isDir ? spec.matchesDirectory : (!spec.matchesDirectory && spec.extension == ext))
|
|
return &spec;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::unique_ptr<FormatReader> FormatRegistry::createReader(const QString &filePath) const
|
|
{
|
|
const FormatSpec *spec = specFor(filePath);
|
|
return spec ? spec->makeReader() : 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
|
|
{
|
|
for (const FormatSpec &spec : m_specs) {
|
|
if (!spec.scheme.isEmpty() && spec.makeSource)
|
|
engine.addImageProvider(spec.scheme,
|
|
new EntryImageProvider(spec.makeSource, spec.scheme));
|
|
}
|
|
}
|