summaryrefslogtreecommitdiff
path: root/vdr_actions.c
blob: dd35831b07bdd1a4f0f58cf6a0af4013eccfe9ba (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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
/*!								-*- c++ -*-
 * \file   vdr_actions.c
 * \brief  Implements all actions for browsing media libraries within VDR
 *
 * \version $Revision: 1.27 $
 * \date    $Date: 2004-12-25 16:52:35 +0100 (Sat, 25 Dec 2004) $
 * \author  Wolfgang Rohdewald
 * \author  Responsible author: $Author: wr61 $
 *
 * $Id: vdr_actions.c 276 2004-12-25 15:52:35Z wr61 $
 */

#include <stdio.h>
#include <libintl.h>

#include <typeinfo>
#include <string>
#include <vector>
#include <assert.h>

#include <vdr/menuitems.h>
#define __STL_CONFIG_H
#include <vdr/tools.h>
#include <vdr/plugin.h>

#include "mg_setup.h"
#include "vdr_actions.h"
#include "vdr_player.h"
#include "lyrics.h"
#include <vdr/interface.h>

#include "mg_tools.h"
#include "mg_thread_sync.h"

static bool
IsEntry(mgActions i) {
	return i == actEntry;
}

void
mgAction::setHandle(unsigned int handle) {
	m_handle = handle;
}

eOSState
mgAction::ProcessKey(eKeys key) {
	eOSState result = Process(key);
	if (result != osUnknown)
		IgnoreNextEvent = true;
	return result;
}

class mgNone: public mgOsdItem
{
	public:
		void Notify() {};
		bool Enabled(mgActions on) { return false; }
		eOSState Process(eKeys key) { return osUnknown; }
};

class mgBack : public mgAction
{
	const char* ButtonName() { return tr("Parent"); }
	bool Execute() { Back(); return true; }
};

class mgBack2 : public mgAction
{
	const char* ButtonName() { return tr("Parent"); }
	bool Execute() { osd()->CloseMenu();Back(); return true; }
};

//! \brief used for normal data base items
class mgEntry : public mgOsdItem
{
	public:
		void Notify();
		bool Enabled(mgActions on) { return IsEntry(on);}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
		eOSState Process(eKeys key);
		bool Execute();
		eOSState Back();
};

class mgKeyItem : public mgAction, public cMenuEditStraItem
{
	public:
		mgKeyItem(const char *Name, int *Value, int NumStrings, const char *const *Strings) : cMenuEditStraItem(Name, Value, NumStrings, Strings) {}
		eOSState ProcessKey(eKeys key) { return mgAction::ProcessKey(key); }
		eOSState Process(eKeys key);
};

class mgBoolItem: public mgAction, public cMenuEditBoolItem
{
	public:
		mgBoolItem(const char *Name,int *Value) : cMenuEditBoolItem(Name, Value) {}
		eOSState ProcessKey(eKeys key) { return mgAction::ProcessKey(key); }
		eOSState Process(eKeys key);
};

class mgDoCollEntry : public mgEntry
{
	public:
		virtual eOSState Process(eKeys key);
};

class mgAddCollEntry : public mgDoCollEntry
{
	public:
		bool Execute();
};

class mgRemoveCollEntry : public mgDoCollEntry
{
	public:
		bool Execute();
};

void
mgAction::TryNotify() {
	if (IgnoreNextEvent)
		IgnoreNextEvent = false;
	else
		Notify();
}

eOSState
mgDoCollEntry::Process(eKeys key) {
	mgMenu *n = Selosd ()->newmenu;
	Selosd ()->newmenu = NULL;
	eOSState result = osContinue;
	switch (key) {
		case kBack:
			break;
		case kOk:
			Execute ();
			break;
		default:
								 // wrong key: stay in submenu
			Selosd ()->newmenu = n;
			result = osUnknown;
			break;
	}
	return result;
}

bool
mgAddCollEntry::Execute() {
	string target = selection()->getCurrentValue();
	Selosd()->default_collection = target;
	if (target == Selosd()->play_collection)
		if (!PlayerControl())
			collselection()->ClearCollection(target);

	osd()->Message1 ("Added %s entries",itos (Selosd()->moveselection->AddToCollection (target)).c_str());
	Selosd()->CollectionChanged(target,true);
	return true;
}

bool
mgRemoveCollEntry::Execute() {
	string target = selection()->getCurrentValue();
	int removed = Selosd()->moveselection->RemoveFromCollection (target);
	osd()->Message1 ("Removed %s entries",ltos(removed).c_str());
	Selosd()->CollectionChanged(target,false);
	return true;
}

void
mgAction::Notify() {
	m->SetHelpKeys(Type());
}

void
mgAction::SetMenu(mgMenu *menu) {
	m = menu;
	m_osd = m->osd();
}

void
mgAction::SetText(const char *text,bool copy) {
	cOsdItem *c = dynamic_cast<cOsdItem*>(this);
	if (!c)
		mgError("mgAction::SetText() on wrong type");
	c->SetText(text,copy);
}

const char *
mgAction::Text() {
	cOsdItem *c = dynamic_cast<cOsdItem*>(this);
	if (!c)
		mgError("mgAction::Text() on wrong type");
	return c->Text();
}

bool
mgAction::Enabled(mgActions on) {
	return true;
}

mgAction::mgAction() {
	m = 0;
	m_osd = 0;
	m_handle = 0;
	IgnoreNextEvent = false;
}

mgAction::~mgAction() {
}

class mgCommand : public mgOsdItem
{
	public:
		bool Enabled(mgActions on);
		virtual eOSState Process(eKeys key);
};

class mgActOrder : public mgOsdItem
{
	public:
		const char* MenuName(const unsigned int idx,const mgListItem* item);
		virtual eOSState Process(eKeys key);
		bool Execute();
};

const char*
mgActOrder::MenuName(const unsigned int idx,const mgListItem* item) {
	return strdup(item->value().c_str());
}

eOSState
mgActOrder::Process(eKeys key) {
	mgMenu *n = osd ()->newmenu;
	osd ()->newmenu = NULL;
	eOSState result = osContinue;
	switch (key) {
		case kBack:
			break;
		case kOk:
			if (Execute ())
				break;
		default:
			osd ()->newmenu = n; // wrong key: stay in submenu
			result = osUnknown;
			break;
	}
	return result;
}

bool
mgActOrder::Execute() {
	return Selosd()->SwitchSelection();
}

class mgPrevTrack : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* ButtonName() { return tr("Track-"); }
		bool Execute();
};

bool
mgPrevTrack::Execute() {
	mgPlayerControl *c = PlayerControl();
	if (c)
		c->Backward();
	return c;
}

class mgNextTrack : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* ButtonName() { return tr("Track+"); }
		bool Execute();
};

bool
mgNextTrack::Execute() {
	mgPlayerControl *c = PlayerControl();
	if (c)
		c->Forward();
	return c;
}

class mgToggleShuffle : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* MenuName(const unsigned int idx,const mgListItem* item);
		bool Execute();
};

const char*
mgToggleShuffle::MenuName(const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Change shuffle mode"));
}

bool
mgToggleShuffle::Execute() {
	mgPlayerControl *c = PlayerControl();
	if (c)
		c->ToggleShuffle();
	return c;
}

class mgShowLyrics : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* ButtonName() { return tr("Lyrics"); }
		bool Execute();
};

bool
mgShowLyrics::Execute() {
	osd()->newmenu = new mgLyrics;
	osd()->newposition = 0;
	return true;
}

class mgLoadExternalLyrics : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* ButtonName() { return tr("Load Lyrics"); }
};

class mgSaveExternalLyrics : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* ButtonName() { return tr("Save Lyrics"); }
};

class mgToggleLoop : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char* MenuName(const unsigned int idx,const mgListItem* item);
		bool Execute();
};

const char*
mgToggleLoop::MenuName(const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Change loop mode"));
}

bool
mgToggleLoop::Execute() {
	mgPlayerControl *c = PlayerControl();
	if (c)
		c->ToggleLoop();
	return c;
}

bool
mgCommand::Enabled(mgActions on) {
	return IsEntry(on);
}

mgSelection*
mgAction::playselection () {
	return Selosd()->playselection();
}

mgOsd*
mgAction::osd () {
	return m_osd;
}

mgSelOsd*
mgAction::Selosd () {
	return dynamic_cast<mgSelOsd *>(m_osd);
}

eOSState
mgAction::Back() {
	osd()->newmenu = NULL;
	return osContinue;
}

void
mgEntry::Notify() {
	selection()->setPosition(m_handle);
	selection()->gotoPosition();
	Selosd()->SaveState();
	mgAction::Notify();			 // only after selection is updated
}

const char *
mgEntry::MenuName(const unsigned int idx,const mgListItem* item) {
	char ct[20];
	unsigned int selcount = item->count();
	if (!selection()->inItems()) {
		char numct[20];
		sprintf(numct,"%u",selcount);
		memset(ct,' ',19);
		if (strlen(numct)<4)
			ct[6-strlen(numct)*2]=0;
		else
			ct[0]=0;
		strcat(ct,numct);
		strcat(ct," ");
		assert(strlen(ct)<20);
	}
	else
		ct[0]=0;
	char *result;
	if (selection()->isCollectionlist()) {
		if (item->value() == Selosd()->default_collection)
			msprintf(&result,"-> %s%s",ct,item->value().c_str());
		else
			msprintf(&result,"     %s%s",ct,item->value().c_str());
	}
	else if (selection()->inCollection())
		msprintf(&result,"%4d %s",idx,item->value().c_str());
	else if (selection()->isLanguagelist())
		msprintf(&result,"%s%s",ct,dgettext("iso_639",item->value().c_str()));
	else
		msprintf(&result,"%s%s",ct,item->value().c_str());
	return result;
}

bool
mgEntry::Execute() {
	if (selection()->inItems())
		m->ExecuteAction(actInstantPlay,Type());
	else {
		selection()->enter();
		osd()->forcerefresh = true;
	}
	return true;
}

eOSState
mgEntry::Process(eKeys key) {
	eOSState result = osUnknown;

								 // und 0 abfangen
	mgTree *menu = dynamic_cast<mgTree*>(m);
	switch (key) {
		case kOk:
		{
			if (menu)
				menu->TerminateIncrementalSearch( true );
			Execute();

			result = osContinue;
		} break;
		case k0...k9:
		{
			if (menu) {
				menu->UpdateIncrementalSearch( key );
				result = osContinue;
			}
		} break;
		case kBack:
		{
			if( menu && menu->UpdateIncrementalSearch( key ) ) {
								 // search is continued
				result = osContinue;
			}
			else {
								 // search is not active at all
				result = Back();
			}
		} break;
		default:
		{
			if( key != kNone ) {
				if (menu)
					menu->TerminateIncrementalSearch( true );
			}
			result = osUnknown;
		}
	}

	return result;
}

eOSState
mgEntry::Back() {
	osd()->forcerefresh = true;
	if (selection()) {
		if (!selection ()->leave ())
			osd()->newmenu = NULL;
	} else
	osd()->newmenu=NULL;
	return osContinue;
}

eOSState
mgCommand::Process(eKeys key) {
	mgMenu *n = osd ()->newmenu;
	osd ()->newmenu = NULL;
	eOSState result = osContinue;
	switch (key) {
		case kRed:
		case kGreen:
		case kYellow:
			if (strlen(ButtonName()))
				osd()->SetHotkeyAction(key,Type());
			else
				result=osUnknown;
			break;
		case kBack:
			break;
		case kOk:
			Execute ();
			break;
		default:
			osd ()->newmenu = n; // wrong key: stay in submenu
			result = osUnknown;
			break;
	}
	return result;
}

class mgExternal : public mgCommand
{
	public:
		const char *ButtonName();
		bool Execute();
		bool Enabled(mgActions on) { return true; }
	private:
		cCommand * Command();
};

class mgExternal0 : public mgExternal { };
class mgExternal1 : public mgExternal { };
class mgExternal2 : public mgExternal { };
class mgExternal3 : public mgExternal { };
class mgExternal4 : public mgExternal { };
class mgExternal5 : public mgExternal { };
class mgExternal6 : public mgExternal { };
class mgExternal7 : public mgExternal { };
class mgExternal8 : public mgExternal { };
class mgExternal9 : public mgExternal { };
class mgExternal10 : public mgExternal { };
class mgExternal11 : public mgExternal { };
class mgExternal12 : public mgExternal { };
class mgExternal13 : public mgExternal { };
class mgExternal14 : public mgExternal { };
class mgExternal15 : public mgExternal { };
class mgExternal16 : public mgExternal { };
class mgExternal17 : public mgExternal { };
class mgExternal18 : public mgExternal { };
class mgExternal19 : public mgExternal { };

const char*
mgExternal::ButtonName() {
	cCommand *command = Command();
	if (command) {
		return command->Title();
	}
	else
		return "";
}

cCommand *
mgExternal::Command() {
	cCommand *command = NULL;
	if (osd()->external_commands) {
		unsigned int idx = Type() - actExternal0;
		command = osd()->external_commands->Get (idx);
	}
	return command;
}

bool
mgExternal::Execute() {
	cCommand *command = Command();
	if (command) {
		bool confirmed = true;
		if (command->Confirm ()) {
			char *buffer;
			msprintf (&buffer, "%s?", command->Title ());
			confirmed = Interface->Confirm (buffer);
			free (buffer);
		}
		if (confirmed) {
			osd()->Message1 ("%s...", command->Title ());
			string m3u_file = selection ()->exportM3U ();
			if (!m3u_file.empty ()) {
				/*char *result = (char *)*/
				string quoted = "'" + m3u_file + "'";
				char prev[1000];
				if (!getcwd(prev,1000))
					mgError("cannot get current directory: %s", strerror(errno));
				if (chdir(the_setup.ToplevelDir))
					mgError("cannot change to directory %s",
						the_setup.ToplevelDir);
				command->Execute (quoted.c_str ());
				if (chdir(prev))
					mgError("cannot change to directory %s", prev);
				selection()->clearCache();
								 // the ext cmd could change the database
				osd()->forcerefresh = true;
				/* What to do? Recode cMenuText (not much)?
								if (result)
								{
							free( result );
										return AddSubMenu( new cMenuText( command->Title(), result ) );
								}
				*/
			}
		}
	}
	return true;
}

//! \brief select search order
class mgChooseOrder : public mgCommand
{
	public:
		bool Enabled(mgActions on=mgActions(0));
		virtual eOSState Process(eKeys key);
		bool Execute ();
		const char *ButtonName() { return tr("Order"); }
		const char *MenuName(const unsigned int idx,const mgListItem* item)
			{ return strdup(tr("Select an order")); }
};

bool
mgChooseOrder::Enabled(mgActions on) {
	bool result = !Selosd()->UsingCollection;
	result &= IsEntry(on);
	return result;
}

eOSState
mgChooseOrder::Process(eKeys key) {
	if (key == kOk) {
		osd()->CloseMenu();
		Execute();
		return osContinue;
	}
	else
		return mgCommand::Process(key);
}

bool
mgChooseOrder::Execute() {
	Selosd ()->newmenu = new mgSelMenuOrders;
	osd ()->newposition = Selosd()->getCurrentSelection();
	return true;

}

class mgEditOrder : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		eOSState Process(eKeys key);
		bool Execute () { osd ()->newmenu = new mgSelMenuOrder; return true; }
		const char *ButtonName() { return tr("Button$Edit"); }
};

eOSState
mgEditOrder::Process(eKeys key) {
	if (key == kOk) {
		Execute();
		return osContinue;
	}
	else
		return mgCommand::Process(key);
}

class mgCreateOrder : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		bool Execute ();
		const char *ButtonName() { return tr("Create"); }
};

bool
mgCreateOrder::Execute() {
	Selosd()->AddSelection();
	Selosd()->SaveState();
	osd()->forcerefresh = true;
	return true;
}

class mgDeleteOrder : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		bool Execute ();
		const char *ButtonName() { return tr("Button$Delete"); }
};

bool
mgDeleteOrder::Execute() {
	Selosd()->DeleteSelection();
	Selosd()->SaveState();
	osd()->forcerefresh = true;
	osd()->newposition = osd()->Current();
	return true;
}

//! \brief show the normal selection list
class mgShowList: public mgOsdItem
{
	public:
		bool Enabled(mgActions) { return true; }
		const char *ButtonName () { return tr("List"); }
		bool Execute() { osd()->newmenu=NULL; return true;}
};

//! \brief show the command submenu
class mgShowCommands: public mgOsdItem
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char *ButtonName () { return tr("Commands"); }
		bool Execute() { osd()->newmenu = new mgSubmenu;return true; }
};

//! \brief toggles between the normal and the collection selection
class mgToggleSelection:public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		bool Execute ();
		const char *ButtonName ();
};

const char *
mgToggleSelection::ButtonName () {
	if (Selosd ()->UsingCollection)
		return tr ("Browse");
	else
		return tr ("Collections");
}

bool
mgToggleSelection::Execute () {
	if (Selosd ()->UsingCollection)
		Selosd ()->UseNormalSelection ();
	else {
		Selosd ()->UseCollectionSelection ();
		selection()->clearCache();
	}
	selection()->Activate();
	osd()->newposition = selection ()->gotoPosition ();
	return true;
}

class mgCmdSync : public mgOsdItem
{
	public:
		bool Enabled(mgActions on) { return true; }
		eOSState ProcessKey(eKeys key);
		const char *ButtonName() { return tr("Synchronize database"); }
};

eOSState
mgCmdSync::ProcessKey(eKeys key) {
	if (key==kOk) {
		extern bool import();
		import();
		return osContinue;
	}
	return osUnknown;
}

//! \brief sets the default collection selection
class mgSetDefaultCollection:public mgCommand
{
	public:
		bool Enabled(mgActions on);
		bool Execute ();
		const char *ButtonName () {
			return tr ("Default");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
};

const char * mgSetDefaultCollection::MenuName(const unsigned int idx,const mgListItem* item) {
	char *b;
	msprintf (&b, tr("Set default to collection '%s'"),
		selection ()->getCurrentValue().c_str());
	return b;
}

bool
mgSetDefaultCollection::Enabled(mgActions on) {
	bool result = IsEntry(on);
	result &= (!Selosd()->DefaultCollectionSelected());
	result &= Selosd()->UsingCollection;
	result &= (selection ()->orderlevel () == 0);
	return result;
}

bool
mgSetDefaultCollection::Execute () {
	Selosd ()->default_collection = selection ()->getCurrentValue();
	osd()->Message1 (tr("Default collection is now '%s'"),
		Selosd ()->default_collection.c_str());
	return true;
}

class mgSetButton : public mgCommand
{
	public:
		bool Enabled(mgActions on) { return true; }
		const char *ButtonName() { return tr("Set"); }
};

//! \brief instant play
class mgInstantPlay : public mgCommand
{
	public:
		bool Execute ();
		const char *ButtonName () { return tr ("Instant play"); }
};

bool
mgInstantPlay::Execute() {
	Selosd()->PlayInstant(true);
	return true;
}

//! \brief add selected items to a collection
class mgAddAllToCollection:public mgCommand
{
	public:
		bool Enabled(mgActions on);
		bool Execute ();
		//! \brief adds the whole selection to a collection
		// \param collection the target collection. Default is the default collection
		const char *ButtonName () {
			return tr ("Add");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
	protected:
		void ExecuteMove();
};

const char *
mgAddAllToCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Add all to a collection"));
}

bool
mgAddAllToCollection::Execute() {
	// work on a copy, so we don't have to clear the cache of selection()
	// which would result in an osd()->forcerefresh which could scroll.
	Selosd()->moveselection = GenerateSelection(selection());
	ExecuteMove();
	return true;
}

void
mgAddAllToCollection::ExecuteMove() {
	if (osd() ->Menus.size()>1)
		osd ()->CloseMenu();	 // TODO Gebastel...
	char *b;
	msprintf(&b,tr("'%s' to collection"),selection()->getCurrentValue().c_str());
	osd ()->newmenu = new mgTreeAddToCollSelector(string(b));
	Selosd ()->collselection()->leave_all();
	osd ()->newposition = Selosd()->collselection()->getPosition();
	free(b);
}

//! \brief add selected items to default collection
class mgAddAllToDefaultCollection:public mgCommand
{
	public:
		bool Enabled(mgActions on);
		bool Execute ();
		const char *MenuName (const unsigned int idx,const mgListItem* item);
		//! \brief adds the whole selection to the default collection
		// \param collection the default collection.
		void ExecuteSelection (mgSelection *s);
};

const char *
mgAddAllToDefaultCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	char *b;
	msprintf (&b, tr ("Add all to '%s'"),
		Selosd ()->default_collection.c_str ());
	return b;
}

bool
mgAddAllToDefaultCollection::Execute() {
	mgSelection *sel = GenerateSelection(selection());
	ExecuteSelection(sel);
	delete sel;
	return true;
}

void
mgAddAllToDefaultCollection::ExecuteSelection (mgSelection *s) {
	string target = Selosd()->default_collection;
	if (target == Selosd()->play_collection)
		if (!PlayerControl())
			collselection()->ClearCollection(target);

	osd()->Message1 ("Added %s entries",itos (s->AddToCollection (target)).c_str());

	if (target == Selosd()->play_collection) {
		playselection()->clearCache();
		mgPlayerControl *c = PlayerControl();
		if (c)
			c->ReloadPlaylist();
		else
			Selosd()->PlayQueue();
	}
}

//! \brief add selected items to a collection
class mgAddThisToCollection:public mgAddAllToCollection
{
	public:
		bool Execute ();
		const char *MenuName (const unsigned int idx,const mgListItem* item);
};

bool
mgAddThisToCollection::Execute () {
	// work on a copy, so we don't have to clear the cache of selection()
	// which would result in an osd()->forcerefresh which could scroll.
	Selosd()->moveselection = GenerateSelection(selection());
	Selosd()->moveselection->enter();
	ExecuteMove();
	return true;
}

bool
mgAddAllToCollection::Enabled(mgActions on) {
	return IsEntry(on);
}

const char *
mgAddThisToCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Add to a collection"));
}

bool
mgAddAllToDefaultCollection::Enabled(mgActions on) {
	bool result = IsEntry(on);
	result &= (!Selosd()->DefaultCollectionSelected());
	return result;
}

//! \brief add selected items to default collection
class mgAddThisToDefaultCollection:public mgAddAllToDefaultCollection
{
	public:
		bool Execute ();
		const char *ButtonName () {
			return tr ("Add");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
};

bool
mgAddThisToDefaultCollection::Execute () {
	// work on a copy, so we don't have to clear the cache of selection()
	// which would result in an osd()->forcerefresh which could scroll.
	mgSelection *sel = GenerateSelection(selection());
	sel->enter ();
	ExecuteSelection(sel);
	delete sel;
	return true;
}

const char *
mgAddThisToDefaultCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	char *b;
	msprintf (&b, tr ("Add to '%s'"), Selosd ()->default_collection.c_str ());
	return b;
}

//! \brief remove selected items from default collection
class mgRemoveAllFromCollection:public mgCommand
{
	public:
		bool Enabled(mgActions on);
		bool Execute ();
		const char *MenuName (const unsigned int idx,const mgListItem* item);
	protected:
		void ExecuteRemove();
};

bool
mgRemoveAllFromCollection::Enabled(mgActions on) {
	return IsEntry(on);
}

bool
mgRemoveAllFromCollection::Execute () {
	Selosd()->moveselection = GenerateSelection(selection());
	ExecuteRemove();
	return true;
}

void
mgRemoveAllFromCollection::ExecuteRemove () {
	if (osd() ->Menus.size()>1)
		osd ()->CloseMenu();	 // TODO Gebastel...
	char *b;
	msprintf(&b,tr("Remove '%s' from collection"),Selosd()->moveselection->getListname().c_str());
	osd ()->newmenu = new mgTreeRemoveFromCollSelector(string(b));
	Selosd ()->collselection()->leave_all();
	osd ()->newposition = Selosd()->collselection()->getPosition();
	free(b);
}

const char *
mgRemoveAllFromCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Remove all from a collection"));
}

class mgClearCollection : public mgCommand
{
	public:
		bool Enabled(mgActions on);
		bool Execute ();
		const char *ButtonName () {
			return tr ("Clear");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
};

const char *
mgClearCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Clear the collection"));
}

bool
mgClearCollection::Enabled(mgActions on) {
	return selection()->isCollectionlist();
}

bool
mgClearCollection::Execute() {
	if (Interface->Confirm(tr("Clear the collection?"))) {
		string target = selection()->getCurrentValue();
		selection()->ClearCollection(target);
		Selosd()->CollectionChanged(target,false);
	}
	return true;
}

//! \brief remove selected items from default collection
class mgRemoveThisFromCollection:public mgRemoveAllFromCollection
{
	public:
		bool Execute ();
		const char *ButtonName () {
			return tr ("Remove");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
};

bool
mgRemoveThisFromCollection::Execute () {
	// work on a copy, so we don't have to clear the cache of selection()
	// which would result in an osd()->forcerefresh which could scroll.
	Selosd()->moveselection = GenerateSelection(selection());
	Selosd()->moveselection->enter();
	ExecuteRemove();
	return true;
}

const char *
mgRemoveThisFromCollection::MenuName (const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Remove from a collection"));
}

class mgEditAction : public mgAction
{
	public:
		mgEditAction() : mgAction() { memset(m_value,0,30); }
	protected:
		char m_value[30];
};

class mgCreate : public mgEditAction, public cMenuEditStrItem
{
	public:
		mgCreate(const char* mn);
		virtual bool Enabled(mgActions on)=0;
		void Notify();
		eOSState ProcessKey(eKeys key) { return mgAction::ProcessKey(key); }
		eOSState Process(eKeys key);
	protected:
		bool Editing();
};

mgCreate::mgCreate(const char *mn) : mgEditAction(), cMenuEditStrItem(mn,m_value,30,tr(FileNameChars)) {
}

bool
mgCreate::Editing() {
	return (strchr(cOsdItem::Text(),'[') && strchr(cOsdItem::Text(),']'));
}

void
mgCreate::Notify() {
	if (!Editing())
		m->SetHelpKeys();
}

eOSState
mgCreate::Process(eKeys key) {
	if (key == kOk) {
		if (Editing())
			Execute();
		else
			return cMenuEditStrItem::ProcessKey(kRight);
	}
	if (key != kYellow || Editing())
		return cMenuEditStrItem::ProcessKey(key);
	else
		return osUnknown;
}

class mgCreateCollection : public mgCreate
{
	public:
		mgCreateCollection();
		bool Enabled(mgActions on);
		bool Execute ();
		const char *MenuName (const unsigned int idx=0,const mgListItem* item=0);
};

mgCreateCollection::mgCreateCollection() : mgCreate(MenuName()) {
}

const char*
mgCreateCollection::MenuName(const unsigned int idx,const mgListItem* item) {
	return strdup(tr ("Create collection"));
}

bool
mgCreateCollection::Execute () {
	string name = trim(m_value);
	if (name.empty()) return false;
	bool created = selection ()->CreateCollection (name);
	if (created) {
		mgDebug(1,"created collection %s",name.c_str());
		Selosd()->default_collection = name;
		selection ()->clearCache();
		if (selection()->isCollectionlist()) {
			selection ()->setPosition(name);
		}
		osd()->forcerefresh = true;
		return true;
	}
	else {
		osd()->Message1 ("Collection '%s' NOT created", name.c_str());
		return false;
	}
}

bool
mgCreateCollection::Enabled(mgActions on) {
	return selection()->isCollectionlist();
}

//! \brief delete collection
class mgDeleteCollection:public mgCommand
{
	public:
		bool Execute ();
		bool Enabled(mgActions on);
		const char *ButtonName () {
			return tr ("Button$Delete");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item);
};

bool
mgDeleteCollection::Enabled(mgActions on) {
	bool result = IsEntry(on);
	result &= selection()->isCollectionlist();
	if (result) {
		string name = selection ()->getCurrentValue();
		result &= (name != Selosd()->play_collection);
	}
	return result;
}

const char* mgDeleteCollection::MenuName(const unsigned int idx,const mgListItem* item) {
	return strdup(tr("Delete the collection"));
}

bool
mgDeleteCollection::Execute () {
	if (!Interface->Confirm(tr("Delete the collection?"))) return false;
	string name = selection ()->getCurrentValue();
	if (selection ()->DeleteCollection (name)) {
		osd()->Message1 ("Collection '%s' deleted", name.c_str());
		mgDebug(1,"Deleted collection %s",name.c_str());
		selection ()->clearCache();
		osd()->forcerefresh = true;
		return true;
	}
	else {
		osd()->Message1 ("Collection '%s' NOT deleted", name.c_str());
		return false;
	}
}

//! \brief export item list for all selected items
class mgExportItemlist:public mgCommand
{
	public:
		bool Execute ();
		const char *ButtonName () {
			return tr ("Export");
		}
		const char *MenuName (const unsigned int idx,const mgListItem* item) {
			return strdup(tr ("Export track list"));
		}
};

bool
mgExportItemlist::Execute () {
	string m3u_file = selection ()->exportM3U ();
	osd()->Message1 ("written to %s", m3u_file.c_str());
	return true;
}

mgActions
mgAction::Type() {
	const type_info& t = typeid(*this);
	if (t == typeid(mgNone)) return actNone;
	if (t == typeid(mgChooseOrder)) return actChooseOrder;
	if (t == typeid(mgToggleSelection)) return actToggleSelection;
	if (t == typeid(mgClearCollection)) return actClearCollection;
	if (t == typeid(mgCreateCollection)) return actCreateCollection;
	if (t == typeid(mgInstantPlay)) return actInstantPlay;
	if (t == typeid(mgAddAllToCollection)) return actAddAllToCollection;
	if (t == typeid(mgAddAllToDefaultCollection)) return actAddAllToDefaultCollection;
	if (t == typeid(mgRemoveAllFromCollection)) return actRemoveAllFromCollection;
	if (t == typeid(mgDeleteCollection)) return actDeleteCollection;
	if (t == typeid(mgExportItemlist)) return actExportItemlist;
	if (t == typeid(mgAddCollEntry)) return actAddCollEntry;
	if (t == typeid(mgRemoveCollEntry)) return actRemoveCollEntry;
	if (t == typeid(mgAddThisToCollection)) return actAddThisToCollection;
	if (t == typeid(mgAddThisToDefaultCollection)) return actAddThisToDefaultCollection;
	if (t == typeid(mgRemoveThisFromCollection)) return actRemoveThisFromCollection;
	if (t == typeid(mgEntry)) return actEntry;
	if (t == typeid(mgSetButton)) return actSetButton;
	if (t == typeid(mgShowList)) return actShowList;
	if (t == typeid(mgShowCommands)) return actShowCommands;
	if (t == typeid(mgSetDefaultCollection)) return actSetDefaultCollection;
	if (t == typeid(mgActOrder)) return actOrder;
	if (t == typeid(mgToggleShuffle)) return actToggleShuffle;
	if (t == typeid(mgToggleLoop)) return actToggleLoop;
	if (t == typeid(mgCreateOrder)) return actCreateOrder;
	if (t == typeid(mgDeleteOrder)) return actDeleteOrder;
	if (t == typeid(mgEditOrder)) return actEditOrder;
	if (t == typeid(mgCmdSync)) return actSync;
	if (t == typeid(mgShowLyrics)) return actShowLyrics;
	if (t == typeid(mgPrevTrack)) return actPrevTrack;
	if (t == typeid(mgNextTrack)) return actNextTrack;
	if (t == typeid(mgExternal0)) return actExternal0;
	if (t == typeid(mgExternal1)) return actExternal1;
	if (t == typeid(mgExternal2)) return actExternal2;
	if (t == typeid(mgExternal3)) return actExternal3;
	if (t == typeid(mgExternal4)) return actExternal4;
	if (t == typeid(mgExternal5)) return actExternal5;
	if (t == typeid(mgExternal6)) return actExternal6;
	if (t == typeid(mgExternal7)) return actExternal7;
	if (t == typeid(mgExternal8)) return actExternal8;
	if (t == typeid(mgExternal9)) return actExternal9;
	if (t == typeid(mgExternal10)) return actExternal10;
	if (t == typeid(mgExternal11)) return actExternal11;
	if (t == typeid(mgExternal12)) return actExternal12;
	if (t == typeid(mgExternal13)) return actExternal13;
	if (t == typeid(mgExternal14)) return actExternal14;
	if (t == typeid(mgExternal15)) return actExternal15;
	if (t == typeid(mgExternal16)) return actExternal16;
	if (t == typeid(mgExternal17)) return actExternal17;
	if (t == typeid(mgExternal18)) return actExternal18;
	if (t == typeid(mgExternal19)) return actExternal19;
	return mgActions(0);
}

mgAction*
actGenerateKeyItem(const char *Name, int *Value, int NumStrings, const char * const * Strings) {
	return new mgKeyItem(Name,Value,NumStrings,Strings);
}

mgAction*
actGenerateBoolItem(const char *Name, int *Value) {
	return new mgBoolItem(Name,Value);
}

mgAction*
actGenerate(const mgActions action) {
	mgAction * result = NULL;
	switch (action) {
		case actNone:           result = new mgNone;break;
		case actChooseOrder:        result = new mgChooseOrder;break;
		case actToggleSelection:    result = new mgToggleSelection;break;
		case actClearCollection:    result = new mgClearCollection;break;
		case actCreateCollection:   result = new mgCreateCollection;break;
		case actInstantPlay:        result = new mgInstantPlay;break;
		case actAddAllToCollection: result = new mgAddAllToCollection;break;
		case actAddAllToDefaultCollection:  result = new mgAddAllToDefaultCollection;break;
		case actRemoveAllFromCollection:result = new mgRemoveAllFromCollection;break;
		case actDeleteCollection:   result = new mgDeleteCollection;break;
		case actExportItemlist: result = new mgExportItemlist;break;
		case actAddCollEntry:       result = new mgAddCollEntry;break;
		case actRemoveCollEntry:        result = new mgRemoveCollEntry;break;
		case actAddThisToCollection:    result = new mgAddThisToCollection;break;
		case actAddThisToDefaultCollection: result = new mgAddThisToDefaultCollection;break;
		case actRemoveThisFromCollection: result = new mgRemoveThisFromCollection;break;
		case actEntry: result = new mgEntry;break;
		case actSetButton: result = new mgSetButton;break;
		case actShowList:       result = new mgShowList;break;
		case actShowCommands:       result = new mgShowCommands;break;
		case actSync:           result = new mgCmdSync;break;
		case actSetDefaultCollection:   result = new mgSetDefaultCollection;break;
		case actOrder: result = new mgActOrder;break;
		case actToggleShuffle: result = new mgToggleShuffle;break;
		case actToggleLoop: result = new mgToggleLoop;break;
		case actUnused6: break;
		case actCreateOrder: result = new mgCreateOrder;break;
		case actDeleteOrder: result = new mgDeleteOrder;break;
		case actEditOrder: result = new mgEditOrder;break;
		case actPrevTrack: result = new mgPrevTrack;break;
		case actNextTrack: result = new mgNextTrack;break;
		case actShowLyrics: result = new mgShowLyrics;break;
		case actLoadExternalLyrics: result = new mgLoadExternalLyrics;break;
		case actSaveExternalLyrics: result = new mgSaveExternalLyrics;break;
		case actBack: result = new mgBack;break;
		case actBack2: result = new mgBack2;break;
		case actExternal0: result = new mgExternal0;break;
		case actExternal1: result = new mgExternal1;break;
		case actExternal2: result = new mgExternal2;break;
		case actExternal3: result = new mgExternal3;break;
		case actExternal4: result = new mgExternal4;break;
		case actExternal5: result = new mgExternal5;break;
		case actExternal6: result = new mgExternal6;break;
		case actExternal7: result = new mgExternal7;break;
		case actExternal8: result = new mgExternal8;break;
		case actExternal9: result = new mgExternal9;break;
		case actExternal10: result = new mgExternal10;break;
		case actExternal11: result = new mgExternal11;break;
		case actExternal12: result = new mgExternal12;break;
		case actExternal13: result = new mgExternal13;break;
		case actExternal14: result = new mgExternal14;break;
		case actExternal15: result = new mgExternal15;break;
		case actExternal16: result = new mgExternal16;break;
		case actExternal17: result = new mgExternal17;break;
		case actExternal18: result = new mgExternal18;break;
		case actExternal19: result = new mgExternal19;break;
	}
	return result;
}

mgSelection *
mgAction::selection() {
	if (Selosd())
		return Selosd()->selection();
	else
		return 0;
}

mgSelection *
mgAction::collselection() {
	return Selosd()->collselection();
}

eOSState
mgKeyItem::Process(eKeys key) {
	mgSelMenuOrder *menu = dynamic_cast<mgSelMenuOrder*>(m);
	if (key==kOk) {
		if (menu->ChangeSelection(key))
			return osContinue;
		else {
			menu->SaveSelection();
			osd ()->newmenu = NULL;
			return osContinue;
		}
	} else if (key==kBack)
	{
		osd ()->newmenu = NULL;
		return osContinue;
	}
	if (key==kUp || key==kDown)
		if (menu->ChangeSelection(key))
			return osContinue;
	return cMenuEditStraItem::ProcessKey(key);
}

eOSState
mgBoolItem::Process(eKeys key) {
	mgSelMenuOrder *menu = dynamic_cast<mgSelMenuOrder*>(m);
	if (key==kOk) {
		if (menu->ChangeSelection(key))
			return osContinue;
		else {
			menu->SaveSelection();
			osd ()->newmenu = NULL;
			return osContinue;
		}
	} else if (key==kBack)
	{
		osd ()->newmenu = NULL;
		return osContinue;
	}
	if (key==kUp || key==kDown)
		if (menu->ChangeSelection(key))
			return osContinue;
	return cMenuEditBoolItem::ProcessKey(key);
}