summaryrefslogtreecommitdiff
path: root/lyrics.c
blob: 0cb8e6dcd1e9c687cb35f200aac70b01ea8ab63f (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
#include <string.h>
#include <fstream>

#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>

#include "lyrics.h"
#include "mg_item_gd.h"
#include "vdr_actions.h"
#include "mg_setup.h"
#include "vdr_player.h"

int mgLyrics::RunCommand(const string cmd) {
	int res=-1;
	if (access(cmd.c_str(),R_OK|X_OK)) return res;
	char *tmp;
	msprintf(&tmp,"%s \"%s\" \"%s\" \"%s\"",cmd.c_str(),
		playItem->getArtist().c_str(),
		playItem->getTitle().c_str(),
		playItem->getCachedFilename("lyrics.tmp").c_str());
	mgDebug(1,"muggle[%d]: lyrics: executing '%s'\n",getpid (), tmp);
	res=SystemExec(tmp,true); // run detached
	free(tmp);
	return res;
}

void
mgLyrics::LoadExternal() {
mgLog("LoadExternal");
	osd()->Message1(tr("Loading lyrics from internet..."));
	string script=the_setup.ConfigDirectory + "/scripts/muggle_getlyrics";
	if (RunCommand(script)==0) {
		state=lyricsLoading;
//		BlueAction=actNone;
//		SetHelpKeys();
		playItem->setCheckedForTmpLyrics(time(0));
	}
}

void
mgLyrics::SaveExternal() {
	const mgItemGd *item = PlayingItem();
	if (!item) return;
	string local=item->getCachedFilename("lyrics");
	string tmp=item->getCachedFilename("lyrics.tmp");
	PlayerControl()->CurrentItem()->resetHasLyricsFile();
	char *cmd;
	msprintf(&cmd, "mv \"%s\" \"%s\"", tmp.c_str(), local.c_str());
	mgDebug(1,"muggle[%d]: lyrics: Executing %s\n",getpid(), cmd);
	if (!SystemExec(cmd)) {
		BlueAction=actLoadExternalLyrics;
		SetHelpKeys();
	}
	free(cmd);
}

eOSState
mgLyrics::Process(eKeys key) {
	playItem=mutPlayingItem();
	long cl=playItem->getCheckedForTmpLyrics();
	LyricsState prevstate=state;
	if (displayItem!=playItem || cl>0 && cl<time(0)) {
		if (!access(playItem->getCachedFilename("lyrics.tmp.loading").c_str(),R_OK)) {
			state=lyricsLoading;
			playItem->setCheckedForTmpLyrics(time(0));
		} else if (!access(playItem->getCachedFilename("lyrics.tmp").c_str(),R_OK)) {
			state=lyricsLoaded;
			playItem->setCheckedForTmpLyrics(0);
		} else if (displayItem!=playItem) {
			if (!access(playItem->getCachedFilename("lyrics").c_str(),R_OK)) {
				state=lyricsSaved;
				playItem->setCheckedForTmpLyrics(0);
			} else {
				LoadExternal();
			}
		} else {
			state=lyricsSaved;
			playItem->setCheckedForTmpLyrics(0);
		}
	} 
	if (displayItem!=playItem || state!=prevstate) {
		BuildOsd();
		osd()->Display();
	}
	eOSState result = osContinue;
	switch (key) {
		case kBlue:
			if (BlueAction==actSaveExternalLyrics)
				SaveExternal();
			else if (BlueAction==actLoadExternalLyrics) {
				LoadExternal();
				BuildOsd();
				osd()->Display();
			}
			break;
		default:
			result = mgMenu::Process(key);
			break;
	}
	return result;
}

mgLyrics::mgLyrics() {
	playItem=0;
	displayItem=0;
	state=lyricsSaved;
}

string
mgLyrics::Title() const
{
	const mgItemGd *item = PlayingItem();
	if (!item) return "";
	return item->getArtist() + ":" + item->getTitle();
}

void
mgLyrics::BuildOsd () {
	playItem = mutPlayingItem();
	if (!playItem) return;
	RedAction=actBack2;
	GreenAction=actPrevTrack;
	YellowAction=actNextTrack;
	string loadfile="";
	switch (state) {
		case lyricsLoading:
			BlueAction=actNone;
			break;
		case lyricsLoaded:
			BlueAction=actSaveExternalLyrics;
			loadfile=playItem->getCachedFilename("lyrics.tmp");
			break;
		default:
			BlueAction=actLoadExternalLyrics;
			loadfile=playItem->getCachedFilename("lyrics");
			break;
	}
	InitOsd();
	if (!access(loadfile.c_str(),R_OK))
		osd()->AddFile(loadfile);
	displayItem=playItem;
}