File indexing completed on 2024-04-28 04:58:03

0001 /*  This file is part of the Calligra project.
0002     SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kritacreator.h"
0008 
0009 #include <KPluginFactory>
0010 #include <KZip>
0011 
0012 #include <QIODevice>
0013 #include <QImage>
0014 #include <memory>
0015 
0016 K_PLUGIN_CLASS_WITH_JSON(KritaCreator, "kraorathumbnail.json")
0017 
0018 KritaCreator::KritaCreator(QObject *parent, const QVariantList &args)
0019     : KIO::ThumbnailCreator(parent, args)
0020 {
0021 }
0022 
0023 KritaCreator::~KritaCreator()
0024 {
0025 }
0026 
0027 KIO::ThumbnailResult KritaCreator::create(const KIO::ThumbnailRequest &request)
0028 {
0029     // for now just rely on the rendered data inside the file,
0030     // do not load Krita code for rendering ourselves, as that currently (2.9)
0031     // means loading all plugins, resources etc.
0032     KZip zip(request.url().toLocalFile());
0033     if (!zip.open(QIODevice::ReadOnly)) {
0034         return KIO::ThumbnailResult::fail();
0035     }
0036 
0037     // first check if normal thumbnail is good enough
0038     // ORA thumbnail?
0039     const KArchiveFile *entry = zip.directory()->file(QLatin1String("Thumbnails/thumbnail.png"));
0040     if (!entry) {
0041         // KRA thumbnail
0042         entry = zip.directory()->file(QLatin1String("preview.png"));
0043     }
0044 
0045     if (!entry) {
0046         return KIO::ThumbnailResult::fail();
0047     }
0048 
0049     std::unique_ptr<QIODevice> fileDevice{entry->createDevice()};
0050     QImage image;
0051     bool thumbLoaded = image.load(fileDevice.get(), "PNG");
0052     // The requested size is a boundingbox, so meeting one size is sufficient
0053     if (thumbLoaded && ((image.width() >= request.targetSize().width()) || (image.height() >= request.targetSize().height()))) {
0054         return KIO::ThumbnailResult::pass(image);
0055     }
0056 
0057     entry = zip.directory()->file(QLatin1String("mergedimage.png"));
0058     if (entry) {
0059         QImage thumbnail;
0060         fileDevice.reset(entry->createDevice());
0061         thumbLoaded = thumbnail.load(fileDevice.get(), "PNG");
0062         if (thumbLoaded) {
0063             return KIO::ThumbnailResult::pass(thumbnail);
0064         }
0065     }
0066 
0067     return KIO::ThumbnailResult::fail();
0068 }
0069 
0070 #include "kritacreator.moc"
0071 #include "moc_kritacreator.cpp"