filters

mswordimport.cpp

00001 /* This file is part of the KOffice project
00002    Copyright (C) 2002 Werner Trobin <trobin@kde.org>
00003    Copyright (C) 2002 David Faure <faure@kde.org>
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public
00007    License version 2 as published by the Free Software Foundation.
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 GNU
00012     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; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "mswordimport.h"
00021 
00022 #include <qdom.h>
00023 #include <qfontinfo.h>
00024 #include <qfile.h>
00025 
00026 #include <kdebug.h>
00027 #include <kgenericfactory.h>
00028 
00029 #include <KoFilterChain.h>
00030 
00031 #include <document.h>
00032 
00033 typedef KGenericFactory<MSWordImport, KoFilter> MSWordImportFactory;
00034 K_EXPORT_COMPONENT_FACTORY( libmswordimport, MSWordImportFactory( "kofficefilters" ) )
00035 
00036 MSWordImport::MSWordImport( KoFilter *, const char *, const QStringList& ) : KoFilter()
00037 {
00038 }
00039 
00040 MSWordImport::~MSWordImport()
00041 {
00042 }
00043 
00044 KoFilter::ConversionStatus MSWordImport::convert( const QCString& from, const QCString& to )
00045 {
00046     // check for proper conversion
00047     if ( to != "application/x-kword" || from != "application/msword" )
00048         return KoFilter::NotImplemented;
00049 
00050     kdDebug(30513) << "######################## MSWordImport::convert ########################" << endl;
00051 
00052     QDomDocument mainDocument;
00053     QDomElement framesetsElem;
00054     prepareDocument( mainDocument, framesetsElem );
00055 
00056     QDomDocument documentInfo;
00057     documentInfo.appendChild (documentInfo.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00058 
00059     Document document( QFile::encodeName( m_chain->inputFile() ).data(), mainDocument, documentInfo, framesetsElem, m_chain );
00060 
00061     if ( !document.hasParser() )
00062         return KoFilter::WrongFormat;
00063     if ( !document.parse() )
00064         return KoFilter::ParsingError;
00065     document.processSubDocQueue();
00066     document.finishDocument();
00067     if ( !document.bodyFound() )
00068         return KoFilter::WrongFormat;
00069 
00070     KoStoreDevice* out = m_chain->storageFile( "root", KoStore::Write );
00071     if ( !out ) {
00072         kdError(30502) << "Unable to open output file!" << endl;
00073         return KoFilter::StorageCreationError;
00074     }
00075     QCString cstr = mainDocument.toCString();
00076     // WARNING: we cannot use KoStore::write(const QByteArray&) because it gives an extra NULL character at the end.
00077     out->writeBlock( cstr, cstr.length() );
00078     out->close();
00079 
00080     out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
00081     if ( !out ) {
00082     return KoFilter::StorageCreationError;
00083     }
00084 
00085     cstr = documentInfo.toCString();
00086     out->writeBlock( cstr, cstr.length() );
00087     out->close();
00088 
00089     kdDebug(30513) << "######################## MSWordImport::convert done ####################" << endl;
00090     return KoFilter::OK;
00091 }
00092 
00093 void MSWordImport::prepareDocument( QDomDocument& mainDocument, QDomElement& framesetsElem )
00094 {
00095     mainDocument.appendChild( mainDocument.createProcessingInstruction( "xml","version=\"1.0\" encoding=\"UTF-8\"" ) );
00096 
00097     QDomElement elementDoc;
00098     elementDoc=mainDocument.createElement("DOC");
00099     elementDoc.setAttribute("editor","KWord's MS Word Import Filter");
00100     elementDoc.setAttribute("mime","application/x-kword");
00101     elementDoc.setAttribute("syntaxVersion",2);
00102     mainDocument.appendChild(elementDoc);
00103 
00104     framesetsElem=mainDocument.createElement("FRAMESETS");
00105     mainDocument.documentElement().appendChild(framesetsElem);
00106 }
00107 
00108 #include <mswordimport.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys