filters

pdfimport.cpp
1/*
2 * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
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 "pdfimport.h"
21#include "pdfimport.moc"
22
23#include <tqdom.h>
24#include <tqdatetime.h> // debug
25
26#include <KoFilterChain.h>
27#include <kgenericfactory.h>
28#include <kdebug.h>
29#include <KoGlobal.h>
30#include <KoStore.h>
31#include <tdeapplication.h>
32#include <kprogress.h>
33
34#include "data.h"
35
36
37using namespace PDFImport;
38
39//-----------------------------------------------------------------------------
40class PdfImportFactory : KGenericFactory<PdfImport, KoFilter>
41{
42 public:
43 PdfImportFactory()
44 : KGenericFactory<PdfImport, KoFilter>("kwordpdfimport") {}
45
46 protected:
47 virtual void setupTranslations() {
48 TDEGlobal::locale()->insertCatalogue("kofficefilters");
49 }
50};
51
52K_EXPORT_COMPONENT_FACTORY(libpdfimport, PdfImportFactory())
53
54//-----------------------------------------------------------------------------
55PdfImport::PdfImport(KoFilter *, const char *, const TQStringList&)
56{}
57
58KoFilter::ConversionStatus PdfImport::convert(const TQCString& from,
59 const TQCString& to)
60{
61 // check for proper conversion
62 if ( to!="application/x-kword" || from!="application/pdf" )
63 return KoFilter::NotImplemented;
64
65 // read file
66 KoFilter::ConversionStatus result
67 = _doc.init(m_chain->inputFile(), TQString(), TQString());
68 if ( result!=KoFilter::OK ) return result;
69
70 // options dialog
71 {
72 Dialog dialog(_doc.nbPages(), _doc.isEncrypted(), 0);
73 dialog.exec();
74 if ( dialog.result()==TQDialog::Rejected )
75 return KoFilter::UserCancelled;
76 _options = dialog.options();
77 }
78
79 // progress dialog
80 KProgressDialog pd(0, "progress_dialog", i18n("PDF Import"),
81 i18n("Initializing..."), true);
82 pd.setMinimumDuration(0);
83 pd.progressBar()->setTotalSteps( _options.range.nbPages()*2 );
84 pd.progressBar()->setValue(1);
85 tqApp->processEvents();
86
87 // if passwords : reread file
88 if ( !_options.ownerPassword.isEmpty()
89 || !_options.userPassword.isEmpty() ) {
90 result = _doc.init(m_chain->inputFile(), _options.ownerPassword,
91 _options.userPassword);
92 if ( result!=KoFilter::OK ) return result;
93 }
94
95 // data
96 KoPageLayout page;
97 DRect rect = _doc.paperSize(page.format);
98 kdDebug(30516) << "paper size: " << rect.toString() << endl;
99 page.orientation = _doc.paperOrientation();
100 Data data(m_chain, rect, page, _options);
101 _doc.initDevice(data);
102
103 // treat pages
104 TQTime time;
105 time.start();
106 SelectionRangeIterator it(_options.range);
107 for (uint k=0; k<2; k++) {
108 bool first = ( k==0 );
109 data.pageIndex = 0;
110 if ( !first ) _doc.init();
111 for (it.toFirst(); it.current()!=it.end(); it.next()) {
112 TQString s = (first ? i18n("First pass: page #%1...")
113 : i18n("Second pass: page #%1..."));
114 pd.setLabel( s.arg(it.current()) );
115 tqApp->processEvents();
116 if (pd.wasCancelled()) return KoFilter::UserCancelled;
117 kdDebug(30516) << "-- " << "pass #" << k
118 << " treat page: " << it.current()
119 << "----------------" << endl;
120 if (first) _doc.treatPage( it.current() );
121 else _doc.dumpPage(data.pageIndex);
122 pd.progressBar()->advance(1);
123 data.pageIndex++;
124 }
125 }
126 data.endDump();
127 kdDebug(30516) << "treatement elapsed=" << time.elapsed() << endl;
128
129 // output
130 KoStoreDevice* out = m_chain->storageFile("root", KoStore::Write);
131 if( !out ) {
132 kdError(30516) << "Unable to open output file!" << endl;
133 return KoFilter::StorageCreationError;
134 }
135// kdDebug(30516) << data.document().toCString() << endl;
136 TQCString cstr = data.document().toCString();
137 out->writeBlock(cstr, cstr.length());
138 out->close();
139
140 treatInfoDocument();
141
142 return KoFilter::OK;
143}
144
145void PdfImport::treatInfoDocument()
146{
147 TQDomDocument infoDocument("document-info");
148 infoDocument.appendChild(
149 infoDocument.createProcessingInstruction(
150 "xml", "version=\"1.0\" encoding=\"UTF-8\""));
151 TQDomElement infoElement = infoDocument.createElement( "document-info" );
152 infoDocument.appendChild(infoElement);
153
154 TQDomElement aboutTag = infoDocument.createElement("about");
155 infoElement.appendChild(aboutTag);
156
157 TQDomElement authorTag = infoDocument.createElement("author");
158 infoElement.appendChild(authorTag);
159 TQDomElement fullNameTag = infoDocument.createElement("full-name");
160 authorTag.appendChild(fullNameTag);
161 TQDomText authorText = infoDocument.createTextNode( _doc.info("Author") );
162 fullNameTag.appendChild(authorText);
163
164 TQDomElement titleTag = infoDocument.createElement("title");
165 aboutTag.appendChild(titleTag);
166 TQDomText titleText = infoDocument.createTextNode( _doc.info("Title") );
167 titleTag.appendChild(titleText);
168
169 // output
170 KoStoreDevice *out =
171 m_chain->storageFile("documentinfo.xml", KoStore::Write);
172 if ( !out )
173 kdWarning(30516) << "unable to open doc info. continuing anyway\n";
174 else {
175 TQCString cstr = infoDocument.toCString();
176 out->writeBlock(cstr, cstr.length());
177 out->close();
178 }
179}