summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmpluginmanager.cpp
blob: 7af50d5cc7e4242e2f7c69fa337a4f5ce25a5e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2003 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/

#include "pmpluginmanager.h"
#include "pmpart.h"
#include "pmshell.h"
#include "pmfactory.h"
#include "pmdebug.h"

#include <tqmap.h>
#include <tqapplication.h>

#include <kparts/plugin.h>
#include <kxmlguifactory.h>
#include <kconfig.h>

PMPluginManager* PMPluginManager::s_pInstance = 0;
KStaticDeleter<PMPluginManager> PMPluginManager::s_staticDeleter;

using namespace KParts;

// workaround for protected Plugin::pluginInfos
class PMPluginWorkaround : public Plugin
{
public:
   PMPluginWorkaround( ) : Plugin( 0, 0 ) { };
   static TQValueList<Plugin::PluginInfo> installedPlugins( const KInstance* instance )
   {
      return pluginInfos( instance );
   }
};

PMPluginManager::PMPluginManager( )
{
   // find installed plugins
   KConfigGroup cfgGroup( PMFactory::instance( )->config( ),
                          "KParts Plugins" );
   TQValueList<Plugin::PluginInfo> plugins
      = PMPluginWorkaround::installedPlugins( PMFactory::instance( ) );
   TQValueList<Plugin::PluginInfo>::ConstIterator pIt = plugins.begin( );
   TQValueList<Plugin::PluginInfo>::ConstIterator pEnd = plugins.end( );
   
   for( ; pIt != pEnd; ++pIt )
   {
      TQDomElement docElem = ( *pIt ).m_document.documentElement( );
      TQString name = docElem.attribute( "name" );
      TQString description = docElem.attribute( "description" );
      if( !description.isEmpty( ) )
         description = i18n( description.latin1( ) );
      bool enabled = cfgGroup.readBoolEntry( name + "Enabled", false );
      m_plugins.append( new PMPluginInfo( name, description, enabled ) );
   }
}

PMPluginManager::~PMPluginManager( )
{
   m_plugins.setAutoDelete( true );
   m_plugins.clear( );
}

PMPluginManager* PMPluginManager::theManager( )
{
   if( !s_pInstance )
      s_staticDeleter.setObject( s_pInstance, new PMPluginManager( ) );
   return s_pInstance;
}

void PMPluginManager::registerPart( PMPart* p )
{
   if( !m_parts.containsRef( p ) )
   {
      m_parts.append( p );
      Plugin::loadPlugins( p, p, PMFactory::instance( ), false );
   }
}

void PMPluginManager::removePart( PMPart* p )
{
   m_parts.removeRef( p );
}

void PMPluginManager::updatePlugins( )
{
   KConfigGroup cfgGroup( PMFactory::instance( )->config( ),
                          "KParts Plugins" );
   TQPtrListIterator<PMPluginInfo> pit( m_plugins );
   for( ; *pit; ++pit )
      cfgGroup.writeEntry( ( *pit )->name( ) + "Enabled",
                           ( *pit )->enabled( ) );
   cfgGroup.sync( );

   TQPtrListIterator<PMPart> it( m_parts );
   for( ; *it; ++it )
   {
      Plugin::loadPlugins( *it, *it, PMFactory::instance( ), false );
      PMShell* shell = ( *it )->shell( );
      if( shell )
         shell->updateGUI( );
      // TODO find a solution to update the gui without using the shell
   }
}