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>
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 /*************************************************
40 *************************************************/
41 class KDataToolInfo::KDataToolInfoPrivate
44 KDataToolInfoPrivate()
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
)
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";
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();
85 KDataToolInfo::~KDataToolInfo()
90 QString
KDataToolInfo::dataType() const
95 return d
->service
->property( "DataType" ).toString();
98 QStringList
KDataToolInfo::mimeTypes() const
101 return QStringList();
103 return d
->service
->property( "DataMimeTypes" ).toStringList();
106 bool KDataToolInfo::isReadOnly() const
111 return d
->service
->property( "ReadOnly" ).toBool();
114 QPixmap
KDataToolInfo::icon() const
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() )
128 QPixmap
KDataToolInfo::miniIcon() const
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() )
142 QString
KDataToolInfo::iconName() const
146 return d
->service
->icon();
149 QStringList
KDataToolInfo::commands() const
152 return QStringList();
154 return d
->service
->property( "Commands" ).toStringList();
157 QStringList
KDataToolInfo::userCommands() const
160 return QStringList();
162 return d
->service
->comment().split( ',', QString::SkipEmptyParts
);
165 KDataTool
* KDataToolInfo::createTool( QObject
* parent
) const
170 KDataTool
* tool
= d
->service
->createInstance
<KDataTool
>(parent
);
172 tool
->setComponentData(d
->componentData
);
176 KService::Ptr
KDataToolInfo::service() const
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
;
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() )
202 constr
= constr
+ " and " + tmp
;
204 /* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs!
207 QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( componentData.componentName() );
208 if ( constr.isEmpty() )
211 constr = constr + " and " + tmp;
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
));
226 kDebug() << (*it
)->entryPath() << " excluded.";
233 bool KDataToolInfo::isValid() const
235 return( !d
->service
.isNull() );
238 /*************************************************
242 *************************************************/
243 class KDataToolAction::KDataToolActionPrivate
246 KDataToolActionPrivate() {}
252 KDataToolAction::KDataToolAction( const QString
& text
, const KDataToolInfo
& info
, const QString
& command
,
254 : KAction( text
, parent
),
255 d(new KDataToolActionPrivate
)
257 setIcon( KIcon( info
.iconName() ) );
258 d
->command
= command
;
262 KDataToolAction::~KDataToolAction()
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() )
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
& ) ),
301 actionList
.append( action
);
308 /*************************************************
312 *************************************************/
313 class KDataTool::KDataToolPrivate
316 KDataToolPrivate() {}
318 KComponentData componentData
;
321 KDataTool::KDataTool( QObject
* parent
)
322 : QObject(parent
), d(new KDataToolPrivate
)
326 KDataTool::~KDataTool()
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"