filters

kis_pdf_import.cpp
1/*
2 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kis_pdf_import.h"
21
22// poppler's headers
23#include <poppler-qt.h>
24
25// TQt's headers
26#include <tqfile.h>
27#include <tqimage.h> // TODO that too
28#include <tqradiobutton.h>
29
30// KDE's headers
31#include <tdeapplication.h>
32#include <kdebug.h>
33#include <kdialogbase.h>
34#include <kgenericfactory.h>
35#include <knuminput.h>
36#include <kpassdlg.h>
37
38#include <tdeio/netaccess.h>
39
40// koffice's headers
41#include <KoFilterChain.h>
42
43// chalk's headers
44#include <kis_doc.h>
45#include <kis_colorspace.h>
46#include <kis_colorspace_factory_registry.h>
47#include <kis_group_layer.h>
48#include <kis_image.h>
49#include <kis_meta_registry.h>
50#include <kis_paint_layer.h>
51
52// plugins's headers
53#include "kis_pdf_import_widget.h"
54
55typedef KGenericFactory<KisPDFImport, KoFilter> PDFImportFactory;
56K_EXPORT_COMPONENT_FACTORY(libchalkpdfimport, PDFImportFactory("kofficefilters"))
57
58KisPDFImport::KisPDFImport(KoFilter *, const char *, const TQStringList&) : KoFilter()
59{
60}
61
62KisPDFImport::~KisPDFImport()
63{
64}
65
66KisPDFImport::ConversionStatus KisPDFImport::convert(const TQCString& , const TQCString& )
67{
68 TQString filename = m_chain -> inputFile();
69 kdDebug(41008) << "Importing using PDFImport!" << filename << endl;
70
71 if (filename.isEmpty())
72 return KoFilter::FileNotFound;
73
74
75 KURL url;
76 url.setPath(filename);
77
78 if (!TDEIO::NetAccess::exists(url, false, tqApp -> mainWidget())) {
79 return KoFilter::FileNotFound;
80 }
81
82 // We're not set up to handle asynchronous loading at the moment.
83 TQString tmpFile;
84 if (TDEIO::NetAccess::download(url, tmpFile, tqApp -> mainWidget())) {
85 url.setPath( tmpFile );
86 }
87
88 Poppler::Document* pdoc = Poppler::Document::load( TQFile::encodeName(url.path() ) );
89
90
91 if ( !pdoc)
92 {
93 kdDebug(41008) << "Error when reading the PDF" << endl;
94 return KoFilter::StorageCreationError;
95 }
96
97
98 while( pdoc->isLocked() )
99 {
100 TQString password;
101 int result = KPasswordDialog::getPassword(password, i18n("A password is required to read that pdf"));
102 if (result == KPasswordDialog::Accepted)
103 {
104 pdoc->unlock(password.utf8());
105 } else {
106 kdDebug(41008) << "Password canceled" << endl;
107 return KoFilter::StorageCreationError;
108 }
109 }
110
111 KDialogBase* kdb = new KDialogBase(0, "", false, i18n("PDF Import Options"), KDialogBase::Ok | KDialogBase::Cancel);
112
113 KisPDFImportWidget* wdg = new KisPDFImportWidget(pdoc, kdb);
114 kdb->setMainWidget(wdg);
115 kapp->restoreOverrideCursor();
116 if(kdb->exec() == TQDialog::Rejected)
117 {
118 delete pdoc;
119 delete kdb;
120 return KoFilter::StorageCreationError; // FIXME Cancel doesn't exist :(
121 }
122
123 // Init kis's doc
124 KisDoc * doc = dynamic_cast<KisDoc*>(m_chain -> outputDocument());
125 if (!doc)
126 {
127 delete pdoc;
128 delete kdb;
129 return KoFilter::CreationError;
130 }
131
132 doc -> prepareForImport();
133 // Create the chalk image
134 KisColorSpace* cs = KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("RGBA"), "");
135 int width = wdg->intWidth->value();
136 int height = wdg->intHeight->value();
137 KisImageSP img = new KisImage(doc->undoAdapter(), width, height, cs, "built image");
138 img->blockSignals(true); // Don't send out signals while we're building the image
139
140 // create a layer
141 TQValueList<int> pages = wdg->pages();
142 for(TQValueList<int>::const_iterator it = pages.begin(); it != pages.end(); ++it)
143 {
144 KisPaintLayer* layer = new KisPaintLayer(img, TQString(i18n("Page %1")).arg( TQString::number(*it) + 1), TQ_UINT8_MAX);
145 layer->paintDevice()->convertFromTQImage( pdoc->getPage( *it )->renderToImage(wdg->intHorizontal->value(), wdg->intVertical->value() ), "");
146 img->addLayer(layer, img->rootLayer(), 0);
147 }
148
149 img->blockSignals(false);
150 doc -> setCurrentImage( img);
151
152 TDEIO::NetAccess::removeTempFile(tmpFile);
153
154 delete pdoc;
155 delete kdb;
156 return KoFilter::OK;
157}
158