summaryrefslogtreecommitdiffstats
path: root/noatun/library/pluginmodule.cpp
blob: d6b86e866d753cb4cef9b4884577b9582f01b76a (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) 2000-2001 Charles Samuels <charles@kde.org>
// Copyright (c) 2000-2001 Neil Stevens <multivac@fcmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIAB\ILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <noatun/app.h>

#include <kdebug.h>
#include <kdialog.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tqtabwidget.h>
#include <tqheader.h>
#include <tqlabel.h>
#include <tqlayout.h>

#include "noatunlistview.h"
#include "pluginmodule.h"

#include <tqwhatsthis.h>

#include "common.h"

PluginListItem::PluginListItem(const bool _exclusive, bool _checked, const NoatunLibraryInfo &_info, TQListView *_parent)
	: TQCheckListItem(_parent, _info.name, CheckBox)
	, mInfo(_info)
	, silentStateChange(false)
	, exclusive(_exclusive)
{
	setChecked(_checked);
	if(_checked) static_cast<PluginListView *>(listView())->count++;
}


void PluginListItem::setChecked(bool b)
{
	silentStateChange = true;
	setOn(b);
	silentStateChange = false;
}

void PluginListItem::stateChange(bool b)
{
	if(!silentStateChange)
		static_cast<PluginListView *>(listView())->stateChanged(this, b);
}

void PluginListItem::paintCell(TQPainter *p, const TQColorGroup &cg, int a, int b, int c)
{
	if(exclusive) myType = RadioButton;
	TQCheckListItem::paintCell(p, cg, a, b, c);
	if(exclusive) myType = CheckBox;
}

PluginListView::PluginListView(unsigned _min, unsigned _max, TQWidget *_parent, const char *_name)
	: TDEListView(_parent, _name)
	, hasMaximum(true)
	, max(_max)
	, min(_min <= _max ? _min : _max)
	, count(0)
{
}

PluginListView::PluginListView(unsigned _min, TQWidget *_parent, const char *_name)
	: TDEListView(_parent, _name)
	, hasMaximum(false)
	, min(_min)
	, count(0)
{
}

PluginListView::PluginListView(TQWidget *_parent, const char *_name)
	: TDEListView(_parent, _name)
	, hasMaximum(false)
	, min(0)
	, count(0)
{
}

void PluginListView::clear()
{
	count = 0;
	TDEListView::clear();
}

void PluginListView::stateChanged(PluginListItem *item, bool b)
{
	if(b)
	{
		count++;
		emit stateChange(item, b);

		if(hasMaximum && count > max)
		{
			// Find a different one and turn it off

			TQListViewItem *cur = firstChild();
			PluginListItem *curItem = dynamic_cast<PluginListItem *>(cur);

			while(cur == item || !curItem || !curItem->isOn())
			{
				cur = cur->nextSibling();
				curItem = dynamic_cast<PluginListItem *>(cur);
			}

			curItem->setOn(false);
		}
	}
	else
	{
		if(count == min)
		{
			item->setChecked(true);
		}
		else
		{
			count--;
			emit stateChange(item, b);
		}
	}
}

Plugins::Plugins(TQObject *_parent)
	: CModule(i18n("Plugins"), i18n("Select Your Plugins"), "gear", _parent)
	, shown(false)
{
	(new TQVBoxLayout(this))->setAutoAdd(true);
	TQTabWidget *tabControl = new TQTabWidget(this,"tabControl");

	TQFrame *interfaceTab = new TQFrame(tabControl);
	(new TQVBoxLayout(interfaceTab, KDialog::marginHint(), KDialog::spacingHint()))->setAutoAdd(true);
	(void)new TQLabel(i18n("<b>Select one or more interfaces to use:</b>"), interfaceTab);
	// At least one interface is required
	interfaceList = new PluginListView(1, interfaceTab);
	interfaceList->addColumn(i18n("Name"));
	interfaceList->addColumn(i18n("Description"));
	interfaceList->addColumn(i18n("Author"));
	interfaceList->addColumn(i18n("License"));
	connect(interfaceList, TQT_SIGNAL(stateChange(PluginListItem *, bool)), this, TQT_SLOT(stateChange(PluginListItem *, bool)));
	tabControl->addTab(interfaceTab, i18n("&Interfaces"));

	TQFrame *playlistTab = new TQFrame(tabControl);
	(new TQVBoxLayout(playlistTab, KDialog::marginHint(), KDialog::spacingHint()))->setAutoAdd(true);
	(void)new TQLabel(i18n("<b>Select one playlist to use:</b>"), playlistTab);
	// Exactly one playlist is required
	playlistList = new PluginListView(1, 1, playlistTab);
	playlistList->addColumn(i18n("Name"));
	playlistList->addColumn(i18n("Description"));
	playlistList->addColumn(i18n("Author"));
	playlistList->addColumn(i18n("License"));
	connect(playlistList, TQT_SIGNAL(stateChange(PluginListItem *, bool)), this, TQT_SLOT(stateChange(PluginListItem *, bool)));
	tabControl->addTab(playlistTab, i18n("&Playlist"));

	TQFrame *visTab = new TQFrame(tabControl);
	(new TQVBoxLayout(visTab, KDialog::marginHint(), KDialog::spacingHint()))->setAutoAdd(true);
	(void)new TQLabel(i18n("<b>Select any visualizations to use:</b>"), visTab);
	visList = new PluginListView(0, visTab);
	visList->addColumn(i18n("Name"));
	visList->addColumn(i18n("Description"));
	visList->addColumn(i18n("Author"));
	visList->addColumn(i18n("License"));
	connect(visList, TQT_SIGNAL(stateChange(PluginListItem *, bool)), this, TQT_SLOT(stateChange(PluginListItem *, bool)));
	tabControl->addTab(visTab, i18n("&Visualizations"));

	// Other plugins are not restricted
	TQFrame *otherTab = new TQFrame(tabControl);
	(new TQVBoxLayout(otherTab, KDialog::marginHint(), KDialog::spacingHint()))->setAutoAdd(true);
	(void)new TQLabel(i18n("<b>Select any other plugins to use:</b>"), otherTab);
	otherList = new PluginListView(0, otherTab);
	otherList->addColumn(i18n("Name"));
	otherList->addColumn(i18n("Description"));
	otherList->addColumn(i18n("Author"));
	otherList->addColumn(i18n("License"));
	connect(otherList, TQT_SIGNAL(stateChange(PluginListItem *, bool)), this, TQT_SLOT(stateChange(PluginListItem *, bool)));
	tabControl->addTab(otherTab, i18n("O&ther Plugins"));
}

void Plugins::reopen()
{
	playlistList->clear();
	interfaceList->clear();
	otherList->clear();
	visList->clear();

	TQValueList<NoatunLibraryInfo> available = napp->libraryLoader()->available();
	TQValueList<NoatunLibraryInfo> loaded = napp->libraryLoader()->loaded();

	for(TQValueList<NoatunLibraryInfo>::Iterator i = available.begin(); i != available.end(); ++i)
	{
		PluginListView *parent;
		bool exclusive = false;

		if((*i).type == "userinterface")
		{
			parent = interfaceList;
		}
		else if((*i).type == "playlist")
		{
			parent = playlistList;
			exclusive = true;
		}
		else if((*i).type == "sm" || (*i).type=="hidden")
		{
			parent = 0;
		}
		else if ((*i).type == "visualization")
		{
			parent = visList;
		}
		else
		{
			parent = otherList;
		}

		if(parent)
		{
			PluginListItem *item = new PluginListItem(exclusive, loaded.contains(*i), *i, parent);
			item->setText(0, (*i).name);
			item->setText(1, (*i).comment);
			item->setText(2, (*i).author);
			item->setText(3, (*i).license);
		}
	}
}

void Plugins::stateChange(PluginListItem *item, bool b)
{
	if(b)
		addPlugin(item->info());
	else
		removePlugin(item->info());
}

void Plugins::addPlugin(const NoatunLibraryInfo &info)
{
	// Load any that this one depends upon
	for(TQStringList::ConstIterator i = info.require.begin(); i != info.require.end(); ++i)
	{
		NoatunLibraryInfo requiredInfo = napp->libraryLoader()->getInfo(*i);
		PluginListItem *item = findItem(requiredInfo);
		if(item) item->setOn(true);
	}

	if(mDeleted.contains(info.specfile))
		mDeleted.remove(info.specfile);
	else if(!mAdded.contains(info.specfile))
		mAdded.append(info.specfile);
}

void Plugins::removePlugin(const NoatunLibraryInfo &info)
{
	LibraryLoader &loader = *(napp->libraryLoader());

	// Here are the ones loaded
	TQValueList<NoatunLibraryInfo> loaded = napp->libraryLoader()->loaded();

	// Add the ones marked for loading
	for(TQStringList::ConstIterator i = mAdded.begin(); i != mAdded.end(); ++i)
		loaded.append(loader.getInfo(*i));

	// Subtract the ones marked for removal
	for(TQStringList::ConstIterator i = mDeleted.begin(); i != mDeleted.end(); ++i)
		loaded.remove(loader.getInfo(*i));

	// If any depend on this plugin, mark them for removal (or remove them from mAdded)
	for(TQValueList<NoatunLibraryInfo>::Iterator i = loaded.begin(); i != loaded.end(); ++i)
	{
		for(TQStringList::ConstIterator j = (*i).require.begin(); j != (*i).require.end(); ++j)
		{
			if(*j == info.specfile)
			{
				PluginListItem *item = findItem(*i);
				if(item) item->setOn(false);
			}
		}
	}

	if (mAdded.contains(info.specfile))
		mAdded.remove(info.specfile);
	else if(!mDeleted.contains(info.specfile))
		mDeleted.append(info.specfile);
}

PluginListItem *Plugins::findItem(const NoatunLibraryInfo &info) const
{
	for(TQListViewItem *cur = otherList->firstChild(); cur != 0; cur = cur->itemBelow())
	{
		PluginListItem *item = dynamic_cast<PluginListItem *>(cur);
		if(item && item->info() == info)
			return item;
	}

	// visualizations
	for(TQListViewItem *cur = visList->firstChild(); cur != 0; cur = cur->itemBelow())
	{
		PluginListItem *item = dynamic_cast<PluginListItem *>(cur);
		if(item && item->info() == info)
			return item;
	}

	// If our only interface has a dependency removed, that's a double dose of trouble
	// We may as well have this here for completeness, though
	for(TQListViewItem *cur = interfaceList->firstChild(); cur != 0; cur = cur->itemBelow())
	{
		PluginListItem *item = dynamic_cast<PluginListItem *>(cur);
		if(item && item->info() == info)
			return item;
	}

	// If a playlist is added or removed due to a dependency, we're doom-diddly-oomed
	// We may as well have this here for completeness, though
	for(TQListViewItem *cur = playlistList->firstChild(); cur != 0; cur = cur->itemBelow())
	{
		PluginListItem *item = dynamic_cast<PluginListItem *>(cur);
		if(item && item->info() == info)
			return item;
	}

	return 0;
}

void Plugins::save()
{
	LibraryLoader &loader = *(napp->libraryLoader());

	// Load the plugins the user added
	//loader.loadAll(mAdded);

	TQString oldPlaylist, newPlaylist;

	// first load all non playlist things
	for (TQStringList::Iterator i = mAdded.begin(); i != mAdded.end(); ++i)
	{
		NoatunLibraryInfo info = loader.getInfo(*i);
		if(info.type != "playlist")
			loader.loadAll(TQStringList(*i));
		else
			newPlaylist = (*i);
	}

	// Remove the plugins the user removed
	for (TQStringList::Iterator i = mDeleted.begin(); i != mDeleted.end(); ++i)
	{
		NoatunLibraryInfo info = loader.getInfo(*i);
		if(info.type != "playlist")
			loader.remove(*i);
		else
			oldPlaylist = *i;
	}

	// Loading normal plugins works the other way round!
	// If you unload a playlist it sets the global playlist pointer to NULL,
	// that also means you cannot first load the new and then unload the old one.
	if(!newPlaylist.isEmpty() && !oldPlaylist.isEmpty())
	{
		kdDebug(66666) << k_funcinfo << "Unloading " << oldPlaylist << endl;
		loader.remove(oldPlaylist);
		kdDebug(66666) << k_funcinfo << "Loading " << oldPlaylist << endl;
		loader.loadAll(TQStringList(newPlaylist));
	}


	// Round up the ones that weren't loaded right now, for saving in the configuration
	TQStringList specList(mAdded);

	TQValueList<NoatunLibraryInfo> loaded = loader.loaded();
	for(TQValueList<NoatunLibraryInfo>::Iterator i = loaded.begin(); i != loaded.end(); ++i)
	{
		if(!specList.contains((*i).specfile) && loader.isLoaded((*i).specfile))
			specList += (*i).specfile;
	}

	// Now we actually save
	loader.setModules(specList);

	mDeleted.clear();
	mAdded.clear();
}

void Plugins::showEvent(TQShowEvent *e)
{
	if(!shown)
	{
		shown = true;
		KMessageBox::information(this, i18n("<qt>Changing your playlist plugin will stop playback. Different playlists may use different methods of storing information, so after changing playlists you may have to recreate your playlist.</qt>"), TQString(), "Plugin warning");
	}
	CModule::showEvent(e);
}

#include "pluginmodule.moc"