Commit 13dde409 authored by Albert Astals Cid's avatar Albert Astals Cid

use the nice code brad wrote for poppler that allows you to extract embedded...

use the nice code brad wrote for poppler that allows you to extract embedded files from a pdf document.

svn path=/branches/work/kde4/playground/graphics/okular/; revision=545966
parent 91dc3ef0
...@@ -61,6 +61,7 @@ kde4_add_ui_files(oKularcore_SRCS ...@@ -61,6 +61,7 @@ kde4_add_ui_files(oKularcore_SRCS
) )
set(oKularui_SRCS set(oKularui_SRCS
${CMAKE_SOURCE_DIR}/okular/ui/embeddedfilesdialog.cpp
${CMAKE_SOURCE_DIR}/okular/ui/minibar.cpp ${CMAKE_SOURCE_DIR}/okular/ui/minibar.cpp
${CMAKE_SOURCE_DIR}/okular/ui/newstuff.cpp ${CMAKE_SOURCE_DIR}/okular/ui/newstuff.cpp
${CMAKE_SOURCE_DIR}/okular/ui/pagepainter.cpp ${CMAKE_SOURCE_DIR}/okular/ui/pagepainter.cpp
......
...@@ -484,6 +484,11 @@ const DocumentFonts * KPDFDocument::documentFonts() const ...@@ -484,6 +484,11 @@ const DocumentFonts * KPDFDocument::documentFonts() const
return generator ? generator->generateDocumentFonts() : NULL; return generator ? generator->generateDocumentFonts() : NULL;
} }
const QList<EmbeddedFile*> *KPDFDocument::embeddedFiles() const
{
return generator ? generator->embeddedFiles() : NULL;
}
const KPDFPage * KPDFDocument::page( int n ) const const KPDFPage * KPDFDocument::page( int n ) const
{ {
return ( n < pages_vector.count() ) ? pages_vector[n] : 0; return ( n < pages_vector.count() ) ? pages_vector[n] : 0;
...@@ -1790,4 +1795,14 @@ DocumentFonts::DocumentFonts() ...@@ -1790,4 +1795,14 @@ DocumentFonts::DocumentFonts()
// void implementation, only subclassed for naming // void implementation, only subclassed for naming
} }
/** EmbeddedFile **/
EmbeddedFile::EmbeddedFile()
{
}
EmbeddedFile::~EmbeddedFile()
{
}
#include "document.moc" #include "document.moc"
...@@ -28,6 +28,7 @@ class DocumentViewport; ...@@ -28,6 +28,7 @@ class DocumentViewport;
class DocumentInfo; class DocumentInfo;
class DocumentSynopsis; class DocumentSynopsis;
class DocumentFonts; class DocumentFonts;
class EmbeddedFile;
class Generator; class Generator;
class PixmapRequest; class PixmapRequest;
class Annotation; class Annotation;
...@@ -87,6 +88,7 @@ class OKULAR_EXPORT KPDFDocument : public QObject ...@@ -87,6 +88,7 @@ class OKULAR_EXPORT KPDFDocument : public QObject
const DocumentInfo * documentInfo() const; const DocumentInfo * documentInfo() const;
const DocumentSynopsis * documentSynopsis() const; const DocumentSynopsis * documentSynopsis() const;
const DocumentFonts * documentFonts() const; const DocumentFonts * documentFonts() const;
const QList<EmbeddedFile*> *embeddedFiles() const;
const KPDFPage * page( int page ) const; const KPDFPage * page( int page ) const;
const DocumentViewport & viewport() const; const DocumentViewport & viewport() const;
uint currentPage() const; uint currentPage() const;
...@@ -269,4 +271,22 @@ class OKULAR_EXPORT DocumentFonts : public QDomDocument ...@@ -269,4 +271,22 @@ class OKULAR_EXPORT DocumentFonts : public QDomDocument
DocumentFonts(); DocumentFonts();
}; };
/**
* @short An embedded file into the document, has name, description, dates and the data
*/
class OKULAR_EXPORT EmbeddedFile
{
public:
EmbeddedFile();
virtual ~EmbeddedFile();
virtual QString name() const = 0;
virtual QString description() const = 0;
virtual QByteArray data() const = 0;
virtual QDateTime modificationDate() const = 0;
virtual QDateTime creationDate() const = 0;
};
#endif #endif
...@@ -70,6 +70,7 @@ class OKULAR_EXPORT Generator : public QObject ...@@ -70,6 +70,7 @@ class OKULAR_EXPORT Generator : public QObject
virtual const DocumentInfo * generateDocumentInfo() { return 0L; } virtual const DocumentInfo * generateDocumentInfo() { return 0L; }
virtual const DocumentSynopsis * generateDocumentSynopsis() { return 0L; } virtual const DocumentSynopsis * generateDocumentSynopsis() { return 0L; }
virtual const DocumentFonts * generateDocumentFonts() { return 0L; } virtual const DocumentFonts * generateDocumentFonts() { return 0L; }
virtual const QList<EmbeddedFile*> * embeddedFiles() { return 0L; }
// DRM handling // DRM handling
virtual bool isAllowed( int /*Document::Permisison(s)*/ ) { return true; } virtual bool isAllowed( int /*Document::Permisison(s)*/ ) { return true; }
......
...@@ -31,8 +31,41 @@ ...@@ -31,8 +31,41 @@
#include <config.h> #include <config.h>
#include <config-okular.h> #include <config-okular.h>
// id for DATA_READY PDFPixmapGeneratorThread Event class PDFEmbeddedFile : public EmbeddedFile
#define TGE_DATAREADY_ID 6969 {
public:
PDFEmbeddedFile(Poppler::EmbeddedFile *f) : ef(f)
{
}
QString name() const
{
return ef->name();
}
QString description() const
{
return ef->description();
}
QByteArray data() const
{
return ef->data();
}
QDateTime modificationDate() const
{
return ef->modDate();
}
QDateTime creationDate() const
{
return ef->createDate();
}
private:
Poppler::EmbeddedFile *ef;
};
static void fillViewportFromLinkDestination( DocumentViewport &viewport, const Poppler::LinkDestination &destination, const Poppler::Document *pdfdoc ) static void fillViewportFromLinkDestination( DocumentViewport &viewport, const Poppler::LinkDestination &destination, const Poppler::Document *pdfdoc )
{ {
...@@ -153,7 +186,7 @@ KPDF_EXPORT_PLUGIN(PDFGenerator) ...@@ -153,7 +186,7 @@ KPDF_EXPORT_PLUGIN(PDFGenerator)
PDFGenerator::PDFGenerator( KPDFDocument * doc ) PDFGenerator::PDFGenerator( KPDFDocument * doc )
: Generator( doc ), pdfdoc( 0 ), ready( true ), : Generator( doc ), pdfdoc( 0 ), ready( true ),
pixmapRequest( 0 ), docInfoDirty( true ), docSynopsisDirty( true ), pixmapRequest( 0 ), docInfoDirty( true ), docSynopsisDirty( true ),
docFontsDirty( true ) docFontsDirty( true ), docEmbeddedFilesDirty( true )
{ {
// generate kpdfOutputDev and cache page color // generate kpdfOutputDev and cache page color
reparseConfig(); reparseConfig();
...@@ -490,6 +523,24 @@ const DocumentFonts * PDFGenerator::generateDocumentFonts() ...@@ -490,6 +523,24 @@ const DocumentFonts * PDFGenerator::generateDocumentFonts()
return &docFonts; return &docFonts;
} }
const QList<EmbeddedFile*> *PDFGenerator::embeddedFiles()
{
if (docEmbeddedFilesDirty)
{
docLock.lock();
const QList<Poppler::EmbeddedFile*> &popplerFiles = pdfdoc->embeddedFiles();
foreach(Poppler::EmbeddedFile* pef, popplerFiles)
{
docEmbeddedFiles.append(new PDFEmbeddedFile(pef));
}
docLock.unlock();
docEmbeddedFilesDirty = false;
}
return &docEmbeddedFiles;
}
bool PDFGenerator::isAllowed( int permissions ) bool PDFGenerator::isAllowed( int permissions )
{ {
#if !OKULAR_FORCE_DRM #if !OKULAR_FORCE_DRM
......
...@@ -55,6 +55,7 @@ class PDFGenerator : public Generator ...@@ -55,6 +55,7 @@ class PDFGenerator : public Generator
const DocumentInfo * generateDocumentInfo(); const DocumentInfo * generateDocumentInfo();
const DocumentSynopsis * generateDocumentSynopsis(); const DocumentSynopsis * generateDocumentSynopsis();
const DocumentFonts * generateDocumentFonts(); const DocumentFonts * generateDocumentFonts();
const QList<EmbeddedFile*> * embeddedFiles();
// [INHERITED] document information // [INHERITED] document information
bool isAllowed( int permissions ); bool isAllowed( int permissions );
...@@ -127,6 +128,8 @@ class PDFGenerator : public Generator ...@@ -127,6 +128,8 @@ class PDFGenerator : public Generator
DocumentSynopsis docSyn; DocumentSynopsis docSyn;
bool docFontsDirty; bool docFontsDirty;
DocumentFonts docFonts; DocumentFonts docFonts;
bool docEmbeddedFilesDirty;
QList<EmbeddedFile*> docEmbeddedFiles;
}; };
......
...@@ -67,6 +67,7 @@ ...@@ -67,6 +67,7 @@
#include "ui/side_reviews.h" #include "ui/side_reviews.h"
#include "ui/minibar.h" #include "ui/minibar.h"
#include "ui/newstuff.h" #include "ui/newstuff.h"
#include "ui/embeddedfilesdialog.h"
#include "ui/propertiesdialog.h" #include "ui/propertiesdialog.h"
#include "ui/presentationwidget.h" #include "ui/presentationwidget.h"
#include "conf/preferencesdialog.h" #include "conf/preferencesdialog.h"
...@@ -273,6 +274,10 @@ Part::Part(QWidget *parentWidget, ...@@ -273,6 +274,10 @@ Part::Part(QWidget *parentWidget,
m_showProperties = new KAction(KIcon("info"), i18n("&Properties"), ac, "properties"); m_showProperties = new KAction(KIcon("info"), i18n("&Properties"), ac, "properties");
connect(m_showProperties, SIGNAL(triggered()), this, SLOT(slotShowProperties())); connect(m_showProperties, SIGNAL(triggered()), this, SLOT(slotShowProperties()));
m_showProperties->setEnabled( false ); m_showProperties->setEnabled( false );
m_showEmbeddedFiles = new KAction(i18n("&Embedded Files"), ac, "embeddedFiles");
connect(m_showEmbeddedFiles, SIGNAL(triggered()), this, SLOT(slotShowEmbeddedFiles()));
m_showEmbeddedFiles->setEnabled( false );
m_showPresentation = new KAction( KIcon( "kpresenter_kpr" ), i18n("P&resentation"), ac, "presentation"); m_showPresentation = new KAction( KIcon( "kpresenter_kpr" ), i18n("P&resentation"), ac, "presentation");
connect(m_showPresentation, SIGNAL(triggered()), this, SLOT(slotShowPresentation())); connect(m_showPresentation, SIGNAL(triggered()), this, SLOT(slotShowPresentation()));
...@@ -524,6 +529,7 @@ bool Part::openFile() ...@@ -524,6 +529,7 @@ bool Part::openFile()
m_saveAs->setEnabled( ok ); m_saveAs->setEnabled( ok );
m_printPreview->setEnabled( ok ); m_printPreview->setEnabled( ok );
m_showProperties->setEnabled( ok ); m_showProperties->setEnabled( ok );
m_showEmbeddedFiles->setEnabled( ok && m_document->embeddedFiles() && m_document->embeddedFiles()->count() > 0);
m_showPresentation->setEnabled( ok ); m_showPresentation->setEnabled( ok );
// update viewing actions // update viewing actions
...@@ -584,6 +590,7 @@ bool Part::closeURL() ...@@ -584,6 +590,7 @@ bool Part::closeURL()
m_saveAs->setEnabled( false ); m_saveAs->setEnabled( false );
m_printPreview->setEnabled( false ); m_printPreview->setEnabled( false );
m_showProperties->setEnabled( false ); m_showProperties->setEnabled( false );
m_showEmbeddedFiles->setEnabled( false );
m_showPresentation->setEnabled( false ); m_showPresentation->setEnabled( false );
emit setWindowCaption(""); emit setWindowCaption("");
emit enablePrintAction(false); emit enablePrintAction(false);
...@@ -997,6 +1004,13 @@ void Part::slotShowProperties() ...@@ -997,6 +1004,13 @@ void Part::slotShowProperties()
delete d; delete d;
} }
void Part::slotShowEmbeddedFiles()
{
EmbeddedFilesDialog *d = new EmbeddedFilesDialog(widget(), m_document);
d->exec();
delete d;
}
void Part::slotShowPresentation() void Part::slotShowPresentation()
{ {
if ( !m_presentationWidget ) if ( !m_presentationWidget )
......
...@@ -112,6 +112,7 @@ protected slots: ...@@ -112,6 +112,7 @@ protected slots:
void slotPrintPreview(); void slotPrintPreview();
void slotShowMenu(const KPDFPage *page, const QPoint &point); void slotShowMenu(const KPDFPage *page, const QPoint &point);
void slotShowProperties(); void slotShowProperties();
void slotShowEmbeddedFiles();
void slotShowLeftPanel(); void slotShowLeftPanel();
void slotShowPresentation(); void slotShowPresentation();
void slotHidePresentation(); void slotHidePresentation();
...@@ -119,7 +120,7 @@ protected slots: ...@@ -119,7 +120,7 @@ protected slots:
void close(); void close();
void cannotQuit(); void cannotQuit();
void setMimeTypes(KIO::Job *job); void setMimeTypes(KIO::Job *job);
void saveSplitterSize(); void saveSplitterSize();
// can be connected to widget elements // can be connected to widget elements
void updateViewActions(); void updateViewActions();
void enableTOC(bool enable); void enableTOC(bool enable);
...@@ -170,6 +171,7 @@ private: ...@@ -170,6 +171,7 @@ private:
KAction *m_saveAs; KAction *m_saveAs;
KAction *m_printPreview; KAction *m_printPreview;
KAction *m_showProperties; KAction *m_showProperties;
KAction *m_showEmbeddedFiles;
KAction *m_showPresentation; KAction *m_showPresentation;
KToggleAction* m_showMenuBarAction; KToggleAction* m_showMenuBarAction;
KToggleAction* m_showLeftPanel; KToggleAction* m_showLeftPanel;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
<Action name="file_print" group="file_print"/> <Action name="file_print" group="file_print"/>
<Action name="file_print_preview" group="file_print"/> <Action name="file_print_preview" group="file_print"/>
<Action name="properties" group="file_print"/> <Action name="properties" group="file_print"/>
<Action name="embeddedFiles" group="file_print"/>
</Menu> </Menu>
<Menu name="edit"><text>&amp;Edit</text> <Menu name="edit"><text>&amp;Edit</text>
<Action name="find"/> <Action name="find"/>
......
/***************************************************************************
* Copyright (C) 2006 by Albert Astals Cid <aacid@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <QDateTime>
#include <QTreeWidget>
#include <kfiledialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include "core/document.h"
#include "embeddedfilesdialog.h"
EmbeddedFilesDialog::EmbeddedFilesDialog(QWidget *parent, const KPDFDocument *document) : KDialog(parent, i18n("Embedded Files"), Close | User1, 0, i18n("Save"))
{
m_tw = new QTreeWidget(this);
setMainWidget(m_tw);
QStringList header;
header.append(i18n("Name"));
header.append(i18n("Description"));
header.append(i18n("Created"));
header.append(i18n("Modificated"));
m_tw->setHeaderLabels(header);
m_tw->setRootIsDecorated(false);
foreach(EmbeddedFile* ef, *document->embeddedFiles())
{
QTreeWidgetItem *twi = new QTreeWidgetItem();
twi->setText(0, ef->name());
twi->setText(1, ef->description());
twi->setText(2, KGlobal::locale()->formatDateTime( ef->creationDate(), false, true ));
twi->setText(3, KGlobal::locale()->formatDateTime( ef->modificationDate(), false, true ));
m_tw->addTopLevelItem(twi);
m_files.insert(twi, ef);
}
connect(this, SIGNAL(user1Clicked()), this, SLOT(saveFile()));
}
void EmbeddedFilesDialog::saveFile()
{
QList<QTreeWidgetItem *> selected = m_tw->selectedItems();
foreach(QTreeWidgetItem *twi, selected)
{
EmbeddedFile* ef = m_files[twi];
QString path = KFileDialog::getSaveFileName(ef->name(), QString(), this, i18n("Where do you want to save %1?", ef->name()));
if (!path.isEmpty())
{
QFile f(path);
if (!f.exists() || KMessageBox::warningContinueCancel( this, i18n("A file named \"%1\" already exists. Are you sure you want to overwrite it?", path), QString::null, i18n("Overwrite")) == KMessageBox::Continue)
{
f.open(QIODevice::WriteOnly);
f.write(ef->data());
f.close();
}
}
}
}
#include "embeddedfilesdialog.moc"
/***************************************************************************
* Copyright (C) 2006 by Albert Astals Cid <aacid@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#ifndef _EMBEDDEDFILESDIALOG_H_
#define _EMBEDDEDFILESDIALOG_H_
#include <kdialog.h>
class KPDFDocument;
class EmbeddedFilesDialog : public KDialog
{
Q_OBJECT
public:
EmbeddedFilesDialog(QWidget *parent, const KPDFDocument *document);
private slots:
void saveFile();
private:
QTreeWidget *m_tw;
QHash<QTreeWidgetItem *, EmbeddedFile*> m_files;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment