kformula/latex/latexexport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 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 <tqstring.h>
21#include <tqtextstream.h>
22#include <tqfile.h>
23
24#include <tdeapplication.h>
25#include <kdebug.h>
26#include <kgenericfactory.h>
27#include <tdemessagebox.h>
28
29#include <KoFilterChain.h>
30#include <KoStoreDevice.h>
31
32#include <kformuladocument.h>
33#include <kformulacontainer.h>
34#include <kformulamimesource.h>
35
36#include "latexexport.h"
37
38
39typedef KGenericFactory<LATEXExport, KoFilter> LATEXExportFactory;
40K_EXPORT_COMPONENT_FACTORY( libkfolatexexport, LATEXExportFactory( "kofficefilters" ) )
41
42
43LATEXExport::LATEXExport( KoFilter */*parent*/, const char */*name*/, const TQStringList& )
44 : KoFilter()
45{
46}
47
48
49KoFilter::ConversionStatus LATEXExport::convert( const TQCString& from, const TQCString& to )
50{
51 if ( to != "text/x-tex" || from != "application/x-kformula" )
52 return KoFilter::NotImplemented;
53
54 KoStoreDevice* in = m_chain->storageFile( "root", KoStore::Read );
55 if(!in) {
56 TQApplication::restoreOverrideCursor();
57 KMessageBox::error( 0, i18n( "Failed to read data." ), i18n( "LaTeX Export Error" ) );
58 return KoFilter::StorageCreationError;
59 }
60
61 TQDomDocument dom( "KFORMULA" );
62 if ( !dom.setContent( in, false ) ) {
63 TQApplication::restoreOverrideCursor();
64 KMessageBox::error( 0, i18n( "Malformed XML data." ), i18n( "LaTeX Export Error" ) );
65 return KoFilter::WrongFormat;
66 }
67
68 TQFile f( m_chain->outputFile() );
69 if( !f.open( IO_Truncate | IO_ReadWrite ) ) {
70 TQApplication::restoreOverrideCursor();
71 KMessageBox::error( 0, i18n( "Failed to write file." ), i18n( "LaTeX Export Error" ) );
72 return KoFilter::FileNotFound;
73 }
74
75 KFormula::DocumentWrapper* wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
76 KFormula::Document* doc = new KFormula::Document;
77 wrapper->document( doc );
78 KFormula::Container* formula = doc->createFormula();
79 if ( !doc->loadXML( dom ) ) {
80 kdError(30522) << "Failed." << endl;
81 }
82
83 TQTextStream stream(&f);
84 //stream.setEncoding(TQTextStream::UnicodeUTF8);
85 stream << "\\documentclass{article}\n\\usepackage{amsmath}\n\\begin{document}\n\\[\n"
86 << formula->texString()
87 << "\n\\]\n\\end{document}";
88 f.close();
89
90 delete formula;
91 delete wrapper;
92
93 return KoFilter::OK;
94}
95
96#include "latexexport.moc"