• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
kdatatool.cpp
Go to the documentation of this file.
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>
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 as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
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.
14
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.
19*/
20
21#include "kdatatool.h"
22
23#include <kstandarddirs.h>
24#include <kdebug.h>
25#include <kicon.h>
26#include <kcomponentdata.h>
27#include <kactioncollection.h>
28
29#include <kservicetypetrader.h>
30
31#include <QtGui/QPixmap>
32#include <QtCore/QFile>
33
34/*************************************************
35 *
36 * KDataToolInfo
37 *
38 *************************************************/
39class KDataToolInfo::KDataToolInfoPrivate
40{
41public:
42 KDataToolInfoPrivate()
43 : service(0)
44 {}
45
46 KService::Ptr service;
47 KComponentData componentData;
48};
49
50KDataToolInfo::KDataToolInfo()
51 : d(new KDataToolInfoPrivate)
52{
53}
54
55KDataToolInfo::KDataToolInfo(const KService::Ptr& service, const KComponentData &componentData)
56 : d(new KDataToolInfoPrivate)
57{
58 d->service = service;
59 d->componentData = componentData;
60
61 if ( !d->service && !d->service->serviceTypes().contains( "KDataTool" ) )
62 {
63 kDebug(30003) << "The service" << d->service->name()
64 << "does not feature the service type KDataTool";
65 d->service = 0;
66 }
67}
68
69KDataToolInfo::KDataToolInfo( const KDataToolInfo& info )
70 : d(new KDataToolInfoPrivate)
71{
72 d->service = info.service();
73 d->componentData = info.componentData();
74}
75
76KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info )
77{
78 d->service = info.service();
79 d->componentData = info.componentData();
80 return *this;
81}
82
83KDataToolInfo::~KDataToolInfo()
84{
85 delete d;
86}
87
88QString KDataToolInfo::dataType() const
89{
90 if ( !d->service )
91 return QString();
92
93 return d->service->property( "DataType" ).toString();
94}
95
96QStringList KDataToolInfo::mimeTypes() const
97{
98 if ( !d->service )
99 return QStringList();
100
101 return d->service->property( "DataMimeTypes" ).toStringList();
102}
103
104bool KDataToolInfo::isReadOnly() const
105{
106 if ( !d->service )
107 return true;
108
109 return d->service->property( "ReadOnly" ).toBool();
110}
111
112#ifndef KDE_NO_DEPRECATED
113QPixmap KDataToolInfo::icon() const
114{
115 if ( !d->service )
116 return QPixmap();
117
118 QPixmap pix;
119 const QStringList lst = KGlobal::dirs()->resourceDirs("icon");
120 QStringList::ConstIterator it = lst.begin();
121 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() )
122 it++;
123
124 return pix;
125}
126#endif
127
128#ifndef KDE_NO_DEPRECATED
129QPixmap KDataToolInfo::miniIcon() const
130{
131 if ( !d->service )
132 return QPixmap();
133
134 QPixmap pix;
135 const QStringList lst = KGlobal::dirs()->resourceDirs("mini");
136 QStringList::ConstIterator it = lst.begin();
137 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() )
138 it++;
139
140 return pix;
141}
142#endif
143
144QString KDataToolInfo::iconName() const
145{
146 if ( !d->service )
147 return QString();
148 return d->service->icon();
149}
150
151QStringList KDataToolInfo::commands() const
152{
153 if ( !d->service )
154 return QStringList();
155
156 return d->service->property( "Commands" ).toStringList();
157}
158
159QStringList KDataToolInfo::userCommands() const
160{
161 if ( !d->service )
162 return QStringList();
163
164 return d->service->comment().split( ',', QString::SkipEmptyParts );
165}
166
167KDataTool* KDataToolInfo::createTool( QObject* parent ) const
168{
169 if ( !d->service )
170 return 0;
171
172 KDataTool* tool = d->service->createInstance<KDataTool>(parent);
173 if ( tool )
174 tool->setComponentData(d->componentData);
175 return tool;
176}
177
178KService::Ptr KDataToolInfo::service() const
179{
180 return d->service;
181}
182
183KComponentData KDataToolInfo::componentData() const
184{
185 return d->componentData;
186}
187
188QList<KDataToolInfo> KDataToolInfo::query(const QString& datatype, const QString& mimetype, const KComponentData &componentData)
189{
190 QList<KDataToolInfo> lst;
191
192 QString constr;
193
194 if ( !datatype.isEmpty() )
195 {
196 constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype );
197 }
198 if ( !mimetype.isEmpty() )
199 {
200 QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype );
201 if ( constr.isEmpty() )
202 constr = tmp;
203 else
204 constr = constr + " and " + tmp;
205 }
206/* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs!
207 if ( componentData )
208 {
209 QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( componentData.componentName() );
210 if ( constr.isEmpty() )
211 constr = tmp;
212 else
213 constr = constr + " and " + tmp;
214 } */
215
216 // Query the trader
217 //kDebug() << constr;
218 const KService::List offers = KServiceTypeTrader::self()->query( "KDataTool", constr );
219
220 KService::List::ConstIterator it = offers.begin();
221 for( ; it != offers.end(); ++it )
222 {
223 // Temporary replacement for the non-working trader query above
224 if (!componentData.isValid() || !(*it)->property("ExcludeFrom").toStringList()
225 .contains(componentData.componentName())) {
226 lst.append(KDataToolInfo(*it, componentData));
227 } else {
228 kDebug() << (*it)->entryPath() << " excluded.";
229 }
230 }
231
232 return lst;
233}
234
235bool KDataToolInfo::isValid() const
236{
237 return( !d->service.isNull() );
238}
239
240/*************************************************
241 *
242 * KDataToolAction
243 *
244 *************************************************/
245class KDataToolAction::KDataToolActionPrivate
246{
247public:
248 KDataToolActionPrivate() {}
249
250 QString command;
251 KDataToolInfo info;
252};
253
254KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command,
255 QObject *parent )
256 : KAction( text, parent ),
257 d(new KDataToolActionPrivate)
258{
259 setIcon( KIcon( info.iconName() ) );
260 d->command = command;
261 d->info = info;
262}
263
264KDataToolAction::~KDataToolAction()
265{
266 delete d;
267}
268
269void KDataToolAction::slotActivated()
270{
271 emit toolActivated( d->info, d->command );
272}
273
274QList<QAction*> KDataToolAction::dataToolActionList( const QList<KDataToolInfo> & tools, const QObject *receiver, const char* slot, KActionCollection* parent )
275{
276 QList<QAction*> actionList;
277 if ( tools.isEmpty() )
278 return actionList;
279
280 QAction *sep_action = new QAction(parent);
281 sep_action->setSeparator(true);
282 actionList.append( sep_action );
283 QList<KDataToolInfo>::ConstIterator entry = tools.begin();
284 for( ; entry != tools.end(); ++entry )
285 {
286 const QStringList userCommands = (*entry).userCommands();
287 const QStringList commands = (*entry).commands();
288 Q_ASSERT(!commands.isEmpty());
289 if ( commands.count() != userCommands.count() )
290 kWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath()
291 << ")." << commands.count() << "commands and"
292 << userCommands.count() << " descriptions.";
293 QStringList::ConstIterator uit = userCommands.begin();
294 QStringList::ConstIterator cit = commands.begin();
295 for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit )
296 {
297 //kDebug() << "creating action " << *uit << " " << *cit;
298 const QString name = (*entry).service()->entryPath(); // something unique
299 KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit, parent );
300 parent->addAction( name, action );
301 connect( action, SIGNAL(toolActivated(KDataToolInfo,QString)),
302 receiver, slot );
303 actionList.append( action );
304 }
305 }
306
307 return actionList;
308}
309
310/*************************************************
311 *
312 * KDataTool
313 *
314 *************************************************/
315class KDataTool::KDataToolPrivate
316{
317public:
318 KDataToolPrivate() {}
319
320 KComponentData componentData;
321};
322
323KDataTool::KDataTool( QObject* parent )
324 : QObject(parent), d(new KDataToolPrivate)
325{
326}
327
328KDataTool::~KDataTool()
329{
330 delete d;
331}
332
333void KDataTool::setComponentData(const KComponentData &componentData)
334{
335 d->componentData = componentData;
336}
337
338const KComponentData &KDataTool::componentData() const
339{
340 return d->componentData;
341}
342
343#include "kdatatool.moc"
KActionCollection
KActionCollection::addAction
KAction * addAction(const QString &name, const QObject *receiver=0, const char *member=0)
KAction
KComponentData
KComponentData::isValid
bool isValid() const
KComponentData::componentName
QString componentName() const
KDataToolAction
This class helps applications implement support for KDataTool.
Definition: kdatatool.h:202
KDataToolAction::dataToolActionList
static QList< QAction * > dataToolActionList(const QList< KDataToolInfo > &tools, const QObject *receiver, const char *slot, KActionCollection *parent)
Creates a list of actions from a list of information about data-tools.
Definition: kdatatool.cpp:274
KDataToolAction::slotActivated
virtual void slotActivated()
Definition: kdatatool.cpp:269
KDataToolAction::~KDataToolAction
~KDataToolAction()
Destructor.
Definition: kdatatool.cpp:264
KDataToolAction::toolActivated
void toolActivated(const KDataToolInfo &info, const QString &command)
Emitted when a tool has been activated.
KDataToolAction::KDataToolAction
KDataToolAction(const QString &text, const KDataToolInfo &info, const QString &command, QObject *parent)
Constructs a new KDataToolAction.
Definition: kdatatool.cpp:254
KDataToolInfo
This is a convenience class for KService.
Definition: kdatatool.h:50
KDataToolInfo::service
KService::Ptr service() const
The KDataToolInfo's service that is represented by this class.
Definition: kdatatool.cpp:178
KDataToolInfo::isValid
bool isValid() const
A DataToolInfo may be invalid if the KService passed to its constructor does not feature the service ...
Definition: kdatatool.cpp:235
KDataToolInfo::iconName
QString iconName() const
Returns the icon name for this DataTool.
Definition: kdatatool.cpp:144
KDataToolInfo::createTool
KDataTool * createTool(QObject *parent=0) const
Creates the data tool described by this KDataToolInfo.
Definition: kdatatool.cpp:167
KDataToolInfo::userCommands
QStringList userCommands() const
Returns a list of strings that you can put in a QPopupMenu item, for example to offer the DataTools s...
Definition: kdatatool.cpp:159
KDataToolInfo::mimeTypes
QStringList mimeTypes() const
Returns a list of mime type that will be accepted by the DataTool.
Definition: kdatatool.cpp:96
KDataToolInfo::operator=
KDataToolInfo & operator=(const KDataToolInfo &info)
Assignment operator.
Definition: kdatatool.cpp:76
KDataToolInfo::~KDataToolInfo
~KDataToolInfo()
Destructor.
Definition: kdatatool.cpp:83
KDataToolInfo::commands
QStringList commands() const
Returns the list of commands the DataTool can execute.
Definition: kdatatool.cpp:151
KDataToolInfo::icon
QPixmap icon() const
Returns the icon of this data tool.
Definition: kdatatool.cpp:113
KDataToolInfo::isReadOnly
bool isReadOnly() const
Checks whether the DataTool is read-only.
Definition: kdatatool.cpp:104
KDataToolInfo::miniIcon
QPixmap miniIcon() const
Returns the mini icon of this data tool.
Definition: kdatatool.cpp:129
KDataToolInfo::query
static QList< KDataToolInfo > query(const QString &datatype, const QString &mimetype, const KComponentData &instance)
Queries the KServiceTypeTrader about installed KDataTool implementations.
Definition: kdatatool.cpp:188
KDataToolInfo::dataType
QString dataType() const
Returns the data type that the DataTool can accept.
Definition: kdatatool.cpp:88
KDataToolInfo::KDataToolInfo
KDataToolInfo()
Create an invalid KDataToolInfo.
Definition: kdatatool.cpp:50
KDataToolInfo::componentData
KComponentData componentData() const
The instance of the service.
Definition: kdatatool.cpp:183
KDataTool
A generic tool that processes data.
Definition: kdatatool.h:263
KDataTool::setComponentData
void setComponentData(const KComponentData &componentData)
Definition: kdatatool.cpp:333
KDataTool::componentData
const KComponentData & componentData() const
Returns the instance of the part that created this tool.
Definition: kdatatool.cpp:338
KDataTool::KDataTool
KDataTool(QObject *parent=0)
Constructor The data-tool is only created when a menu-item, that relates to it, is activated.
Definition: kdatatool.cpp:323
KDataTool::~KDataTool
~KDataTool()
Destructor.
Definition: kdatatool.cpp:328
KIcon
KServiceTypeTrader::self
static KServiceTypeTrader * self()
KServiceTypeTrader::query
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
KSharedPtr< KService >
KStandardDirs::resourceDirs
QStringList resourceDirs(const char *type) const
QAction
QList
QObject
kDebug
#define kDebug
kWarning
#define kWarning
kactioncollection.h
kcomponentdata.h
kdatatool.h
kdebug.h
kicon.h
kservicetypetrader.h
kstandarddirs.h
KGlobal::dirs
KStandardDirs * dirs()
name
const char * name(StandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal