00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <KoFilter.h>
00021
00022 #include <QFile>
00023
00024
00025 #include <kurl.h>
00026 #include <kmimetype.h>
00027 #include <ktemporaryfile.h>
00028 #include <kdebug.h>
00029 #include <KoFilterManager.h>
00030
00031
00032 KoFilter::KoFilter( QObject* parent ) : QObject( parent ), m_chain( 0 )
00033 {
00034 }
00035
00036 KoFilter::~KoFilter()
00037 {
00038 }
00039
00040
00041 KoEmbeddingFilter::~KoEmbeddingFilter()
00042 {
00043 if ( m_partStack.count() != 1 )
00044 kWarning() << "Someone messed with the part stack" << endl;
00045 delete m_partStack.pop();
00046 }
00047
00048 int KoEmbeddingFilter::lruPartIndex() const
00049 {
00050 return m_partStack.top()->m_lruPartIndex;
00051 }
00052
00053 QString KoEmbeddingFilter::mimeTypeByExtension( const QString& extension )
00054 {
00055
00056
00057 KUrl url;
00058 url.setPath( QString( "dummy.%1" ).arg( extension ) );
00059 KMimeType::Ptr m( KMimeType::findByUrl( url, 0, true, true ) );
00060 return m->name();
00061 }
00062
00063 KoEmbeddingFilter::KoEmbeddingFilter() : KoFilter()
00064 {
00065 m_partStack.push( new PartState() );
00066 }
00067
00068 int KoEmbeddingFilter::embedPart( const QByteArray& from, QByteArray& to,
00069 KoFilter::ConversionStatus& status, const QString& key )
00070 {
00071 ++( m_partStack.top()->m_lruPartIndex );
00072
00073 KTemporaryFile tempIn;
00074 tempIn.open();
00075 savePartContents( &tempIn );
00076
00077 KoFilterManager *manager = new KoFilterManager( tempIn.fileName(), from, m_chain );
00078 status = manager->exp0rt( QString::null, to );
00079 delete manager;
00080
00081
00082
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 QByteArray& 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 kError( 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 QMap<QString, PartReference>::const_iterator it = m_partStack.top()->m_partReferences.find( key );
00110 if ( it == m_partStack.top()->m_partReferences.end() )
00111 return -1;
00112 return it.value().m_index;
00113 }
00114
00115 QByteArray KoEmbeddingFilter::internalPartMimeType( const QString& key ) const
00116 {
00117 QMap<QString, PartReference>::const_iterator it = m_partStack.top()->m_partReferences.find( key );
00118 if ( it == m_partStack.top()->m_partReferences.end() )
00119 return QByteArray();
00120 return it.value().m_mimeType;
00121 }
00122
00123 KoEmbeddingFilter::PartReference::PartReference( int index, const QByteArray& 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>