filters

generic_filter.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, 2003 Lukas Tinkl <lukas@kde.org>
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 <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     //find the right script to use
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     //decide between import/export
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"
KDE Home | KDE Accessibility Home | Description of Access Keys