File indexing completed on 2024-05-05 17:04:27

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2012 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  This program is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation; either version 2 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #include "RecentImageImageProvider.h"
0020 #include <QFile>
0021 #include <QDir>
0022 #include <QProcess>
0023 #include <QApplication>
0024 #include <QImage>
0025 #include <QImageReader>
0026 #include <QPainter>
0027 
0028 #include <KoStore.h>
0029 #include <KoDocument.h>
0030 #include <KoPart.h>
0031 
0032 RecentImageImageProvider::RecentImageImageProvider()
0033     : QQuickImageProvider(QQuickImageProvider::Image)
0034 {
0035 }
0036 
0037 QImage RecentImageImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
0038 {
0039     int width = 512;
0040     int height = 512;
0041     if(id.endsWith(QLatin1String("odt"), Qt::CaseInsensitive) ||
0042        id.endsWith(QLatin1String("doc"), Qt::CaseInsensitive) ||
0043        id.endsWith(QLatin1String("docx"), Qt::CaseInsensitive)) {
0044         width *= 0.72413793;
0045     } else {
0046         height *= 0.72413793;
0047     }
0048 
0049     if (size) {
0050         *size = QSize(width, height);
0051     }
0052 
0053     QSize sz(requestedSize.width() > 0 ? requestedSize.width() : width,
0054              requestedSize.height() > 0 ? requestedSize.height() : height);
0055 
0056     QFile f(id);
0057     QImage thumbnail(sz, QImage::Format_ARGB32_Premultiplied);
0058     thumbnail.fill(Qt::white);
0059 
0060     if (f.exists()) {
0061         // try to use any embedded thumbnail
0062         KoStore *store = KoStore::createStore(id, KoStore::Read);
0063 
0064         bool thumbnailFound = false;
0065         if (store &&
0066             // ODF thumbnail?
0067             (store->open(QLatin1String("Thumbnails/thumbnail.png")) ||
0068             // old KOffice/Calligra thumbnail?
0069             store->open(QLatin1String("preview.png")) ||
0070             // OOXML?
0071             store->open(QLatin1String("docProps/thumbnail.jpeg")))) {
0072             // Hooray! No long delay for the user...
0073             const QByteArray thumbnailData = store->read(store->size());
0074 
0075             QImage thumbnailImage;
0076             if (thumbnailImage.loadFromData(thumbnailData) ){//&&
0077                 //thumbnailImage.width() >= width && thumbnailImage.height() >= height) {
0078                 // put a white background behind the thumbnail
0079                 // as lots of old(?) OOo files have thumbnails with transparent background
0080                 thumbnail = QImage(thumbnailImage.size(), QImage::Format_RGB32);
0081                 thumbnail.fill(QColor(Qt::white).rgb());
0082                 QPainter p(&thumbnail);
0083                 p.drawImage(QPoint(0, 0), thumbnailImage);
0084                 p.end();
0085                 thumbnail = thumbnail.scaled(sz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0086                 delete store;
0087                 thumbnailFound = true;
0088             }
0089         }
0090 
0091          if(!thumbnailFound) {
0092             // load document and render the thumbnail ourselves
0093             QProcess thumbnailer;
0094             QString thumbnailerProgram = QString("%1%2calligrageminithumbnailhelper").arg(qApp->applicationDirPath()).arg(QDir::separator());
0095             QStringList arguments;
0096             arguments << "--in" << id;
0097             QString thumbFile = id;
0098             thumbFile.replace("/", "-").replace("\\", "-");
0099             thumbFile.prepend(QDir::separator()).prepend(QDir::tempPath());
0100             thumbFile.append(".png");
0101             arguments << "--out" << thumbFile;
0102             arguments << "--width" << QString::number(sz.width());
0103             arguments << "--height" << QString::number(sz.height());
0104             bool fileExists = QFile::exists(thumbFile);
0105             if(!fileExists)
0106                 thumbnailer.start(thumbnailerProgram, arguments);
0107             if(fileExists || thumbnailer.waitForFinished(3000)) {
0108                 thumbnail.load(thumbFile);
0109                 thumbnail = thumbnail.scaled(sz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0110             }
0111             else {
0112                 // some error, final failure...
0113                 qDebug() << "Failed completely to find a preview for" << id;
0114             }
0115         }
0116     }
0117     return thumbnail;
0118 }