KDECore
kplugininfo.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2003,2007 Matthias Kretz <kretz@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 00018 */ 00019 00020 #include "kplugininfo.h" 00021 #include <kservicetypetrader.h> 00022 #include <kdebug.h> 00023 #include <kglobal.h> 00024 #include <kstandarddirs.h> 00025 #include <kdesktopfile.h> 00026 #include <kservice.h> 00027 #include <QList> 00028 #include <kconfiggroup.h> 00029 00030 //#ifndef NDEBUG 00031 #define KPLUGININFO_ISVALID_ASSERTION \ 00032 do { \ 00033 if (!d) { \ 00034 kFatal(703) << "Accessed invalid KPluginInfo object"; \ 00035 } \ 00036 } while (false) 00037 //#else 00038 //#define KPLUGININFO_ISVALID_ASSERTION 00039 //#endif 00040 00041 class KPluginInfoPrivate : public QSharedData 00042 { 00043 public: 00044 KPluginInfoPrivate() 00045 : hidden( false ) 00046 , enabledbydefault( false ) 00047 , pluginenabled( false ) 00048 , kcmservicesCached( false ) 00049 {} 00050 00051 QString entryPath; // the filename of the file containing all the info 00052 QString name; 00053 QString comment; 00054 QString icon; 00055 QString author; 00056 QString email; 00057 QString pluginName; // the name attribute in the .rc file 00058 QString version; 00059 QString website; // URL to the website of the plugin/author 00060 QString category; 00061 QString license; 00062 QStringList dependencies; 00063 00064 bool hidden : 1; 00065 bool enabledbydefault : 1; 00066 bool pluginenabled : 1; 00067 mutable bool kcmservicesCached : 1; 00068 00069 KConfigGroup config; 00070 KService::Ptr service; 00071 mutable QList<KService::Ptr> kcmservices; 00072 00073 static int debugArea() { 00074 static int s_area = KDebug::registerArea("kdecore (KPluginInfo)"); 00075 return s_area; 00076 } 00077 }; 00078 00079 KPluginInfo::KPluginInfo( const QString & filename, const char* resource ) 00080 : d( new KPluginInfoPrivate ) 00081 { 00082 KDesktopFile file( resource, filename ); 00083 00084 d->entryPath = filename; 00085 00086 KConfigGroup cg = file.desktopGroup(); 00087 d->hidden = cg.readEntry("Hidden", false); 00088 if( d->hidden ) 00089 return; 00090 00091 d->name = file.readName(); 00092 d->comment = file.readComment(); 00093 d->icon = cg.readEntryUntranslated( "Icon" ); 00094 d->author = cg.readEntryUntranslated( "X-KDE-PluginInfo-Author" ); 00095 d->email = cg.readEntryUntranslated( "X-KDE-PluginInfo-Email" ); 00096 d->pluginName = cg.readEntryUntranslated( "X-KDE-PluginInfo-Name" ); 00097 d->version = cg.readEntryUntranslated( "X-KDE-PluginInfo-Version" ); 00098 d->website = cg.readEntryUntranslated( "X-KDE-PluginInfo-Website" ); 00099 d->category = cg.readEntryUntranslated( "X-KDE-PluginInfo-Category" ); 00100 d->license = cg.readEntryUntranslated( "X-KDE-PluginInfo-License" ); 00101 d->dependencies = cg.readEntry( "X-KDE-PluginInfo-Depends", QStringList() ); 00102 d->enabledbydefault = cg.readEntry( 00103 "X-KDE-PluginInfo-EnabledByDefault", false); 00104 } 00105 00106 KPluginInfo::KPluginInfo( const KService::Ptr service ) 00107 : d( new KPluginInfoPrivate ) 00108 { 00109 if (!service) { 00110 d = 0; // isValid() == false 00111 return; 00112 } 00113 d->service = service; 00114 d->entryPath = service->entryPath(); 00115 00116 if ( service->isDeleted() ) 00117 { 00118 d->hidden = true; 00119 return; 00120 } 00121 00122 d->name = service->name(); 00123 d->comment = service->comment(); 00124 d->icon = service->icon(); 00125 d->author = service->property( QLatin1String("X-KDE-PluginInfo-Author") ).toString(); 00126 d->email = service->property( QLatin1String("X-KDE-PluginInfo-Email") ).toString(); 00127 d->pluginName = service->property( QLatin1String("X-KDE-PluginInfo-Name") ).toString(); 00128 d->version = service->property( QLatin1String("X-KDE-PluginInfo-Version") ).toString(); 00129 d->website = service->property( QLatin1String("X-KDE-PluginInfo-Website") ).toString(); 00130 d->category = service->property( QLatin1String("X-KDE-PluginInfo-Category") ).toString(); 00131 d->license = service->property( QLatin1String("X-KDE-PluginInfo-License") ).toString(); 00132 d->dependencies = 00133 service->property( QLatin1String("X-KDE-PluginInfo-Depends") ).toStringList(); 00134 QVariant tmp = service->property( QLatin1String("X-KDE-PluginInfo-EnabledByDefault") ); 00135 d->enabledbydefault = tmp.isValid() ? tmp.toBool() : false; 00136 } 00137 00138 KPluginInfo::KPluginInfo() 00139 : d(0) // isValid() == false 00140 { 00141 } 00142 00143 bool KPluginInfo::isValid() const 00144 { 00145 return d.data() != 0; 00146 } 00147 00148 KPluginInfo::KPluginInfo(const KPluginInfo &rhs) 00149 : d(rhs.d) 00150 { 00151 } 00152 00153 KPluginInfo &KPluginInfo::operator=(const KPluginInfo &rhs) 00154 { 00155 d = rhs.d; 00156 return *this; 00157 } 00158 00159 bool KPluginInfo::operator==(const KPluginInfo &rhs) const 00160 { 00161 return d == rhs.d; 00162 } 00163 00164 bool KPluginInfo::operator!=(const KPluginInfo &rhs) const 00165 { 00166 return d != rhs.d; 00167 } 00168 00169 bool KPluginInfo::operator<(const KPluginInfo &rhs) const 00170 { 00171 if (category() < rhs.category()) { 00172 return true; 00173 } 00174 if (category() == rhs.category()) { 00175 return name() < rhs.name(); 00176 } 00177 return false; 00178 } 00179 00180 bool KPluginInfo::operator>(const KPluginInfo &rhs) const 00181 { 00182 if (category() > rhs.category()) { 00183 return true; 00184 } 00185 if (category() == rhs.category()) { 00186 return name() > rhs.name(); 00187 } 00188 return false; 00189 } 00190 00191 KPluginInfo::~KPluginInfo() 00192 { 00193 } 00194 00195 QList<KPluginInfo> KPluginInfo::fromServices(const KService::List &services, const KConfigGroup &config) 00196 { 00197 QList<KPluginInfo> infolist; 00198 for( KService::List::ConstIterator it = services.begin(); 00199 it != services.end(); ++it ) 00200 { 00201 KPluginInfo info(*it); 00202 info.setConfig(config); 00203 infolist += info; 00204 } 00205 return infolist; 00206 } 00207 00208 QList<KPluginInfo> KPluginInfo::fromFiles(const QStringList &files, const KConfigGroup &config) 00209 { 00210 QList<KPluginInfo> infolist; 00211 for( QStringList::ConstIterator it = files.begin(); it != files.end(); ++it ) 00212 { 00213 KPluginInfo info(*it); 00214 info.setConfig(config); 00215 infolist += info; 00216 } 00217 return infolist; 00218 } 00219 00220 QList<KPluginInfo> KPluginInfo::fromKPartsInstanceName(const QString &name, const KConfigGroup &config) 00221 { 00222 const QStringList files = KGlobal::dirs()->findAllResources( 00223 "data", name + QString::fromLatin1("/kpartplugins/*.desktop"), 00224 KStandardDirs::Recursive ); 00225 return fromFiles(files, config); 00226 } 00227 00228 bool KPluginInfo::isHidden() const 00229 { 00230 KPLUGININFO_ISVALID_ASSERTION; 00231 return d->hidden; 00232 } 00233 00234 void KPluginInfo::setPluginEnabled( bool enabled ) 00235 { 00236 KPLUGININFO_ISVALID_ASSERTION; 00237 //kDebug( d->debugArea() ) ; 00238 d->pluginenabled = enabled; 00239 } 00240 00241 bool KPluginInfo::isPluginEnabled() const 00242 { 00243 KPLUGININFO_ISVALID_ASSERTION; 00244 //kDebug( d->debugArea() ) ; 00245 return d->pluginenabled; 00246 } 00247 00248 bool KPluginInfo::isPluginEnabledByDefault() const 00249 { 00250 KPLUGININFO_ISVALID_ASSERTION; 00251 //kDebug( d->debugArea() ) ; 00252 return d->enabledbydefault; 00253 } 00254 00255 QString KPluginInfo::name() const 00256 { 00257 KPLUGININFO_ISVALID_ASSERTION; 00258 return d->name; 00259 } 00260 00261 QString KPluginInfo::comment() const 00262 { 00263 KPLUGININFO_ISVALID_ASSERTION; 00264 return d->comment; 00265 } 00266 00267 QString KPluginInfo::icon() const 00268 { 00269 KPLUGININFO_ISVALID_ASSERTION; 00270 return d->icon; 00271 } 00272 00273 QString KPluginInfo::entryPath() const 00274 { 00275 KPLUGININFO_ISVALID_ASSERTION; 00276 return d->entryPath; 00277 } 00278 00279 QString KPluginInfo::author() const 00280 { 00281 KPLUGININFO_ISVALID_ASSERTION; 00282 return d->author; 00283 } 00284 00285 QString KPluginInfo::email() const 00286 { 00287 KPLUGININFO_ISVALID_ASSERTION; 00288 return d->email; 00289 } 00290 00291 QString KPluginInfo::category() const 00292 { 00293 KPLUGININFO_ISVALID_ASSERTION; 00294 return d->category; 00295 } 00296 00297 QString KPluginInfo::pluginName() const 00298 { 00299 KPLUGININFO_ISVALID_ASSERTION; 00300 return d->pluginName; 00301 } 00302 00303 QString KPluginInfo::version() const 00304 { 00305 KPLUGININFO_ISVALID_ASSERTION; 00306 return d->version; 00307 } 00308 00309 QString KPluginInfo::website() const 00310 { 00311 KPLUGININFO_ISVALID_ASSERTION; 00312 return d->website; 00313 } 00314 00315 QString KPluginInfo::license() const 00316 { 00317 KPLUGININFO_ISVALID_ASSERTION; 00318 return d->license; 00319 } 00320 00321 KAboutLicense KPluginInfo::fullLicense() const 00322 { 00323 KPLUGININFO_ISVALID_ASSERTION; 00324 return KAboutLicense::byKeyword(d->license); 00325 } 00326 00327 QStringList KPluginInfo::dependencies() const 00328 { 00329 KPLUGININFO_ISVALID_ASSERTION; 00330 return d->dependencies; 00331 } 00332 00333 KService::Ptr KPluginInfo::service() const 00334 { 00335 KPLUGININFO_ISVALID_ASSERTION; 00336 return d->service; 00337 } 00338 00339 QList<KService::Ptr> KPluginInfo::kcmServices() const 00340 { 00341 KPLUGININFO_ISVALID_ASSERTION; 00342 if ( !d->kcmservicesCached ) 00343 { 00344 d->kcmservices = KServiceTypeTrader::self()->query( QLatin1String("KCModule"), QLatin1Char('\'') + d->pluginName + 00345 QString::fromLatin1("' in [X-KDE-ParentComponents]") ); 00346 kDebug(d->debugArea()) << "found" << d->kcmservices.count() << "offers for" << d->pluginName; 00347 00348 d->kcmservicesCached = true; 00349 } 00350 00351 return d->kcmservices; 00352 } 00353 00354 void KPluginInfo::setConfig(const KConfigGroup &config) 00355 { 00356 KPLUGININFO_ISVALID_ASSERTION; 00357 d->config = config; 00358 } 00359 00360 KConfigGroup KPluginInfo::config() const 00361 { 00362 KPLUGININFO_ISVALID_ASSERTION; 00363 return d->config; 00364 } 00365 00366 QVariant KPluginInfo::property( const QString & key ) const 00367 { 00368 KPLUGININFO_ISVALID_ASSERTION; 00369 if( d->service ) 00370 return d->service->property( key ); 00371 else 00372 return QVariant(); 00373 } 00374 00375 void KPluginInfo::save(KConfigGroup config) 00376 { 00377 KPLUGININFO_ISVALID_ASSERTION; 00378 //kDebug( d->debugArea() ) ; 00379 if (config.isValid()) { 00380 config.writeEntry(d->pluginName + QString::fromLatin1("Enabled"), isPluginEnabled()); 00381 } else { 00382 if (!d->config.isValid()) { 00383 kWarning( d->debugArea() ) << "no KConfigGroup, cannot save"; 00384 return; 00385 } 00386 d->config.writeEntry(d->pluginName + QString::fromLatin1("Enabled"), isPluginEnabled()); 00387 } 00388 } 00389 00390 void KPluginInfo::load(const KConfigGroup &config) 00391 { 00392 KPLUGININFO_ISVALID_ASSERTION; 00393 //kDebug( d->debugArea() ) ; 00394 if (config.isValid()) { 00395 setPluginEnabled(config.readEntry(d->pluginName + QString::fromLatin1("Enabled"), isPluginEnabledByDefault())); 00396 } else { 00397 if (!d->config.isValid()) { 00398 kWarning( d->debugArea() ) << "no KConfigGroup, cannot load"; 00399 return; 00400 } 00401 setPluginEnabled(d->config.readEntry(d->pluginName + QString::fromLatin1("Enabled"), isPluginEnabledByDefault())); 00402 } 00403 } 00404 00405 void KPluginInfo::defaults() 00406 { 00407 //kDebug( d->debugArea() ) ; 00408 setPluginEnabled( isPluginEnabledByDefault() ); 00409 } 00410 00411 uint qHash(const KPluginInfo &p) 00412 { 00413 return qHash(reinterpret_cast<quint64>(p.d.data())); 00414 } 00415 00416 #undef KPLUGININFO_ISVALID_ASSERTION 00417 00418 // vim: sw=4 sts=4 et
KDE 4.6 API Reference