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

View File

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