Warning, file /office/calligra/gemini/RecentImageImageProvider.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2012 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "RecentImageImageProvider.h"
0008 #include <QFile>
0009 #include <QDir>
0010 #include <QProcess>
0011 #include <QApplication>
0012 #include <QImage>
0013 #include <QImageReader>
0014 #include <QPainter>
0015 
0016 #include <KoStore.h>
0017 #include <KoDocument.h>
0018 #include <KoPart.h>
0019 
0020 RecentImageImageProvider::RecentImageImageProvider()
0021     : QQuickImageProvider(QQuickImageProvider::Image)
0022 {
0023 }
0024 
0025 QImage RecentImageImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
0026 {
0027     int width = 512;
0028     int height = 512;
0029     if(id.endsWith(QLatin1String("odt"), Qt::CaseInsensitive) ||
0030        id.endsWith(QLatin1String("doc"), Qt::CaseInsensitive) ||
0031        id.endsWith(QLatin1String("docx"), Qt::CaseInsensitive)) {
0032         width *= 0.72413793;
0033     } else {
0034         height *= 0.72413793;
0035     }
0036 
0037     if (size) {
0038         *size = QSize(width, height);
0039     }
0040 
0041     QSize sz(requestedSize.width() > 0 ? requestedSize.width() : width,
0042              requestedSize.height() > 0 ? requestedSize.height() : height);
0043 
0044     QFile f(id);
0045     QImage thumbnail(sz, QImage::Format_ARGB32_Premultiplied);
0046     thumbnail.fill(Qt::white);
0047 
0048     if (f.exists()) {
0049         // try to use any embedded thumbnail
0050         KoStore *store = KoStore::createStore(id, KoStore::Read);
0051 
0052         bool thumbnailFound = false;
0053         if (store &&
0054             // ODF thumbnail?
0055             (store->open(QLatin1String("Thumbnails/thumbnail.png")) ||
0056             // old KOffice/Calligra thumbnail?
0057             store->open(QLatin1String("preview.png")) ||
0058             // OOXML?
0059             store->open(QLatin1String("docProps/thumbnail.jpeg")))) {
0060             // Hooray! No long delay for the user...
0061             const QByteArray thumbnailData = store->read(store->size());
0062 
0063             QImage thumbnailImage;
0064             if (thumbnailImage.loadFromData(thumbnailData) ){//&&
0065                 //thumbnailImage.width() >= width && thumbnailImage.height() >= height) {
0066                 // put a white background behind the thumbnail
0067                 // as lots of old(?) OOo files have thumbnails with transparent background
0068                 thumbnail = QImage(thumbnailImage.size(), QImage::Format_RGB32);
0069                 thumbnail.fill(QColor(Qt::white).rgb());
0070                 QPainter p(&thumbnail);
0071                 p.drawImage(QPoint(0, 0), thumbnailImage);
0072                 p.end();
0073                 thumbnail = thumbnail.scaled(sz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0074                 delete store;
0075                 thumbnailFound = true;
0076             }
0077         }
0078 
0079          if(!thumbnailFound) {
0080             // load document and render the thumbnail ourselves
0081             QProcess thumbnailer;
0082             QString thumbnailerProgram = QString("%1%2calligrageminithumbnailhelper").arg(qApp->applicationDirPath()).arg(QDir::separator());
0083             QStringList arguments;
0084             arguments << "--in" << id;
0085             QString thumbFile = id;
0086             thumbFile.replace("/", "-").replace("\\", "-");
0087             thumbFile.prepend(QDir::separator()).prepend(QDir::tempPath());
0088             thumbFile.append(".png");
0089             arguments << "--out" << thumbFile;
0090             arguments << "--width" << QString::number(sz.width());
0091             arguments << "--height" << QString::number(sz.height());
0092             bool fileExists = QFile::exists(thumbFile);
0093             if(!fileExists)
0094                 thumbnailer.start(thumbnailerProgram, arguments);
0095             if(fileExists || thumbnailer.waitForFinished(3000)) {
0096                 thumbnail.load(thumbFile);
0097                 thumbnail = thumbnail.scaled(sz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0098             }
0099             else {
0100                 // some error, final failure...
0101                 qDebug() << "Failed completely to find a preview for" << id;
0102             }
0103         }
0104     }
0105     return thumbnail;
0106 }