File indexing completed on 2024-05-12 16:28:29

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 KO GmbH <jos.van.den.oever@kogmbh.com>
0003    Copyright (C) 2010 Sven Langkamp <sven.langkamp@gmail.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include <OkularOdpGenerator.h>
0022 
0023 #include <QDebug>
0024 #include <QImage>
0025 #include <QPainter>
0026 #include <QMimeDatabase>
0027 #include <QMimeType>
0028 
0029 #include <KoDocumentEntry.h>
0030 #include <KoPart.h>
0031 #include <KoPADocument.h>
0032 #include <KoPAPageBase.h>
0033 #include <KoDocumentInfo.h>
0034 #include <KoGlobal.h>
0035 
0036 #include <okular/core/page.h>
0037 
0038 #include <KLocalizedString>
0039 
0040 
0041 OkularOdpGenerator::OkularOdpGenerator( QObject *parent, const QVariantList &args )
0042     : Okular::Generator( parent, args )
0043 {
0044     m_doc = 0;
0045 }
0046 
0047 OkularOdpGenerator::~OkularOdpGenerator()
0048 {
0049 }
0050 
0051 bool OkularOdpGenerator::loadDocument( const QString &fileName, QVector<Okular::Page*> &pages )
0052 {
0053     const QString mimetype = QMimeDatabase().mimeTypeForFile(fileName).name();
0054 
0055     QString error;
0056     KoDocumentEntry documentEntry = KoDocumentEntry::queryByMimeType(mimetype);
0057     KoPart *part = documentEntry.createKoPart(&error);
0058 
0059     if (!error.isEmpty()) {
0060         qWarning() << "Error creating document" << mimetype << error;
0061         return 0;
0062     }
0063 
0064     KoPADocument* doc = qobject_cast<KoPADocument*>(part->document());
0065     m_doc = doc;
0066     const QUrl url = QUrl::fromLocalFile(fileName);
0067     doc->setCheckAutoSaveFile(false);
0068     doc->setAutoErrorHandlingEnabled(false); // show error dialogs
0069     if (!doc->openUrl(url)) {
0070         return false;
0071     }
0072     doc->setReadWrite(false);
0073     doc->setAutoSave(0);
0074 
0075 
0076     int pageCount = m_doc->pageCount();
0077     for(int i = 0; i < pageCount; i++) {
0078         KoPAPageBase* kprpage = m_doc->pages().value(i);
0079         if (!kprpage) {
0080             continue;
0081         }
0082         QSize size = kprpage->size().toSize();
0083 
0084         Okular::Page * page = new Okular::Page( i, size.width(), size.height(), Okular::Rotation0 );
0085         pages.append(page);
0086     }
0087 
0088     const KoDocumentInfo *documentInfo = m_doc->documentInfo();
0089     m_documentInfo.set( Okular::DocumentInfo::MimeType, mimetype );
0090     m_documentInfo.set( Okular::DocumentInfo::Producer, documentInfo->originalGenerator() );
0091     m_documentInfo.set( Okular::DocumentInfo::Title,       documentInfo->aboutInfo("title") );
0092     m_documentInfo.set( Okular::DocumentInfo::Subject,     documentInfo->aboutInfo("subject") );
0093     m_documentInfo.set( Okular::DocumentInfo::Keywords,     documentInfo->aboutInfo("keyword") );
0094     m_documentInfo.set( Okular::DocumentInfo::Description, documentInfo->aboutInfo("description") );
0095     m_documentInfo.set( "language",    KoGlobal::languageFromTag(documentInfo->aboutInfo("language")),  i18n("Language"));
0096 
0097     const QString creationDate = documentInfo->aboutInfo("creation-date");
0098     if (!creationDate.isEmpty()) {
0099         QDateTime t = QDateTime::fromString(creationDate, Qt::ISODate);
0100         m_documentInfo.set( Okular::DocumentInfo::CreationDate, QLocale().toString(t, QLocale::ShortFormat) );
0101     }
0102     m_documentInfo.set( Okular::DocumentInfo::Creator,  documentInfo->aboutInfo("initial-creator") );
0103 
0104     const QString modificationDate = documentInfo->aboutInfo("date");
0105     if (!modificationDate.isEmpty()) {
0106         QDateTime t = QDateTime::fromString(modificationDate, Qt::ISODate);
0107         m_documentInfo.set( Okular::DocumentInfo::ModificationDate, QLocale().toString(t, QLocale::ShortFormat) );
0108     }
0109     m_documentInfo.set( Okular::DocumentInfo::Author, documentInfo->aboutInfo("creator") );
0110 
0111     return true;
0112 }
0113 
0114 bool OkularOdpGenerator::doCloseDocument()
0115 {
0116     delete m_doc;
0117     m_doc = 0;
0118 
0119     m_documentInfo = Okular::DocumentInfo();
0120 
0121     return true;
0122 }
0123 
0124 bool OkularOdpGenerator::canGeneratePixmap() const
0125 {
0126     return true;
0127 }
0128 
0129 void OkularOdpGenerator::generatePixmap( Okular::PixmapRequest *request )
0130 {
0131     QPixmap* pix;
0132     if (!m_doc) {
0133         pix = new QPixmap(request->width(), request->height());
0134         QPainter painter(pix);
0135         painter.fillRect(0 ,0 , request->width(), request->height(), Qt::white);
0136     } else {
0137         KoPAPageBase* page = m_doc->pages().value(request->pageNumber());
0138         pix = new QPixmap(page->thumbnail(QSize(request->width(), request->height())));
0139     }
0140 
0141    request->page()->setPixmap( request->observer(), pix );
0142 
0143     signalPixmapRequestDone( request );
0144 }
0145 
0146 Okular::DocumentInfo OkularOdpGenerator::generateDocumentInfo( const QSet<Okular::DocumentInfo::Key> &keys ) const
0147 {
0148     Q_UNUSED(keys);
0149 
0150     return m_documentInfo;
0151 }