summaryrefslogtreecommitdiffstats
path: root/noatun/library/app.cpp
blob: 1f05502a63cc771e5a3f81e8d90c4564a34269ec (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include "cmodule.h"
#include "downloader.h"
#include "effects.h"
#include "effectview.h"
#include "engine.h"
#include "equalizer.h"
#include "equalizerview.h"
#include "vequalizer.h"
#include "app.h"
#include "playlist.h"
#include "pref.h"
#include "player.h"
#include "plugin.h"
#include "pluginloader.h"
#include "globalvideo.h"
#include "stdaction.h"

#include <common.h>
#include <dcopobject.h>
#include <dispatcher.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdefiledialog.h>
#include <kglobal.h>
#include <klibloader.h>
#include <klocale.h>
#include <kmainwindow.h>
#include <kmessagebox.h>
#include <knotifyclient.h>
#include <kpopupmenu.h>
#include <tqfile.h>
#include <tqimage.h>
#include <qiomanager.h>
#include <tqsessionmanager.h>
#include <tqtextstream.h>
#include <signal.h>
#include <kmimetype.h>


using std::string;
using std::vector;

// forgot the d pointer in Player TODO
static GlobalVideo *globalVideo;
struct NoatunApp::Private
{
	Effects *effects;
	VEqualizer *vequalizer;
};


NoatunApp::NoatunApp()
	: KUniqueApplication(true, true, true), mPluginMenu(0), mPluginActionMenu(0), mEqualizer(0)
{
	d = new Private;
	d->vequalizer=0;
	d->effects=0;

	Visualization::internalVis=true;

	mDownloader=new Downloader;

	Visualization::initDispatcher(); // 316

	showingInterfaces = true;

	// set the default config data
	// TODO: Maybe a first time wizard instead?
	TDEConfig *config=TDEGlobal::config(); // +
	config->setGroup(TQString()); // 1
	if (!config->readEntry("Modules").length())
	{
		TQStringList modules;
		modules.append("excellent.plugin");
		modules.append("splitplaylist.plugin");
		modules.append("marquis.plugin");
		modules.append("systray.plugin");
		modules.append("metatag.plugin");
		config->writeEntry("Modules", modules);
		config->sync();
	} // 1

	mPref=new NoatunPreferences(0L); // 115
	mPref->hide(); // 1
	mLibraryLoader = new LibraryLoader; // 0

	mLibraryLoader->add("dcopiface.plugin");

	new General(TQT_TQOBJECT(this)); // 25
	new Plugins(TQT_TQOBJECT(this)); // 149
//	new Types(TQT_TQOBJECT(this));

	mPlayer=new Player; // 139
	d->effects=new Effects; // 1
	d->vequalizer = new VEqualizer;
	d->vequalizer->init();
	mEqualizer=new Equalizer; // 0
	mEqualizer->init();  // 41
	mEffectView=new EffectView; // 859
	mEqualizerView=new EqualizerView; // 24

	TQTimer::singleShot(0, mDownloader, TQT_SLOT(start()));

	::globalVideo = new GlobalVideo;

	if(isRestored())
	{
		mLibraryLoader->add("marquis.plugin");
		static_cast<SessionManagement*>(mLibraryLoader->plugins().first())->restore();
	}
	else
	{
		loadPlugins(); //  1531
		if (!playlist())
		{
			KMessageBox::error(0,i18n("No playlist plugin was found. " \
				"Please make sure that Noatun was installed correctly."));
			TDEApplication::quit();
			delete this;
		}
		else
		{
			config->setGroup(TQString()); // 0
			player()->setVolume(config->readNumEntry("Volume", 100)); // 10
			player()->loop(config->readNumEntry("LoopStyle", (int)Player::None));
			mPlayer->engine()->setInitialized(); // 0

			switch (startupPlayMode())
			{
			case Restore:
				restoreEngineState();
				break;
			case Play:
				player()->play();
				break;
			case DontPlay:
			default:
				break;
			}
		}
	}
}

NoatunApp::~NoatunApp()
{
	saveEngineState();
	TDEConfig *config = TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("Volume", player()->volume());
	config->writeEntry("LoopStyle", player()->loopStyle());
	// for version continuity in the future
	config->writeEntry("Version", TQString(version())); // 1
	config->sync(); // 40

	mPlayer->stop();
	delete ::globalVideo;
	delete mLibraryLoader;
	delete mEffectView;
	delete mDownloader;
	delete mEqualizer;
	delete d->vequalizer;
	delete mEqualizerView;
	delete d->effects;

	delete mPlayer;
	delete mPref;
	config->sync();

	delete d;
}

TQCString NoatunApp::version() const
{
	return aboutData()->version().ascii();
}

inline bool logicalXOR(bool A, bool B)
{
	return (A || B) && !(A && B);
}

#define READBOOLOPT_EX(name, string, def, group, reversal) \
bool NoatunApp::name() const \
{ \
	TDEConfig *config=TDEGlobal::config(); \
	config->setGroup(group); \
	return logicalXOR(config->readBoolEntry(string, def), reversal); \
}

#define READBOOLOPT(name, string, def) \
	READBOOLOPT_EX(name, string, def, "", false)


READBOOLOPT(autoPlay, "AutoPlay", false)
READBOOLOPT(loopList, "LoopList", true)
READBOOLOPT(hackUpPlaylist, "HackUpPlaylist", true)
READBOOLOPT(fastMixer, "FastMixer", false)
READBOOLOPT(displayRemaining, "DisplayRemaining", false)
READBOOLOPT(clearOnOpen, "ClearOnOpen", false)
READBOOLOPT_EX(oneInstance, "MultipleInstances", true, "KDE", true)

#undef READBOOLOPT
#undef READBOOLOPT_EX

bool NoatunApp::clearOnStart() const
{
	return clearOnOpen();
}

int NoatunApp::startupPlayMode() const
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	return config->readNumEntry("StartupPlayMode", autoPlay() ? Play : Restore);
}

void NoatunApp::setStartupPlayMode(int mode)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("StartupPlayMode", mode);
	config->sync();
}

void NoatunApp::setHackUpPlaylist(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("HackUpPlaylist", b);
	config->sync();
}

void NoatunApp::setFastMixer(bool b)
{
	bool whatBefore=fastMixer();
	if (whatBefore!=b)
	{
		TDEConfig *config=TDEGlobal::config();
		config->setGroup(TQString());
		config->writeEntry("FastMixer", b);
		config->sync();
		player()->engine()->useHardwareMixer(b);
	}
}

void NoatunApp::setOneInstance(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup("KDE");
	config->writeEntry("MultipleInstances", !b);
	config->sync();
}

void NoatunApp::setLoopList(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("LoopList", b);
	TDEGlobal::config()->sync();
}

void NoatunApp::setAutoPlay(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("AutoPlay", b);
	TDEGlobal::config()->sync();
}

void NoatunApp::setRememberPositions(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("RememberPositions", b);
	TDEGlobal::config()->sync();
}

void NoatunApp::setSaveDirectory(const TQString &s)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writePathEntry("SaveDirectory", s);
	config->sync();
}

TQString NoatunApp::saveDirectory() const
{
	TDEConfig *c=TDEGlobal::config();
	c->setGroup(TQString());
	return c->readPathEntry("SaveDirectory", TQString(getenv("HOME")));
}

TQString NoatunApp::titleFormat() const
{
	TDEConfig *c=TDEGlobal::config();
	c->setGroup(TQString());
	return c->readEntry("TitleFormat", "$(\"[\"author\"] - \")$(title)$(\" (\"bitrate\"kbps)\")");
}

void NoatunApp::setTitleFormat(const TQString &format)
{
	TDEConfig *c=TDEGlobal::config();
	c->setGroup(TQString());
	return c->writeEntry("TitleFormat", format);
}

void NoatunApp::setClearOnStart(bool b)
{
	setClearOnOpen(b);
}

void NoatunApp::setClearOnOpen(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("ClearOnOpen", b);
	config->sync();
}

void NoatunApp::setDisplayRemaining(bool b)
{
	TDEConfig *config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("DisplayRemaining", b);
	config->sync();
}

void NoatunApp::toggleInterfaces()
{
	showingInterfaces ^= true;

	if(showingInterfaces)
		emit showYourself();
	else
		emit hideYourself();
}

void NoatunApp::showInterfaces()
{
	showingInterfaces = true;
	emit showYourself();
}

void NoatunApp::hideInterfaces()
{
	showingInterfaces = false;
	emit hideYourself();
}


int NoatunApp::pluginMenuAdd(const TQString &text, const TQObject *receiver, const char *member)
{
	return pluginActionMenu()->menuAdd(text, receiver, member);
}

void NoatunApp::pluginMenuRemove(int id)
{
	pluginActionMenu()->menuRemove(id);
}

NoatunStdAction::PluginActionMenu *NoatunApp::pluginActionMenu()
{
	if (!mPluginActionMenu)
		mPluginActionMenu = new NoatunStdAction::PluginActionMenu(TQT_TQOBJECT(this), "menu_actions");
	return mPluginActionMenu;
}

TDEPopupMenu *NoatunApp::pluginMenu()
{
	if(!mPluginMenu)
		mPluginMenu = pluginActionMenu()->popupMenu();
	return mPluginMenu;
}

int NoatunApp::newInstance()
{
	// TODO, this should call playlist()->handleArguments();
	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
	bool clear = clearOnOpen();
	bool playme=true;

	for (int i=0; i<args->count(); i++)
	{
		player()->openFile(args->url(i), clear, playme);
		playme=false;
		clear=false;
	}

	args->clear();
	return 0;
}

void NoatunApp::preferences()
{
	mPref->show();
	mPref->raise();
}

void NoatunApp::quit()
{
	TDEApplication::quit();
}

void NoatunApp::fileOpen()
{
	KURL::List files = KFileDialog::getOpenURLs(":mediadir", mimeTypes(), 0,
		i18n("Select File to Play"));

	if (files.count())
		mPlayer->openFile(files, clearOnOpen(), true);
}

void NoatunApp::effectView()
{
	mEffectView->show();
}

void NoatunApp::equalizerView()
{
	mEqualizerView->show();
}

VEqualizer * NoatunApp::vequalizer()
{
	return d->vequalizer;
}

Effects *NoatunApp::effects() const
{
	return d->effects;
}

TQString NoatunApp::mimeTypes()
{
	TQString mimeTypes;
	Arts::TraderQuery q;
	vector<Arts::TraderOffer> *results = q.query();
	vector<Arts::TraderOffer>::iterator i;

	for (i=results->begin(); i != results->end(); i++)
	{
		vector<string> *prop = (*i).getProperty("MimeType");

		vector<string>::iterator istr;
		for (istr = prop->begin(); istr != prop->end(); istr++)
		{
			if ( !(*istr).length())
				continue;

			const char *m = (*istr).c_str();
			if ((KServiceType::serviceType(m)) && !mimeTypes.contains(m))
			{
				mimeTypes += m;
				mimeTypes += ' ';
			}
		}
		delete prop;
	}
	delete results;
	return mimeTypes;
}

void NoatunApp::loadPlugins()
{
	mLibraryLoader->loadAll();
	// backup in case of trouble
	if(!mLibraryLoader->playlist())
	{
		kdDebug(66666) << k_funcinfo << "NO playlist plugin found!" << endl;
		//mLibraryLoader->add("splitplaylist.plugin");
	}
}

Playlist *NoatunApp::playlist() const
{
	return mLibraryLoader->playlist();
}

void NoatunApp::commitData(TQSessionManager &)
{
}

void NoatunApp::saveState(TQSessionManager &sm)
{
	TQStringList restartCommand = sm.restartCommand();
	sm.setRestartCommand( restartCommand );

	TDEApplication::saveState(sm);
}

// Deprecated
TQImage NoatunApp::readPNG(const TQString &filename)
{
	TQImageIO file(filename, "PNG");
	file.setGamma(0.0);
	file.read();
	return file.image();
}

void NoatunApp::restoreEngineState()
{
	TDEConfig* config = TDEGlobal::config();
	config->setGroup(TQString());
	int state = config->readNumEntry("EngineState", Arts::posPlaying);
	switch (state)
	{
		case Arts::posPlaying:
			player()->play();
			break;
		case Arts::posPaused:
			if (player()->isPlaying())
				player()->playpause();
			break;
		case Arts::posIdle:
		default:
			break;
	}
}

void NoatunApp::saveEngineState()
{
	TDEConfig* config=TDEGlobal::config();
	config->setGroup(TQString());
	config->writeEntry("EngineState", player()->engine()->state());
	// we don't sync here since it's done in the destructor afterwards anyway
}

#include "app.moc"