summaryrefslogtreecommitdiffstats
path: root/src/plugincontroller.cpp
blob: 8a3da03f8dc2490b7a1dd977744f1b9f48319c3a (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <tqfile.h>
#include <tqvbox.h>

#include <tdecmdlineargs.h>
#include <tdeapplication.h>
#include <klibloader.h>
#include <kservice.h>
#include <ktrader.h>
#include <kmessagebox.h>
#include <tdeconfig.h>
#include <klocale.h>
#include <tdemainwindow.h>
#include <tdeparts/componentfactory.h>
#include <assert.h>
#include <kdebug.h>
#include <kdialogbase.h>
#include <tdecmdlineargs.h>
#include <kstandarddirs.h>
#include <kstatusbar.h>
#include <kiconloader.h>

#include <kdevapi.h>
#include <kdevplugin.h>
#include <kdevmakefrontend.h>
#include <kdevappfrontend.h>
#include <kdevdifffrontend.h>
#include <kdevsourceformatter.h>
#include <kdevcreatefile.h>
#include <kdevplugininfo.h>
#include <tdeaction.h>

#include <profileengine.h>

#include "core.h"
#include "api.h"
#include "toplevel.h"
#include "projectmanager.h"
//#include "partselectwidget.h"
#include "domutil.h"
#include "plugincontroller.h"
#include "pluginselectdialog.h"

#include "shellextension.h"

// a separate method in this anonymous namespace to avoid having it all
// inline in plugincontroller.h
namespace
{
  template <class ComponentType>
  ComponentType *loadDefaultPart( const TQString &serviceType )
  {
    TDETrader::OfferList offers = TDETrader::self()->query(serviceType, TQString("[X-TDevelop-Version] == %1").arg(TDEVELOP_PLUGIN_VERSION));
    TDETrader::OfferList::ConstIterator serviceIt = offers.begin();
    for ( ; serviceIt != offers.end(); ++serviceIt ) {
      KService::Ptr service = *serviceIt;

      ComponentType *part = KParts::ComponentFactory
        ::createInstanceFromService< ComponentType >( service, API::getInstance(), 0,
                                                      PluginController::argumentsFromService( service ) );

      if ( part )
        return part;
    }
    return 0;
  }
}

PluginController *PluginController::s_instance = 0;


PluginController *PluginController::getInstance()
{
  if (!s_instance)
    s_instance = new PluginController();
  return s_instance;
}


PluginController::PluginController()
  : KDevPluginController()
{
/*  m_defaultProfile = TQString::fromLatin1( "FullIDE" );
  m_defaultProfilePath = kapp->dirs()->localtdedir() + "/" +
			 TDEStandardDirs::kde_default( "data" ) +
			 TQString::fromLatin1("/tdevelop/profiles/FullIDE");*/

    TDECmdLineArgs* args = TDECmdLineArgs::parsedArgs();
    if( args->isSet("profile") ){
        m_profile = TQString::fromLocal8Bit( args->getOption("profile") );
    } else {
        m_profile = ShellExtension::getInstance()->defaultProfile();
    }

}


void PluginController::loadInitialPlugins()
{
	loadCorePlugins();

	TQStringList disableList;
	Profile * profile = engine().findProfile( currentProfile() );
	if( profile )
	{
		Profile::EntryList disableEntryList = profile->list( Profile::ExplicitDisable );
		for ( Profile::EntryList::const_iterator it = disableEntryList.constBegin(); it != disableEntryList.constEnd(); ++it )
		{
			disableList << (*it).name;
		}
	}
	loadGlobalPlugins( disableList );
}


PluginController::~PluginController()
{
  unloadPlugins();
}

void PluginController::loadCorePlugins()
{
  TDETrader::OfferList coreOffers = m_engine.offers(m_profile, ProfileEngine::Core);
  loadPlugins( coreOffers );
}

void PluginController::loadGlobalPlugins( const TQStringList & ignorePlugins )
{
  TDETrader::OfferList globalOffers = m_engine.offers(m_profile, ProfileEngine::Global);
  loadPlugins( globalOffers, ignorePlugins );
}

void PluginController::loadProjectPlugins( const TQStringList & ignorePlugins )
{
	TDETrader::OfferList projectOffers = m_engine.offers(m_profile, ProfileEngine::Project);
	loadPlugins( projectOffers, ignorePlugins );
}

void PluginController::loadPlugins( TDETrader::OfferList offers, const TQStringList & ignorePlugins )
{

  TopLevel::getInstance()->main()->setFocus();
  for (TDETrader::OfferList::ConstIterator it = offers.begin(); it != offers.end(); ++it)
  {
    TQString name = (*it)->desktopEntryName();

    // Check if it is already loaded or shouldn't be
    if( m_parts[ name ] != 0 || ignorePlugins.contains( name ) )
      continue;

    emit loadingPlugin(i18n("Loading: %1").arg((*it)->genericName()));

    KDevPlugin *plugin = loadPlugin( *it );
    if ( plugin )
    {
        m_parts.insert( name, plugin );
        integratePart( plugin );
    }
  }
}

void PluginController::unloadPlugins()
{
  for( TQDictIterator<KDevPlugin> it( m_parts ); !it.isEmpty(); )
  {
    KDevPlugin* part = it.current();
    removePart( part );
    m_parts.remove( it.currentKey() );
    delete part;
  }
}

void PluginController::unloadProjectPlugins( )
{
	// this is nasty, but we need to unload the version control plugin too, and the 
	// right moment to do this is here
	TDETrader::OfferList offers = TDETrader::self()->query("TDevelop/VersionControl", "");	

 	offers += m_engine.offers(m_profile, ProfileEngine::Project);
	for (TDETrader::OfferList::ConstIterator it = offers.begin(); it != offers.end(); ++it)
	{
		TQString name = (*it)->desktopEntryName();

		if ( KDevPlugin * plugin = m_parts[ name ] )
		{
			kdDebug(9000) << " *** Removing: " << name << endl;
			removeAndForgetPart( name, plugin );
			delete plugin;
		}
	}
}

void PluginController::unloadPlugins( TQStringList const & unloadParts )
{
	TQStringList::ConstIterator it = unloadParts.begin();
	while ( it != unloadParts.end() )
	{
		KDevPlugin* part = m_parts[ *it ];
		if( part )
		{
			kdDebug(9000) << " *** Removing: " << *it << endl;
			removePart( part );
			m_parts.remove( *it );
			delete part;
		}
		++it;
	}
}

KDevPlugin *PluginController::loadPlugin( const KService::Ptr &service )
{
    int err = 0;
    KDevPlugin * pl = KParts::ComponentFactory
        ::createInstanceFromService<KDevPlugin>( service, API::getInstance(), 0,
                                                 argumentsFromService( service ), &err );
    if (!pl)
    {
        KMessageBox::error(
            0,
            i18n("<b>Could not load plugin</b><br>"
                 "Plugin %1 could not be loaded<br>"
                 "Library loader error: %2").arg(service->name()).
            arg(KLibLoader::self()->lastErrorMessage()),
            i18n("Could not load plugin"));
    }
//    kdDebug() << "ERR: " << err << endl;
    return pl;
}

TQStringList PluginController::argumentsFromService( const KService::Ptr &service )
{
	TQStringList args;
	if ( !service )
		// service is a reference to a pointer, so a check whether it is 0 is still required
		return args;
	TQVariant prop = service->property( "X-TDevelop-Args" );
	if ( prop.isValid() )
		args = TQStringList::split( " ", prop.toString() );
	return args;
}

void PluginController::integratePart(KXMLGUIClient *part)
{
    if ( ! part ) return;
    Core::setupShourtcutTips(part);
    
    TopLevel::getInstance()->main()->guiFactory()->addClient(part);
    connect( part->actionCollection(), TQT_SIGNAL( actionStatusText( const TQString & ) ),
        TopLevel::getInstance()->main()->actionCollection(), TQT_SIGNAL( actionStatusText( const TQString & ) ) );
}

void PluginController::integrateAndRememberPart(const TQString &name, KDevPlugin *part)
{
    m_parts.insert(name, part);
    integratePart(part);
}

void PluginController::removePart(KXMLGUIClient *part)
{
  if (TopLevel::mainWindowValid()) // is 0 when window was already closed
  TopLevel::getInstance()->main()->guiFactory()->removeClient(part);
}

void PluginController::removeAndForgetPart(const TQString &name, KDevPlugin *part)
{
    kdDebug() << "removing: " << name << endl;
    m_parts.remove(name);
    removePart(part);
}

const TQValueList<KDevPlugin*> PluginController::loadedPlugins()
{
	TQValueList<KDevPlugin*> plugins;
	TQDictIterator<KDevPlugin> itt(m_parts);
	while( itt.current() )
	{
		plugins.append( itt.current() );
		++itt;
	}
	return plugins;
}

KDevPlugin * PluginController::extension( const TQString & serviceType, const TQString & constraint )
{
    TDETrader::OfferList offers = KDevPluginController::query(serviceType, constraint);
    for (TDETrader::OfferList::const_iterator it = offers.constBegin(); it != offers.end(); ++it)
    {
        KDevPlugin *ext = m_parts[(*it)->desktopEntryName()];
        if (ext) return ext;
    }
    return 0;
}

KDevPlugin * PluginController::loadPlugin( const TQString & serviceType, const TQString & constraint )
{
	TDETrader::OfferList offers = KDevPluginController::query( serviceType, constraint );
	if ( !offers.size() == 1 ) return 0;

	TDETrader::OfferList::const_iterator it = offers.constBegin();
	TQString name = (*it)->desktopEntryName();

	KDevPlugin * plugin = 0;
	if ( plugin = m_parts[ name ] )
	{
		return plugin;
	}

	if ( plugin = loadPlugin( *it ) )
	{
		m_parts.insert( name, plugin );
		integratePart( plugin );
	}

	return plugin;
}

void PluginController::unloadPlugin( const TQString & plugin )
{
	TQStringList pluginList;
	pluginList << plugin;
	unloadPlugins( pluginList );
}

KURL::List PluginController::profileResources(const TQString &nameFilter)
{
    return m_engine.resources(currentProfile(), nameFilter);
}

KURL::List PluginController::profileResourcesRecursive(const TQString &nameFilter)
{
    return m_engine.resourcesRecursive(currentProfile(), nameFilter);
}

TQString PluginController::changeProfile(const TQString &newProfile)
{
    kdDebug() << "CHANGING PROFILE: from " << currentProfile() << " to " << newProfile << endl;
    TQStringList unload;
    TDETrader::OfferList coreLoad;
    TDETrader::OfferList globalLoad;
    m_engine.diffProfiles(ProfileEngine::Core, currentProfile(), newProfile, unload, coreLoad);
    m_engine.diffProfiles(ProfileEngine::Global, currentProfile(), newProfile, unload, globalLoad);

    TQString oldProfile = m_profile;
    m_profile = newProfile;

    unloadPlugins(unload);
    loadPlugins( coreLoad );
    loadPlugins( globalLoad );

    return oldProfile;
}

void PluginController::selectPlugins( )
{
	kdDebug(9000) << k_funcinfo << endl;

	PluginSelectDialog dlg;
	if ( dlg.exec() == TQDialog::Accepted )
	{
		TQStringList unselectedPlugins = dlg.unselectedPluginNames();

	kdDebug(9000) << unselectedPlugins << endl;

		unloadPlugins( unselectedPlugins );
		loadGlobalPlugins( unselectedPlugins );

		if ( ProjectManager::getInstance()->projectLoaded() )
		{
			loadProjectPlugins( unselectedPlugins );
			DomUtil::writeListEntry( *API::getInstance()->projectDom(), "/general/ignoreparts", "part", unselectedPlugins );
		}
	}
}

/*
KDevPlugin * PluginController::getPlugin( const KService::Ptr & service )
{
	KDevPlugin * plugin = m_parts[ (*it)->name() ];
	if ( !plugin )
	{
		KDevPlugin * plugin = loadPlugin( *it );
		if ( plugin )
		{
			integratePart( plugin );
			m_parts.insert( (*it)->name(), plugin );
		}
	}
	return plugin;
}
*/
#include "plugincontroller.moc"