KoQueryTrader.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
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 <tdeparts/factory.h>
21
22#include <KoQueryTrader.h>
23#include <KoDocument.h>
24#include <KoFilter.h>
25#include <ktrader.h>
26#include <kservicetype.h>
27#include <kdebug.h>
28
29#include <tqfile.h>
30
31#include <limits.h> // UINT_MAX
32
40/*******************************************************************
41 *
42 * KoDocumentEntry
43 *
44 *******************************************************************/
45
46KoDocumentEntry::KoDocumentEntry( KService::Ptr service )
47 : m_service( service )
48{
49}
50
51KoDocument* KoDocumentEntry::createDoc( KoDocument* parent, const char* name ) const
52{
53 KLibFactory* factory = KLibLoader::self()->factory( TQFile::encodeName(m_service->library()) );
54
55 if( !factory ) {
56 kdWarning(30003) << KLibLoader::self()->lastErrorMessage() << endl;
57 return 0;
58 }
59
60 TQObject* obj;
61 if ( factory->inherits( "KParts::Factory" ) )
62 obj = static_cast<KParts::Factory *>(factory)->createPart( 0L, "", parent, name, "KoDocument" );
63 else {
64 kdWarning(30003) << "factory doesn't inherit KParts::Factory ! It is a " << factory->className() << endl; // This shouldn't happen...
65 obj = factory->create( parent, name, "KoDocument" );
66 }
67
68 if ( !obj || !obj->inherits( "KoDocument" ) )
69 {
70 delete obj;
71 return 0;
72 }
73
74 return static_cast<KoDocument*>(obj);
75}
76
78{
79 TQString constr = TQString::fromLatin1( "[X-TDE-NativeMimeType] == '%1' or '%2' in [X-TDE-ExtraNativeMimeTypes]" ).arg( mimetype ).arg( mimetype );
80
81 TQValueList<KoDocumentEntry> vec = query( false,constr );
82 if ( vec.isEmpty() )
83 {
84 kdWarning(30003) << "Got no results with " << constr << endl;
85 // Fallback to the old way (which was probably wrong, but better be safe)
86 TQString constr = TQString::fromLatin1( "'%1' in ServiceTypes" ).arg( mimetype );
87 vec = query( constr );
88 if ( vec.isEmpty() )
89 {
90 // Still no match. Either the mimetype itself is unknown, or we have no service for it.
91 // Help the user debugging stuff by providing some more diagnostics
92 if ( KServiceType::serviceType( mimetype ) == 0L )
93 {
94 kdError(30003) << "Unknown KOffice MimeType " << mimetype << "." << endl;
95 kdError(30003) << "Check your installation (for instance, run 'tde-config --path mime' and check the result)." << endl;
96 } else
97 {
98 kdError(30003) << "Found no KOffice part able to handle " << mimetype << "!" << endl;
99 kdError(30003) << "Check your installation (does the desktop file have X-TDE-NativeMimeType and KOfficePart, did you install KOffice in a different prefix than TDE, without adding the prefix to /etc/tderc ?)" << endl;
100 }
101 return KoDocumentEntry();
102 }
103 }
104
105 return vec[0];
106}
107
108TQValueList<KoDocumentEntry> KoDocumentEntry::query( const TQString & _constr )
109{
110 return query(true,_constr);
111}
112
113TQValueList<KoDocumentEntry> KoDocumentEntry::query( bool _onlyDocEmb, const TQString & _constr )
114{
115
116 TQValueList<KoDocumentEntry> lst;
117 TQString constr;
118 if ( !_constr.isEmpty() ) {
119 constr = "(";
120 constr += _constr;
121 constr += ") and ";
122 }
123 constr += " exist Library";
124
125 // Query the trader
126 TDETrader::OfferList offers = TDETrader::self()->query( "KOfficePart", constr );
127
128 TDETrader::OfferList::ConstIterator it = offers.begin();
129 unsigned int max = offers.count();
130 for( unsigned int i = 0; i < max; i++, ++it )
131 {
132 //kdDebug(30003) << " desktopEntryPath=" << (*it)->desktopEntryPath()
133 // << " library=" << (*it)->library() << endl;
134
135 if ( (*it)->noDisplay() )
136 continue;
137 // Maybe this could be done as a trader constraint too.
138 if ( (!_onlyDocEmb) || ((*it)->property("X-TDE-NOTKoDocumentEmbeddable").toString()!="1") )
139 {
140 KoDocumentEntry d( *it );
141 // Append converted offer
142 lst.append( d );
143 // Next service
144 }
145 }
146
147 if ( lst.count() > 1 && !_constr.isEmpty() )
148 kdWarning(30003) << "KoDocumentEntry::query " << constr << " got " << max << " offers!" << endl;
149
150 return lst;
151}
152
153
154
155
156/*******************************************************************
157 *
158 * KoFilterEntry
159 *
160 *******************************************************************/
161
162KoFilterEntry::KoFilterEntry( KService::Ptr service )
163 : m_service( service )
164{
165 import = service->property( "X-TDE-Import" ).toStringList();
166 export_ = service->property( "X-TDE-Export" ).toStringList();
167 int w = service->property( "X-TDE-Weight" ).toInt();
168 weight = w < 0 ? UINT_MAX : static_cast<unsigned int>( w );
169 available = service->property( "X-TDE-Available" ).toString();
170}
171
172TQValueList<KoFilterEntry::Ptr> KoFilterEntry::query( const TQString & _constr )
173{
174 kdDebug(30500) << "KoFilterEntry::query( " << _constr << " )" << endl;
175 TQValueList<KoFilterEntry::Ptr> lst;
176
177 TDETrader::OfferList offers = TDETrader::self()->query( "KOfficeFilter", _constr );
178
179 TDETrader::OfferList::ConstIterator it = offers.begin();
180 unsigned int max = offers.count();
181 //kdDebug(30500) << "Query returned " << max << " offers" << endl;
182 for( unsigned int i = 0; i < max; i++ )
183 {
184 //kdDebug(30500) << " desktopEntryPath=" << (*it)->desktopEntryPath()
185 // << " library=" << (*it)->library() << endl;
186 // Append converted offer
187 lst.append( new KoFilterEntry( *it ) );
188 // Next service
189 it++;
190 }
191
192 return lst;
193}
194
195KoFilter* KoFilterEntry::createFilter( KoFilterChain* chain, TQObject* parent, const char* name )
196{
197 KLibFactory* factory = KLibLoader::self()->factory( TQFile::encodeName( m_service->library() ) );
198
199 if ( !factory ) {
200 kdWarning(30003) << KLibLoader::self()->lastErrorMessage() << endl;
201 return 0;
202 }
203
204 TQObject* obj = factory->create( parent, name, "KoFilter" );
205 if ( !obj || !obj->inherits( "KoFilter" ) )
206 {
207 delete obj;
208 return 0;
209 }
210
211 KoFilter* filter = static_cast<KoFilter*>( obj );
212 filter->m_chain = chain;
213 return filter;
214}
KXMLGUIFactory * factory() const
Represents an available KOffice component that supports the document interface.
static KoDocumentEntry queryByMimeType(const TQString &mimetype)
This is a convenience function.
static TQValueList< KoDocumentEntry > query(const TQString &_constr=TQString())
This function will query tdesycoca to find all available components.
KoDocument * createDoc(KoDocument *parent=0, const char *name=0) const
Uses the factory of the component to create a document.
TQString name() const
The KOffice document class.
Definition KoDocument.h:61
This class represents a chain of plain KoFilter instances.
Represents an available filter.
static TQValueList< KoFilterEntry::Ptr > query(const TQString &_constr=TQString())
This function will query KDED to find all available filters.
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