KIO
kdatatool.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
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
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
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
00296 const QString name = (*entry).service()->entryPath();
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
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"