filters

mathmlimport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. 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 <tdeapplication.h>
21#include <kdebug.h>
22#include <kgenericfactory.h>
23#include <tdeglobal.h>
24#include <tdelocale.h>
25#include <tdemessagebox.h>
26#include <KoFilterChain.h>
27
28#include <tqtextcodec.h>
29
30#include <kformuladocument.h>
31#include <kformulacontainer.h>
32#include <kformulamimesource.h>
33
34#include "mathmlimport.h"
35#include "mathmlimport.moc"
36
37
38typedef KGenericFactory<MathMLImport, KoFilter> MathMLImportFactory;
39K_EXPORT_COMPONENT_FACTORY( libkfomathmlimport, MathMLImportFactory( "kofficefilters" ) )
40
41
42MathMLImport::MathMLImport(KoFilter *, const char *, const TQStringList&)
43 : KoFilter()
44{
45}
46
47KoFilter::ConversionStatus MathMLImport::convert( const TQCString& from, const TQCString& to )
48{
49 kdDebug( KFormula::DEBUGID ) << "From: " << from << endl;
50 kdDebug( KFormula::DEBUGID ) << "To: " << to << endl;
51
52 if(from != "application/mathml+xml" || to != "application/x-kformula")
53 return KoFilter::NotImplemented;
54
55 KoStore* out = KoStore::createStore(TQString(m_chain->outputFile()), KoStore::Write);
56 if(!out || !out->open("root")) {
57 KMessageBox::error( 0, i18n( "Unable to open output file." ), i18n( "MathML Import Error" ) );
58 delete out;
59 return KoFilter::FileNotFound;
60 }
61
62 KFormula::DocumentWrapper* wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
63 KFormula::Document* doc = new KFormula::Document;
64 wrapper->document( doc );
65 KFormula::Container* formula = doc->createFormula();
66
67 //formula->loadMathML( m_chain->inputFile() );
68 const TQString filename( m_chain->inputFile() );
69 TQFile f( filename );
70 if ( !f.open( IO_ReadOnly ) ) {
71 KMessageBox::error( 0, i18n( "Failed to open input file: %1" ).arg( filename ), i18n( "MathML Import Error" ) );
72 delete wrapper;
73 return KoFilter::FileNotFound;
74 }
75
76 TQDomDocument mathML;
77 // Error variables for TQDomDocument::setContent
78 TQString errorMsg;
79 int errorLine, errorColumn;
80 if ( !mathML.setContent( &f, true, &errorMsg, &errorLine, &errorColumn ) ) {
81 delete wrapper;
82 TQApplication::restoreOverrideCursor();
83 kdError(KFormula::DEBUGID) << "Parsing error in " << filename << "! Aborting!" << endl
84 << " In line: " << errorLine << ", column: " << errorColumn << endl
85 << " Error message: " << errorMsg << endl;
86 KMessageBox::error( 0, i18n( "Parsing error in MathML file %4 at line %1, column %2\nError message: %3" )
87 .arg( errorLine ).arg( errorColumn ).arg( i18n ( "TQXml", errorMsg.utf8() ).arg( filename ) ), i18n( "MathML Import Error" ) );
88 return KoFilter::WrongFormat;
89 }
90 f.close();
91 if ( !formula->loadMathML( mathML ) ) {
92 delete wrapper;
93 return KoFilter::StupidError;
94 }
95
96 // taken from KoDocument::saveToStore
97 KoStoreDevice dev( out );
98 const TQCString s = doc->saveXML().toCString(); // utf8 already
99 const int nwritten = dev.writeBlock( s.data(), s.size()-1 );
100 if ( nwritten != (int)s.size()-1 ) {
101 kdWarning() << "wrote " << nwritten << " - expected " << s.size()-1 << endl;
102 KMessageBox::error( 0, i18n( "Failed to write formula." ), i18n( "MathML Import Error" ) );
103 }
104
105 out->close();
106 delete out;
107
108 delete wrapper;
109 return KoFilter::OK;
110}
111