filters

mswordimport.cpp
1/* This file is part of the KOffice project
2 Copyright (C) 2002 Werner Trobin <trobin@kde.org>
3 Copyright (C) 2002 David Faure <faure@kde.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License version 2 as published by the Free Software Foundation.
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 GNU
12 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; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "mswordimport.h"
21
22#include <tqdom.h>
23#include <tqfontinfo.h>
24#include <tqfile.h>
25
26#include <kdebug.h>
27#include <kgenericfactory.h>
28
29#include <KoFilterChain.h>
30
31#include <document.h>
32
33typedef KGenericFactory<MSWordImport, KoFilter> MSWordImportFactory;
34K_EXPORT_COMPONENT_FACTORY( libmswordimport, MSWordImportFactory( "kofficefilters" ) )
35
36MSWordImport::MSWordImport( KoFilter *, const char *, const TQStringList& ) : KoFilter()
37{
38}
39
40MSWordImport::~MSWordImport()
41{
42}
43
44KoFilter::ConversionStatus MSWordImport::convert( const TQCString& from, const TQCString& to )
45{
46 // check for proper conversion
47 if ( to != "application/x-kword" || from != "application/msword" )
48 return KoFilter::NotImplemented;
49
50 kdDebug(30513) << "######################## MSWordImport::convert ########################" << endl;
51
52 TQDomDocument mainDocument;
53 TQDomElement framesetsElem;
54 prepareDocument( mainDocument, framesetsElem );
55
56 TQDomDocument documentInfo;
57 documentInfo.appendChild (documentInfo.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
58
59 Document document( TQFile::encodeName( m_chain->inputFile() ).data(), mainDocument, documentInfo, framesetsElem, m_chain );
60
61 if ( !document.hasParser() )
62 return KoFilter::WrongFormat;
63 if ( !document.parse() )
64 return KoFilter::ParsingError;
65 document.processSubDocQueue();
66 document.finishDocument();
67 if ( !document.bodyFound() )
68 return KoFilter::WrongFormat;
69
70 KoStoreDevice* out = m_chain->storageFile( "root", KoStore::Write );
71 if ( !out ) {
72 kdError(30502) << "Unable to open output file!" << endl;
73 return KoFilter::StorageCreationError;
74 }
75 TQCString cstr = mainDocument.toCString();
76 // WARNING: we cannot use KoStore::write(const TQByteArray&) because it gives an extra NULL character at the end.
77 out->writeBlock( cstr, cstr.length() );
78 out->close();
79
80 out = m_chain->storageFile( "documentinfo.xml", KoStore::Write );
81 if ( !out ) {
82 return KoFilter::StorageCreationError;
83 }
84
85 cstr = documentInfo.toCString();
86 out->writeBlock( cstr, cstr.length() );
87 out->close();
88
89 kdDebug(30513) << "######################## MSWordImport::convert done ####################" << endl;
90 return KoFilter::OK;
91}
92
93void MSWordImport::prepareDocument( TQDomDocument& mainDocument, TQDomElement& framesetsElem )
94{
95 mainDocument.appendChild( mainDocument.createProcessingInstruction( "xml","version=\"1.0\" encoding=\"UTF-8\"" ) );
96
97 TQDomElement elementDoc;
98 elementDoc=mainDocument.createElement("DOC");
99 elementDoc.setAttribute("editor","KWord's MS Word Import Filter");
100 elementDoc.setAttribute("mime","application/x-kword");
101 elementDoc.setAttribute("syntaxVersion",2);
102 mainDocument.appendChild(elementDoc);
103
104 framesetsElem=mainDocument.createElement("FRAMESETS");
105 mainDocument.documentElement().appendChild(framesetsElem);
106}
107
108#include <mswordimport.moc>