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

KIO

  • sources
  • kde-4.14
  • kdelibs
  • 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  *************************************************/
39 class KDataToolInfo::KDataToolInfoPrivate
40 {
41 public:
42  KDataToolInfoPrivate()
43  : service(0)
44  {}
45 
46  KService::Ptr service;
47  KComponentData componentData;
48 };
49 
50 KDataToolInfo::KDataToolInfo()
51  : d(new KDataToolInfoPrivate)
52 {
53 }
54 
55 KDataToolInfo::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 
69 KDataToolInfo::KDataToolInfo( const KDataToolInfo& info )
70  : d(new KDataToolInfoPrivate)
71 {
72  d->service = info.service();
73  d->componentData = info.componentData();
74 }
75 
76 KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info )
77 {
78  d->service = info.service();
79  d->componentData = info.componentData();
80  return *this;
81 }
82 
83 KDataToolInfo::~KDataToolInfo()
84 {
85  delete d;
86 }
87 
88 QString KDataToolInfo::dataType() const
89 {
90  if ( !d->service )
91  return QString();
92 
93  return d->service->property( "DataType" ).toString();
94 }
95 
96 QStringList KDataToolInfo::mimeTypes() const
97 {
98  if ( !d->service )
99  return QStringList();
100 
101  return d->service->property( "DataMimeTypes" ).toStringList();
102 }
103 
104 bool 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
113 QPixmap 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
129 QPixmap 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 
144 QString KDataToolInfo::iconName() const
145 {
146  if ( !d->service )
147  return QString();
148  return d->service->icon();
149 }
150 
151 QStringList KDataToolInfo::commands() const
152 {
153  if ( !d->service )
154  return QStringList();
155 
156  return d->service->property( "Commands" ).toStringList();
157 }
158 
159 QStringList KDataToolInfo::userCommands() const
160 {
161  if ( !d->service )
162  return QStringList();
163 
164  return d->service->comment().split( ',', QString::SkipEmptyParts );
165 }
166 
167 KDataTool* 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 
178 KService::Ptr KDataToolInfo::service() const
179 {
180  return d->service;
181 }
182 
183 KComponentData KDataToolInfo::componentData() const
184 {
185  return d->componentData;
186 }
187 
188 QList<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 
235 bool KDataToolInfo::isValid() const
236 {
237  return( !d->service.isNull() );
238 }
239 
240 /*************************************************
241  *
242  * KDataToolAction
243  *
244  *************************************************/
245 class KDataToolAction::KDataToolActionPrivate
246 {
247 public:
248  KDataToolActionPrivate() {}
249 
250  QString command;
251  KDataToolInfo info;
252 };
253 
254 KDataToolAction::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 
264 KDataToolAction::~KDataToolAction()
265 {
266  delete d;
267 }
268 
269 void KDataToolAction::slotActivated()
270 {
271  emit toolActivated( d->info, d->command );
272 }
273 
274 QList<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  *************************************************/
315 class KDataTool::KDataToolPrivate
316 {
317 public:
318  KDataToolPrivate() {}
319 
320  KComponentData componentData;
321 };
322 
323 KDataTool::KDataTool( QObject* parent )
324  : QObject(parent), d(new KDataToolPrivate)
325 {
326 }
327 
328 KDataTool::~KDataTool()
329 {
330  delete d;
331 }
332 
333 void KDataTool::setComponentData(const KComponentData &componentData)
334 {
335  d->componentData = componentData;
336 }
337 
338 const KComponentData &KDataTool::componentData() const
339 {
340  return d->componentData;
341 }
342 
343 #include "kdatatool.moc"
KDataToolAction::KDataToolAction
KDataToolAction(const QString &text, const KDataToolInfo &info, const QString &command, QObject *parent)
Constructs a new KDataToolAction.
Definition: kdatatool.cpp:254
KSharedPtr
Definition: kprotocolmanager.h:31
KActionCollection
kdebug.h
QAction::setSeparator
void setSeparator(bool b)
KDataToolAction::toolActivated
void toolActivated(const KDataToolInfo &info, const QString &command)
Emitted when a tool has been activated.
KDataToolAction
This class helps applications implement support for KDataTool.
Definition: kdatatool.h:201
KDataToolInfo::iconName
QString iconName() const
Returns the icon name for this DataTool.
Definition: kdatatool.cpp:144
KServiceTypeTrader::self
static KServiceTypeTrader * self()
KIO::mimetype
MimetypeJob * mimetype(const KUrl &url, JobFlags flags=DefaultFlags)
Find mimetype for one file or directory.
Definition: job.cpp:1856
kactioncollection.h
QAction::setIcon
void setIcon(const QIcon &icon)
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
KGlobal::dirs
KStandardDirs * dirs()
KDataToolInfo::miniIcon
QPixmap miniIcon() const
Returns the mini icon of this data tool.
Definition: kdatatool.cpp:129
KActionCollection::addAction
QAction * addAction(const QString &name, QAction *action)
KDataToolInfo::operator=
KDataToolInfo & operator=(const KDataToolInfo &info)
Assignment operator.
Definition: kdatatool.cpp:76
kdatatool.h
KDataToolInfo::createTool
KDataTool * createTool(QObject *parent=0) const
Creates the data tool described by this KDataToolInfo.
Definition: kdatatool.cpp:167
KDataToolInfo::commands
QStringList commands() const
Returns the list of commands the DataTool can execute.
Definition: kdatatool.cpp:151
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KDataToolAction::slotActivated
virtual void slotActivated()
Definition: kdatatool.cpp:269
KDataToolInfo
This is a convenience class for KService.
Definition: kdatatool.h:49
kservicetypetrader.h
QObject::name
const char * name() const
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
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KDataToolInfo::icon
QPixmap icon() const
Returns the icon of this data tool.
Definition: kdatatool.cpp:113
QObject
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KDataToolInfo::service
KService::Ptr service() const
The KDataToolInfo's service that is represented by this class.
Definition: kdatatool.cpp:178
KDataTool::componentData
const KComponentData & componentData() const
Returns the instance of the part that created this tool.
Definition: kdatatool.cpp:338
KIcon
KComponentData::componentName
QString componentName() const
QString
QList
KDataToolInfo::componentData
KComponentData componentData() const
The instance of the service.
Definition: kdatatool.cpp:183
QPixmap::load
bool load(const QString &fileName, const char *format, QFlags< Qt::ImageConversionFlag > flags)
QStringList
QPixmap
KDataToolInfo::KDataToolInfo
KDataToolInfo()
Create an invalid KDataToolInfo.
Definition: kdatatool.cpp:50
KDataToolInfo::isReadOnly
bool isReadOnly() const
Checks whether the DataTool is read-only.
Definition: kdatatool.cpp:104
QList::end
iterator end()
KDataToolInfo::dataType
QString dataType() const
Returns the data type that the DataTool can accept.
Definition: kdatatool.cpp:88
KStandardDirs::resourceDirs
QStringList resourceDirs(const char *type) const
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
KServiceTypeTrader::query
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
kstandarddirs.h
QAction
KAction
QList::ConstIterator
typedef ConstIterator
KComponentData::isValid
bool isValid() const
KDataToolInfo::~KDataToolInfo
~KDataToolInfo()
Destructor.
Definition: kdatatool.cpp:83
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KDataTool::setComponentData
void setComponentData(const KComponentData &componentData)
Definition: kdatatool.cpp:333
KDataToolInfo::mimeTypes
QStringList mimeTypes() const
Returns a list of mime type that will be accepted by the DataTool.
Definition: kdatatool.cpp:96
KDataToolAction::~KDataToolAction
~KDataToolAction()
Destructor.
Definition: kdatatool.cpp:264
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
QAction::QAction
QAction(QObject *parent)
kcomponentdata.h
kicon.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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
KDataTool
A generic tool that processes data.
Definition: kdatatool.h:262
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QList::begin
iterator begin()
KComponentData
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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