filters
mathmlimport.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <kapplication.h>
00021 #include <kdebug.h>
00022 #include <kgenericfactory.h>
00023 #include <kglobal.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 #include <KoFilterChain.h>
00027
00028 #include <qtextcodec.h>
00029
00030 #include <kformuladocument.h>
00031 #include <kformulacontainer.h>
00032 #include <kformulamimesource.h>
00033
00034 #include "mathmlimport.h"
00035 #include "mathmlimport.moc"
00036
00037
00038 typedef KGenericFactory<MathMLImport, KoFilter> MathMLImportFactory;
00039 K_EXPORT_COMPONENT_FACTORY( libkfomathmlimport, MathMLImportFactory( "kofficefilters" ) )
00040
00041
00042 MathMLImport::MathMLImport(KoFilter *, const char *, const QStringList&)
00043 : KoFilter()
00044 {
00045 }
00046
00047 KoFilter::ConversionStatus MathMLImport::convert( const QCString& from, const QCString& to )
00048 {
00049 kdDebug( KFormula::DEBUGID ) << "From: " << from << endl;
00050 kdDebug( KFormula::DEBUGID ) << "To: " << to << endl;
00051
00052 if(from != "application/mathml+xml" || to != "application/x-kformula")
00053 return KoFilter::NotImplemented;
00054
00055 KoStore* out = KoStore::createStore(QString(m_chain->outputFile()), KoStore::Write);
00056 if(!out || !out->open("root")) {
00057 KMessageBox::error( 0, i18n( "Unable to open output file." ), i18n( "MathML Import Error" ) );
00058 delete out;
00059 return KoFilter::FileNotFound;
00060 }
00061
00062 KFormula::DocumentWrapper* wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
00063 KFormula::Document* doc = new KFormula::Document;
00064 wrapper->document( doc );
00065 KFormula::Container* formula = doc->createFormula();
00066
00067
00068 const QString filename( m_chain->inputFile() );
00069 QFile f( filename );
00070 if ( !f.open( IO_ReadOnly ) ) {
00071 KMessageBox::error( 0, i18n( "Failed to open input file: %1" ).arg( filename ), i18n( "MathML Import Error" ) );
00072 delete wrapper;
00073 return KoFilter::FileNotFound;
00074 }
00075
00076 QDomDocument mathML;
00077
00078 QString errorMsg;
00079 int errorLine, errorColumn;
00080 if ( !mathML.setContent( &f, true, &errorMsg, &errorLine, &errorColumn ) ) {
00081 delete wrapper;
00082 QApplication::restoreOverrideCursor();
00083 kdError(KFormula::DEBUGID) << "Parsing error in " << filename << "! Aborting!" << endl
00084 << " In line: " << errorLine << ", column: " << errorColumn << endl
00085 << " Error message: " << errorMsg << endl;
00086 KMessageBox::error( 0, i18n( "Parsing error in MathML file %4 at line %1, column %2\nError message: %3" )
00087 .arg( errorLine ).arg( errorColumn ).arg( i18n ( "QXml", errorMsg.utf8() ).arg( filename ) ), i18n( "MathML Import Error" ) );
00088 return KoFilter::WrongFormat;
00089 }
00090 f.close();
00091 if ( !formula->loadMathML( mathML ) ) {
00092 delete wrapper;
00093 return KoFilter::StupidError;
00094 }
00095
00096
00097 KoStoreDevice dev( out );
00098 const QCString s = doc->saveXML().toCString();
00099 const int nwritten = dev.writeBlock( s.data(), s.size()-1 );
00100 if ( nwritten != (int)s.size()-1 ) {
00101 kdWarning() << "wrote " << nwritten << " - expected " << s.size()-1 << endl;
00102 KMessageBox::error( 0, i18n( "Failed to write formula." ), i18n( "MathML Import Error" ) );
00103 }
00104
00105 out->close();
00106 delete out;
00107
00108 delete wrapper;
00109 return KoFilter::OK;
00110 }
00111
|