lyrics: general code refactoring
This commit is contained in:
189
src/lyrics.cpp
189
src/lyrics.cpp
@@ -18,8 +18,8 @@
|
|||||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
# include <io.h>
|
# include <io.h>
|
||||||
#else
|
#else
|
||||||
@@ -27,23 +27,16 @@
|
|||||||
#endif // WIN32
|
#endif // WIN32
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include "lyrics.h"
|
|
||||||
#include "lyrics_fetcher.h"
|
|
||||||
|
|
||||||
#include "browser.h"
|
#include "browser.h"
|
||||||
#include "charset.h"
|
#include "charset.h"
|
||||||
#include "curl_handle.h"
|
#include "curl_handle.h"
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
|
#include "lyrics.h"
|
||||||
#include "media_library.h"
|
#include "lyrics_fetcher.h"
|
||||||
#include "playlist.h"
|
#include "playlist.h"
|
||||||
#include "playlist_editor.h"
|
|
||||||
#include "search_engine.h"
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "song.h"
|
#include "song.h"
|
||||||
#include "status.h"
|
|
||||||
#include "tag_editor.h"
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
# define LYRICS_FOLDER HOME_FOLDER"\\lyrics\\"
|
# define LYRICS_FOLDER HOME_FOLDER"\\lyrics\\"
|
||||||
@@ -57,12 +50,10 @@ using Global::myScreen;
|
|||||||
using Global::myOldScreen;
|
using Global::myOldScreen;
|
||||||
|
|
||||||
const std::string Lyrics::Folder = home_path + LYRICS_FOLDER;
|
const std::string Lyrics::Folder = home_path + LYRICS_FOLDER;
|
||||||
|
bool Lyrics::ReloadNP = 0;
|
||||||
bool Lyrics::Reload = 0;
|
|
||||||
|
|
||||||
#ifdef HAVE_CURL_CURL_H
|
#ifdef HAVE_CURL_CURL_H
|
||||||
bool Lyrics::Ready = 0;
|
bool Lyrics::ReadyToTake = 0;
|
||||||
|
|
||||||
pthread_t *Lyrics::Downloader = 0;
|
pthread_t *Lyrics::Downloader = 0;
|
||||||
#endif // HAVE_CURL_CURL_H
|
#endif // HAVE_CURL_CURL_H
|
||||||
|
|
||||||
@@ -84,7 +75,7 @@ void Lyrics::Resize()
|
|||||||
void Lyrics::Update()
|
void Lyrics::Update()
|
||||||
{
|
{
|
||||||
# ifdef HAVE_CURL_CURL_H
|
# ifdef HAVE_CURL_CURL_H
|
||||||
if (Ready)
|
if (ReadyToTake)
|
||||||
Take();
|
Take();
|
||||||
|
|
||||||
if (Downloader)
|
if (Downloader)
|
||||||
@@ -93,14 +84,22 @@ void Lyrics::Update()
|
|||||||
w->Refresh();
|
w->Refresh();
|
||||||
}
|
}
|
||||||
# endif // HAVE_CURL_CURL_H
|
# endif // HAVE_CURL_CURL_H
|
||||||
|
if (ReloadNP)
|
||||||
if (Reload)
|
{
|
||||||
SwitchTo();
|
if (const MPD::Song *s = myPlaylist->NowPlayingSong())
|
||||||
|
{
|
||||||
|
Global::RedrawHeader = 1;
|
||||||
|
itsScrollBegin = 0;
|
||||||
|
itsSong = *s;
|
||||||
|
Load();
|
||||||
|
}
|
||||||
|
ReloadNP = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lyrics::SwitchTo()
|
void Lyrics::SwitchTo()
|
||||||
{
|
{
|
||||||
if (myScreen == this && !Reload)
|
if (myScreen == this)
|
||||||
{
|
{
|
||||||
myOldScreen->SwitchTo();
|
myOldScreen->SwitchTo();
|
||||||
}
|
}
|
||||||
@@ -109,88 +108,23 @@ void Lyrics::SwitchTo()
|
|||||||
if (!isInitialized)
|
if (!isInitialized)
|
||||||
Init();
|
Init();
|
||||||
|
|
||||||
# ifdef HAVE_CURL_CURL_H
|
if (hasToBeResized)
|
||||||
if (Downloader && !Ready)
|
Resize();
|
||||||
|
|
||||||
|
itsScrollBegin = 0;
|
||||||
|
|
||||||
|
myOldScreen = myScreen;
|
||||||
|
myScreen = this;
|
||||||
|
|
||||||
|
Update();
|
||||||
|
|
||||||
|
if (const MPD::Song *s = myOldScreen->CurrentSong())
|
||||||
{
|
{
|
||||||
if (hasToBeResized)
|
|
||||||
Resize();
|
|
||||||
|
|
||||||
itsScrollBegin = 0;
|
|
||||||
|
|
||||||
myOldScreen = myScreen;
|
|
||||||
myScreen = this;
|
|
||||||
|
|
||||||
Global::RedrawHeader = 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (Ready)
|
|
||||||
Take();
|
|
||||||
# endif // HAVE_CURL_CURL_H
|
|
||||||
|
|
||||||
const MPD::Song *s = Reload ? myPlaylist->NowPlayingSong() : myScreen->CurrentSong();
|
|
||||||
|
|
||||||
if (!s)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!s->GetArtist().empty() && !s->GetTitle().empty())
|
|
||||||
{
|
|
||||||
if (hasToBeResized)
|
|
||||||
Resize();
|
|
||||||
itsScrollBegin = 0;
|
|
||||||
itsSong = *s;
|
itsSong = *s;
|
||||||
itsSong.Localize();
|
Load();
|
||||||
if (!Reload)
|
|
||||||
{
|
|
||||||
myOldScreen = myScreen;
|
|
||||||
myScreen = this;
|
|
||||||
}
|
|
||||||
Global::RedrawHeader = 1;
|
|
||||||
w->Clear();
|
|
||||||
w->Reset();
|
|
||||||
# ifdef HAVE_CURL_CURL_H
|
|
||||||
if (!Downloader)
|
|
||||||
# endif // HAVE_CURL_CURL_H
|
|
||||||
{
|
|
||||||
std::string file = locale_to_utf_cpy(itsSong.GetArtist()) + " - " + locale_to_utf_cpy(itsSong.GetTitle()) + ".txt";
|
|
||||||
EscapeUnallowedChars(file);
|
|
||||||
itsFilenamePath = Folder + "/" + file;
|
|
||||||
|
|
||||||
mkdir(Folder.c_str()
|
|
||||||
# ifndef WIN32
|
|
||||||
, 0755
|
|
||||||
# endif // !WIN32
|
|
||||||
);
|
|
||||||
|
|
||||||
std::ifstream input(itsFilenamePath.c_str());
|
|
||||||
if (input.is_open())
|
|
||||||
{
|
|
||||||
bool first = 1;
|
|
||||||
std::string line;
|
|
||||||
while (getline(input, line))
|
|
||||||
{
|
|
||||||
if (!first)
|
|
||||||
*w << "\n";
|
|
||||||
utf_to_locale(line);
|
|
||||||
*w << line;
|
|
||||||
first = 0;
|
|
||||||
}
|
|
||||||
w->Flush();
|
|
||||||
if (Reload)
|
|
||||||
w->Refresh();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
# ifdef HAVE_CURL_CURL_H
|
|
||||||
Downloader = new pthread_t;
|
|
||||||
pthread_create(Downloader, 0, Get, this);
|
|
||||||
# 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();
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reload = 0;
|
|
||||||
|
Global::RedrawHeader = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,11 +172,63 @@ void *Lyrics::Get(void *screen_void_ptr)
|
|||||||
else
|
else
|
||||||
*screen->w << "\nLyrics weren't found.";
|
*screen->w << "\nLyrics weren't found.";
|
||||||
|
|
||||||
Ready = 1;
|
ReadyToTake = 1;
|
||||||
pthread_exit(0);
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
#endif // HAVE_CURL_CURL_H
|
#endif // HAVE_CURL_CURL_H
|
||||||
|
|
||||||
|
void Lyrics::Load()
|
||||||
|
{
|
||||||
|
# ifdef HAVE_CURL_CURL_H
|
||||||
|
if (Downloader)
|
||||||
|
return;
|
||||||
|
# endif // HAVE_CURL_CURL_H
|
||||||
|
if (itsSong.GetArtist().empty() || itsSong.GetTitle().empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
itsSong.Localize();
|
||||||
|
std::string file = locale_to_utf_cpy(itsSong.GetArtist()) + " - " + locale_to_utf_cpy(itsSong.GetTitle()) + ".txt";
|
||||||
|
EscapeUnallowedChars(file);
|
||||||
|
itsFilenamePath = Folder + "/" + file;
|
||||||
|
|
||||||
|
mkdir(Folder.c_str()
|
||||||
|
# ifndef WIN32
|
||||||
|
, 0755
|
||||||
|
# endif // !WIN32
|
||||||
|
);
|
||||||
|
|
||||||
|
w->Clear();
|
||||||
|
w->Reset();
|
||||||
|
|
||||||
|
std::ifstream input(itsFilenamePath.c_str());
|
||||||
|
if (input.is_open())
|
||||||
|
{
|
||||||
|
bool first = 1;
|
||||||
|
std::string line;
|
||||||
|
while (getline(input, line))
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
*w << "\n";
|
||||||
|
utf_to_locale(line);
|
||||||
|
*w << line;
|
||||||
|
first = 0;
|
||||||
|
}
|
||||||
|
w->Flush();
|
||||||
|
if (ReloadNP)
|
||||||
|
w->Refresh();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
# ifdef HAVE_CURL_CURL_H
|
||||||
|
Downloader = new pthread_t;
|
||||||
|
pthread_create(Downloader, 0, Get, this);
|
||||||
|
# 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();
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Lyrics::Edit()
|
void Lyrics::Edit()
|
||||||
{
|
{
|
||||||
if (myScreen != this)
|
if (myScreen != this)
|
||||||
@@ -297,14 +283,13 @@ void Lyrics::Refetch()
|
|||||||
#ifdef HAVE_CURL_CURL_H
|
#ifdef HAVE_CURL_CURL_H
|
||||||
void Lyrics::Take()
|
void Lyrics::Take()
|
||||||
{
|
{
|
||||||
if (!Ready)
|
assert(ReadyToTake);
|
||||||
return;
|
|
||||||
pthread_join(*Downloader, 0);
|
pthread_join(*Downloader, 0);
|
||||||
w->Flush();
|
w->Flush();
|
||||||
w->Refresh();
|
w->Refresh();
|
||||||
delete Downloader;
|
delete Downloader;
|
||||||
Downloader = 0;
|
Downloader = 0;
|
||||||
Ready = 0;
|
ReadyToTake = 0;
|
||||||
}
|
}
|
||||||
#endif // HAVE_CURL_CURL_H
|
#endif // HAVE_CURL_CURL_H
|
||||||
|
|
||||||
|
|||||||
@@ -49,12 +49,14 @@ class Lyrics : public Screen<Scrollpad>
|
|||||||
void Save(const std::string &lyrics);
|
void Save(const std::string &lyrics);
|
||||||
void Refetch();
|
void Refetch();
|
||||||
|
|
||||||
static bool Reload;
|
static bool ReloadNP;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void Init();
|
virtual void Init();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void Load();
|
||||||
|
|
||||||
std::string itsFilenamePath;
|
std::string itsFilenamePath;
|
||||||
|
|
||||||
static const std::string Folder;
|
static const std::string Folder;
|
||||||
@@ -64,7 +66,7 @@ class Lyrics : public Screen<Scrollpad>
|
|||||||
|
|
||||||
void Take();
|
void Take();
|
||||||
|
|
||||||
static bool Ready;
|
static bool ReadyToTake;
|
||||||
|
|
||||||
static pthread_t *Downloader;
|
static pthread_t *Downloader;
|
||||||
|
|
||||||
|
|||||||
@@ -412,7 +412,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
|
|||||||
myPlaylist->Items->Highlight(myPlaylist->NowPlaying);
|
myPlaylist->Items->Highlight(myPlaylist->NowPlaying);
|
||||||
|
|
||||||
if (Config.now_playing_lyrics && myScreen == myLyrics && Global::myOldScreen == myPlaylist)
|
if (Config.now_playing_lyrics && myScreen == myLyrics && Global::myOldScreen == myPlaylist)
|
||||||
Lyrics::Reload = 1;
|
Lyrics::ReloadNP = 1;
|
||||||
}
|
}
|
||||||
Playlist::ReloadRemaining = 1;
|
Playlist::ReloadRemaining = 1;
|
||||||
playing_song_scroll_begin = 0;
|
playing_song_scroll_begin = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user