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 #ifndef KDE_NO_DEPRECATED 00115 QPixmap KDataToolInfo::icon() const 00116 { 00117 if ( !d->service ) 00118 return QPixmap(); 00119 00120 QPixmap pix; 00121 const QStringList lst = KGlobal::dirs()->resourceDirs("icon"); 00122 QStringList::ConstIterator it = lst.begin(); 00123 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() ) 00124 it++; 00125 00126 return pix; 00127 } 00128 #endif 00129 00130 #ifndef KDE_NO_DEPRECATED 00131 QPixmap KDataToolInfo::miniIcon() const 00132 { 00133 if ( !d->service ) 00134 return QPixmap(); 00135 00136 QPixmap pix; 00137 const QStringList lst = KGlobal::dirs()->resourceDirs("mini"); 00138 QStringList::ConstIterator it = lst.begin(); 00139 while (!pix.load( *it + '/' + d->service->icon() ) && it != lst.end() ) 00140 it++; 00141 00142 return pix; 00143 } 00144 #endif 00145 00146 QString KDataToolInfo::iconName() const 00147 { 00148 if ( !d->service ) 00149 return QString(); 00150 return d->service->icon(); 00151 } 00152 00153 QStringList KDataToolInfo::commands() const 00154 { 00155 if ( !d->service ) 00156 return QStringList(); 00157 00158 return d->service->property( "Commands" ).toStringList(); 00159 } 00160 00161 QStringList KDataToolInfo::userCommands() const 00162 { 00163 if ( !d->service ) 00164 return QStringList(); 00165 00166 return d->service->comment().split( ',', QString::SkipEmptyParts ); 00167 } 00168 00169 KDataTool* KDataToolInfo::createTool( QObject* parent ) const 00170 { 00171 if ( !d->service ) 00172 return 0; 00173 00174 KDataTool* tool = d->service->createInstance<KDataTool>(parent); 00175 if ( tool ) 00176 tool->setComponentData(d->componentData); 00177 return tool; 00178 } 00179 00180 KService::Ptr KDataToolInfo::service() const 00181 { 00182 return d->service; 00183 } 00184 00185 KComponentData KDataToolInfo::componentData() const 00186 { 00187 return d->componentData; 00188 } 00189 00190 QList<KDataToolInfo> KDataToolInfo::query(const QString& datatype, const QString& mimetype, const KComponentData &componentData) 00191 { 00192 QList<KDataToolInfo> lst; 00193 00194 QString constr; 00195 00196 if ( !datatype.isEmpty() ) 00197 { 00198 constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype ); 00199 } 00200 if ( !mimetype.isEmpty() ) 00201 { 00202 QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype ); 00203 if ( constr.isEmpty() ) 00204 constr = tmp; 00205 else 00206 constr = constr + " and " + tmp; 00207 } 00208 /* Bug in KServiceTypeTrader ? Test with HEAD-kdelibs! 00209 if ( componentData ) 00210 { 00211 QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( componentData.componentName() ); 00212 if ( constr.isEmpty() ) 00213 constr = tmp; 00214 else 00215 constr = constr + " and " + tmp; 00216 } */ 00217 00218 // Query the trader 00219 //kDebug() << constr; 00220 const KService::List offers = KServiceTypeTrader::self()->query( "KDataTool", constr ); 00221 00222 KService::List::ConstIterator it = offers.begin(); 00223 for( ; it != offers.end(); ++it ) 00224 { 00225 // Temporary replacement for the non-working trader query above 00226 if (!componentData.isValid() || !(*it)->property("ExcludeFrom").toStringList() 00227 .contains(componentData.componentName())) { 00228 lst.append(KDataToolInfo(*it, componentData)); 00229 } else { 00230 kDebug() << (*it)->entryPath() << " excluded."; 00231 } 00232 } 00233 00234 return lst; 00235 } 00236 00237 bool KDataToolInfo::isValid() const 00238 { 00239 return( !d->service.isNull() ); 00240 } 00241 00242 /************************************************* 00243 * 00244 * KDataToolAction 00245 * 00246 *************************************************/ 00247 class KDataToolAction::KDataToolActionPrivate 00248 { 00249 public: 00250 KDataToolActionPrivate() {} 00251 00252 QString command; 00253 KDataToolInfo info; 00254 }; 00255 00256 KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command, 00257 QObject *parent ) 00258 : KAction( text, parent ), 00259 d(new KDataToolActionPrivate) 00260 { 00261 setIcon( KIcon( info.iconName() ) ); 00262 d->command = command; 00263 d->info = info; 00264 } 00265 00266 KDataToolAction::~KDataToolAction() 00267 { 00268 delete d; 00269 } 00270 00271 void KDataToolAction::slotActivated() 00272 { 00273 emit toolActivated( d->info, d->command ); 00274 } 00275 00276 QList<QAction*> KDataToolAction::dataToolActionList( const QList<KDataToolInfo> & tools, const QObject *receiver, const char* slot, KActionCollection* parent ) 00277 { 00278 QList<QAction*> actionList; 00279 if ( tools.isEmpty() ) 00280 return actionList; 00281 00282 QAction *sep_action = new QAction(parent); 00283 sep_action->setSeparator(true); 00284 actionList.append( sep_action ); 00285 QList<KDataToolInfo>::ConstIterator entry = tools.begin(); 00286 for( ; entry != tools.end(); ++entry ) 00287 { 00288 const QStringList userCommands = (*entry).userCommands(); 00289 const QStringList commands = (*entry).commands(); 00290 Q_ASSERT(!commands.isEmpty()); 00291 if ( commands.count() != userCommands.count() ) 00292 kWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath() 00293 << ")." << commands.count() << "commands and" 00294 << userCommands.count() << " descriptions."; 00295 QStringList::ConstIterator uit = userCommands.begin(); 00296 QStringList::ConstIterator cit = commands.begin(); 00297 for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit ) 00298 { 00299 //kDebug() << "creating action " << *uit << " " << *cit; 00300 const QString name = (*entry).service()->entryPath(); // something unique 00301 KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit, parent ); 00302 parent->addAction( name, action ); 00303 connect( action, SIGNAL( toolActivated( const KDataToolInfo &, const QString & ) ), 00304 receiver, slot ); 00305 actionList.append( action ); 00306 } 00307 } 00308 00309 return actionList; 00310 } 00311 00312 /************************************************* 00313 * 00314 * KDataTool 00315 * 00316 *************************************************/ 00317 class KDataTool::KDataToolPrivate 00318 { 00319 public: 00320 KDataToolPrivate() {} 00321 00322 KComponentData componentData; 00323 }; 00324 00325 KDataTool::KDataTool( QObject* parent ) 00326 : QObject(parent), d(new KDataToolPrivate) 00327 { 00328 } 00329 00330 KDataTool::~KDataTool() 00331 { 00332 delete d; 00333 } 00334 00335 void KDataTool::setComponentData(const KComponentData &componentData) 00336 { 00337 d->componentData = componentData; 00338 } 00339 00340 const KComponentData &KDataTool::componentData() const 00341 { 00342 return d->componentData; 00343 } 00344 00345 #include "kdatatool.moc"
KDE 4.6 API Reference