filters

mathmlimport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library 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    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  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 <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     //formula->loadMathML( m_chain->inputFile() );
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     // Error variables for QDomDocument::setContent
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     // taken from KoDocument::saveToStore
00097     KoStoreDevice dev( out );
00098     const QCString s = doc->saveXML().toCString(); // utf8 already
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 
KDE Home | KDE Accessibility Home | Description of Access Keys