lib Library API Documentation

koFilter.cc

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2001 Werner Trobin <trobin@kde.org>
00003                  2002 Werner Trobin <trobin@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <koFilter.h>
00021 
00022 #include <qfile.h>
00023 
00024 #include <kurl.h>
00025 #include <kmimetype.h>
00026 #include <ktempfile.h>
00027 #include <kdebug.h>
00028 #include <koFilterManager.h>
00029 
00030 
00031 KoFilter::KoFilter() : QObject( 0, 0 ), m_chain( 0 )
00032 {
00033 }
00034 
00035 KoFilter::~KoFilter()
00036 {
00037 }
00038 
00039 
00040 KoEmbeddingFilter::~KoEmbeddingFilter()
00041 {
00042     if ( m_partStack.count() != 1 )
00043         kdWarning() << "Someone messed with the part stack" << endl;
00044     delete m_partStack.pop();
00045 }
00046 
00047 int KoEmbeddingFilter::lruPartIndex() const
00048 {
00049     return m_partStack.top()->m_lruPartIndex;
00050 }
00051 
00052 QString KoEmbeddingFilter::mimeTypeByExtension( const QString& extension )
00053 {
00054     // We need to resort to an ugly hack to determine the mimetype
00055     // from the extension, as kservicetypefactory.h isn't installed
00056     KURL url;
00057     url.setPath( QString( "dummy.%1" ).arg( extension ) );
00058     KMimeType::Ptr m( KMimeType::findByURL( url, 0, true, true ) );
00059     return m->name();
00060 }
00061 
00062 KoEmbeddingFilter::KoEmbeddingFilter() : KoFilter()
00063 {
00064     m_partStack.push( new PartState() );
00065 }
00066 
00067 int KoEmbeddingFilter::embedPart( const QCString& from, QCString& to,
00068                                   KoFilter::ConversionStatus& status, const QString& key )
00069 {
00070     ++( m_partStack.top()->m_lruPartIndex );
00071 
00072     KTempFile tempIn;
00073     tempIn.setAutoDelete( true );
00074     savePartContents( tempIn.file() );
00075     tempIn.file()->close();
00076 
00077     KoFilterManager *manager = new KoFilterManager( tempIn.name(), from, m_chain );
00078     status = manager->exp0rt( QString::null, to );
00079     delete manager;
00080 
00081     // Add the part to the current "stack frame", using the number as key
00082     // if the key string is empty
00083     PartReference ref( lruPartIndex(), to );
00084     m_partStack.top()->m_partReferences.insert( key.isEmpty() ? QString::number( lruPartIndex() ) : key, ref );
00085 
00086     return lruPartIndex();
00087 }
00088 
00089 void KoEmbeddingFilter::startInternalEmbedding( const QString& key, const QCString& mimeType )
00090 {
00091     filterChainEnterDirectory( QString::number( ++( m_partStack.top()->m_lruPartIndex ) ) );
00092     PartReference ref( lruPartIndex(), mimeType );
00093     m_partStack.top()->m_partReferences.insert( key, ref );
00094     m_partStack.push( new PartState() );
00095 }
00096 
00097 void KoEmbeddingFilter::endInternalEmbedding()
00098 {
00099     if ( m_partStack.count() == 1 ) {
00100         kdError( 30500 ) << "You're trying to endInternalEmbedding more often than you started it" << endl;
00101         return;
00102     }
00103     delete m_partStack.pop();
00104     filterChainLeaveDirectory();
00105 }
00106 
00107 int KoEmbeddingFilter::internalPartReference( const QString& key ) const
00108 {
00109     QMapConstIterator<QString, PartReference> it = m_partStack.top()->m_partReferences.find( key );
00110     if ( it == m_partStack.top()->m_partReferences.end() )
00111         return -1;
00112     return it.data().m_index;
00113 }
00114 
00115 QCString KoEmbeddingFilter::internalPartMimeType( const QString& key ) const
00116 {
00117     QMapConstIterator<QString, PartReference> it = m_partStack.top()->m_partReferences.find( key );
00118     if ( it == m_partStack.top()->m_partReferences.end() )
00119         return QCString();
00120     return it.data().m_mimeType;
00121 }
00122 
00123 KoEmbeddingFilter::PartReference::PartReference( int index, const QCString& mimeType ) :
00124     m_index( index ), m_mimeType( mimeType )
00125 {
00126 }
00127 
00128 bool KoEmbeddingFilter::PartReference::isValid() const
00129 {
00130     return m_index != 1 && !m_mimeType.isEmpty();
00131 }
00132 
00133 KoEmbeddingFilter::PartState::PartState() : m_lruPartIndex( 0 )
00134 {
00135 }
00136 
00137 void KoEmbeddingFilter::savePartContents( QIODevice* )
00138 {
00139 }
00140 
00141 void KoEmbeddingFilter::filterChainEnterDirectory( const QString& directory ) const
00142 {
00143     m_chain->enterDirectory( directory );
00144 }
00145 
00146 void KoEmbeddingFilter::filterChainLeaveDirectory() const
00147 {
00148     m_chain->leaveDirectory();
00149 }
00150 
00151 #include <koFilter.moc>
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:23 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003