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

tdeparts

  • tdeparts
plugin.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 1999 Simon Hausmann <hausmann@kde.org>
3  (C) 1999 David Faure <faure@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <config.h>
22 #include <tdeparts/plugin.h>
23 #include <tdeparts/part.h>
24 #include <tdeparts/componentfactory.h>
25 
26 #include <assert.h>
27 
28 #include <tqfile.h>
29 #include <tqobjectlist.h>
30 #include <tqfileinfo.h>
31 
32 #include <klibloader.h>
33 #include <kinstance.h>
34 #include <kstandarddirs.h>
35 #include <kdebug.h>
36 #include <kxmlguifactory.h>
37 #include <tdelocale.h>
38 #include <tdeconfig.h>
39 #include <ksimpleconfig.h>
40 
41 using namespace KParts;
42 
43 class Plugin::PluginPrivate
44 {
45 public:
46  PluginPrivate() : m_parentInstance( 0 ) {}
47 
48  const TDEInstance *m_parentInstance;
49  TQString m_library; // filename of the library
50 };
51 
52 Plugin::Plugin( TQObject* parent, const char* name )
53  : TQObject( parent, name )
54 {
55  //kdDebug() << className() << endl;
56  d = new PluginPrivate();
57 }
58 
59 Plugin::~Plugin()
60 {
61  delete d;
62 }
63 
64 TQString Plugin::xmlFile() const
65 {
66  TQString path = KXMLGUIClient::xmlFile();
67 
68  if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) )
69  return path;
70 
71  TQString absPath = locate( "data", TQString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path );
72  assert( !absPath.isEmpty() );
73  return absPath;
74 }
75 
76 TQString Plugin::localXMLFile() const
77 {
78  TQString path = KXMLGUIClient::xmlFile();
79 
80  if ( !d->m_parentInstance || ( path.length() > 0 && path[ 0 ] == '/' ) )
81  return path;
82 
83  TQString absPath = locateLocal( "data", TQString::fromLatin1( d->m_parentInstance->instanceName() ) + '/' + path );
84  assert( !absPath.isEmpty() );
85  return absPath;
86 }
87 
88 //static
89 TQValueList<Plugin::PluginInfo> Plugin::pluginInfos( const TDEInstance * instance )
90 {
91  if ( !instance )
92  kdError(1000) << "No instance ???" << endl;
93 
94  TQValueList<PluginInfo> plugins;
95 
96  // KDE4: change * into *.rc and remove test for .desktop from the for loop below.
97  const TQStringList pluginDocs = instance->dirs()->findAllResources(
98  "data", instance->instanceName()+"/kpartplugins/*", true, false );
99 
100  TQMap<TQString,TQStringList> sortedPlugins;
101 
102  TQStringList::ConstIterator pIt = pluginDocs.begin();
103  TQStringList::ConstIterator pEnd = pluginDocs.end();
104  for (; pIt != pEnd; ++pIt )
105  {
106  TQFileInfo fInfo( *pIt );
107  if ( fInfo.extension() == TQString::fromLatin1( "desktop" ) )
108  continue;
109 
110  TQMap<TQString,TQStringList>::Iterator mapIt = sortedPlugins.find( fInfo.fileName() );
111  if ( mapIt == sortedPlugins.end() )
112  mapIt = sortedPlugins.insert( fInfo.fileName(), TQStringList() );
113 
114  mapIt.data().append( *pIt );
115  }
116 
117  TQMap<TQString,TQStringList>::ConstIterator mapIt = sortedPlugins.begin();
118  TQMap<TQString,TQStringList>::ConstIterator mapEnd = sortedPlugins.end();
119  for (; mapIt != mapEnd; ++mapIt )
120  {
121  PluginInfo info;
122  TQString doc;
123  info.m_absXMLFileName = KXMLGUIClient::findMostRecentXMLFile( mapIt.data(), doc );
124  if ( info.m_absXMLFileName.isEmpty() )
125  continue;
126 
127  kdDebug( 1000 ) << "found KParts Plugin : " << info.m_absXMLFileName << endl;
128  info.m_relXMLFileName = "kpartplugins/";
129  info.m_relXMLFileName += mapIt.key();
130 
131  info.m_document.setContent( doc );
132  if ( info.m_document.documentElement().isNull() )
133  continue;
134 
135  plugins.append( info );
136  }
137 
138  return plugins;
139 }
140 
141 void Plugin::loadPlugins( TQObject *parent, const TDEInstance *instance )
142 {
143  loadPlugins( parent, pluginInfos( instance ), instance );
144 }
145 
146 void Plugin::loadPlugins( TQObject *parent, const TQValueList<PluginInfo> &pluginInfos, const TDEInstance *instance )
147 {
148  TQValueList<PluginInfo>::ConstIterator pIt = pluginInfos.begin();
149  TQValueList<PluginInfo>::ConstIterator pEnd = pluginInfos.end();
150  for (; pIt != pEnd; ++pIt )
151  {
152  TQString library = (*pIt).m_document.documentElement().attribute( "library" );
153 
154  if ( library.isEmpty() || hasPlugin( parent, library ) )
155  continue;
156 
157  Plugin *plugin = loadPlugin( parent, TQFile::encodeName(library) );
158 
159  if ( plugin )
160  {
161  plugin->d->m_parentInstance = instance;
162  plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
163  plugin->setDOMDocument( (*pIt).m_document );
164 
165  }
166  }
167 
168 }
169 
170 void Plugin::loadPlugins( TQObject *parent, const TQValueList<PluginInfo> &pluginInfos )
171 {
172  loadPlugins(parent, pluginInfos, 0);
173 }
174 
175 // static
176 Plugin* Plugin::loadPlugin( TQObject * parent, const char* libname )
177 {
178  Plugin* plugin = ComponentFactory::createInstanceFromLibrary<Plugin>( libname, parent, libname );
179  if ( !plugin )
180  return 0L;
181  plugin->d->m_library = libname;
182  return plugin;
183 }
184 
185 TQPtrList<KParts::Plugin> Plugin::pluginObjects( TQObject *parent )
186 {
187  TQPtrList<KParts::Plugin> objects;
188 
189  if (!parent )
190  return objects;
191 
192  TQObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false );
193 
194  TQObjectListIt it( *plugins );
195  for ( ; it.current() ; ++it )
196  {
197  objects.append( static_cast<Plugin *>( it.current() ) );
198  }
199 
200  delete plugins;
201 
202  return objects;
203 }
204 
205 bool Plugin::hasPlugin( TQObject* parent, const TQString& library )
206 {
207  TQObjectList *plugins = parent->queryList( "KParts::Plugin", 0, false, false );
208  TQObjectListIt it( *plugins );
209  for ( ; it.current() ; ++it )
210  {
211  if ( static_cast<Plugin *>( it.current() )->d->m_library == library )
212  {
213  delete plugins;
214  return true;
215  }
216  }
217  delete plugins;
218  return false;
219 }
220 
221 void Plugin::setInstance( TDEInstance *instance )
222 {
223  TDEGlobal::locale()->insertCatalogue( instance->instanceName() );
224  KXMLGUIClient::setInstance( instance );
225 }
226 
227 void Plugin::loadPlugins( TQObject *parent, KXMLGUIClient* parentGUIClient, TDEInstance* instance, bool enableNewPluginsByDefault )
228 {
229  TDEConfigGroup cfgGroup( instance->config(), "KParts Plugins" );
230  TQValueList<PluginInfo> plugins = pluginInfos( instance );
231  TQValueList<PluginInfo>::ConstIterator pIt = plugins.begin();
232  TQValueList<PluginInfo>::ConstIterator pEnd = plugins.end();
233  for (; pIt != pEnd; ++pIt )
234  {
235  TQDomElement docElem = (*pIt).m_document.documentElement();
236  TQString library = docElem.attribute( "library" );
237 
238  if ( library.isEmpty() )
239  continue;
240 
241  // Check configuration
242  const TQString name = docElem.attribute( "name" );
243 
244  bool pluginEnabled = enableNewPluginsByDefault;
245  if ( cfgGroup.hasKey( name + "Enabled" ) )
246  {
247  pluginEnabled = cfgGroup.readBoolEntry( name + "Enabled" );
248  }
249  else
250  { // no user-setting, load plugin default setting
251  TQString relPath = TQString( instance->instanceName() ) + "/" + (*pIt).m_relXMLFileName;
252  relPath.truncate( relPath.findRev( '.' ) ); // remove extension
253  relPath += ".desktop";
254  //kdDebug(1000) << "looking for " << relPath << endl;
255  const TQString desktopfile = instance->dirs()->findResource( "data", relPath );
256  if( !desktopfile.isEmpty() )
257  {
258  //kdDebug(1000) << "loadPlugins found desktop file for " << name << ": " << desktopfile << endl;
259  KSimpleConfig desktop( desktopfile, true );
260  desktop.setDesktopGroup();
261  pluginEnabled = desktop.readBoolEntry(
262  "X-TDE-PluginInfo-EnabledByDefault", enableNewPluginsByDefault );
263  }
264  else
265  {
266  //kdDebug(1000) << "loadPlugins no desktop file found in " << relPath << endl;
267  }
268  }
269 
270  // search through already present plugins
271  TQObjectList *pluginList = parent->queryList( "KParts::Plugin", 0, false, false );
272  TQObjectListIt it( *pluginList );
273  bool pluginFound = false;
274  for ( ; it.current() ; ++it )
275  {
276  Plugin * plugin = static_cast<Plugin *>( it.current() );
277  if( plugin->d->m_library == library )
278  {
279  // delete and unload disabled plugins
280  if( !pluginEnabled )
281  {
282  kdDebug( 1000 ) << "remove plugin " << name << endl;
283  KXMLGUIFactory * factory = plugin->factory();
284  if( factory )
285  factory->removeClient( plugin );
286  delete plugin;
287  }
288 
289  pluginFound = true;
290  break;
291  }
292  }
293  delete pluginList;
294 
295  // if the plugin is already loaded or if it's disabled in the
296  // configuration do nothing
297  if( pluginFound || !pluginEnabled )
298  continue;
299 
300  kdDebug( 1000 ) << "load plugin " << name << endl;
301  Plugin *plugin = loadPlugin( parent, TQFile::encodeName(library) );
302 
303  if ( plugin )
304  {
305  plugin->d->m_parentInstance = instance;
306  plugin->setXMLFile( (*pIt).m_relXMLFileName, false, false );
307  plugin->setDOMDocument( (*pIt).m_document );
308  parentGUIClient->insertChildClient( plugin );
309  }
310  }
311 }
312 #include "plugin.moc"
KParts::Plugin
A plugin is the way to add actions to an existing KParts application, or to a Part.
Definition: plugin.h:50
KParts::Plugin::localXMLFile
virtual TQString localXMLFile() const
Reimplemented for internal reasons.
Definition: plugin.cpp:76
KParts::Plugin::Plugin
Plugin(TQObject *parent=0, const char *name=0)
Construct a new KParts plugin.
Definition: plugin.cpp:52
KParts::Plugin::xmlFile
virtual TQString xmlFile() const
Reimplemented for internal reasons.
Definition: plugin.cpp:64
KParts::Plugin::loadPlugins
static void loadPlugins(TQObject *parent, const TDEInstance *instance)
Load the plugin libraries from the directories appropriate to instance and make the Plugin objects ch...
Definition: plugin.cpp:141
KParts::Plugin::pluginObjects
static TQPtrList< Plugin > pluginObjects(TQObject *parent)
Returns a list of plugin objects loaded for parent.
Definition: plugin.cpp:185
KParts::Plugin::~Plugin
virtual ~Plugin()
Destructor.
Definition: plugin.cpp:59
KParts::Plugin::pluginInfos
static TQValueList< Plugin::PluginInfo > pluginInfos(const TDEInstance *instance)
Look for plugins in the instance's "data" directory (+"/kpartplugins")
Definition: plugin.cpp:89
KSimpleConfig
KXMLGUIClient
KXMLGUIClient::xmlFile
virtual TQString xmlFile() const
KXMLGUIClient::instance
virtual TDEInstance * instance() const
KXMLGUIClient::setXMLFile
virtual void setXMLFile(const TQString &file, bool merge=false, bool setXMLDoc=true)
KXMLGUIClient::factory
KXMLGUIFactory * factory() const
KXMLGUIClient::setDOMDocument
virtual void setDOMDocument(const TQDomDocument &document, bool merge=false)
KXMLGUIClient::insertChildClient
void insertChildClient(KXMLGUIClient *child)
KXMLGUIClient::setInstance
virtual void setInstance(TDEInstance *instance)
KXMLGUIFactory
KXMLGUIFactory::removeClient
void removeClient(KXMLGUIClient *client)
TDEConfigBase::readBoolEntry
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
TDEConfigBase::setDesktopGroup
void setDesktopGroup()
TDEConfigBase::hasKey
bool hasKey(const TQString &key) const
TDEConfigGroup
TDEGlobal::locale
static TDELocale * locale()
TDEInstance
TDEInstance::dirs
TDEStandardDirs * dirs() const
TDEInstance::instanceName
TQCString instanceName() const
TDEInstance::config
TDEConfig * config() const
TDELocale::insertCatalogue
void insertCatalogue(const TQString &catalog)
TDEStandardDirs::findAllResources
TQStringList findAllResources(const char *type, const TQString &filter=TQString::null, bool recursive=false, bool unique=false) const
TDEStandardDirs::findResource
TQString findResource(const char *type, const TQString &filename) const
endl
kndbgstream & endl(kndbgstream &s)
kdError
kdbgstream kdError(int area=0)
kdDebug
kdbgstream kdDebug(int area=0)
locate
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
locateLocal
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
KParts::Plugin::PluginInfo
Struct holding information about a plugin.
Definition: plugin.h:57
KParts::Plugin::PluginInfo::m_absXMLFileName
TQString m_absXMLFileName
full path of most recent filename matching the relative filename
Definition: plugin.h:59
KParts::Plugin::PluginInfo::m_relXMLFileName
TQString m_relXMLFileName
relative filename, i.e. kpartplugins/name
Definition: plugin.h:58
tdelocale.h

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.9.1
This website is maintained by Timothy Pearson.