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

KIO

kdatatool.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
00003    Copyright (C) 2001 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kdatatool.h"
00022 
00023 #include <kstandarddirs.h>
00024 #include <kdebug.h>
00025 #include <kicon.h>
00026 #include <kcomponentdata.h>
00027 #include <ktoggleaction.h>
00028 #include <kactioncollection.h>
00029 #include <kactionmenu.h>
00030 
00031 #include <kservicetypetrader.h>
00032 
00033 #include <QtGui/QPixmap>
00034 #include <QtCore/QFile>
00035 
00036 /*************************************************
00037  *
00038  * KDataToolInfo
00039  *
00040  *************************************************/
00041 class KDataToolInfo::KDataToolInfoPrivate
00042 {
00043 public:
00044     KDataToolInfoPrivate()
00045      : service(0)
00046     {}
00047 
00048     KService::Ptr service;
00049     KComponentData componentData;
00050 };
00051 
00052 KDataToolInfo::KDataToolInfo()
00053     : d(new KDataToolInfoPrivate)
00054 {
00055 }
00056 
00057 KDataToolInfo::KDataToolInfo(const KService::Ptr& service, const KComponentData &componentData)
00058     : d(new KDataToolInfoPrivate)
00059 {
00060     d->service = service;
00061     d->componentData = componentData;
00062 
00063     if ( !d->service && !d->service->serviceTypes().contains( "KDataTool" ) )
00064     {
00065         kDebug(30003) << "The service" << d->service->name()
00066                        << "does not feature the service type KDataTool";
00067         d->service = 0;
00068     }
00069 }
00070 
00071 KDataToolInfo::KDataToolInfo( const KDataToolInfo& info )
00072     : d(new KDataToolInfoPrivate)
00073 {
00074     d->service = info.service();
00075     d->componentData = info.componentData();
00076 }
00077 
00078 KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info )
00079 {
00080     d->service = info.service();
00081     d->componentData = info.componentData();
00082     return *this;
00083 }
00084 
00085 KDataToolInfo::~KDataToolInfo()
00086 {
00087     delete d;
00088 }
00089 
00090 QString KDataToolInfo::dataType() const
00091 {
00092     if ( !d->service )
00093         return QString();
00094 
00095     return d->service->property( "DataType" ).toString();
00096 }
00097 
00098 QStringList KDataToolInfo::mimeTypes() const
00099 {
00100     if ( !d->service )
00101         return QStringList();
00102 
00103     return d->service->property( "DataMimeTypes" ).toStringList();
00104 }
00105 
00106 bool KDataToolInfo::isReadOnly() const
00107 {
00108     if ( !d->service )
00109         return true;
00110 
00111     return d->service->property( "ReadOnly" ).toBool();
00112 }
00113 
00114 QPixmap KDataToolInfo::icon() const
00115 {
00116     if ( !d->service )
00117         return QPixmap();
00118 
00119     QPixmap pix;
00120     const QStringList lst = KGlobal::dirs()->resourceDirs("icon");
00121     QStringList::ConstIterator it = lst.begin();
00122     while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() )
00123         it++;
00124 
00125     return pix;
00126 }
00127 
00128 QPixmap KDataToolInfo::miniIcon() const
00129 {
00130     if ( !d->service )
00131         return QPixmap();
00132 
00133     QPixmap pix;
00134     const QStringList lst = KGlobal::dirs()->resourceDirs("mini");
00135     QStringList::ConstIterator it = lst.begin();
00136     while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() )
00137         it++;
00138 
00139     return pix;
00140 }
00141 
00142 QString KDataToolInfo::iconName() const
00143 {
00144     if ( !d->service )
00145         return QString();
00146     return d->service->icon();
00147 }
00148 
00149 QStringList KDataToolInfo::commands() const
00150 {
00151     if ( !d->service )
00152         return QStringList();
00153 
00154     return d->service->property( "Commands" ).toStringList();
00155 }
00156 
00157 QStringList KDataToolInfo::userCommands() const
00158 {
00159     if ( !d->service )
00160         return QStringList();
00161 
00162     return d->service->comment().split( ',', QString::SkipEmptyParts );
00163 }
00164 
00165 KDataTool* KDataToolInfo::createTool( QObject* parent ) const
00166 {
00167     if ( !d->service )
00168         return 0;
00169 
00170     KDataTool* tool = d->service->createInstance<KDataTool>(parent);
00171     if ( tool )
00172         tool->setComponentData(d->componentData);
00173     return tool;
00174 }
00175 
00176 KService::Ptr KDataToolInfo::service() const
00177 {
00178     return d->service;
00179 }
00180 
00181 KComponentData KDataToolInfo::componentData() const
00182 {
00183     return d->componentData;
00184 }
00185 
00186 QList<KDataToolInfo> KDataToolInfo::query(const QString& datatype, const QString& mimetype, const KComponentData &componentData)
00187 {
00188     QList<KDataToolInfo> lst;
00189 
00190     QString constr;
00191 
00192     if ( !datatype.isEmpty() )
00193     {
00194         constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype );
00195     }
00196     if ( !mimetype.isEmpty() )
00197     {
00198         QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype );
00199         if ( constr.isEmpty() )
00200             constr = tmp;
00201         else
00202             constr = constr + " and " + tmp;
00203     }
00204 /* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs!
00205     if ( componentData )
00206     {
00207         QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( componentData.componentName() );
00208         if ( constr.isEmpty() )
00209             constr = tmp;
00210         else
00211             constr = constr + " and " + tmp;
00212     } */
00213 
00214     // Query the trader
00215     //kDebug() << constr;
00216     const KService::List offers = KServiceTypeTrader::self()->query( "KDataTool", constr );
00217 
00218     KService::List::ConstIterator it = offers.begin();
00219     for( ; it != offers.end(); ++it )
00220     {
00221         // Temporary replacement for the non-working trader query above
00222         if (!componentData.isValid() || !(*it)->property("ExcludeFrom").toStringList()
00223              .contains(componentData.componentName())) {
00224             lst.append(KDataToolInfo(*it, componentData));
00225         } else {
00226             kDebug() << (*it)->entryPath() << " excluded.";
00227         }
00228     }
00229 
00230     return lst;
00231 }
00232 
00233 bool KDataToolInfo::isValid() const
00234 {
00235     return( !d->service.isNull() );
00236 }
00237 
00238 /*************************************************
00239  *
00240  * KDataToolAction
00241  *
00242  *************************************************/
00243 class KDataToolAction::KDataToolActionPrivate
00244 {
00245 public:
00246     KDataToolActionPrivate() {}
00247 
00248     QString command;
00249     KDataToolInfo info;
00250 };
00251 
00252 KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command,
00253                                   QObject *parent )
00254     : KAction( text, parent ),
00255       d(new KDataToolActionPrivate)
00256 {
00257     setIcon( KIcon( info.iconName() ) );
00258     d->command = command;
00259     d->info = info;
00260 }
00261 
00262 KDataToolAction::~KDataToolAction()
00263 {
00264     delete d;
00265 }
00266 
00267 void KDataToolAction::slotActivated()
00268 {
00269     emit toolActivated( d->info, d->command );
00270 }
00271 
00272 QList<QAction*> KDataToolAction::dataToolActionList( const QList<KDataToolInfo> & tools, const QObject *receiver, const char* slot, KActionCollection* parent )
00273 {
00274     QList<QAction*> actionList;
00275     if ( tools.isEmpty() )
00276         return actionList;
00277 
00278     QAction *sep_action = new QAction(parent);
00279     sep_action->setSeparator(true);
00280     actionList.append( sep_action );
00281     QList<KDataToolInfo>::ConstIterator entry = tools.begin();
00282     for( ; entry != tools.end(); ++entry )
00283     {
00284         const QStringList userCommands = (*entry).userCommands();
00285         const QStringList commands = (*entry).commands();
00286         Q_ASSERT(!commands.isEmpty());
00287         if ( commands.count() != userCommands.count() )
00288             kWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath()
00289                         << ")." << commands.count() << "commands and"
00290                         << userCommands.count() << " descriptions.";
00291         QStringList::ConstIterator uit = userCommands.begin();
00292         QStringList::ConstIterator cit = commands.begin();
00293         for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit )
00294         {
00295             //kDebug() << "creating action " << *uit << " " << *cit;
00296             const QString name = (*entry).service()->entryPath(); // something unique
00297             KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit, parent );
00298             parent->addAction( name, action );
00299             connect( action, SIGNAL( toolActivated( const KDataToolInfo &, const QString & ) ),
00300                      receiver, slot );
00301             actionList.append( action );
00302         }
00303     }
00304 
00305     return actionList;
00306 }
00307 
00308 /*************************************************
00309  *
00310  * KDataTool
00311  *
00312  *************************************************/
00313 class KDataTool::KDataToolPrivate
00314 {
00315 public:
00316     KDataToolPrivate() {}
00317 
00318     KComponentData componentData;
00319 };
00320 
00321 KDataTool::KDataTool( QObject* parent )
00322     : QObject(parent), d(new KDataToolPrivate)
00323 {
00324 }
00325 
00326 KDataTool::~KDataTool()
00327 {
00328     delete d;
00329 }
00330 
00331 void KDataTool::setComponentData(const KComponentData &componentData)
00332 {
00333     d->componentData = componentData;
00334 }
00335 
00336 const KComponentData &KDataTool::componentData() const
00337 {
00338    return d->componentData;
00339 }
00340 
00341 #include "kdatatool.moc"

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal