fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kio / kio / kdatatool.cpp
blob269e36d85346e94054fd69c7a249cdd9d19a1ce6
1 /* This file is part of the KDE project
2 Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
3 Copyright (C) 2001 David Faure <faure@kde.org>
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 as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "kdatatool.h"
23 #include <kstandarddirs.h>
24 #include <kdebug.h>
25 #include <kicon.h>
26 #include <kcomponentdata.h>
27 #include <ktoggleaction.h>
28 #include <kactioncollection.h>
29 #include <kactionmenu.h>
31 #include <kservicetypetrader.h>
33 #include <QtGui/QPixmap>
34 #include <QtCore/QFile>
36 /*************************************************
38 * KDataToolInfo
40 *************************************************/
41 class KDataToolInfo::KDataToolInfoPrivate
43 public:
44 KDataToolInfoPrivate()
45 : service(0)
48 KService::Ptr service;
49 KComponentData componentData;
52 KDataToolInfo::KDataToolInfo()
53 : d(new KDataToolInfoPrivate)
57 KDataToolInfo::KDataToolInfo(const KService::Ptr& service, const KComponentData &componentData)
58 : d(new KDataToolInfoPrivate)
60 d->service = service;
61 d->componentData = componentData;
63 if ( !d->service && !d->service->serviceTypes().contains( "KDataTool" ) )
65 kDebug(30003) << "The service" << d->service->name()
66 << "does not feature the service type KDataTool";
67 d->service = 0;
71 KDataToolInfo::KDataToolInfo( const KDataToolInfo& info )
72 : d(new KDataToolInfoPrivate)
74 d->service = info.service();
75 d->componentData = info.componentData();
78 KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info )
80 d->service = info.service();
81 d->componentData = info.componentData();
82 return *this;
85 KDataToolInfo::~KDataToolInfo()
87 delete d;
90 QString KDataToolInfo::dataType() const
92 if ( !d->service )
93 return QString();
95 return d->service->property( "DataType" ).toString();
98 QStringList KDataToolInfo::mimeTypes() const
100 if ( !d->service )
101 return QStringList();
103 return d->service->property( "DataMimeTypes" ).toStringList();
106 bool KDataToolInfo::isReadOnly() const
108 if ( !d->service )
109 return true;
111 return d->service->property( "ReadOnly" ).toBool();
114 QPixmap KDataToolInfo::icon() const
116 if ( !d->service )
117 return QPixmap();
119 QPixmap pix;
120 const QStringList lst = KGlobal::dirs()->resourceDirs("icon");
121 QStringList::ConstIterator it = lst.begin();
122 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() )
123 it++;
125 return pix;
128 QPixmap KDataToolInfo::miniIcon() const
130 if ( !d->service )
131 return QPixmap();
133 QPixmap pix;
134 const QStringList lst = KGlobal::dirs()->resourceDirs("mini");
135 QStringList::ConstIterator it = lst.begin();
136 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() )
137 it++;
139 return pix;
142 QString KDataToolInfo::iconName() const
144 if ( !d->service )
145 return QString();
146 return d->service->icon();
149 QStringList KDataToolInfo::commands() const
151 if ( !d->service )
152 return QStringList();
154 return d->service->property( "Commands" ).toStringList();
157 QStringList KDataToolInfo::userCommands() const
159 if ( !d->service )
160 return QStringList();
162 return d->service->comment().split( ',', QString::SkipEmptyParts );
165 KDataTool* KDataToolInfo::createTool( QObject* parent ) const
167 if ( !d->service )
168 return 0;
170 KDataTool* tool = d->service->createInstance<KDataTool>(parent);
171 if ( tool )
172 tool->setComponentData(d->componentData);
173 return tool;
176 KService::Ptr KDataToolInfo::service() const
178 return d->service;
181 KComponentData KDataToolInfo::componentData() const
183 return d->componentData;
186 QList<KDataToolInfo> KDataToolInfo::query(const QString& datatype, const QString& mimetype, const KComponentData &componentData)
188 QList<KDataToolInfo> lst;
190 QString constr;
192 if ( !datatype.isEmpty() )
194 constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype );
196 if ( !mimetype.isEmpty() )
198 QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype );
199 if ( constr.isEmpty() )
200 constr = tmp;
201 else
202 constr = constr + " and " + tmp;
204 /* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs!
205 if ( componentData )
207 QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( componentData.componentName() );
208 if ( constr.isEmpty() )
209 constr = tmp;
210 else
211 constr = constr + " and " + tmp;
212 } */
214 // Query the trader
215 //kDebug() << constr;
216 const KService::List offers = KServiceTypeTrader::self()->query( "KDataTool", constr );
218 KService::List::ConstIterator it = offers.begin();
219 for( ; it != offers.end(); ++it )
221 // Temporary replacement for the non-working trader query above
222 if (!componentData.isValid() || !(*it)->property("ExcludeFrom").toStringList()
223 .contains(componentData.componentName())) {
224 lst.append(KDataToolInfo(*it, componentData));
225 } else {
226 kDebug() << (*it)->entryPath() << " excluded.";
230 return lst;
233 bool KDataToolInfo::isValid() const
235 return( !d->service.isNull() );
238 /*************************************************
240 * KDataToolAction
242 *************************************************/
243 class KDataToolAction::KDataToolActionPrivate
245 public:
246 KDataToolActionPrivate() {}
248 QString command;
249 KDataToolInfo info;
252 KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command,
253 QObject *parent )
254 : KAction( text, parent ),
255 d(new KDataToolActionPrivate)
257 setIcon( KIcon( info.iconName() ) );
258 d->command = command;
259 d->info = info;
262 KDataToolAction::~KDataToolAction()
264 delete d;
267 void KDataToolAction::slotActivated()
269 emit toolActivated( d->info, d->command );
272 QList<QAction*> KDataToolAction::dataToolActionList( const QList<KDataToolInfo> & tools, const QObject *receiver, const char* slot, KActionCollection* parent )
274 QList<QAction*> actionList;
275 if ( tools.isEmpty() )
276 return actionList;
278 QAction *sep_action = new QAction(parent);
279 sep_action->setSeparator(true);
280 actionList.append( sep_action );
281 QList<KDataToolInfo>::ConstIterator entry = tools.begin();
282 for( ; entry != tools.end(); ++entry )
284 const QStringList userCommands = (*entry).userCommands();
285 const QStringList commands = (*entry).commands();
286 Q_ASSERT(!commands.isEmpty());
287 if ( commands.count() != userCommands.count() )
288 kWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath()
289 << ")." << commands.count() << "commands and"
290 << userCommands.count() << " descriptions.";
291 QStringList::ConstIterator uit = userCommands.begin();
292 QStringList::ConstIterator cit = commands.begin();
293 for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit )
295 //kDebug() << "creating action " << *uit << " " << *cit;
296 const QString name = (*entry).service()->entryPath(); // something unique
297 KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit, parent );
298 parent->addAction( name, action );
299 connect( action, SIGNAL( toolActivated( const KDataToolInfo &, const QString & ) ),
300 receiver, slot );
301 actionList.append( action );
305 return actionList;
308 /*************************************************
310 * KDataTool
312 *************************************************/
313 class KDataTool::KDataToolPrivate
315 public:
316 KDataToolPrivate() {}
318 KComponentData componentData;
321 KDataTool::KDataTool( QObject* parent )
322 : QObject(parent), d(new KDataToolPrivate)
326 KDataTool::~KDataTool()
328 delete d;
331 void KDataTool::setComponentData(const KComponentData &componentData)
333 d->componentData = componentData;
336 const KComponentData &KDataTool::componentData() const
338 return d->componentData;
341 #include "kdatatool.moc"