change namespace Lyrics into class

This commit is contained in:
Andrzej Rybczak
2009-02-14 21:30:14 +01:00
parent 6f09d3b592
commit 17352e821c
6 changed files with 198 additions and 166 deletions

View File

@@ -76,7 +76,7 @@ void Browser::SwitchTo()
std::string Browser::Title() std::string Browser::Title()
{ {
string result = "Browse: "; string result = "Browse: ";
result += TO_STRING(Scroller(itsBrowsedDir, COLS-volume_state.length(), itsScrollBeginning)); result += TO_STRING(Scroller(itsBrowsedDir, COLS-result.length()-volume_state.length(), itsScrollBeginning));
return result; return result;
} }

View File

@@ -59,7 +59,7 @@ namespace Global
// extern Menu<MPD::Song> *myPlaylistEditor->Content; // extern Menu<MPD::Song> *myPlaylistEditor->Content;
// extern Scrollpad *sHelp; // extern Scrollpad *sHelp;
extern Scrollpad *sLyrics; // extern Scrollpad *sLyrics;
extern Scrollpad *sInfo; extern Scrollpad *sInfo;
extern Window *wHeader; extern Window *wHeader;
@@ -76,7 +76,7 @@ namespace Global
// extern size_t browsed_dir_scroll_begin; // extern size_t browsed_dir_scroll_begin;
extern size_t main_start_y; extern size_t main_start_y;
extern size_t main_height; extern size_t main_height;
extern size_t lyrics_scroll_begin; // extern size_t lyrics_scroll_begin;
extern time_t timer; extern time_t timer;
@@ -99,7 +99,7 @@ namespace Global
extern bool messages_allowed; extern bool messages_allowed;
extern bool redraw_header; extern bool redraw_header;
extern bool reload_lyrics; // extern bool reload_lyrics;
extern std::string volume_state; extern std::string volume_state;
@@ -117,7 +117,7 @@ namespace Global
extern std::vector<int> vFoundPositions; extern std::vector<int> vFoundPositions;
extern int found_pos; extern int found_pos;
extern MPD::Song lyrics_song; // extern MPD::Song lyrics_song;
} }
#endif #endif

View File

@@ -21,11 +21,13 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fstream> #include <fstream>
#include "lyrics.h"
#include "browser.h" #include "browser.h"
#include "charset.h" #include "charset.h"
#include "global.h" #include "global.h"
#include "helpers.h" #include "helpers.h"
#include "lyrics.h"
#include "media_library.h" #include "media_library.h"
#include "playlist.h" #include "playlist.h"
#include "playlist_editor.h" #include "playlist_editor.h"
@@ -39,52 +41,50 @@ using namespace Global;
using std::vector; using std::vector;
using std::string; using std::string;
Scrollpad *Global::sLyrics; const std::string Lyrics::Folder = home_folder + "/.lyrics";
MPD::Song Global::lyrics_song; bool Lyrics::Reload = 0;
const string lyrics_folder = home_folder + "/.lyrics";
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
pthread_mutex_t Global::curl = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t Global::curl = PTHREAD_MUTEX_INITIALIZER;
bool Lyrics::Ready = 0;
pthread_t Lyrics::Downloader = 0;
#endif #endif
namespace Lyrics *myLyrics = new Lyrics;
{
# ifdef HAVE_CURL_CURL_H
pthread_t lyrics_downloader;
bool lyrics_ready;
# endif
void *GetLyrics(void *);
}
void Lyrics::Init() void Lyrics::Init()
{ {
sLyrics = 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);
sLyrics->SetTimeout(ncmpcpp_window_timeout); w->SetTimeout(ncmpcpp_window_timeout);
} }
void Lyrics::Resize() void Lyrics::Resize()
{ {
sLyrics->Resize(COLS, main_height); w->Resize(COLS, main_height);
} }
void Lyrics::Update() void Lyrics::Update()
{ {
if (!reload_lyrics) # ifdef HAVE_CURL_CURL_H
if (myLyrics->Ready)
myLyrics->Take();
# endif // HAVE_CURL_CURL_H
if (!Reload)
return; return;
const MPD::Song &s = myPlaylist->NowPlayingSong(); const MPD::Song &s = myPlaylist->NowPlayingSong();
if (!s.GetArtist().empty() && !s.GetTitle().empty()) if (!s.GetArtist().empty() && !s.GetTitle().empty())
Get(); SwitchTo();
else else
reload_lyrics = 0; Reload = 0;
} }
void Lyrics::Get() void Lyrics::SwitchTo()
{ {
if (wCurrent == sLyrics && !reload_lyrics) if (wCurrent == w && !Reload)
{ {
wCurrent->Hide(); wCurrent->Hide();
current_screen = prev_screen; current_screen = prev_screen;
@@ -107,7 +107,7 @@ void Lyrics::Get()
# endif // HAVE_TAGLIB_H # endif // HAVE_TAGLIB_H
} }
else if ( else if (
reload_lyrics Reload
|| (wCurrent == myPlaylist->Main() && !myPlaylist->Main()->Empty()) || (wCurrent == myPlaylist->Main() && !myPlaylist->Main()->Empty())
|| (wCurrent == myBrowser->Main() && myBrowser->Main()->Current().type == MPD::itSong) || (wCurrent == myBrowser->Main() && myBrowser->Main()->Current().type == MPD::itSong)
|| (wCurrent == mySearcher->Main() && !mySearcher->Main()->Current().first) || (wCurrent == mySearcher->Main() && !mySearcher->Main()->Current().first)
@@ -119,21 +119,26 @@ void Lyrics::Get()
) )
{ {
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
if (lyrics_downloader) if (Downloader && !Ready)
{ {
ShowMessage("Lyrics are being downloaded..."); ShowMessage("Lyrics are being downloaded...");
return; return;
} }
else if (Ready)
{
Take();
return;
}
# endif # endif
MPD::Song *s = 0; MPD::Song *s = 0;
int id; int id;
if (reload_lyrics) if (Reload)
{ {
current_screen = csPlaylist; current_screen = csPlaylist;
wCurrent = myPlaylist->Main(); wCurrent = myPlaylist->Main();
reload_lyrics = 0; Reload = 0;
id = myPlaylist->NowPlaying; id = myPlaylist->NowPlaying;
} }
else else
@@ -166,104 +171,37 @@ void Lyrics::Get()
} }
if (!s->GetArtist().empty() && !s->GetTitle().empty()) if (!s->GetArtist().empty() && !s->GetTitle().empty())
{ {
lyrics_scroll_begin = 0; itsScrollBegin = 0;
lyrics_song = *s; itsSong = *s;
wPrev = wCurrent; wPrev = wCurrent;
prev_screen = current_screen; prev_screen = current_screen;
wCurrent = sLyrics; wCurrent = w;
current_screen = csLyrics; current_screen = csLyrics;
redraw_header = 1; redraw_header = 1;
sLyrics->Clear(); w->Clear();
sLyrics->WriteXY(0, 0, 0, "Fetching lyrics..."); w->WriteXY(0, 0, 0, "Fetching lyrics...");
sLyrics->Refresh(); w->Refresh();
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
if (!lyrics_downloader) if (!Downloader)
{ {
pthread_create(&lyrics_downloader, NULL, GetLyrics, s); pthread_create(&Downloader, NULL, Get, &itsSong);
} }
# else # else
GetLyrics(s); Get(s);
sLyrics->Flush(); w->Flush();
# endif # endif
} }
} }
} }
#ifdef HAVE_CURL_CURL_H std::string Lyrics::Title()
bool Lyrics::Ready()
{ {
if (!lyrics_ready) string result = "Lyrics: ";
return false; result += TO_STRING(Scroller(itsSong.toString("%a - %t"), COLS-result.length()-volume_state.length(), itsScrollBegin));
pthread_join(lyrics_downloader, NULL); return result;
sLyrics->Flush();
lyrics_downloader = 0;
lyrics_ready = 0;
return true;
} }
namespace void *Lyrics::Get(void *song)
{
bool lyricwiki_not_found(const string &s)
{
return s == "Not found";
}
bool lyricsplugin_not_found(const string &s)
{
if (s.empty())
return true;
for (string::const_iterator it = s.begin(); it != s.end(); it++)
if (isprint(*it))
return false;
return true;
}
const LyricsPlugin lyricwiki =
{
"http://lyricwiki.org/api.php?artist=%artist%&song=%title%&fmt=xml",
"<lyrics>",
"</lyrics>",
lyricwiki_not_found
};
const LyricsPlugin lyricsplugin =
{
"http://www.lyricsplugin.com/winamp03/plugin/?artist=%artist%&title=%title%",
"<div id=\"lyrics\">",
"</div>",
lyricsplugin_not_found
};
const char *lyricsplugins_list[] =
{
"lyricwiki.org",
"lyricsplugin.com",
0
};
const LyricsPlugin *ChooseLyricsPlugin(int i)
{
switch (i)
{
case 0:
return &lyricwiki;
case 1:
return &lyricsplugin;
default:
return &lyricwiki;
}
}
}
const char *GetLyricsPluginName(int offset)
{
return lyricsplugins_list[offset];
}
#endif // HAVE_CURL_CURL_H
namespace {
void *GetLyrics(void *song)
{ {
string artist = static_cast<MPD::Song *>(song)->GetArtist(); string artist = static_cast<MPD::Song *>(song)->GetArtist();
string title = static_cast<MPD::Song *>(song)->GetTitle(); string title = static_cast<MPD::Song *>(song)->GetTitle();
@@ -273,9 +211,9 @@ void *GetLyrics(void *song)
string filename = artist + " - " + title + ".txt"; string filename = artist + " - " + title + ".txt";
EscapeUnallowedChars(filename); EscapeUnallowedChars(filename);
const string fullpath = lyrics_folder + "/" + filename; const string fullpath = Folder + "/" + filename;
mkdir(lyrics_folder.c_str(), 0755); mkdir(Folder.c_str(), 0755);
std::ifstream input(fullpath.c_str()); std::ifstream input(fullpath.c_str());
@@ -286,20 +224,20 @@ void *GetLyrics(void *song)
while (getline(input, line)) while (getline(input, line))
{ {
if (!first) if (!first)
*sLyrics << "\n"; *myLyrics->Main() << "\n";
utf_to_locale(line); utf_to_locale(line);
*sLyrics << line; *myLyrics->Main() << line;
first = 0; first = 0;
} }
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
lyrics_ready = 1; Ready = 1;
pthread_exit(NULL); pthread_exit(NULL);
# endif # endif
} }
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
CURLcode code; CURLcode code;
const LyricsPlugin *my_lyrics = ChooseLyricsPlugin(Config.lyrics_db); const Plugin *my_lyrics = ChoosePlugin(Config.lyrics_db);
string result; string result;
@@ -326,8 +264,8 @@ void *GetLyrics(void *song)
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
*sLyrics << "Error while fetching lyrics: " << curl_easy_strerror(code); *myLyrics->Main() << "Error while fetching lyrics: " << curl_easy_strerror(code);
lyrics_ready = 1; Ready = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -338,8 +276,8 @@ void *GetLyrics(void *song)
if (my_lyrics->not_found(result)) if (my_lyrics->not_found(result))
{ {
*sLyrics << "Not found"; *myLyrics->Main() << "Not found";
lyrics_ready = 1; Ready = 1;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -351,7 +289,7 @@ void *GetLyrics(void *song)
EscapeHtml(result); EscapeHtml(result);
Trim(result); Trim(result);
*sLyrics << utf_to_locale_cpy(result); *myLyrics->Main() << utf_to_locale_cpy(result);
std::ofstream output(fullpath.c_str()); std::ofstream output(fullpath.c_str());
if (output.is_open()) if (output.is_open())
@@ -359,12 +297,82 @@ void *GetLyrics(void *song)
output << result; output << result;
output.close(); output.close();
} }
lyrics_ready = 1; Ready = 1;
pthread_exit(NULL); pthread_exit(NULL);
# else # else
else else
*sLyrics << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support."; *myLyrics->Main() << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support.";
return NULL; return NULL;
# endif # endif
} }
#ifdef HAVE_CURL_CURL_H
void Lyrics::Take()
{
if (!Ready)
return;
pthread_join(Downloader, NULL);
w->Flush();
Downloader = 0;
Ready = 0;
} }
const char *Lyrics::GetPluginName(int offset)
{
return PluginsList[offset];
}
bool Lyrics::LyricWiki_NotFound(const string &s)
{
return s == "Not found";
}
bool Lyrics::LyricsPlugin_NotFound(const string &s)
{
if (s.empty())
return true;
for (string::const_iterator it = s.begin(); it != s.end(); it++)
if (isprint(*it))
return false;
return true;
}
const Lyrics::Plugin Lyrics::LyricWiki =
{
"http://lyricwiki.org/api.php?artist=%artist%&song=%title%&fmt=xml",
"<lyrics>",
"</lyrics>",
LyricWiki_NotFound
};
const Lyrics::Plugin Lyrics::LyricsPlugin =
{
"http://www.lyricsplugin.com/winamp03/plugin/?artist=%artist%&title=%title%",
"<div id=\"lyrics\">",
"</div>",
LyricsPlugin_NotFound
};
const char *Lyrics::PluginsList[] =
{
"lyricwiki.org",
"lyricsplugin.com",
0
};
const Lyrics::Plugin *Lyrics::ChoosePlugin(int i)
{
switch (i)
{
case 0:
return &LyricWiki;
case 1:
return &LyricsPlugin;
default:
return &LyricWiki;
}
}
#endif // HAVE_CURL_CURL_H

View File

@@ -22,34 +22,67 @@
#define _LYRICS_H #define _LYRICS_H
#include "ncmpcpp.h" #include "ncmpcpp.h"
#include "mpdpp.h"
namespace Lyrics #include "screen.h"
{
void Init();
void Resize();
void Update();
void Get();
# ifdef HAVE_CURL_CURL_H
bool Ready();
# endif // HAVE_CURL_CURL_H
}
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
# include <pthread.h> # include <pthread.h>
# include "curl/curl.h" # include "curl/curl.h"
#endif
struct LyricsPlugin class Lyrics : public Screen<Scrollpad>
{ {
struct Plugin
{
const char *url; const char *url;
const char *tag_open; const char *tag_open;
const char *tag_close; const char *tag_close;
bool (*not_found)(const std::string &); bool (*not_found)(const std::string &);
};
public:
Lyrics() : itsScrollBegin(0) { }
~Lyrics() { }
virtual void Init();
virtual void Resize();
virtual void SwitchTo();
virtual std::string Title();
virtual void Update();
static bool Reload;
# ifdef HAVE_CURL_CURL_H
static const char *GetPluginName(int offset);
# endif // HAVE_CURL_CURL_H
protected:
static void *Get(void *);
static const std::string Folder;
# ifdef HAVE_CURL_CURL_H
void Take();
static const Plugin *ChoosePlugin(int);
static bool LyricWiki_NotFound(const std::string &);
static bool LyricsPlugin_NotFound(const std::string &);
static bool Ready;
static pthread_t Downloader;
static const char *PluginsList[];
static const Plugin LyricWiki;
static const Plugin LyricsPlugin;
# endif // HAVE_CURL_CURL_H
size_t itsScrollBegin;
MPD::Song itsSong;
}; };
const char *GetLyricsPluginName(int); extern Lyrics *myLyrics;
#endif // HAVE_CURL_CURL_H
#endif #endif

View File

@@ -75,7 +75,7 @@ int Global::lock_statusbar_delay = -1;
size_t Global::main_start_y; size_t Global::main_start_y;
size_t Global::main_height; size_t Global::main_height;
size_t Global::lyrics_scroll_begin = 0; //size_t Global::lyrics_scroll_begin = 0;
time_t Global::timer; time_t Global::timer;
@@ -93,7 +93,6 @@ bool Global::block_item_list_update = 0;
bool Global::messages_allowed = 0; bool Global::messages_allowed = 0;
bool Global::redraw_header = 1; bool Global::redraw_header = 1;
bool Global::reload_lyrics = 0;
vector<int> Global::vFoundPositions; vector<int> Global::vFoundPositions;
int Global::found_pos = 0; int Global::found_pos = 0;
@@ -164,7 +163,7 @@ int main(int argc, char *argv[])
myHelp->Init(); myHelp->Init();
Info::Init(); Info::Init();
Lyrics::Init(); myLyrics->Init();
if (Config.header_visibility) if (Config.header_visibility)
{ {
@@ -259,7 +258,7 @@ int main(int argc, char *argv[])
screen_title = myLibrary->Title(); screen_title = myLibrary->Title();
break; break;
case csLyrics: case csLyrics:
screen_title = "Lyrics: "; screen_title = myLyrics->Title();
break; break;
case csPlaylistEditor: case csPlaylistEditor:
screen_title = myPlaylistEditor->Title(); screen_title = myPlaylistEditor->Title();
@@ -278,14 +277,6 @@ int main(int argc, char *argv[])
wHeader->Bold(1); wHeader->Bold(1);
wHeader->WriteXY(0, 0, 1, "%s", screen_title.c_str()); wHeader->WriteXY(0, 0, 1, "%s", screen_title.c_str());
wHeader->Bold(0); wHeader->Bold(0);
if (current_screen == csLyrics)
{
wHeader->Bold(1);
*wHeader << Scroller(lyrics_song.toString("%a - %t"), max_allowed_title_length, lyrics_scroll_begin);
wHeader->Bold(0);
}
} }
else else
{ {
@@ -340,11 +331,10 @@ int main(int argc, char *argv[])
# endif # endif
if (current_screen == csLyrics) if (current_screen == csLyrics)
{ {
Lyrics::Update(); myLyrics->Update();
} }
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
if (!Info::Ready()) Info::Ready();
Lyrics::Ready();
# endif # endif
wCurrent->Display(); wCurrent->Display();
@@ -510,7 +500,7 @@ int main(int argc, char *argv[])
myLibrary->Resize(); myLibrary->Resize();
myPlaylistEditor->Resize(); myPlaylistEditor->Resize();
Info::Resize(); Info::Resize();
Lyrics::Resize(); myLyrics->Resize();
# ifdef HAVE_TAGLIB_H # ifdef HAVE_TAGLIB_H
myTinyTagEditor->Resize(); myTinyTagEditor->Resize();
@@ -1187,10 +1177,10 @@ int main(int argc, char *argv[])
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
else if (Keypressed(input, Key.ToggleLyricsDB)) else if (Keypressed(input, Key.ToggleLyricsDB))
{ {
const char *current_lyrics_plugin = GetLyricsPluginName(++Config.lyrics_db); const char *current_lyrics_plugin = Lyrics::GetPluginName(++Config.lyrics_db);
if (!current_lyrics_plugin) if (!current_lyrics_plugin)
{ {
current_lyrics_plugin = GetLyricsPluginName(Config.lyrics_db = 0); current_lyrics_plugin = Lyrics::GetPluginName(Config.lyrics_db = 0);
} }
ShowMessage("Using lyrics database: %s", current_lyrics_plugin); ShowMessage("Using lyrics database: %s", current_lyrics_plugin);
} }
@@ -2016,7 +2006,7 @@ int main(int argc, char *argv[])
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
else if (Keypressed(input, Key.Lyrics)) else if (Keypressed(input, Key.Lyrics))
{ {
Lyrics::Get(); myLyrics->SwitchTo();
} }
else if (Keypressed(input, Key.Help)) else if (Keypressed(input, Key.Help))
{ {

View File

@@ -25,6 +25,7 @@
#include "charset.h" #include "charset.h"
#include "global.h" #include "global.h"
#include "helpers.h" #include "helpers.h"
#include "lyrics.h"
#include "media_library.h" #include "media_library.h"
#include "playlist.h" #include "playlist.h"
#include "playlist_editor.h" #include "playlist_editor.h"
@@ -317,7 +318,7 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *)
mvwhline(wFooter->Raw(), 0, 0, 0, wFooter->GetWidth()); mvwhline(wFooter->Raw(), 0, 0, 0, wFooter->GetWidth());
if (Config.now_playing_lyrics && !Config.repeat_one_mode && current_screen == csLyrics && prev_screen == csPlaylist) if (Config.now_playing_lyrics && !Config.repeat_one_mode && current_screen == csLyrics && prev_screen == csPlaylist)
reload_lyrics = 1; Lyrics::Reload = 1;
} }
playing_song_scroll_begin = 0; playing_song_scroll_begin = 0;