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 <kactioncollection.h> 00028 00029 #include <kservicetypetrader.h> 00030 00031 #include <QtGui/QPixmap> 00032 #include <QtCore/QFile> 00033 00034 /************************************************* 00035 * 00036 * KDataToolInfo 00037 * 00038 *************************************************/ 00039 class KDataToolInfo::KDataToolInfoPrivate 00040 { 00041 public: 00042 KDataToolInfoPrivate() 00043 : service(0) 00044 {} 00045 00046 KService::Ptr service; 00047 KComponentData componentData; 00048 }; 00049 00050 KDataToolInfo::KDataToolInfo() 00051 : d(new KDataToolInfoPrivate) 00052 { 00053 } 00054 00055 KDataToolInfo::KDataToolInfo(const KService::Ptr& service, const KComponentData &componentData) 00056 : d(new KDataToolInfoPrivate) 00057 { 00058 d->service = service; 00059 d->componentData = componentData; 00060 00061 if ( !d->service && !d->service->serviceTypes().contains( "KDataTool" ) ) 00062 { 00063 kDebug(30003) << "The service" << d->service->name() 00064 << "does not feature the service type KDataTool"; 00065 d->service = 0; 00066 } 00067 } 00068 00069 KDataToolInfo::KDataToolInfo( const KDataToolInfo& info ) 00070 : d(new KDataToolInfoPrivate) 00071 { 00072 d->service = info.service(); 00073 d->componentData = info.componentData(); 00074 } 00075 00076 KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info ) 00077 { 00078 d->service = info.service(); 00079 d->componentData = info.componentData(); 00080 return *this; 00081 } 00082 00083 KDataToolInfo::~KDataToolInfo() 00084 { 00085 delete d; 00086 } 00087 00088 QString KDataToolInfo::dataType() const 00089 { 00090 if ( !d->service ) 00091 return QString(); 00092 00093 return d->service->property( "DataType" ).toString(); 00094 } 00095 00096 QStringList KDataToolInfo::mimeTypes() const 00097 { 00098 if ( !d->service ) 00099 return QStringList(); 00100 00101 return d->service->property( "DataMimeTypes" ).toStringList(); 00102 } 00103 00104 bool KDataToolInfo::isReadOnly() const 00105 { 00106 if ( !d->service ) 00107 return true; 00108 00109 return d->service->property( "ReadOnly" ).toBool(); 00110 } 00111 00112 #ifndef KDE_NO_DEPRECATED 00113 QPixmap KDataToolInfo::icon() const 00114 { 00115 if ( !d->service ) 00116 return QPixmap(); 00117 00118 QPixmap pix; 00119 const QStringList lst = KGlobal::dirs()->resourceDirs("icon"); 00120 QStringList::ConstIterator it = lst.begin(); 00121 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() ) 00122 it++; 00123 00124 return pix; 00125 } 00126 #endif 00127 00128 #ifndef KDE_NO_DEPRECATED 00129 QPixmap KDataToolInfo::miniIcon() const 00130 { 00131 if ( !d->service ) 00132 return QPixmap(); 00133 00134 QPixmap pix; 00135 const QStringList lst = KGlobal::dirs()->resourceDirs("mini"); 00136 QStringList::ConstIterator it = lst.begin(); 00137 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() ) 00138 it++; 00139 00140 return pix; 00141 } 00142 #endif 00143 00144 QString KDataToolInfo::iconName() const 00145 { 00146 if ( !d->service ) 00147 return QString(); 00148 return d->service->icon(); 00149 } 00150 00151 QStringList KDataToolInfo::commands() const 00152 { 00153 if ( !d->service ) 00154 return QStringList(); 00155 00156 return d->service->property( "Commands" ).toStringList(); 00157 } 00158 00159 QStringList KDataToolInfo::userCommands() const 00160 { 00161 if ( !d->service ) 00162 return QStringList(); 00163 00164 return d->service->comment().split( ',', QString::SkipEmptyParts ); 00165 } 00166 00167 KDataTool* KDataToolInfo::createTool( QObject* parent ) const 00168 { 00169 if ( !d->service ) 00170 return 0; 00171 00172 KDataTool* tool = d->service->createInstance<KDataTool>(parent); 00173 if ( tool ) 00174 tool->setComponentData(d->componentData); 00175 return tool; 00176 } 00177 00178 KService::Ptr KDataToolInfo::service() const 00179 { 00180 return d->service; 00181 } 00182 00183 KComponentData KDataToolInfo::componentData() const 00184 { 00185 return d->componentData; 00186 } 00187 00188 QList<KDataToolInfo> KDataToolInfo::query(const QString& datatype, const QString& mimetype, const KComponentData &componentData) 00189 { 00190 QList<KDataToolInfo> lst; 00191 00192 QString constr; 00193 00194 if ( !datatype.isEmpty() ) 00195 { 00196 constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype ); 00197 } 00198 if ( !mimetype.isEmpty() ) 00199 { 00200 QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype ); 00201 if ( constr.isEmpty() ) 00202 constr = tmp; 00203 else 00204 constr = constr + " and " + tmp; 00205 } 00206 /* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs! 00207 if ( componentData ) 00208 { 00209 QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( componentData.componentName() ); 00210 if ( constr.isEmpty() ) 00211 constr = tmp; 00212 else 00213 constr = constr + " and " + tmp; 00214 } */ 00215 00216 // Query the trader 00217 //kDebug() << constr; 00218 const KService::List offers = KServiceTypeTrader::self()->query( "KDataTool", constr ); 00219 00220 KService::List::ConstIterator it = offers.begin(); 00221 for( ; it != offers.end(); ++it ) 00222 { 00223 // Temporary replacement for the non-working trader query above 00224 if (!componentData.isValid() || !(*it)->property("ExcludeFrom").toStringList() 00225 .contains(componentData.componentName())) { 00226 lst.append(KDataToolInfo(*it, componentData)); 00227 } else { 00228 kDebug() << (*it)->entryPath() << " excluded."; 00229 } 00230 } 00231 00232 return lst; 00233 } 00234 00235 bool KDataToolInfo::isValid() const 00236 { 00237 return( !d->service.isNull() ); 00238 } 00239 00240 /************************************************* 00241 * 00242 * KDataToolAction 00243 * 00244 *************************************************/ 00245 class KDataToolAction::KDataToolActionPrivate 00246 { 00247 public: 00248 KDataToolActionPrivate() {} 00249 00250 QString command; 00251 KDataToolInfo info; 00252 }; 00253 00254 KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command, 00255 QObject *parent ) 00256 : KAction( text, parent ), 00257 d(new KDataToolActionPrivate) 00258 { 00259 setIcon( KIcon( info.iconName() ) ); 00260 d->command = command; 00261 d->info = info; 00262 } 00263 00264 KDataToolAction::~KDataToolAction() 00265 { 00266 delete d; 00267 } 00268 00269 void KDataToolAction::slotActivated() 00270 { 00271 emit toolActivated( d->info, d->command ); 00272 } 00273 00274 QList<QAction*> KDataToolAction::dataToolActionList( const QList<KDataToolInfo> & tools, const QObject *receiver, const char* slot, KActionCollection* parent ) 00275 { 00276 QList<QAction*> actionList; 00277 if ( tools.isEmpty() ) 00278 return actionList; 00279 00280 QAction *sep_action = new QAction(parent); 00281 sep_action->setSeparator(true); 00282 actionList.append( sep_action ); 00283 QList<KDataToolInfo>::ConstIterator entry = tools.begin(); 00284 for( ; entry != tools.end(); ++entry ) 00285 { 00286 const QStringList userCommands = (*entry).userCommands(); 00287 const QStringList commands = (*entry).commands(); 00288 Q_ASSERT(!commands.isEmpty()); 00289 if ( commands.count() != userCommands.count() ) 00290 kWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath() 00291 << ")." << commands.count() << "commands and" 00292 << userCommands.count() << " descriptions."; 00293 QStringList::ConstIterator uit = userCommands.begin(); 00294 QStringList::ConstIterator cit = commands.begin(); 00295 for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit ) 00296 { 00297 //kDebug() << "creating action " << *uit << " " << *cit; 00298 const QString name = (*entry).service()->entryPath(); // something unique 00299 KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit, parent ); 00300 parent->addAction( name, action ); 00301 connect( action, SIGNAL( toolActivated( const KDataToolInfo &, const QString & ) ), 00302 receiver, slot ); 00303 actionList.append( action ); 00304 } 00305 } 00306 00307 return actionList; 00308 } 00309 00310 /************************************************* 00311 * 00312 * KDataTool 00313 * 00314 *************************************************/ 00315 class KDataTool::KDataToolPrivate 00316 { 00317 public: 00318 KDataToolPrivate() {} 00319 00320 KComponentData componentData; 00321 }; 00322 00323 KDataTool::KDataTool( QObject* parent ) 00324 : QObject(parent), d(new KDataToolPrivate) 00325 { 00326 } 00327 00328 KDataTool::~KDataTool() 00329 { 00330 delete d; 00331 } 00332 00333 void KDataTool::setComponentData(const KComponentData &componentData) 00334 { 00335 d->componentData = componentData; 00336 } 00337 00338 const KComponentData &KDataTool::componentData() const 00339 { 00340 return d->componentData; 00341 } 00342 00343 #include "kdatatool.moc"
KDE 4.7 API Reference