filters

mathmlexport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 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 <qstring.h>
00021 #include <qtextstream.h>
00022 #include <qfile.h>
00023 
00024 #include <kapplication.h>
00025 #include <kdebug.h>
00026 #include <kgenericfactory.h>
00027 #include <kmessagebox.h>
00028 
00029 #include <KoFilterChain.h>
00030 #include <KoStoreDevice.h>
00031 
00032 #include <kformuladocument.h>
00033 #include <kformulacontainer.h>
00034 #include <kformulamimesource.h>
00035 
00036 #include "mathmlexport.h"
00037 
00038 
00039 typedef KGenericFactory<MathMLExport, KoFilter> MathMLExportFactory;
00040 K_EXPORT_COMPONENT_FACTORY( libkfomathmlexport, MathMLExportFactory( "kofficefilters" ) )
00041 
00042 
00043 MathMLExport::MathMLExport( KoFilter */*parent*/, const char */*name*/, const QStringList& )
00044     : KoFilter()
00045 {
00046 }
00047 
00048 
00049 KoFilter::ConversionStatus MathMLExport::convert( const QCString& from, const QCString& to )
00050 {
00051     if ( to != "application/mathml+xml" || from != "application/x-kformula" )
00052         return KoFilter::NotImplemented;
00053 
00054     KoStoreDevice* in = m_chain->storageFile( "root", KoStore::Read );
00055     if(!in) {
00056         QApplication::restoreOverrideCursor();
00057         KMessageBox::error( 0, i18n( "Failed to read data." ), i18n( "MathML Export Error" ) );
00058         return KoFilter::StorageCreationError;
00059     }
00060 
00061     QDomDocument dom;
00062     if ( !dom.setContent( in, false ) ) {
00063         QApplication::restoreOverrideCursor();
00064         KMessageBox::error( 0, i18n( "Malformed XML data." ), i18n( "MathML Export Error" ) );
00065         return KoFilter::WrongFormat;
00066     }
00067 
00068     QFile f( m_chain->outputFile() );
00069     if( !f.open( IO_Truncate | IO_ReadWrite ) ) {
00070         QApplication::restoreOverrideCursor();
00071         KMessageBox::error( 0, i18n( "Failed to write file." ), i18n( "MathML Export Error" ) );
00072         return KoFilter::FileNotFound;
00073     }
00074 
00075     KFormula::DocumentWrapper* wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
00076     KFormula::Document* doc = new KFormula::Document;
00077     wrapper->document( doc );
00078     KFormula::Container* formula = doc->createFormula();
00079     if ( !doc->loadXML( dom ) ) {
00080         kdError() << "Failed." << endl;
00081     }
00082 
00083     QTextStream stream(&f);
00084     stream.setEncoding( QTextStream::UnicodeUTF8 );
00085     formula->saveMathML( stream );
00086     f.close();
00087 
00088     delete formula;
00089     delete wrapper;
00090 
00091     return KoFilter::OK;
00092 }
00093 
00094 #include "mathmlexport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys