summaryrefslogtreecommitdiffstats
path: root/libk3b/plugin/k3bpluginmanager.cpp
blob: 54362510b4c205efe5354d00b46d65bc676fb402 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* 
 *
 * $Id: k3bpluginmanager.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bpluginmanager.h"
#include "k3bplugin.h"
#include "k3bpluginconfigwidget.h"
#include <k3bversion.h>

#include <kdebug.h>
#include <ksimpleconfig.h>
#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kdialogbase.h>
#include <kmessagebox.h>
#include <klibloader.h>

#include <tqptrlist.h>
#include <tqmap.h>
#include <tqdir.h>



class K3bPluginManager::Private
{
public:
  TQPtrList<K3bPlugin> plugins;
};




K3bPluginManager::K3bPluginManager( TQObject* tqparent, const char* name )
  : TQObject( tqparent, name )
{
  d = new Private();
}


K3bPluginManager::~K3bPluginManager()
{
  delete d;
}



TQStringList K3bPluginManager::groups() const
{
  TQStringList grps;

  TQPtrList<K3bPlugin> fl;
  for( TQPtrListIterator<K3bPlugin> it( d->plugins );
       it.current(); ++it ) {
    if( !grps.tqcontains( it.current()->group() ) )
	grps.append( it.current()->group() );
  }

  return grps;
}


TQPtrList<K3bPlugin> K3bPluginManager::plugins( const TQString& group ) const
{
  TQPtrList<K3bPlugin> fl;
  for( TQPtrListIterator<K3bPlugin> it( d->plugins );
       it.current(); ++it ) {
    if( it.current()->group() == group || group.isEmpty() )
      fl.append( it.current() );
  }
  return fl;
}


void K3bPluginManager::loadPlugin( const TQString& fileName )
{
  KSimpleConfig c( fileName, true );
  c.setGroup( "K3b Plugin" );

  TQString libName = c.readEntry( "Lib" );
  if( libName.isEmpty() ) {
    kdDebug() << "(K3bPluginManager) no Lib specified in " << fileName << endl;
    return;
  }

  // read the lib
  KLibFactory* factory = KLibLoader::self()->factory( libName.latin1() );
  if( factory ) {
    K3bPlugin* plugin = dynamic_cast<K3bPlugin*>( factory->create( this ) );
    if( plugin ) {
      // FIXME: improve this versioning stuff
      if( plugin->pluginSystemVersion() != K3B_PLUGIN_SYSTEM_VERSION ) {
	delete plugin;
	kdDebug() << "(K3bPluginManager) plugin system does not fit lib " << libName << endl;
      }
      else {
	plugin->m_pluginInfo = K3bPluginInfo( libName,
					      c.readEntry( "Name" ),
					      c.readEntry( "Author" ),
					      c.readEntry( "Email" ),
					      c.readEntry( "Comment" ),
					      c.readEntry( "Version" ),
					      c.readEntry( "License" ) );

	// make sure to only use the latest version of one plugin
	bool addPlugin = true;
	for( TQPtrListIterator<K3bPlugin> it( d->plugins ); *it; ++it ) {
	  if( it.current()->pluginInfo().name() == plugin->pluginInfo().name() ) {
	    if( K3bVersion(it.current()->pluginInfo().version()) < K3bVersion(plugin->pluginInfo().version()) ) {
	      K3bPlugin* p = it.current();
	      d->plugins.removeRef( p );
	      delete p;
	    }
	    else {
	      addPlugin = false;
	    }
	    break;
	  }
	}
	if( addPlugin )
	  d->plugins.append( plugin );
	else
	  delete plugin;
      }
    }
    else
      kdDebug() << "(K3bPluginManager) lib " << libName << " not a K3b plugin" << endl;
  }
  else
    kdDebug() << "(K3bPluginManager) lib " << libName << " not found" << endl;
}


void K3bPluginManager::loadAll()
{
  // we simply search the K3b plugin dir for now
  TQStringList dirs = KGlobal::dirs()->findDirs( "data", "k3b/plugins/" );

  for( TQStringList::const_iterator it = dirs.begin();
       it != dirs.end(); ++it ) {
    TQStringList entries = TQDir(*it).entryList( "*.plugin", TQDir::Files );
    for( TQStringList::const_iterator it2 = entries.begin();
	 it2 != entries.end(); ++it2 ) {
      loadPlugin( *it + *it2 );
    }
  }
}

int K3bPluginManager::pluginSystemVersion() const
{
  return K3B_PLUGIN_SYSTEM_VERSION;
}


int K3bPluginManager::execPluginDialog( K3bPlugin* plugin, TQWidget* tqparent, const char* name )
{
  KDialogBase dlg( tqparent, 
		   name, 
		   true,
		   i18n("Configure plugin %1").tqarg( plugin->pluginInfo().name() ) );
  
  K3bPluginConfigWidget* configWidget = plugin->createConfigWidget( &dlg );
  if( configWidget ) {
    dlg.setMainWidget( configWidget );
    connect( &dlg, TQT_SIGNAL(applyClicked()), configWidget, TQT_SLOT(saveConfig()) );
    connect( &dlg, TQT_SIGNAL(okClicked()), configWidget, TQT_SLOT(saveConfig()) );
    configWidget->loadConfig();
    int r = dlg.exec();
    delete configWidget;
    return r;
  }
  else {
    KMessageBox::sorry( tqparent, i18n("No settings available for plugin %1.").tqarg( plugin->pluginInfo().name() ) );
    return 0;
  }
}

#include "k3bpluginmanager.moc"