diff --git a/src/global.h b/src/global.h index 1dcb0e65..930255b2 100644 --- a/src/global.h +++ b/src/global.h @@ -60,7 +60,7 @@ namespace Global // extern Scrollpad *sHelp; // extern Scrollpad *sLyrics; - extern Scrollpad *sInfo; +// extern Scrollpad *sInfo; extern Window *wHeader; extern Window *wFooter; @@ -83,7 +83,7 @@ namespace Global // extern std::string browsed_dir; extern std::string editor_browsed_dir; extern std::string editor_highlighted_dir; - extern std::string info_title; +// extern std::string info_title; extern NcmpcppScreen current_screen; extern NcmpcppScreen prev_screen; diff --git a/src/info.cpp b/src/info.cpp index e9aa95fd..13548cae 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -42,37 +42,46 @@ using namespace Global; using std::string; using std::vector; -Scrollpad *Global::sInfo; +#ifdef HAVE_CURL_CURL_H +const std::string Info::Folder = home_folder + "/.ncmpcpp/artists"; +bool Info::ArtistReady = 0; +pthread_t Info::Downloader = 0; +#endif -const string artists_folder = home_folder + "/.ncmpcpp/artists"; - -namespace -{ -# ifdef HAVE_CURL_CURL_H - pthread_t artist_info_downloader; - bool artist_info_ready = 0; - - void *GetArtistInfo(void *); -# endif - - void GetSongInfo(MPD::Song &, Scrollpad &); - const basic_buffer &ShowTagInInfoScreen(const string &); -} +Info *myInfo = new Info; void Info::Init() { - sInfo = new Scrollpad(0, main_start_y, COLS, main_height, "", Config.main_color, brNone); - sInfo->SetTimeout(ncmpcpp_window_timeout); + w = new Scrollpad(0, main_start_y, COLS, main_height, "", Config.main_color, brNone); + w->SetTimeout(ncmpcpp_window_timeout); } void Info::Resize() { - sInfo->Resize(COLS, main_height); + w->Resize(COLS, main_height); +} + +std::string Info::Title() +{ + return itsTitle; +} + +void Info::Update() +{ +# ifdef HAVE_CURL_CURL_H + if (!ArtistReady) + return; + + pthread_join(Downloader, NULL); + w->Flush(); + Downloader = 0; + ArtistReady = 0; +# endif // HAVE_CURL_CURL_H } void Info::GetSong() { - if (wCurrent == sInfo) + if (wCurrent == w) { wCurrent->Hide(); current_screen = prev_screen; @@ -133,33 +142,23 @@ void Info::GetSong() break; } wPrev = wCurrent; - wCurrent = sInfo; + wCurrent = w; prev_screen = current_screen; current_screen = csInfo; redraw_header = 1; - info_title = "Song info"; - sInfo->Clear(); - GetSongInfo(*s, *sInfo); - sInfo->Flush(); - sInfo->Hide(); + itsTitle = "Song info"; + w->Clear(); + PrepareSong(*s); + w->Flush(); + w->Hide(); } } #ifdef HAVE_CURL_CURL_H -bool Info::Ready() -{ - if (!artist_info_ready) - return false; - pthread_join(artist_info_downloader, NULL); - sInfo->Flush(); - artist_info_downloader = 0; - artist_info_ready = 0; - return true; -} void Info::GetArtist() { - if (wCurrent == sInfo) + if (wCurrent == w) { wCurrent->Hide(); current_screen = prev_screen; @@ -193,11 +192,13 @@ void Info::GetArtist() # endif // HAVE_TAGLIB_H ) { - if (artist_info_downloader) + if (Downloader && !ArtistReady) { ShowMessage("Artist's info is being downloaded..."); return; } + else if (ArtistReady) + Update(); string *artist = new string(); int id = ((Menu *)wCurrent)->Choice(); @@ -229,28 +230,25 @@ void Info::GetArtist() if (!artist->empty()) { wPrev = wCurrent; - wCurrent = sInfo; + wCurrent = w; prev_screen = current_screen; current_screen = csInfo; redraw_header = 1; - info_title = "Artist's info - " + *artist; - sInfo->Clear(); - sInfo->WriteXY(0, 0, 0, "Fetching artist's info..."); - sInfo->Refresh(); - if (!artist_info_downloader) + itsTitle = "Artist's info - " + *artist; + w->Clear(); + w->WriteXY(0, 0, 0, "Fetching artist's info..."); + w->Refresh(); + if (!Downloader) { - pthread_create(&artist_info_downloader, NULL, GetArtistInfo, artist); + pthread_create(&Downloader, NULL, PrepareArtist, artist); } } else delete artist; } } -#endif // HVAE_CURL_CURL_H -namespace { -#ifdef HAVE_CURL_CURL_H -void *GetArtistInfo(void *ptr) +void *Info::PrepareArtist(void *ptr) { string *strptr = static_cast(ptr); string artist = *strptr; @@ -262,8 +260,8 @@ void *GetArtistInfo(void *ptr) ToLower(filename); EscapeUnallowedChars(filename); - const string fullpath = artists_folder + "/" + filename; - mkdir(artists_folder.c_str(), 0755); + const string fullpath = Folder + "/" + filename; + mkdir(Folder.c_str(), 0755); string result; std::ifstream input(fullpath.c_str()); @@ -275,15 +273,15 @@ void *GetArtistInfo(void *ptr) while (getline(input, line)) { if (!first) - *sInfo << "\n"; + *myInfo->Main() << "\n"; utf_to_locale(line); - *sInfo << line; + *myInfo->Main() << line; first = 0; } input.close(); - sInfo->SetFormatting(fmtBold, "\n\nSimilar artists:\n", fmtBoldEnd, 0); - sInfo->SetFormatting(Config.color2, "\n * ", clEnd); - artist_info_ready = 1; + myInfo->Main()->SetFormatting(fmtBold, "\n\nSimilar artists:\n", fmtBoldEnd, 0); + myInfo->Main()->SetFormatting(Config.color2, "\n * ", clEnd); + ArtistReady = 1; pthread_exit(NULL); } @@ -310,8 +308,8 @@ void *GetArtistInfo(void *ptr) if (code != CURLE_OK) { - *sInfo << "Error while fetching artist's info: " << curl_easy_strerror(code); - artist_info_ready = 1; + *myInfo->Main() << "Error while fetching artist's info: " << curl_easy_strerror(code); + ArtistReady = 1; pthread_exit(NULL); } @@ -323,8 +321,8 @@ void *GetArtistInfo(void *ptr) if (a != string::npos) { EscapeHtml(result); - *sInfo << "Last.fm returned an error message: " << result; - artist_info_ready = 1; + *myInfo->Main() << "Last.fm returned an error message: " << result; + ArtistReady = 1; pthread_exit(NULL); } @@ -370,24 +368,24 @@ void *GetArtistInfo(void *ptr) if (save) filebuffer << result; utf_to_locale(result); - *sInfo << result; + *myInfo->Main() << result; if (save) filebuffer << "\n\nSimilar artists:\n"; - *sInfo << fmtBold << "\n\nSimilar artists:\n" << fmtBoldEnd; + *myInfo->Main() << fmtBold << "\n\nSimilar artists:\n" << fmtBoldEnd; for (size_t i = 1; i < similar.size(); i++) { if (save) filebuffer << "\n * " << similar[i] << " (" << urls[i] << ")"; utf_to_locale(similar[i]); utf_to_locale(urls[i]); - *sInfo << "\n" << Config.color2 << " * " << clEnd << similar[i] << " (" << urls[i] << ")"; + *myInfo->Main() << "\n" << Config.color2 << " * " << clEnd << similar[i] << " (" << urls[i] << ")"; } if (save) filebuffer << "\n\n" << urls.front(); utf_to_locale(urls.front()); - *sInfo << "\n\n" << urls.front(); + *myInfo->Main() << "\n\n" << urls.front(); if (save) { @@ -398,12 +396,12 @@ void *GetArtistInfo(void *ptr) output.close(); } } - artist_info_ready = 1; + ArtistReady = 1; pthread_exit(NULL); } #endif // HVAE_CURL_CURL_H -void GetSongInfo(MPD::Song &s, Scrollpad &info) +void Info::PrepareSong(MPD::Song &s) { # ifdef HAVE_TAGLIB_H string path_to_file; @@ -415,32 +413,32 @@ void GetSongInfo(MPD::Song &s, Scrollpad &info) s.SetComment(f.tag()->comment().to8Bit(1)); # endif // HAVE_TAGLIB_H - info << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd; - info << fmtBold << "Directory: " << fmtBoldEnd << Config.color2 << ShowTagInInfoScreen(s.GetDirectory()) << "\n\n" << clEnd; - info << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd; + *w << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd; + *w << fmtBold << "Directory: " << fmtBoldEnd << Config.color2 << ShowTag(s.GetDirectory()) << "\n\n" << clEnd; + *w << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd; # ifdef HAVE_TAGLIB_H if (!f.isNull()) { - info << fmtBold << "Bitrate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->bitrate() << " kbps\n" << clEnd; - info << fmtBold << "Sample rate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->sampleRate() << " Hz\n" << clEnd; - info << fmtBold << "Channels: " << fmtBoldEnd << Config.color2 << (f.audioProperties()->channels() == 1 ? "Mono" : "Stereo") << "\n" << clDefault; + *w << fmtBold << "Bitrate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->bitrate() << " kbps\n" << clEnd; + *w << fmtBold << "Sample rate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->sampleRate() << " Hz\n" << clEnd; + *w << fmtBold << "Channels: " << fmtBoldEnd << Config.color2 << (f.audioProperties()->channels() == 1 ? "Mono" : "Stereo") << "\n" << clDefault; } else - info << clDefault; + *w << clDefault; # endif // HAVE_TAGLIB_H - info << fmtBold << "\nTitle: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetTitle()); - info << fmtBold << "\nArtist: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetArtist()); - info << fmtBold << "\nAlbum: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetAlbum()); - info << fmtBold << "\nYear: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetYear()); - info << fmtBold << "\nTrack: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetTrack()); - info << fmtBold << "\nGenre: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetGenre()); - info << fmtBold << "\nComposer: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetComposer()); - info << fmtBold << "\nPerformer: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetPerformer()); - info << fmtBold << "\nDisc: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetDisc()); - info << fmtBold << "\nComment: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetComment()); + *w << fmtBold << "\nTitle: " << fmtBoldEnd << ShowTag(s.GetTitle()); + *w << fmtBold << "\nArtist: " << fmtBoldEnd << ShowTag(s.GetArtist()); + *w << fmtBold << "\nAlbum: " << fmtBoldEnd << ShowTag(s.GetAlbum()); + *w << fmtBold << "\nYear: " << fmtBoldEnd << ShowTag(s.GetYear()); + *w << fmtBold << "\nTrack: " << fmtBoldEnd << ShowTag(s.GetTrack()); + *w << fmtBold << "\nGenre: " << fmtBoldEnd << ShowTag(s.GetGenre()); + *w << fmtBold << "\nComposer: " << fmtBoldEnd << ShowTag(s.GetComposer()); + *w << fmtBold << "\nPerformer: " << fmtBoldEnd << ShowTag(s.GetPerformer()); + *w << fmtBold << "\nDisc: " << fmtBoldEnd << ShowTag(s.GetDisc()); + *w << fmtBold << "\nComment: " << fmtBoldEnd << ShowTag(s.GetComment()); } -const basic_buffer &ShowTagInInfoScreen(const string &tag) +const basic_buffer &Info::ShowTag(const string &tag) { # ifdef _UTF8 static WBuffer result; @@ -451,7 +449,7 @@ const basic_buffer &ShowTagInInfoScreen(const string &tag) result << ToWString(tag); return result; # else - return ShowTag(tag); + return ::ShowTag(tag); # endif } -} + diff --git a/src/info.h b/src/info.h index ed16d4f0..c7be4cfd 100644 --- a/src/info.h +++ b/src/info.h @@ -23,18 +23,40 @@ #include "ncmpcpp.h" #include "mpdpp.h" +#include "screen.h" -namespace Info +class Info : public Screen { - void Init(); - void Resize(); - - void GetSong(); -# ifdef HAVE_CURL_CURL_H - bool Ready(); - void GetArtist(); -# endif -} + public: + virtual void Init(); + virtual void SwitchTo() { } + virtual void Resize(); + + virtual std::string Title(); + + virtual void Update(); + + void GetSong(); +# ifdef HAVE_CURL_CURL_H + void GetArtist(); +# endif // HAVE_CURL_CURL_H + + protected: + void PrepareSong(MPD::Song &); + static const basic_buffer &ShowTag(const std::string &); + +# ifdef HAVE_CURL_CURL_H + static void *PrepareArtist(void *); + + static const std::string Folder; + static bool ArtistReady; + static pthread_t Downloader; +# endif // HAVE_CURL_CURL_H + + std::string itsTitle; +}; + +extern Info *myInfo; #endif diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 5991ca72..1a9e5d08 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -81,7 +81,6 @@ time_t Global::timer; string Global::editor_browsed_dir = "/"; string Global::editor_highlighted_dir; -string Global::info_title; NcmpcppScreen Global::current_screen; NcmpcppScreen Global::prev_screen; @@ -162,7 +161,7 @@ int main(int argc, char *argv[]) # endif // ENABLE_CLOCK myHelp->Init(); - Info::Init(); + myInfo->Init(); myLyrics->Init(); if (Config.header_visibility) @@ -249,7 +248,7 @@ int main(int argc, char *argv[]) break; # endif // HAVE_TAGLIB_H case csInfo: - screen_title = info_title; + screen_title = myInfo->Title(); break; case csSearcher: screen_title = mySearcher->Title(); @@ -333,9 +332,10 @@ int main(int argc, char *argv[]) { myLyrics->Update(); } -# ifdef HAVE_CURL_CURL_H - Info::Ready(); -# endif + else if (current_screen == csInfo) + { + myInfo->Update(); + } wCurrent->Display(); // redraw_screen = 0; @@ -499,7 +499,7 @@ int main(int argc, char *argv[]) mySearcher->Resize(); myLibrary->Resize(); myPlaylistEditor->Resize(); - Info::Resize(); + myInfo->Resize(); myLyrics->Resize(); # ifdef HAVE_TAGLIB_H @@ -1996,12 +1996,12 @@ int main(int argc, char *argv[]) } else if (Keypressed(input, Key.SongInfo)) { - Info::GetSong(); + myInfo->GetSong(); } # ifdef HAVE_CURL_CURL_H else if (Keypressed(input, Key.ArtistInfo)) { - Info::GetArtist(); + myInfo->GetArtist(); } # endif // HAVE_CURL_CURL_H else if (Keypressed(input, Key.Lyrics))