KoFilter.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2001 Werner Trobin <trobin@kde.org>
3 2002 Werner Trobin <trobin@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <KoFilter.h>
21
22#include <tqfile.h>
23
24#include <kurl.h>
25#include <kmimetype.h>
26#include <tdetempfile.h>
27#include <kdebug.h>
28#include <KoFilterManager.h>
29
30
31KoFilter::KoFilter() : TQObject( 0, 0 ), m_chain( 0 )
32{
33}
34
35KoFilter::~KoFilter()
36{
37}
38
39
40KoEmbeddingFilter::~KoEmbeddingFilter()
41{
42 if ( m_partStack.count() != 1 )
43 kdWarning() << "Someone messed with the part stack" << endl;
44 delete m_partStack.pop();
45}
46
47int KoEmbeddingFilter::lruPartIndex() const
48{
49 return m_partStack.top()->m_lruPartIndex;
50}
51
52TQString KoEmbeddingFilter::mimeTypeByExtension( const TQString& extension )
53{
54 // We need to resort to an ugly hack to determine the mimetype
55 // from the extension, as kservicetypefactory.h isn't installed
56 KURL url;
57 url.setPath( TQString( "dummy.%1" ).arg( extension ) );
58 KMimeType::Ptr m( KMimeType::findByURL( url, 0, true, true ) );
59 return m->name();
60}
61
63{
64 m_partStack.push( new PartState() );
65}
66
67int KoEmbeddingFilter::embedPart( const TQCString& from, TQCString& to,
68 KoFilter::ConversionStatus& status, const TQString& key )
69{
70 ++( m_partStack.top()->m_lruPartIndex );
71
72 KTempFile tempIn;
73 tempIn.setAutoDelete( true );
74 savePartContents( tempIn.file() );
75 tempIn.file()->close();
76
77 KoFilterManager *manager = new KoFilterManager( tempIn.name(), from, m_chain );
78 status = manager->exp0rt( TQString(), to );
79 delete manager;
80
81 // Add the part to the current "stack frame", using the number as key
82 // if the key string is empty
83 PartReference ref( lruPartIndex(), to );
84 m_partStack.top()->m_partReferences.insert( key.isEmpty() ? TQString::number( lruPartIndex() ) : key, ref );
85
86 return lruPartIndex();
87}
88
89void KoEmbeddingFilter::startInternalEmbedding( const TQString& key, const TQCString& mimeType )
90{
91 filterChainEnterDirectory( TQString::number( ++( m_partStack.top()->m_lruPartIndex ) ) );
92 PartReference ref( lruPartIndex(), mimeType );
93 m_partStack.top()->m_partReferences.insert( key, ref );
94 m_partStack.push( new PartState() );
95}
96
98{
99 if ( m_partStack.count() == 1 ) {
100 kdError( 30500 ) << "You're trying to endInternalEmbedding more often than you started it" << endl;
101 return;
102 }
103 delete m_partStack.pop();
104 filterChainLeaveDirectory();
105}
106
107int KoEmbeddingFilter::internalPartReference( const TQString& key ) const
108{
109 TQMapConstIterator<TQString, PartReference> it = m_partStack.top()->m_partReferences.find( key );
110 if ( it == m_partStack.top()->m_partReferences.end() )
111 return -1;
112 return it.data().m_index;
113}
114
115TQCString KoEmbeddingFilter::internalPartMimeType( const TQString& key ) const
116{
117 TQMapConstIterator<TQString, PartReference> it = m_partStack.top()->m_partReferences.find( key );
118 if ( it == m_partStack.top()->m_partReferences.end() )
119 return TQCString();
120 return it.data().m_mimeType;
121}
122
123KoEmbeddingFilter::PartReference::PartReference( int index, const TQCString& mimeType ) :
124 m_index( index ), m_mimeType( mimeType )
125{
126}
127
128bool KoEmbeddingFilter::PartReference::isValid() const
129{
130 return m_index != 1 && !m_mimeType.isEmpty();
131}
132
133KoEmbeddingFilter::PartState::PartState() : m_lruPartIndex( 0 )
134{
135}
136
137void KoEmbeddingFilter::savePartContents( TQIODevice* )
138{
139}
140
141void KoEmbeddingFilter::filterChainEnterDirectory( const TQString& directory ) const
142{
143 m_chain->enterDirectory( directory );
144}
145
146void KoEmbeddingFilter::filterChainLeaveDirectory() const
147{
148 m_chain->leaveDirectory();
149}
150
151#include <KoFilter.moc>
int embedPart(const TQCString &from, TQCString &to, KoFilter::ConversionStatus &status, const TQString &key=TQString())
Embed some document using an external filter (i.e.
Definition KoFilter.cpp:67
static TQString mimeTypeByExtension(const TQString &extension)
A static helper method to determine the mimetype via the file extension.
Definition KoFilter.cpp:52
int internalPartReference(const TQString &key) const
Query the internal part map for the reference of the part matching the given key.
Definition KoFilter.cpp:107
TQCString internalPartMimeType(const TQString &key) const
Query the internal part map for the mimetype of the part matching the given key.
Definition KoFilter.cpp:115
KoEmbeddingFilter()
Constructs a filter.
Definition KoFilter.cpp:62
void endInternalEmbedding()
This method signals the end of an internal embedding session.
Definition KoFilter.cpp:97
void startInternalEmbedding(const TQString &key, const TQCString &mimeType)
Method to perform "internal" embedding of parts in olefilter-style.
Definition KoFilter.cpp:89
The class managing all the filters.
KoFilter::ConversionStatus exp0rt(const TQString &url, TQCString &mimeType)
Exports the given file/document to the specified URL/mimetype.
The base class for import and export filters.
Definition KoFilter.h:50
KoFilterChain * m_chain
Use this pointer to access all information about input/output during the conversion.
Definition KoFilter.h:108
ConversionStatus
This enum is used to signal the return state of your filter.
Definition KoFilter.h:64
KoFilter()
This is the constructor your filter has to call, obviously.
Definition KoFilter.cpp:31