generic_filter.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, 2003 Lukas Tinkl <lukas@kde.org>
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 <stdlib.h>
21
22#include <tqtextcodec.h>
23#include <tqfile.h>
24
25#include <kdebug.h>
26#include <KoFilterChain.h>
27#include <kgenericfactory.h>
28#include <tdeglobal.h>
29#include <tdelocale.h>
30#include <ktrader.h>
31#include <kservice.h>
32#include <tdetempfile.h>
33
34#include "generic_filter.h"
35
36typedef KGenericFactory<GenericFilter, KoFilter> GenericFilterFactory;
37K_EXPORT_COMPONENT_FACTORY( libgenerickofilter, GenericFilterFactory )
38
39
40GenericFilter::GenericFilter(KoFilter *, const char *, const TQStringList&) :
41 KoFilter() {
42}
43
44KoFilter::ConversionStatus GenericFilter::convert( const TQCString &from, const TQCString &to )
45{
46
47 //find the right script to use
48 TDETrader::OfferList offers = TDETrader::self()->query("KOfficeGenericFilter",
49 "(Type == 'Service') and ('KOfficeGenericFilter' in ServiceTypes) and (exist Exec)");
50
51 if (offers.isEmpty())
52 return KoFilter::NotImplemented;
53
54 TDETrader::OfferList::ConstIterator it;
55 for (it=offers.begin(); it!=offers.end(); ++it)
56 {
57 kdDebug() << "Got a filter script, exec: " << (*it)->exec() <<
58 ", imports: " << (*it)->property("X-TDE-Wrapper-Import").toString() <<
59 ", exports: " << (*it)->property("X-TDE-Wrapper-Export").toString() << endl;
60 if ((*it)->property("X-TDE-Wrapper-Import").toCString()==from
61 && (*it)->property("X-TDE-Wrapper-Export").toCString()==to)
62 {
63 m_exec=(*it)->exec();
64 m_from=from;
65 m_to=to;
66 break;
67 }
68 }
69
70 //decide between import/export
71 if( m_to == "application/x-kword" || m_to == "application/x-karbon" ||
72 m_to == "application/x-kspread" || m_to == "application/x-kivio" ||
73 m_to == "application/x-kchart" || m_to == "application/x-kpresenter" )
74 return doImport();
75 else if ( m_from == "application/x-kword" || m_from == "application/x-karbon" ||
76 m_from == "application/x-kspread" || m_from == "application/x-kivio" ||
77 m_from == "application/x-kchart" || m_from == "application/x-kpresenter" )
78 return doExport();
79 else
80 return KoFilter::NotImplemented;
81}
82
83KoFilter::ConversionStatus GenericFilter::doImport()
84{
85 KTempFile temp(TQString("genericfilter-"));
86 temp.setAutoDelete(true);
87
88 TQFile tempFile(temp.name());
89
90 m_out = KoStore::createStore(&tempFile, KoStore::Write);
91
92 if (!m_out || !m_out->open("root"))
93 {
94 kdError() << "Unable to create output store!" << endl;
95 m_out->close();
96 return KoFilter::StorageCreationError;
97 }
98 else
99 {
100 TQString exec = m_exec + " " + TDEProcess::quote(m_chain->inputFile()) + " "
101 + TDEProcess::quote(m_chain->outputFile());
102 system(TQFile::encodeName(exec));
103
104 kdDebug() << "Executing: " << exec << endl;
105
106 TQFile outFile(m_chain->outputFile());
107 outFile.open(IO_ReadOnly);
108 TQByteArray outData = outFile.readAll();
109 if (outData.size()==0) {
110 m_out->close();
111 return KoFilter::UnexpectedEOF;
112 }
113 else {
114 m_out->write(outData);
115 m_out->close();
116 }
117 }
118
119 return KoFilter::OK;
120}
121
122KoFilter::ConversionStatus GenericFilter::doExport()
123{
124 return KoFilter::NotImplemented;
125}
126
127#include "generic_filter.moc"