summaryrefslogtreecommitdiff
path: root/lyrics.c
blob: e62c618dca46d14a2a1d9cbc96fd44d60b0ca770 (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
//								-*- c++ -*-

#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"

static int script_warned;

int mgLyrics::RunCommand(const string cmd) {
	int res=-1;
	if (access(cmd.c_str(),R_OK|X_OK)) {
		if (!script_warned) {
			mgDebug(1,"muggle[%d]: lyrics: Script %s not found\n",getpid(),cmd.c_str());
			script_warned = 1;
		}
		return res;
	}
	char *tmp;
#if VDRVERSNUM < 10504
	const char *backgr="&";
#else
	const char *backgr="";
#endif	
	msprintf(&tmp,"%s \"%s\" \"%s\" \"%s\" %s",cmd.c_str(),
		playItem->getArtist().c_str(),
		playItem->getTitle().c_str(),
		playItem->getCachedFilename("lyrics.tmp").c_str(),
		backgr);
	mgDebug(1,"muggle[%d]: lyrics: executing '%s'\n",getpid (), tmp);
#if VDRVERSNUM >= 10504
	res=SystemExec(tmp,true); // run detached
#else
	res=system(tmp); // SystemExec cannot yet run detached 	
#endif
	free(tmp);
	return res;
}

bool
mgLyrics::HasMoreVersions() {
	struct stat stbuf;
	if (!displayItem)
		return 0;
	return (!stat(displayItem->getCachedFilename("lyrics.tmp.new").c_str(),&stbuf));
}

void
mgLyrics::LoadExternal() {
mgLog("LoadExternal");
	if (!HasMoreVersions())
		osd()->Message1(tr("Loading lyrics from internet..."));
	string script=the_setup.ConfigDirectory + "/scripts/muggle_getlyrics";
	if (RunCommand(script)==0) {
		state=lyricsLoading;
		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 -f \"%s\" \"%s\" >/dev/null 2>&1", tmp.c_str(), local.c_str());
	mgDebug(1,"muggle[%d]: lyrics: Executing %s\n",getpid(), cmd);
	if (!SystemExec(cmd)) {
		BlueAction=actLoadExternalLyrics;
		SetHelpKeys();
	}
	free(cmd);
}

void
mgLyrics::ThrowTmpAway(const mgItemGd& item) {
	char *cmd;
	msprintf(&cmd,"rm -f \"%s\"",item.getCachedFilename("lyrics.tmp").c_str());
	mgDebug(5,"muggle[%d]: lyrics: ThrowTmpAway: Executing %s\n",getpid(), cmd);
	SystemExec(cmd);
	free(cmd);
	state=lyricsSaved;
}

eOSState
mgLyrics::Process(eKeys key) {
	playItem=mutPlayingItem();
	LyricsState prevstate=state;
	if (displayItem!=playItem && prevstate==lyricsAsking) 
		ThrowTmpAway(displayItem);
	long cl=playItem->getCheckedForTmpLyrics();
	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 {
			bool normfound=!access(playItem->getCachedFilename("lyrics").c_str(),R_OK);
			if (!access(playItem->getCachedFilename("lyrics.tmp").c_str(),R_OK)) {
				playItem->setCheckedForTmpLyrics(0);
				if (!normfound) {
					SaveExternal();
					state=lyricsSaved;
				} else {
					state=lyricsAsking;
					BuildOsd();
					osd()->Display();
					if (Interface->Confirm(tr("Save this version?"))) {
						SaveExternal();
						state=lyricsSaved;
					}  else {
						if (HasMoreVersions())
							LoadExternal();
						else {
							ThrowTmpAway(playItem);
							state=lyricsSaved;
						}
					}
					key = kNone;
				}
			} else if (displayItem!=playItem) {
				if (normfound) {
					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;
	}
	displayItem=playItem;
	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 lyricsAsking:
			BlueAction=actLoadExternalLyrics;
			loadfile=playItem->getCachedFilename("lyrics.tmp");
			break;
		default:
			BlueAction=actLoadExternalLyrics;
			loadfile=playItem->getCachedFilename("lyrics");
			break;
	}
	InitOsd();
	if (state == lyricsLoading) {
		osd()->AddText(tr("Loading lyrics from internet..."),0);
	} else {
		if (!access(loadfile.c_str(),R_OK))
			osd()->AddFile(loadfile);
	}
}