change namespace Info into class

This commit is contained in:
Andrzej Rybczak
2009-02-14 22:25:44 +01:00
parent 17352e821c
commit 49abc4d42c
4 changed files with 126 additions and 106 deletions

View File

@@ -60,7 +60,7 @@ namespace Global
// extern Scrollpad *sHelp; // extern Scrollpad *sHelp;
// extern Scrollpad *sLyrics; // extern Scrollpad *sLyrics;
extern Scrollpad *sInfo; // extern Scrollpad *sInfo;
extern Window *wHeader; extern Window *wHeader;
extern Window *wFooter; extern Window *wFooter;
@@ -83,7 +83,7 @@ namespace Global
// extern std::string browsed_dir; // extern std::string browsed_dir;
extern std::string editor_browsed_dir; extern std::string editor_browsed_dir;
extern std::string editor_highlighted_dir; extern std::string editor_highlighted_dir;
extern std::string info_title; // extern std::string info_title;
extern NcmpcppScreen current_screen; extern NcmpcppScreen current_screen;
extern NcmpcppScreen prev_screen; extern NcmpcppScreen prev_screen;

View File

@@ -42,37 +42,46 @@ using namespace Global;
using std::string; using std::string;
using std::vector; using std::vector;
Scrollpad *Global::sInfo;
const string artists_folder = home_folder + "/.ncmpcpp/artists";
namespace
{
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
pthread_t artist_info_downloader; const std::string Info::Folder = home_folder + "/.ncmpcpp/artists";
bool artist_info_ready = 0; bool Info::ArtistReady = 0;
pthread_t Info::Downloader = 0;
void *GetArtistInfo(void *);
#endif #endif
void GetSongInfo(MPD::Song &, Scrollpad &); Info *myInfo = new Info;
const basic_buffer<my_char_t> &ShowTagInInfoScreen(const string &);
}
void Info::Init() void Info::Init()
{ {
sInfo = new Scrollpad(0, main_start_y, COLS, main_height, "", Config.main_color, brNone); w = new Scrollpad(0, main_start_y, COLS, main_height, "", Config.main_color, brNone);
sInfo->SetTimeout(ncmpcpp_window_timeout); w->SetTimeout(ncmpcpp_window_timeout);
} }
void Info::Resize() 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() void Info::GetSong()
{ {
if (wCurrent == sInfo) if (wCurrent == w)
{ {
wCurrent->Hide(); wCurrent->Hide();
current_screen = prev_screen; current_screen = prev_screen;
@@ -133,33 +142,23 @@ void Info::GetSong()
break; break;
} }
wPrev = wCurrent; wPrev = wCurrent;
wCurrent = sInfo; wCurrent = w;
prev_screen = current_screen; prev_screen = current_screen;
current_screen = csInfo; current_screen = csInfo;
redraw_header = 1; redraw_header = 1;
info_title = "Song info"; itsTitle = "Song info";
sInfo->Clear(); w->Clear();
GetSongInfo(*s, *sInfo); PrepareSong(*s);
sInfo->Flush(); w->Flush();
sInfo->Hide(); w->Hide();
} }
} }
#ifdef HAVE_CURL_CURL_H #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() void Info::GetArtist()
{ {
if (wCurrent == sInfo) if (wCurrent == w)
{ {
wCurrent->Hide(); wCurrent->Hide();
current_screen = prev_screen; current_screen = prev_screen;
@@ -193,11 +192,13 @@ void Info::GetArtist()
# endif // HAVE_TAGLIB_H # endif // HAVE_TAGLIB_H
) )
{ {
if (artist_info_downloader) if (Downloader && !ArtistReady)
{ {
ShowMessage("Artist's info is being downloaded..."); ShowMessage("Artist's info is being downloaded...");
return; return;
} }
else if (ArtistReady)
Update();
string *artist = new string(); string *artist = new string();
int id = ((Menu<MPD::Song> *)wCurrent)->Choice(); int id = ((Menu<MPD::Song> *)wCurrent)->Choice();
@@ -229,28 +230,25 @@ void Info::GetArtist()
if (!artist->empty()) if (!artist->empty())
{ {
wPrev = wCurrent; wPrev = wCurrent;
wCurrent = sInfo; wCurrent = w;
prev_screen = current_screen; prev_screen = current_screen;
current_screen = csInfo; current_screen = csInfo;
redraw_header = 1; redraw_header = 1;
info_title = "Artist's info - " + *artist; itsTitle = "Artist's info - " + *artist;
sInfo->Clear(); w->Clear();
sInfo->WriteXY(0, 0, 0, "Fetching artist's info..."); w->WriteXY(0, 0, 0, "Fetching artist's info...");
sInfo->Refresh(); w->Refresh();
if (!artist_info_downloader) if (!Downloader)
{ {
pthread_create(&artist_info_downloader, NULL, GetArtistInfo, artist); pthread_create(&Downloader, NULL, PrepareArtist, artist);
} }
} }
else else
delete artist; delete artist;
} }
} }
#endif // HVAE_CURL_CURL_H
namespace { void *Info::PrepareArtist(void *ptr)
#ifdef HAVE_CURL_CURL_H
void *GetArtistInfo(void *ptr)
{ {
string *strptr = static_cast<string *>(ptr); string *strptr = static_cast<string *>(ptr);
string artist = *strptr; string artist = *strptr;
@@ -262,8 +260,8 @@ void *GetArtistInfo(void *ptr)
ToLower(filename); ToLower(filename);
EscapeUnallowedChars(filename); EscapeUnallowedChars(filename);
const string fullpath = artists_folder + "/" + filename; const string fullpath = Folder + "/" + filename;
mkdir(artists_folder.c_str(), 0755); mkdir(Folder.c_str(), 0755);
string result; string result;
std::ifstream input(fullpath.c_str()); std::ifstream input(fullpath.c_str());
@@ -275,15 +273,15 @@ void *GetArtistInfo(void *ptr)
while (getline(input, line)) while (getline(input, line))
{ {
if (!first) if (!first)
*sInfo << "\n"; *myInfo->Main() << "\n";
utf_to_locale(line); utf_to_locale(line);
*sInfo << line; *myInfo->Main() << line;
first = 0; first = 0;
} }
input.close(); input.close();
sInfo->SetFormatting(fmtBold, "\n\nSimilar artists:\n", fmtBoldEnd, 0); myInfo->Main()->SetFormatting(fmtBold, "\n\nSimilar artists:\n", fmtBoldEnd, 0);
sInfo->SetFormatting(Config.color2, "\n * ", clEnd); myInfo->Main()->SetFormatting(Config.color2, "\n * ", clEnd);
artist_info_ready = 1; ArtistReady = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -310,8 +308,8 @@ void *GetArtistInfo(void *ptr)
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
*sInfo << "Error while fetching artist's info: " << curl_easy_strerror(code); *myInfo->Main() << "Error while fetching artist's info: " << curl_easy_strerror(code);
artist_info_ready = 1; ArtistReady = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -323,8 +321,8 @@ void *GetArtistInfo(void *ptr)
if (a != string::npos) if (a != string::npos)
{ {
EscapeHtml(result); EscapeHtml(result);
*sInfo << "Last.fm returned an error message: " << result; *myInfo->Main() << "Last.fm returned an error message: " << result;
artist_info_ready = 1; ArtistReady = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -370,24 +368,24 @@ void *GetArtistInfo(void *ptr)
if (save) if (save)
filebuffer << result; filebuffer << result;
utf_to_locale(result); utf_to_locale(result);
*sInfo << result; *myInfo->Main() << result;
if (save) if (save)
filebuffer << "\n\nSimilar artists:\n"; 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++) for (size_t i = 1; i < similar.size(); i++)
{ {
if (save) if (save)
filebuffer << "\n * " << similar[i] << " (" << urls[i] << ")"; filebuffer << "\n * " << similar[i] << " (" << urls[i] << ")";
utf_to_locale(similar[i]); utf_to_locale(similar[i]);
utf_to_locale(urls[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) if (save)
filebuffer << "\n\n" << urls.front(); filebuffer << "\n\n" << urls.front();
utf_to_locale(urls.front()); utf_to_locale(urls.front());
*sInfo << "\n\n" << urls.front(); *myInfo->Main() << "\n\n" << urls.front();
if (save) if (save)
{ {
@@ -398,12 +396,12 @@ void *GetArtistInfo(void *ptr)
output.close(); output.close();
} }
} }
artist_info_ready = 1; ArtistReady = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
#endif // HVAE_CURL_CURL_H #endif // HVAE_CURL_CURL_H
void GetSongInfo(MPD::Song &s, Scrollpad &info) void Info::PrepareSong(MPD::Song &s)
{ {
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
string path_to_file; string path_to_file;
@@ -415,32 +413,32 @@ void GetSongInfo(MPD::Song &s, Scrollpad &info)
s.SetComment(f.tag()->comment().to8Bit(1)); s.SetComment(f.tag()->comment().to8Bit(1));
# endif // HAVE_TAGLIB_H # endif // HAVE_TAGLIB_H
info << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd; *w << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd;
info << fmtBold << "Directory: " << fmtBoldEnd << Config.color2 << ShowTagInInfoScreen(s.GetDirectory()) << "\n\n" << clEnd; *w << fmtBold << "Directory: " << fmtBoldEnd << Config.color2 << ShowTag(s.GetDirectory()) << "\n\n" << clEnd;
info << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd; *w << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd;
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
if (!f.isNull()) if (!f.isNull())
{ {
info << fmtBold << "Bitrate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->bitrate() << " kbps\n" << clEnd; *w << fmtBold << "Bitrate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->bitrate() << " kbps\n" << clEnd;
info << fmtBold << "Sample rate: " << fmtBoldEnd << Config.color2 << f.audioProperties()->sampleRate() << " Hz\n" << clEnd; *w << 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 << "Channels: " << fmtBoldEnd << Config.color2 << (f.audioProperties()->channels() == 1 ? "Mono" : "Stereo") << "\n" << clDefault;
} }
else else
info << clDefault; *w << clDefault;
# endif // HAVE_TAGLIB_H # endif // HAVE_TAGLIB_H
info << fmtBold << "\nTitle: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetTitle()); *w << fmtBold << "\nTitle: " << fmtBoldEnd << ShowTag(s.GetTitle());
info << fmtBold << "\nArtist: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetArtist()); *w << fmtBold << "\nArtist: " << fmtBoldEnd << ShowTag(s.GetArtist());
info << fmtBold << "\nAlbum: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetAlbum()); *w << fmtBold << "\nAlbum: " << fmtBoldEnd << ShowTag(s.GetAlbum());
info << fmtBold << "\nYear: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetYear()); *w << fmtBold << "\nYear: " << fmtBoldEnd << ShowTag(s.GetYear());
info << fmtBold << "\nTrack: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetTrack()); *w << fmtBold << "\nTrack: " << fmtBoldEnd << ShowTag(s.GetTrack());
info << fmtBold << "\nGenre: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetGenre()); *w << fmtBold << "\nGenre: " << fmtBoldEnd << ShowTag(s.GetGenre());
info << fmtBold << "\nComposer: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetComposer()); *w << fmtBold << "\nComposer: " << fmtBoldEnd << ShowTag(s.GetComposer());
info << fmtBold << "\nPerformer: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetPerformer()); *w << fmtBold << "\nPerformer: " << fmtBoldEnd << ShowTag(s.GetPerformer());
info << fmtBold << "\nDisc: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetDisc()); *w << fmtBold << "\nDisc: " << fmtBoldEnd << ShowTag(s.GetDisc());
info << fmtBold << "\nComment: " << fmtBoldEnd << ShowTagInInfoScreen(s.GetComment()); *w << fmtBold << "\nComment: " << fmtBoldEnd << ShowTag(s.GetComment());
} }
const basic_buffer<my_char_t> &ShowTagInInfoScreen(const string &tag) const basic_buffer<my_char_t> &Info::ShowTag(const string &tag)
{ {
# ifdef _UTF8 # ifdef _UTF8
static WBuffer result; static WBuffer result;
@@ -451,7 +449,7 @@ const basic_buffer<my_char_t> &ShowTagInInfoScreen(const string &tag)
result << ToWString(tag); result << ToWString(tag);
return result; return result;
# else # else
return ShowTag(tag); return ::ShowTag(tag);
# endif # endif
} }
}

View File

@@ -23,18 +23,40 @@
#include "ncmpcpp.h" #include "ncmpcpp.h"
#include "mpdpp.h" #include "mpdpp.h"
#include "screen.h"
namespace Info class Info : public Screen<Scrollpad>
{ {
void Init(); public:
void Resize(); virtual void Init();
virtual void SwitchTo() { }
virtual void Resize();
virtual std::string Title();
virtual void Update();
void GetSong(); void GetSong();
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
bool Ready();
void GetArtist(); void GetArtist();
# endif # endif // HAVE_CURL_CURL_H
}
protected:
void PrepareSong(MPD::Song &);
static const basic_buffer<my_char_t> &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 #endif

View File

@@ -81,7 +81,6 @@ time_t Global::timer;
string Global::editor_browsed_dir = "/"; string Global::editor_browsed_dir = "/";
string Global::editor_highlighted_dir; string Global::editor_highlighted_dir;
string Global::info_title;
NcmpcppScreen Global::current_screen; NcmpcppScreen Global::current_screen;
NcmpcppScreen Global::prev_screen; NcmpcppScreen Global::prev_screen;
@@ -162,7 +161,7 @@ int main(int argc, char *argv[])
# endif // ENABLE_CLOCK # endif // ENABLE_CLOCK
myHelp->Init(); myHelp->Init();
Info::Init(); myInfo->Init();
myLyrics->Init(); myLyrics->Init();
if (Config.header_visibility) if (Config.header_visibility)
@@ -249,7 +248,7 @@ int main(int argc, char *argv[])
break; break;
# endif // HAVE_TAGLIB_H # endif // HAVE_TAGLIB_H
case csInfo: case csInfo:
screen_title = info_title; screen_title = myInfo->Title();
break; break;
case csSearcher: case csSearcher:
screen_title = mySearcher->Title(); screen_title = mySearcher->Title();
@@ -333,9 +332,10 @@ int main(int argc, char *argv[])
{ {
myLyrics->Update(); myLyrics->Update();
} }
# ifdef HAVE_CURL_CURL_H else if (current_screen == csInfo)
Info::Ready(); {
# endif myInfo->Update();
}
wCurrent->Display(); wCurrent->Display();
// redraw_screen = 0; // redraw_screen = 0;
@@ -499,7 +499,7 @@ int main(int argc, char *argv[])
mySearcher->Resize(); mySearcher->Resize();
myLibrary->Resize(); myLibrary->Resize();
myPlaylistEditor->Resize(); myPlaylistEditor->Resize();
Info::Resize(); myInfo->Resize();
myLyrics->Resize(); myLyrics->Resize();
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
@@ -1996,12 +1996,12 @@ int main(int argc, char *argv[])
} }
else if (Keypressed(input, Key.SongInfo)) else if (Keypressed(input, Key.SongInfo))
{ {
Info::GetSong(); myInfo->GetSong();
} }
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
else if (Keypressed(input, Key.ArtistInfo)) else if (Keypressed(input, Key.ArtistInfo))
{ {
Info::GetArtist(); myInfo->GetArtist();
} }
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
else if (Keypressed(input, Key.Lyrics)) else if (Keypressed(input, Key.Lyrics))