filters
generic_filter.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdlib.h>
00021
00022 #include <qtextcodec.h>
00023 #include <qfile.h>
00024
00025 #include <kdebug.h>
00026 #include <KoFilterChain.h>
00027 #include <kgenericfactory.h>
00028 #include <kglobal.h>
00029 #include <klocale.h>
00030 #include <ktrader.h>
00031 #include <kservice.h>
00032 #include <ktempfile.h>
00033
00034 #include "generic_filter.h"
00035
00036 typedef KGenericFactory<GenericFilter, KoFilter> GenericFilterFactory;
00037 K_EXPORT_COMPONENT_FACTORY( libgenerickofilter, GenericFilterFactory )
00038
00039
00040 GenericFilter::GenericFilter(KoFilter *, const char *, const QStringList&) :
00041 KoFilter() {
00042 }
00043
00044 KoFilter::ConversionStatus GenericFilter::convert( const QCString &from, const QCString &to )
00045 {
00046
00047
00048 KTrader::OfferList offers = KTrader::self()->query("KOfficeGenericFilter",
00049 "(Type == 'Service') and ('KOfficeGenericFilter' in ServiceTypes) and (exist Exec)");
00050
00051 if (offers.isEmpty())
00052 return KoFilter::NotImplemented;
00053
00054 KTrader::OfferList::ConstIterator it;
00055 for (it=offers.begin(); it!=offers.end(); ++it)
00056 {
00057 kdDebug() << "Got a filter script, exec: " << (*it)->exec() <<
00058 ", imports: " << (*it)->property("X-KDE-Wrapper-Import").toString() <<
00059 ", exports: " << (*it)->property("X-KDE-Wrapper-Export").toString() << endl;
00060 if ((*it)->property("X-KDE-Wrapper-Import").toCString()==from
00061 && (*it)->property("X-KDE-Wrapper-Export").toCString()==to)
00062 {
00063 m_exec=(*it)->exec();
00064 m_from=from;
00065 m_to=to;
00066 break;
00067 }
00068 }
00069
00070
00071 if( m_to == "application/x-kword" || m_to == "application/x-karbon" ||
00072 m_to == "application/x-kspread" || m_to == "application/x-kivio" ||
00073 m_to == "application/x-kchart" || m_to == "application/x-kpresenter" )
00074 return doImport();
00075 else if ( m_from == "application/x-kword" || m_from == "application/x-karbon" ||
00076 m_from == "application/x-kspread" || m_from == "application/x-kivio" ||
00077 m_from == "application/x-kchart" || m_from == "application/x-kpresenter" )
00078 return doExport();
00079 else
00080 return KoFilter::NotImplemented;
00081 }
00082
00083 KoFilter::ConversionStatus GenericFilter::doImport()
00084 {
00085 KTempFile temp(QString("genericfilter-"));
00086 temp.setAutoDelete(true);
00087
00088 QFile tempFile(temp.name());
00089
00090 m_out = KoStore::createStore(&tempFile, KoStore::Write);
00091
00092 if (!m_out || !m_out->open("root"))
00093 {
00094 kdError() << "Unable to create output store!" << endl;
00095 m_out->close();
00096 return KoFilter::StorageCreationError;
00097 }
00098 else
00099 {
00100 QString exec = m_exec + " " + KProcess::quote(m_chain->inputFile()) + " "
00101 + KProcess::quote(m_chain->outputFile());
00102 system(QFile::encodeName(exec));
00103
00104 kdDebug() << "Executing: " << exec << endl;
00105
00106 QFile outFile(m_chain->outputFile());
00107 outFile.open(IO_ReadOnly);
00108 QByteArray outData = outFile.readAll();
00109 if (outData.size()==0) {
00110 m_out->close();
00111 return KoFilter::UnexpectedEOF;
00112 }
00113 else {
00114 m_out->write(outData);
00115 m_out->close();
00116 }
00117 }
00118
00119 return KoFilter::OK;
00120 }
00121
00122 KoFilter::ConversionStatus GenericFilter::doExport()
00123 {
00124 return KoFilter::NotImplemented;
00125 }
00126
00127 #include "generic_filter.moc"
|