lyrics: some code beautification + Lyrics::SwitchTo() corrections

This commit is contained in:
Andrzej Rybczak
2010-08-13 05:45:34 +02:00
parent a1cd5ae1aa
commit 3afa0028e6
2 changed files with 46 additions and 50 deletions

View File

@@ -68,10 +68,10 @@ void Lyrics::Resize()
void Lyrics::Update()
{
# ifdef HAVE_CURL_CURL_H
if (ReadyToTake)
if (isReadyToTake)
Take();
if (DownloadInProgress)
if (isDownloadInProgress)
{
w->Flush();
w->Refresh();
@@ -104,22 +104,23 @@ void Lyrics::SwitchTo()
itsScrollBegin = 0;
myOldScreen = myScreen;
myScreen = this;
// take lyrics if they were downloaded
if (isReadyToTake)
Take();
// for taking lyrics if they were downloaded
Update();
const MPD::Song *s = myOldScreen->CurrentSong();
if (s && !s->GetArtist().empty() && !s->GetTitle().empty())
if (const MPD::Song *s = myScreen->CurrentSong())
{
itsSong = *s;
Load();
}
else
return myOldScreen->SwitchTo();
myOldScreen = myScreen;
myScreen = this;
Global::RedrawHeader = 1;
if (!s->GetArtist().empty() && !s->GetTitle().empty())
{
itsSong = *s;
Load();
}
Global::RedrawHeader = 1;
}
}
std::basic_string<my_char_t> Lyrics::Title()
@@ -150,8 +151,8 @@ void *Lyrics::Download()
// if one of plugins is selected, try only this one,
// otherwise try all of them until one of them succeeds
bool fetcher_defined = Fetcher && *Fetcher;
for (LyricsFetcher **plugin = fetcher_defined ? Fetcher : lyricsPlugins; *plugin != 0; ++plugin)
bool fetcher_defined = itsFetcher && *itsFetcher;
for (LyricsFetcher **plugin = fetcher_defined ? itsFetcher : lyricsPlugins; *plugin != 0; ++plugin)
{
*w << "Fetching lyrics from " << fmtBold << (*plugin)->name() << fmtBoldEnd << "... ";
result = (*plugin)->fetch(artist, title);
@@ -174,7 +175,7 @@ void *Lyrics::Download()
else
*w << "\nLyrics weren't found.";
ReadyToTake = 1;
isReadyToTake = 1;
pthread_exit(0);
}
#endif // HAVE_CURL_CURL_H
@@ -182,7 +183,7 @@ void *Lyrics::Download()
void Lyrics::Load()
{
# ifdef HAVE_CURL_CURL_H
if (DownloadInProgress)
if (isDownloadInProgress)
return;
# endif // HAVE_CURL_CURL_H
@@ -192,7 +193,7 @@ void Lyrics::Load()
itsSong.Localize();
std::string file = locale_to_utf_cpy(itsSong.GetArtist()) + " - " + locale_to_utf_cpy(itsSong.GetTitle()) + ".txt";
EscapeUnallowedChars(file);
itsFilenamePath = Folder + "/" + file;
itsFilename = Folder + "/" + file;
mkdir(Folder.c_str()
# ifndef WIN32
@@ -203,7 +204,7 @@ void Lyrics::Load()
w->Clear();
w->Reset();
std::ifstream input(itsFilenamePath.c_str());
std::ifstream input(itsFilename.c_str());
if (input.is_open())
{
bool first = 1;
@@ -223,8 +224,8 @@ void Lyrics::Load()
else
{
# ifdef HAVE_CURL_CURL_H
pthread_create(&Downloader, 0, DownloadWrapper, this);
DownloadInProgress = 1;
pthread_create(&itsDownloader, 0, DownloadWrapper, this);
isDownloadInProgress = 1;
# else
*w << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into " << Folder << " directory (file syntax is \"$ARTIST - $TITLE.txt\") or recompile ncmpcpp with curl support.";
w->Flush();
@@ -247,7 +248,7 @@ void Lyrics::Edit()
if (Config.use_console_editor)
{
system(("/bin/sh -c \"" + Config.external_editor + " \\\"" + itsFilenamePath + "\\\"\"").c_str());
system(("/bin/sh -c \"" + Config.external_editor + " \\\"" + itsFilename + "\\\"\"").c_str());
// below is needed as screen gets cleared, but apparently
// ncurses doesn't know about it, so we need to reload main screen
endwin();
@@ -255,12 +256,12 @@ void Lyrics::Edit()
curs_set(0);
}
else
system(("nohup " + Config.external_editor + " \"" + itsFilenamePath + "\" > /dev/null 2>&1 &").c_str());
system(("nohup " + Config.external_editor + " \"" + itsFilename + "\" > /dev/null 2>&1 &").c_str());
}
void Lyrics::Save(const std::string &lyrics)
{
std::ofstream output(itsFilenamePath.c_str());
std::ofstream output(itsFilename.c_str());
if (output.is_open())
{
output << lyrics;
@@ -270,42 +271,38 @@ void Lyrics::Save(const std::string &lyrics)
void Lyrics::Refetch()
{
std::string file = locale_to_utf_cpy(itsSong.GetArtist()) + " - " + locale_to_utf_cpy(itsSong.GetTitle()) + ".txt";
EscapeUnallowedChars(file);
std::string path = Folder + "/" + file;
if (!remove(path.c_str()))
if (!remove(itsFilename.c_str()))
{
Load();
}
else
{
static const char msg[] = "Couldn't remove \"%s\": %s";
ShowMessage(msg, Shorten(TO_WSTRING(path), COLS-static_strlen(msg)-25).c_str(), strerror(errno));
ShowMessage(msg, Shorten(TO_WSTRING(itsFilename), COLS-static_strlen(msg)-25).c_str(), strerror(errno));
}
}
#ifdef HAVE_CURL_CURL_H
void Lyrics::ToggleFetcher()
{
if (Fetcher && *Fetcher)
++Fetcher;
if (itsFetcher && *itsFetcher)
++itsFetcher;
else
Fetcher = &lyricsPlugins[0];
if (*Fetcher)
ShowMessage("Using lyrics database: %s", (*Fetcher)->name());
itsFetcher = &lyricsPlugins[0];
if (*itsFetcher)
ShowMessage("Using lyrics database: %s", (*itsFetcher)->name());
else
ShowMessage("Using all lyrics databases");
}
void Lyrics::Take()
{
assert(ReadyToTake);
pthread_join(Downloader, 0);
assert(isReadyToTake);
pthread_join(itsDownloader, 0);
w->Flush();
w->Refresh();
DownloadInProgress = 0;
ReadyToTake = 0;
isDownloadInProgress = 0;
isReadyToTake = 0;
}
#endif // HAVE_CURL_CURL_H

View File

@@ -31,12 +31,10 @@ class Lyrics : public Screen<Scrollpad>
public:
Lyrics() : ReloadNP(0),
# ifdef HAVE_CURL_CURL_H
ReadyToTake(0), DownloadInProgress(0), Fetcher(0),
isReadyToTake(0), isDownloadInProgress(0), itsFetcher(0),
# endif // HAVE_CURL_CURL_H
itsScrollBegin(0) { }
~Lyrics() { }
virtual void Resize();
virtual void SwitchTo();
@@ -52,7 +50,6 @@ class Lyrics : public Screen<Scrollpad>
virtual List *GetList() { return 0; }
void Edit();
void Save(const std::string &lyrics);
void Refetch();
# ifdef HAVE_CURL_CURL_H
void ToggleFetcher();
@@ -66,18 +63,20 @@ class Lyrics : public Screen<Scrollpad>
private:
void Load();
std::string itsFilenamePath;
std::string itsFilename;
static const std::string Folder;
# ifdef HAVE_CURL_CURL_H
void *Download();
static void *DownloadWrapper(void *);
void Save(const std::string &lyrics);
void Take();
bool ReadyToTake;
bool DownloadInProgress;
pthread_t Downloader;
LyricsFetcher **Fetcher;
bool isReadyToTake;
bool isDownloadInProgress;
pthread_t itsDownloader;
LyricsFetcher **itsFetcher;
# endif // HAVE_CURL_CURL_H
size_t itsScrollBegin;