20#include <tdeapplication.h>
22#include <kgenericfactory.h>
25#include <tdemessagebox.h>
26#include <KoFilterChain.h>
28#include <tqtextcodec.h>
30#include <kformuladocument.h>
31#include <kformulacontainer.h>
32#include <kformulamimesource.h>
34#include "mathmlimport.h"
35#include "mathmlimport.moc"
38typedef KGenericFactory<MathMLImport, KoFilter> MathMLImportFactory;
39K_EXPORT_COMPONENT_FACTORY( libkfomathmlimport, MathMLImportFactory(
"kofficefilters" ) )
42MathMLImport::MathMLImport(KoFilter *, const
char *, const TQStringList&)
47KoFilter::ConversionStatus MathMLImport::convert(
const TQCString& from,
const TQCString& to )
49 kdDebug( KFormula::DEBUGID ) <<
"From: " << from << endl;
50 kdDebug( KFormula::DEBUGID ) <<
"To: " << to << endl;
52 if(from !=
"application/mathml+xml" || to !=
"application/x-kformula")
53 return KoFilter::NotImplemented;
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" ) );
59 return KoFilter::FileNotFound;
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();
68 const TQString filename( m_chain->inputFile() );
70 if ( !f.open( IO_ReadOnly ) ) {
71 KMessageBox::error( 0, i18n(
"Failed to open input file: %1" ).arg( filename ), i18n(
"MathML Import Error" ) );
73 return KoFilter::FileNotFound;
79 int errorLine, errorColumn;
80 if ( !mathML.setContent( &f,
true, &errorMsg, &errorLine, &errorColumn ) ) {
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;
91 if ( !formula->loadMathML( mathML ) ) {
93 return KoFilter::StupidError;
97 KoStoreDevice dev( out );
98 const TQCString s = doc->saveXML().toCString();
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" ) );