• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeparts
 

tdeparts

plugin.cpp
00001 /* This file is part of the KDE project
00002    Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
00003              (C) 1999 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 <config.h>
00022 #include <tdeparts/plugin.h>
00023 #include <tdeparts/part.h>
00024 #include <tdeparts/componentfactory.h>
00025 
00026 #include <assert.h>
00027 
00028 #include <tqfile.h>
00029 #include <tqobjectlist.h>
00030 #include <tqfileinfo.h>
00031 
00032 #include <klibloader.h>
00033 #include <kinstance.h>
00034 #include <kstandarddirs.h>
00035 #include <kdebug.h>
00036 #include <kxmlguifactory.h>
00037 #include <tdelocale.h>
00038 #include <tdeconfig.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 TDEInstance *m_parentInstance;
00049     TQString m_library; // filename of the library
00050 };
00051 
00052 Plugin::Plugin( TQObject* parent, const char* name )
00053     : TQObject( parent, name )
00054 {
00055   //kdDebug() << className() << endl;
00056   d = new PluginPrivate();
00057 }
00058 
00059 Plugin::~Plugin()
00060 {
00061     delete d;
00062 }
00063 
00064 TQString Plugin::xmlFile() const
00065 {
00066     TQString path = KXMLGUIClient::xmlFile();
00067 
00068     if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) )
00069         return path;
00070 
00071     TQString absPath = locate( "data", TQString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path );
00072     assert( !absPath.isEmpty() );
00073     return absPath;
00074 }
00075 
00076 TQString Plugin::localXMLFile() const
00077 {
00078     TQString path = KXMLGUIClient::xmlFile();
00079 
00080     if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) )
00081         return path;
00082 
00083     TQString absPath = locateLocal( "data", TQString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path );
00084     assert( !absPath.isEmpty() );
00085     return absPath;
00086 }
00087 
00088 //static
00089 TQValueList<Plugin::PluginInfo> Plugin::pluginInfos( const TDEInstance * instance )
00090 {
00091   if ( !instance )
00092     kdError(1000) << "No instance ???" << endl;
00093 
00094   TQValueList<PluginInfo> plugins;
00095 
00096   // KDE4: change * into *.rc and remove test for .desktop from the for loop below.
00097   const TQStringList pluginDocs = instance->dirs()->findAllResources(
00098     "data", instance->instanceName()+"/kpartplugins/*", true, false );
00099 
00100   TQMap<TQString,TQStringList> sortedPlugins;
00101 
00102   TQStringList::ConstIterator pIt = pluginDocs.begin();
00103   TQStringList::ConstIterator pEnd = pluginDocs.end();
00104   for (; pIt != pEnd; ++pIt )
00105   {
00106       TQFileInfo fInfo( *pIt );
00107       if ( fInfo.extension() == TQString::fromLatin1( "desktop" ) )
00108           continue;
00109 
00110       TQMap<TQString,TQStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() );
00111       if ( mapIt == sortedPlugins.end() )
00112           mapIt = sortedPlugins.insert( fInfo.fileName(), TQStringList() );
00113 
00114       mapIt.data().append( *pIt );
00115   }
00116 
00117   TQMap<TQString,TQStringList>::ConstIterator mapIt = sortedPlugins.begin();
00118   TQMap<TQString,TQStringList>::ConstIterator mapEnd = sortedPlugins.end();
00119   for (; mapIt != mapEnd; ++mapIt )
00120   {
00121       PluginInfo info;
00122       TQString 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( TQObject *parent, const TDEInstance *instance )
00142 {
00143   loadPlugins( parent, pluginInfos( instance ), instance );
00144 }
00145 
00146 void Plugin::loadPlugins( TQObject *parent, const TQValueList<PluginInfo> &pluginInfos, const TDEInstance *instance )
00147 {
00148    TQValueList<PluginInfo>::ConstIterator pIt = pluginInfos.begin();
00149    TQValueList<PluginInfo>::ConstIterator pEnd = pluginInfos.end();
00150    for (; pIt != pEnd; ++pIt )
00151    {
00152      TQString library = (*pIt).m_document.documentElement().attribute( "library" );
00153 
00154      if ( library.isEmpty() || hasPlugin( parent, library ) )
00155        continue;
00156 
00157      Plugin *plugin = loadPlugin( parent, TQFile::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( TQObject *parent, const TQValueList<PluginInfo> &pluginInfos )
00171 {
00172    loadPlugins(parent, pluginInfos, 0);
00173 }
00174 
00175 // static
00176 Plugin* Plugin::loadPlugin( TQObject * 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 TQPtrList<KParts::Plugin> Plugin::pluginObjects( TQObject *parent )
00186 {
00187   TQPtrList<KParts::Plugin> objects;
00188 
00189   if (!parent )
00190     return objects;
00191 
00192   TQObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false );
00193 
00194   TQObjectListIt 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( TQObject* parent, const TQString& library )
00206 {
00207   TQObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false );
00208   TQObjectListIt 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( TDEInstance *instance )
00222 {
00223     TDEGlobal::locale()->insertCatalogue( instance->instanceName() );
00224     KXMLGUIClient::setInstance( instance );
00225 }
00226 
00227 void Plugin::loadPlugins( TQObject *parent, KXMLGUIClient* parentGUIClient, TDEInstance* instance, bool enableNewPluginsByDefault )
00228 {
00229     TDEConfigGroup cfgGroup( instance->config(), "KParts Plugins" );
00230     TQValueList<PluginInfo> plugins = pluginInfos( instance );
00231     TQValueList<PluginInfo>::ConstIterator pIt = plugins.begin();
00232     TQValueList<PluginInfo>::ConstIterator pEnd = plugins.end();
00233     for (; pIt != pEnd; ++pIt )
00234     {
00235         TQDomElement docElem = (*pIt).m_document.documentElement();
00236         TQString library = docElem.attribute( "library" );
00237 
00238         if ( library.isEmpty() )
00239             continue;
00240 
00241         // Check configuration
00242         const TQString 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         { // no user-setting, load plugin default setting
00251             TQString relPath = TQString( instance->instanceName() ) + "/" + (*pIt).m_relXMLFileName;
00252             relPath.truncate( relPath.findRev( '.' ) ); // remove extension
00253             relPath += ".desktop";
00254             //kdDebug(1000) << "looking for " << relPath << endl;
00255             const TQString desktopfile = instance->dirs()->findResource( "data", relPath );
00256             if( !desktopfile.isEmpty() )
00257             {
00258                 //kdDebug(1000) << "loadPlugins found desktop file for " << name << ": " << desktopfile << endl;
00259                 KSimpleConfig desktop( desktopfile, true );
00260                 desktop.setDesktopGroup();
00261                 pluginEnabled = desktop.readBoolEntry(
00262                     "X-TDE-PluginInfo-EnabledByDefault", enableNewPluginsByDefault );
00263             }
00264             else
00265             {
00266                 //kdDebug(1000) << "loadPlugins no desktop file found in " << relPath << endl;
00267             }
00268         }
00269 
00270         // search through already present plugins
00271         TQObjectList *pluginList = parent->queryList( "KParts::Plugin", 0, false, false );
00272         TQObjectListIt 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                 // delete and unload disabled plugins
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         // if the plugin is already loaded or if it's disabled in the
00296         // configuration do nothing
00297         if( pluginFound || !pluginEnabled )
00298             continue;
00299 
00300         kdDebug( 1000 ) << "load plugin " << name << endl;
00301         Plugin *plugin = loadPlugin( parent, TQFile::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 // vim:sw=4:et:sts=4
00314 
00315 #include "plugin.moc"

tdeparts

Skip menu "tdeparts"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeparts

Skip menu "tdeparts"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeparts by doxygen 1.7.6.1
This website is maintained by Timothy Pearson.