00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022 #include <kparts/plugin.h>
00023 #include <kparts/part.h>
00024 #include <kparts/componentfactory.h>
00025
00026 #include <assert.h>
00027
00028 #include <qfile.h>
00029 #include <qobjectlist.h>
00030 #include <qfileinfo.h>
00031
00032 #include <klibloader.h>
00033 #include <kinstance.h>
00034 #include <kstandarddirs.h>
00035 #include <kdebug.h>
00036 #include <kxmlguifactory.h>
00037 #include <klocale.h>
00038 #include <kconfig.h>
00039 #include <ksimpleconfig.h>
00040
00041 using namespace KParts;
00042
00043 class Plugin::PluginPrivate
00044 {
00045 public:
00046 PluginPrivate() : m_parentInstance( 0 ) {}
00047
00048 const KInstance *m_parentInstance;
00049 QString m_library;
00050 };
00051
00052 Plugin::Plugin( QObject* parent, const char* name )
00053 : QObject( parent, name )
00054 {
00055
00056 d = new PluginPrivate();
00057 }
00058
00059 Plugin::~Plugin()
00060 {
00061 delete d;
00062 }
00063
00064 QString Plugin::xmlFile() const
00065 {
00066 QString path = KXMLGUIClient::xmlFile();
00067
00068 if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) )
00069 return path;
00070
00071 QString absPath = locate( "data", QString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path );
00072 assert( !absPath.isEmpty() );
00073 return absPath;
00074 }
00075
00076 QString Plugin::localXMLFile() const
00077 {
00078 QString path = KXMLGUIClient::xmlFile();
00079
00080 if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) )
00081 return path;
00082
00083 QString absPath = locateLocal( "data", QString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path );
00084 assert( !absPath.isEmpty() );
00085 return absPath;
00086 }
00087
00088
00089 QValueList<Plugin::PluginInfo> Plugin::pluginInfos( const KInstance * instance )
00090 {
00091 if ( !instance )
00092 kdError(1000) << "No instance ???" << endl;
00093
00094 QValueList<PluginInfo> plugins;
00095
00096
00097 const QStringList pluginDocs = instance->dirs()->findAllResources(
00098 "data", instance->instanceName()+"/kpartplugins/*", true, false );
00099
00100 QMap<QString,QStringList> sortedPlugins;
00101
00102 QStringList::ConstIterator pIt = pluginDocs.begin();
00103 QStringList::ConstIterator pEnd = pluginDocs.end();
00104 for (; pIt != pEnd; ++pIt )
00105 {
00106 QFileInfo fInfo( *pIt );
00107 if ( fInfo.extension() == QString::fromLatin1( "desktop" ) )
00108 continue;
00109
00110 QMap<QString,QStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() );
00111 if ( mapIt == sortedPlugins.end() )
00112 mapIt = sortedPlugins.insert( fInfo.fileName(), QStringList() );
00113
00114 mapIt.data().append( *pIt );
00115 }
00116
00117 QMap<QString,QStringList>::ConstIterator mapIt = sortedPlugins.begin();
00118 QMap<QString,QStringList>::ConstIterator mapEnd = sortedPlugins.end();
00119 for (; mapIt != mapEnd; ++mapIt )
00120 {
00121 PluginInfo info;
00122 QString doc;
00123 info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.data(), doc );
00124 if ( info.m_absXMLFileName.isEmpty() )
00125 continue;
00126
00127 kdDebug( 1000 ) << "found KParts Plugin : " << info.m_absXMLFileName << endl;
00128 info.m_relXMLFileName = "kpartplugins/";
00129 info.m_relXMLFileName += mapIt.key();
00130
00131 info.m_document.setContent( doc );
00132 if ( info.m_document.documentElement().isNull() )
00133 continue;
00134
00135 plugins.append( info );
00136 }
00137
00138 return plugins;
00139 }
00140
00141 void Plugin::loadPlugins( QObject *parent, const KInstance *instance )
00142 {
00143 loadPlugins( parent, pluginInfos( instance ), instance );
00144 }
00145
00146 void Plugin::loadPlugins( QObject *parent, const QValueList<PluginInfo> &pluginInfos, const KInstance *instance )
00147 {
00148 QValueList<PluginInfo>::ConstIterator pIt = pluginInfos.begin();
00149 QValueList<PluginInfo>::ConstIterator pEnd = pluginInfos.end();
00150 for (; pIt != pEnd; ++pIt )
00151 {
00152 QString library = (*pIt).m_document.documentElement().attribute( "library" );
00153
00154 if ( library.isEmpty() || hasPlugin( parent, library ) )
00155 continue;
00156
00157 Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) );
00158
00159 if ( plugin )
00160 {
00161 plugin->d->m_parentInstance = instance;
00162 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
00163 plugin->setDOMDocument( (*pIt).m_document );
00164
00165 }
00166 }
00167
00168 }
00169
00170 void Plugin::loadPlugins( QObject *parent, const QValueList<PluginInfo> &pluginInfos )
00171 {
00172 loadPlugins(parent, pluginInfos, 0);
00173 }
00174
00175
00176 Plugin* Plugin::loadPlugin( QObject * parent, const char* libname )
00177 {
00178 Plugin* plugin = ComponentFactory::createInstanceFromLibrary<Plugin>( libname, parent, libname );
00179 if ( !plugin )
00180 return 0L;
00181 plugin->d->m_library = libname;
00182 return plugin;
00183 }
00184
00185 QPtrList<KParts::Plugin> Plugin::pluginObjects( QObject *parent )
00186 {
00187 QPtrList<KParts::Plugin> objects;
00188
00189 if (!parent )
00190 return objects;
00191
00192 QObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false );
00193
00194 QObjectListIt it( *plugins );
00195 for ( ; it.current() ; ++it )
00196 {
00197 objects.append( static_cast<Plugin *>( it.current() ) );
00198 }
00199
00200 delete plugins;
00201
00202 return objects;
00203 }
00204
00205 bool Plugin::hasPlugin( QObject* parent, const QString& library )
00206 {
00207 QObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false );
00208 QObjectListIt it( *plugins );
00209 for ( ; it.current() ; ++it )
00210 {
00211 if ( static_cast<Plugin *>( it.current() )->d->m_library == library )
00212 {
00213 delete plugins;
00214 return true;
00215 }
00216 }
00217 delete plugins;
00218 return false;
00219 }
00220
00221 void Plugin::setInstance( KInstance *instance )
00222 {
00223 KGlobal::locale()->insertCatalogue( instance->instanceName() );
00224 KXMLGUIClient::setInstance( instance );
00225 }
00226
00227 void Plugin::loadPlugins( QObject *parent, KXMLGUIClient* parentGUIClient, KInstance* instance, bool enableNewPluginsByDefault )
00228 {
00229 KConfigGroup cfgGroup( instance->config(), "KParts Plugins" );
00230 QValueList<PluginInfo> plugins = pluginInfos( instance );
00231 QValueList<PluginInfo>::ConstIterator pIt = plugins.begin();
00232 QValueList<PluginInfo>::ConstIterator pEnd = plugins.end();
00233 for (; pIt != pEnd; ++pIt )
00234 {
00235 QDomElement docElem = (*pIt).m_document.documentElement();
00236 QString library = docElem.attribute( "library" );
00237
00238 if ( library.isEmpty() )
00239 continue;
00240
00241
00242 const QString name = docElem.attribute( "name" );
00243
00244 bool pluginEnabled = enableNewPluginsByDefault;
00245 if ( cfgGroup.hasKey( name + "Enabled" ) )
00246 {
00247 pluginEnabled = cfgGroup.readBoolEntry( name + "Enabled" );
00248 }
00249 else
00250 {
00251 QString relPath = QString( instance->instanceName() ) + "/" + (*pIt).m_relXMLFileName;
00252 relPath.truncate( relPath.findRev( '.' ) );
00253 relPath += ".desktop";
00254
00255 const QString desktopfile = instance->dirs()->findResource( "data", relPath );
00256 if( !desktopfile.isEmpty() )
00257 {
00258
00259 KSimpleConfig desktop( desktopfile, true );
00260 desktop.setDesktopGroup();
00261 pluginEnabled = desktop.readBoolEntry(
00262 "X-KDE-PluginInfo-EnabledByDefault", enableNewPluginsByDefault );
00263 }
00264 else
00265 {
00266
00267 }
00268 }
00269
00270
00271 QObjectList *pluginList = parent->queryList( "KParts::Plugin", 0, false, false );
00272 QObjectListIt it( *pluginList );
00273 bool pluginFound = false;
00274 for ( ; it.current() ; ++it )
00275 {
00276 Plugin * plugin = static_cast<Plugin *>( it.current() );
00277 if( plugin->d->m_library == library )
00278 {
00279
00280 if( !pluginEnabled )
00281 {
00282 kdDebug( 1000 ) << "remove plugin " << name << endl;
00283 KXMLGUIFactory * factory = plugin->factory();
00284 if( factory )
00285 factory->removeClient( plugin );
00286 delete plugin;
00287 }
00288
00289 pluginFound = true;
00290 break;
00291 }
00292 }
00293 delete pluginList;
00294
00295
00296
00297 if( pluginFound || !pluginEnabled )
00298 continue;
00299
00300 kdDebug( 1000 ) << "load plugin " << name << endl;
00301 Plugin *plugin = loadPlugin( parent, QFile::encodeName(library) );
00302
00303 if ( plugin )
00304 {
00305 plugin->d->m_parentInstance = instance;
00306 plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
00307 plugin->setDOMDocument( (*pIt).m_document );
00308 parentGUIClient->insertChildClient( plugin );
00309 }
00310 }
00311 }
00312
00313
00314
00315 #include "plugin.moc"