KParts
plugin.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 <kparts/plugin.h>
00022 #include <kparts/part.h>
00023 #include <kparts/componentfactory.h>
00024
00025 #include <assert.h>
00026
00027 #include <QtCore/QFile>
00028 #include <QtCore/QObject>
00029 #include <QtCore/QFileInfo>
00030
00031 #include <klibloader.h>
00032 #include <kcomponentdata.h>
00033 #include <kstandarddirs.h>
00034 #include <kdebug.h>
00035 #include <kxmlguifactory.h>
00036 #include <klocale.h>
00037 #include <kdesktopfile.h>
00038 #include <kconfiggroup.h>
00039
00040 using namespace KParts;
00041
00042 class Plugin::PluginPrivate
00043 {
00044 public:
00045 KComponentData m_parentInstance;
00046 QString m_library;
00047 };
00048
00049 Plugin::Plugin( QObject* parent )
00050 : QObject( parent ),d(new PluginPrivate())
00051 {
00052
00053 }
00054
00055 Plugin::~Plugin()
00056 {
00057 delete d;
00058 }
00059
00060 QString Plugin::xmlFile() const
00061 {
00062 QString path = KXMLGUIClient::xmlFile();
00063
00064 if ( !d->m_parentInstance.isValid() || ( path.length() > 0 && path[ 0 ] == '/' ) )
00065 return path;
00066
00067 QString absPath = KStandardDirs::locate( "data", d->m_parentInstance.componentName() + '/' + path );
00068 assert( !absPath.isEmpty() );
00069 return absPath;
00070 }
00071
00072 QString Plugin::localXMLFile() const
00073 {
00074 QString path = KXMLGUIClient::xmlFile();
00075
00076 if ( !d->m_parentInstance.isValid() || ( path.length() > 0 && path[ 0 ] == '/' ) )
00077 return path;
00078
00079 QString absPath = KStandardDirs::locateLocal( "data", d->m_parentInstance.componentName() + '/' + path );
00080 assert( !absPath.isEmpty() );
00081 return absPath;
00082 }
00083
00084
00085 QList<Plugin::PluginInfo> Plugin::pluginInfos(const KComponentData &componentData)
00086 {
00087 if (!componentData.isValid())
00088 kError(1000) << "No componentData ???" << endl;
00089
00090 QList<PluginInfo> plugins;
00091
00092
00093 const QStringList pluginDocs = componentData.dirs()->findAllResources(
00094 "data", componentData.componentName()+"/kpartplugins/*", KStandardDirs::Recursive );
00095
00096 QMap<QString,QStringList> sortedPlugins;
00097
00098 QStringList::ConstIterator pIt = pluginDocs.begin();
00099 QStringList::ConstIterator pEnd = pluginDocs.end();
00100 for (; pIt != pEnd; ++pIt )
00101 {
00102 QFileInfo fInfo( *pIt );
00103 if ( fInfo.completeSuffix() == QLatin1String( "desktop" ) )
00104 continue;
00105
00106 QMap<QString,QStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() );
00107 if ( mapIt == sortedPlugins.end() )
00108 mapIt = sortedPlugins.insert( fInfo.fileName(), QStringList() );
00109
00110 mapIt.value().append( *pIt );
00111 }
00112
00113 QMap<QString,QStringList>::ConstIterator mapIt = sortedPlugins.constBegin();
00114 QMap<QString,QStringList>::ConstIterator mapEnd = sortedPlugins.constEnd();
00115 for (; mapIt != mapEnd; ++mapIt )
00116 {
00117 PluginInfo info;
00118 QString doc;
00119 info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.value(), doc );
00120 if ( info.m_absXMLFileName.isEmpty() )
00121 continue;
00122
00123 kDebug( 1000 ) << "found KParts Plugin : " << info.m_absXMLFileName;
00124 info.m_relXMLFileName = "kpartplugins/";
00125 info.m_relXMLFileName += mapIt.key();
00126
00127 info.m_document.setContent( doc );
00128 if ( info.m_document.documentElement().isNull() )
00129 continue;
00130
00131 plugins.append( info );
00132 }
00133
00134 return plugins;
00135 }
00136
00137 void Plugin::loadPlugins(QObject *parent, const KComponentData &componentData)
00138 {
00139 loadPlugins( parent, pluginInfos( componentData ), componentData );
00140 }
00141
00142 void Plugin::loadPlugins(QObject *parent, const QList<PluginInfo> &pluginInfos, const KComponentData &componentData)
00143 {
00144 QList<PluginInfo>::ConstIterator pIt = pluginInfos.begin();
00145 QList<PluginInfo>::ConstIterator pEnd = pluginInfos.end();
00146 for (; pIt != pEnd; ++pIt )
00147 {
00148 QString library = (*pIt).m_document.documentElement().attribute( "library" );
00149
00150 if ( library.isEmpty() || hasPlugin( parent, library ) )
00151 continue;
00152
00153 Plugin *plugin = loadPlugin( parent, library );
00154
00155 if ( plugin )
00156 {
00157 plugin->d->m_parentInstance = componentData;
00158 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
00159 plugin->setDOMDocument( (*pIt).m_document );
00160
00161 }
00162 }
00163
00164 }
00165
00166 void Plugin::loadPlugins( QObject *parent, const QList<PluginInfo> &pluginInfos )
00167 {
00168 loadPlugins(parent, pluginInfos, KComponentData());
00169 }
00170
00171
00172 Plugin* Plugin::loadPlugin( QObject * parent, const char* libname )
00173 {
00174 Plugin* plugin = KLibLoader::createInstance<Plugin>( libname, parent );
00175 if ( !plugin )
00176 return 0;
00177 plugin->d->m_library = libname;
00178 return plugin;
00179 }
00180
00181
00182 Plugin* Plugin::loadPlugin( QObject * parent, const QByteArray &libname )
00183 {
00184 return loadPlugin( parent, libname.data() );
00185 }
00186
00187
00188 Plugin* Plugin::loadPlugin( QObject * parent, const QString &libname )
00189 {
00190 Plugin* plugin = KLibLoader::createInstance<Plugin>( libname, parent );
00191 if ( !plugin )
00192 return 0;
00193 plugin->d->m_library = libname;
00194 return plugin;
00195 }
00196
00197 QList<KParts::Plugin *> Plugin::pluginObjects( QObject *parent )
00198 {
00199 QList<KParts::Plugin *> objects;
00200
00201 if (!parent )
00202 return objects;
00203
00204 const QObjectList plugins = parent->children();
00205
00206 QObjectList::ConstIterator it = plugins.begin();
00207 for ( ; it != plugins.end() ; ++it )
00208 {
00209 Plugin * plugin = qobject_cast<Plugin *>( *it );
00210 if ( plugin )
00211 objects.append( plugin );
00212 }
00213
00214 return objects;
00215 }
00216
00217 bool Plugin::hasPlugin( QObject* parent, const QString& library )
00218 {
00219 const QObjectList plugins = parent->children();
00220
00221 QObjectList::ConstIterator it = plugins.begin();
00222 for ( ; it != plugins.end() ; ++it )
00223 {
00224 Plugin * plugin = qobject_cast<Plugin *>( *it );
00225 if ( plugin && plugin->d->m_library == library )
00226 {
00227 return true;
00228 }
00229 }
00230 return false;
00231 }
00232
00233 void Plugin::setComponentData(const KComponentData &componentData)
00234 {
00235 KGlobal::locale()->insertCatalog(componentData.catalogName());
00236 KXMLGUIClient::setComponentData(componentData);
00237 }
00238
00239 void Plugin::loadPlugins(QObject *parent, KXMLGUIClient* parentGUIClient,
00240 const KComponentData &componentData, bool enableNewPluginsByDefault,
00241 int interfaceVersionRequired)
00242 {
00243 KConfigGroup cfgGroup( componentData.config(), "KParts Plugins" );
00244 const QList<PluginInfo> plugins = pluginInfos( componentData );
00245 QList<PluginInfo>::ConstIterator pIt = plugins.begin();
00246 const QList<PluginInfo>::ConstIterator pEnd = plugins.end();
00247 for (; pIt != pEnd; ++pIt )
00248 {
00249 QDomElement docElem = (*pIt).m_document.documentElement();
00250 QString library = docElem.attribute( "library" );
00251
00252 if ( library.isEmpty() )
00253 continue;
00254
00255
00256 const QString name = docElem.attribute( "name" );
00257
00258 bool pluginEnabled = enableNewPluginsByDefault;
00259 if ( cfgGroup.hasKey( name + "Enabled" ) )
00260 {
00261 pluginEnabled = cfgGroup.readEntry( name + "Enabled" , false );
00262 }
00263 else
00264 {
00265 QString relPath = QString( componentData.componentName() ) + '/' + (*pIt).m_relXMLFileName;
00266 relPath.truncate( relPath.lastIndexOf( '.' ) );
00267 relPath += ".desktop";
00268
00269 const QString desktopfile = componentData.dirs()->findResource( "data", relPath );
00270 if( !desktopfile.isEmpty() )
00271 {
00272
00273 KDesktopFile _desktop( desktopfile );
00274 const KConfigGroup desktop = _desktop.desktopGroup();
00275 pluginEnabled = desktop.readEntry( "X-KDE-PluginInfo-EnabledByDefault",
00276 enableNewPluginsByDefault );
00277 if ( interfaceVersionRequired != 0 )
00278 {
00279 const int version = desktop.readEntry( "X-KDE-InterfaceVersion", 1 );
00280 if ( version != interfaceVersionRequired )
00281 {
00282 kDebug(1000) << "Discarding plugin " << name << ", interface version " << version << ", expected " << interfaceVersionRequired;
00283 pluginEnabled = false;
00284 }
00285 }
00286 }
00287 else
00288 {
00289
00290 }
00291 }
00292
00293
00294 const QObjectList pluginList = parent->children();
00295
00296 bool pluginFound = false;
00297 for ( QObjectList::ConstIterator it = pluginList.begin(); it != pluginList.end() ; ++it )
00298 {
00299 Plugin * plugin = qobject_cast<Plugin *>( *it );
00300 if( plugin && plugin->d->m_library == library )
00301 {
00302
00303 if( !pluginEnabled )
00304 {
00305 kDebug( 1000 ) << "remove plugin " << name;
00306 KXMLGUIFactory * factory = plugin->factory();
00307 if( factory )
00308 factory->removeClient( plugin );
00309 delete plugin;
00310 }
00311
00312 pluginFound = true;
00313 break;
00314 }
00315 }
00316
00317
00318
00319 if( pluginFound || !pluginEnabled )
00320 continue;
00321
00322 kDebug( 1000 ) << "load plugin " << name;
00323 Plugin *plugin = loadPlugin( parent, library );
00324
00325 if ( plugin )
00326 {
00327 plugin->d->m_parentInstance = componentData;
00328 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
00329 plugin->setDOMDocument( (*pIt).m_document );
00330 parentGUIClient->insertChildClient( plugin );
00331 }
00332 }
00333 }
00334
00335
00336
00337 #include "plugin.moc"