filters

pdfimport.cpp

00001 /*
00002  * Copyright (c) 2002-2003 Nicolas HADACEK (hadacek@kde.org)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "pdfimport.h"
00021 #include "pdfimport.moc"
00022 
00023 #include <qdom.h>
00024 #include <qdatetime.h> // debug
00025 
00026 #include <KoFilterChain.h>
00027 #include <kgenericfactory.h>
00028 #include <kdebug.h>
00029 #include <KoGlobal.h>
00030 #include <KoStore.h>
00031 #include <kapplication.h>
00032 #include <kprogress.h>
00033 
00034 #include "data.h"
00035 
00036 
00037 using namespace PDFImport;
00038 
00039 //-----------------------------------------------------------------------------
00040 class PdfImportFactory : KGenericFactory<PdfImport, KoFilter>
00041 {
00042  public:
00043     PdfImportFactory()
00044         : KGenericFactory<PdfImport, KoFilter>("kwordpdfimport") {}
00045 
00046  protected:
00047     virtual void setupTranslations() {
00048         KGlobal::locale()->insertCatalogue("kofficefilters");
00049     }
00050 };
00051 
00052 K_EXPORT_COMPONENT_FACTORY(libpdfimport, PdfImportFactory())
00053 
00054 //-----------------------------------------------------------------------------
00055 PdfImport::PdfImport(KoFilter *, const char *, const QStringList&)
00056 {}
00057 
00058 KoFilter::ConversionStatus PdfImport::convert(const QCString& from,
00059                                               const QCString& to)
00060 {
00061     // check for proper conversion
00062     if ( to!="application/x-kword" || from!="application/pdf" )
00063         return KoFilter::NotImplemented;
00064 
00065     // read file
00066     KoFilter::ConversionStatus result
00067         = _doc.init(m_chain->inputFile(), QString::null, QString::null);
00068     if ( result!=KoFilter::OK ) return result;
00069 
00070     // options dialog
00071     {
00072         Dialog dialog(_doc.nbPages(), _doc.isEncrypted(), 0);
00073         dialog.exec();
00074         if ( dialog.result()==QDialog::Rejected )
00075             return KoFilter::UserCancelled;
00076         _options = dialog.options();
00077     }
00078 
00079     // progress dialog
00080     KProgressDialog pd(0, "progress_dialog", i18n("PDF Import"),
00081                        i18n("Initializing..."), true);
00082     pd.setMinimumDuration(0);
00083     pd.progressBar()->setTotalSteps( _options.range.nbPages()*2 );
00084     pd.progressBar()->setValue(1);
00085     qApp->processEvents();
00086 
00087     // if passwords : reread file
00088     if ( !_options.ownerPassword.isEmpty()
00089          || !_options.userPassword.isEmpty() ) {
00090         result = _doc.init(m_chain->inputFile(), _options.ownerPassword,
00091                            _options.userPassword);
00092         if ( result!=KoFilter::OK ) return result;
00093     }
00094 
00095     // data
00096     KoPageLayout page;
00097     DRect rect = _doc.paperSize(page.format);
00098     kdDebug(30516) << "paper size: " << rect.toString() << endl;
00099     page.orientation = _doc.paperOrientation();
00100     Data data(m_chain, rect, page, _options);
00101     _doc.initDevice(data);
00102 
00103     // treat pages
00104     QTime time;
00105     time.start();
00106     SelectionRangeIterator it(_options.range);
00107     for (uint k=0; k<2; k++) {
00108         bool first = ( k==0 );
00109         data.pageIndex = 0;
00110         if ( !first ) _doc.init();
00111         for (it.toFirst(); it.current()!=it.end(); it.next()) {
00112             QString s = (first ? i18n("First pass: page #%1...")
00113                          : i18n("Second pass: page #%1..."));
00114             pd.setLabel( s.arg(it.current()) );
00115             qApp->processEvents();
00116             if (pd.wasCancelled()) return KoFilter::UserCancelled;
00117             kdDebug(30516) << "-- " << "pass #" << k
00118                            << "  treat page: " << it.current()
00119                            << "----------------" << endl;
00120             if (first) _doc.treatPage( it.current() );
00121             else _doc.dumpPage(data.pageIndex);
00122             pd.progressBar()->advance(1);
00123             data.pageIndex++;
00124         }
00125     }
00126     data.endDump();
00127     kdDebug(30516) << "treatement elapsed=" << time.elapsed() << endl;
00128 
00129     // output
00130     KoStoreDevice* out = m_chain->storageFile("root", KoStore::Write);
00131     if( !out ) {
00132         kdError(30516) << "Unable to open output file!" << endl;
00133         return KoFilter::StorageCreationError;
00134     }
00135 //    kdDebug(30516) << data.document().toCString() << endl;
00136     QCString cstr = data.document().toCString();
00137     out->writeBlock(cstr, cstr.length());
00138     out->close();
00139 
00140     treatInfoDocument();
00141 
00142     return KoFilter::OK;
00143 }
00144 
00145 void PdfImport::treatInfoDocument()
00146 {
00147     QDomDocument infoDocument("document-info");
00148     infoDocument.appendChild(
00149         infoDocument.createProcessingInstruction(
00150             "xml", "version=\"1.0\" encoding=\"UTF-8\""));
00151     QDomElement infoElement = infoDocument.createElement( "document-info" );
00152     infoDocument.appendChild(infoElement);
00153 
00154     QDomElement aboutTag = infoDocument.createElement("about");
00155     infoElement.appendChild(aboutTag);
00156 
00157     QDomElement authorTag = infoDocument.createElement("author");
00158     infoElement.appendChild(authorTag);
00159     QDomElement fullNameTag = infoDocument.createElement("full-name");
00160     authorTag.appendChild(fullNameTag);
00161     QDomText authorText = infoDocument.createTextNode( _doc.info("Author") );
00162     fullNameTag.appendChild(authorText);
00163 
00164     QDomElement titleTag = infoDocument.createElement("title");
00165     aboutTag.appendChild(titleTag);
00166     QDomText titleText = infoDocument.createTextNode( _doc.info("Title") );
00167     titleTag.appendChild(titleText);
00168 
00169     // output
00170     KoStoreDevice *out =
00171         m_chain->storageFile("documentinfo.xml", KoStore::Write);
00172     if ( !out )
00173         kdWarning(30516) << "unable to open doc info. continuing anyway\n";
00174     else {
00175         QCString cstr = infoDocument.toCString();
00176         out->writeBlock(cstr, cstr.length());
00177         out->close();
00178     }
00179 }
KDE Home | KDE Accessibility Home | Description of Access Keys