F:/KPlato/koffice/libs/kofficecore/KoFilter.cpp

Aller à la documentation de ce fichier.
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., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <KoFilter.h>
00021 
00022 #include <QFile>
00023 //Added by qt3to4:
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     // We need to resort to an ugly hack to determine the mimetype
00056     // from the extension, as kservicetypefactory.h isn't installed
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     // 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 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>

Généré le Wed Nov 22 23:41:02 2006 pour KPlato par  doxygen 1.5.1-p1