separate 'song info' functionality from Info class

This commit is contained in:
Andrzej Rybczak
2010-08-10 23:46:57 +02:00
parent 87298907e1
commit de5f3b22e0
9 changed files with 208 additions and 116 deletions

View File

@@ -3,8 +3,8 @@ ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp conv.cpp curl_handle.cpp \
display.cpp error.cpp help.cpp helpers.cpp info.cpp lyrics.cpp lyrics_fetcher.cpp \
media_library.cpp menu.cpp mpdpp.cpp ncmpcpp.cpp outputs.cpp playlist.cpp \
playlist_editor.cpp scrollpad.cpp search_engine.cpp sel_items_adder.cpp server_info.cpp \
settings.cpp song.cpp status.cpp tag_editor.cpp tiny_tag_editor.cpp tolower.cpp \
visualizer.cpp window.cpp
settings.cpp song.cpp song_info.cpp status.cpp tag_editor.cpp tiny_tag_editor.cpp \
tolower.cpp visualizer.cpp window.cpp
# set the include path found by configure
INCLUDES= $(all_includes)
@@ -14,5 +14,5 @@ ncmpcpp_LDFLAGS = $(all_libraries)
noinst_HEADERS = browser.h charset.h clock.h conv.h curl_handle.h display.h \
error.h global.h help.h helpers.h home.h info.h lyrics.h lyrics_fetcher.h \
media_library.h menu.h mpdpp.h outputs.h playlist_editor.h screen.h scrollpad.h \
search_engine.h sel_items_adder.h server_info.h settings.h song.h tag_editor.h \
tiny_tag_editor.h tolower.h visualizer.h window.h
search_engine.h sel_items_adder.h server_info.h settings.h song.h song_info.h \
tag_editor.h tiny_tag_editor.h tolower.h visualizer.h window.h

View File

@@ -22,7 +22,7 @@
#include "display.h"
#include "helpers.h"
#include "info.h"
#include "song_info.h"
#include "playlist.h"
#include "global.h"
@@ -375,7 +375,7 @@ void Display::Tags(const MPD::Song &s, void *data, Menu<MPD::Song> *menu)
size_t i = static_cast<Menu<std::string> *>(data)->Choice();
if (i < 11)
{
ShowTag(*menu, s.GetTags(Info::Tags[i].Get));
ShowTag(*menu, s.GetTags(SongInfo::Tags[i].Get));
}
else if (i == 12)
{

View File

@@ -55,22 +55,6 @@ pthread_t *Info::Downloader = 0;
Info *myInfo = new Info;
const Info::Metadata Info::Tags[] =
{
{ "Title", &MPD::Song::GetTitle, &MPD::Song::SetTitle },
{ "Artist", &MPD::Song::GetArtist, &MPD::Song::SetArtist },
{ "Album Artist", &MPD::Song::GetAlbumArtist, &MPD::Song::SetAlbumArtist },
{ "Album", &MPD::Song::GetAlbum, &MPD::Song::SetAlbum },
{ "Year", &MPD::Song::GetDate, &MPD::Song::SetDate },
{ "Track", &MPD::Song::GetTrack, &MPD::Song::SetTrack },
{ "Genre", &MPD::Song::GetGenre, &MPD::Song::SetGenre },
{ "Composer", &MPD::Song::GetComposer, &MPD::Song::SetComposer },
{ "Performer", &MPD::Song::GetPerformer, &MPD::Song::SetPerformer },
{ "Disc", &MPD::Song::GetDisc, &MPD::Song::SetDisc },
{ "Comment", &MPD::Song::GetComment, &MPD::Song::SetComment },
{ 0, 0, 0 }
};
void Info::Init()
{
w = new Scrollpad(0, MainStartY, COLS, MainHeight, "", Config.main_color, brNone);
@@ -101,40 +85,6 @@ void Info::Update()
Downloader = 0;
ArtistReady = 0;
}
#endif // HAVE_CURL_CURL_H
void Info::GetSong()
{
if (myScreen == this)
{
myOldScreen->SwitchTo();
}
else
{
if (!isInitialized)
Init();
MPD::Song *s = myScreen->CurrentSong();
if (!s)
return;
if (hasToBeResized)
Resize();
myOldScreen = myScreen;
myScreen = this;
Global::RedrawHeader = 1;
itsTitle = "Song info";
w->Clear();
w->Reset();
PrepareSong(*s);
w->Window::Clear();
w->Flush();
}
}
#ifdef HAVE_CURL_CURL_H
void Info::GetArtist()
{
@@ -339,37 +289,3 @@ void *Info::PrepareArtist(void *screen_void_ptr)
}
#endif // HVAE_CURL_CURL_H
void Info::PrepareSong(MPD::Song &s)
{
# ifdef HAVE_TAGLIB_H
std::string path_to_file;
if (s.isFromDB())
path_to_file += Config.mpd_music_dir;
path_to_file += s.GetFile();
TagLib::FileRef f(path_to_file.c_str());
if (!f.isNull())
s.SetComment(f.tag()->comment().to8Bit(1));
# endif // HAVE_TAGLIB_H
*w << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd;
*w << fmtBold << "Directory: " << fmtBoldEnd << Config.color2;
ShowTag(*w, s.GetDirectory());
*w << "\n\n" << clEnd;
*w << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd;
# ifdef HAVE_TAGLIB_H
if (!f.isNull())
{
*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;
}
# endif // HAVE_TAGLIB_H
*w << clDefault;
for (const Metadata *m = Tags; m->Name; ++m)
{
*w << fmtBold << "\n" << m->Name << ": " << fmtBoldEnd;
ShowTag(*w, s.GetTags(m->Get));
}
}

View File

@@ -28,13 +28,6 @@
class Info : public Screen<Scrollpad>
{
public:
struct Metadata
{
const char *Name;
MPD::Song::GetFunction Get;
MPD::Song::SetFunction Set;
};
virtual void SwitchTo() { }
virtual void Resize();
@@ -51,13 +44,10 @@ class Info : public Screen<Scrollpad>
virtual List *GetList() { return 0; }
void GetSong();
# ifdef HAVE_CURL_CURL_H
void GetArtist();
# endif // HAVE_CURL_CURL_H
static const Metadata Tags[];
protected:
virtual void Init();
@@ -66,8 +56,6 @@ class Info : public Screen<Scrollpad>
std::string itsTitle;
std::string itsFilenamePath;
void PrepareSong(MPD::Song &);
# ifdef HAVE_CURL_CURL_H
static void *PrepareArtist(void *);

View File

@@ -47,6 +47,7 @@
#include "search_engine.h"
#include "settings.h"
#include "song.h"
#include "song_info.h"
#include "info.h"
#include "outputs.h"
#include "status.h"
@@ -2044,7 +2045,7 @@ int main(int argc, char *argv[])
}
else if (Keypressed(input, Key.SongInfo))
{
myInfo->GetSong();
mySongInfo->SwitchTo();
}
# ifdef HAVE_CURL_CURL_H
else if (Keypressed(input, Key.ArtistInfo))

126
src/song_info.cpp Normal file
View File

@@ -0,0 +1,126 @@
/***************************************************************************
* Copyright (C) 2008-2010 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "global.h"
#include "song_info.h"
#include "tag_editor.h"
using Global::MainHeight;
using Global::MainStartY;
using Global::myScreen;
using Global::myOldScreen;
SongInfo *mySongInfo = new SongInfo;
const SongInfo::Metadata SongInfo::Tags[] =
{
{ "Title", &MPD::Song::GetTitle, &MPD::Song::SetTitle },
{ "Artist", &MPD::Song::GetArtist, &MPD::Song::SetArtist },
{ "Album Artist", &MPD::Song::GetAlbumArtist, &MPD::Song::SetAlbumArtist },
{ "Album", &MPD::Song::GetAlbum, &MPD::Song::SetAlbum },
{ "Year", &MPD::Song::GetDate, &MPD::Song::SetDate },
{ "Track", &MPD::Song::GetTrack, &MPD::Song::SetTrack },
{ "Genre", &MPD::Song::GetGenre, &MPD::Song::SetGenre },
{ "Composer", &MPD::Song::GetComposer, &MPD::Song::SetComposer },
{ "Performer", &MPD::Song::GetPerformer, &MPD::Song::SetPerformer },
{ "Disc", &MPD::Song::GetDisc, &MPD::Song::SetDisc },
{ "Comment", &MPD::Song::GetComment, &MPD::Song::SetComment },
{ 0, 0, 0 }
};
void SongInfo::Init()
{
w = new Scrollpad(0, MainStartY, COLS, MainHeight, "", Config.main_color, brNone);
isInitialized = 1;
}
void SongInfo::Resize()
{
w->Resize(COLS, MainHeight);
w->MoveTo(0, MainStartY);
hasToBeResized = 0;
}
std::basic_string<my_char_t> SongInfo::Title()
{
return U("Song info");
}
void SongInfo::SwitchTo()
{
if (myScreen == this)
return myOldScreen->SwitchTo();
if (!isInitialized)
Init();
MPD::Song *s = myScreen->CurrentSong();
if (!s)
return;
if (hasToBeResized)
Resize();
myOldScreen = myScreen;
myScreen = this;
Global::RedrawHeader = 1;
w->Clear();
w->Reset();
PrepareSong(*s);
w->Flush();
}
void SongInfo::PrepareSong(MPD::Song &s)
{
# ifdef HAVE_TAGLIB_H
std::string path_to_file;
if (s.isFromDB())
path_to_file += Config.mpd_music_dir;
path_to_file += s.GetFile();
TagLib::FileRef f(path_to_file.c_str());
if (!f.isNull())
s.SetComment(f.tag()->comment().to8Bit(1));
# endif // HAVE_TAGLIB_H
*w << fmtBold << Config.color1 << "Filename: " << fmtBoldEnd << Config.color2 << s.GetName() << "\n" << clEnd;
*w << fmtBold << "Directory: " << fmtBoldEnd << Config.color2;
ShowTag(*w, s.GetDirectory());
*w << "\n\n" << clEnd;
*w << fmtBold << "Length: " << fmtBoldEnd << Config.color2 << s.GetLength() << "\n" << clEnd;
# ifdef HAVE_TAGLIB_H
if (!f.isNull())
{
*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;
}
# endif // HAVE_TAGLIB_H
*w << clDefault;
for (const Metadata *m = Tags; m->Name; ++m)
{
*w << fmtBold << "\n" << m->Name << ": " << fmtBoldEnd;
ShowTag(*w, s.GetTags(m->Get));
}
}

61
src/song_info.h Normal file
View File

@@ -0,0 +1,61 @@
/***************************************************************************
* Copyright (C) 2008-2010 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifndef _SONG_INFO_H
#define _SONG_INFO_H
#include "screen.h"
#include "song.h"
class SongInfo : public Screen<Scrollpad>
{
public:
struct Metadata
{
const char *Name;
MPD::Song::GetFunction Get;
MPD::Song::SetFunction Set;
};
virtual void SwitchTo();
virtual void Resize();
virtual std::basic_string<my_char_t> Title();
virtual void EnterPressed() { }
virtual void SpacePressed() { }
virtual bool allowsSelection() { return false; }
virtual List *GetList() { return 0; }
static const Metadata Tags[];
protected:
virtual void Init();
private:
void PrepareSong(MPD::Song &);
};
extern SongInfo *mySongInfo;
#endif

View File

@@ -36,7 +36,7 @@
#include "charset.h"
#include "display.h"
#include "global.h"
#include "info.h"
#include "song_info.h"
#include "playlist.h"
using Global::MainHeight;
@@ -86,7 +86,7 @@ void TagEditor::Init()
TagTypes->CenteredCursor(Config.centered_cursor);
TagTypes->SetItemDisplayer(Display::Generic);
for (const Info::Metadata *m = Info::Tags; m->Name; ++m)
for (const SongInfo::Metadata *m = SongInfo::Tags; m->Name; ++m)
TagTypes->AddOption(m->Name);
TagTypes->AddSeparator();
TagTypes->AddOption("Filename");
@@ -550,8 +550,8 @@ void TagEditor::EnterPressed()
if (id < 11)
{
MPD::Song::GetFunction get = Info::Tags[id].Get;
MPD::Song::SetFunction set = Info::Tags[id].Set;
MPD::Song::GetFunction get = SongInfo::Tags[id].Get;
MPD::Song::SetFunction set = SongInfo::Tags[id].Set;
if (id > 0 && w == TagTypes)
{
LockStatusbar();
@@ -1069,7 +1069,7 @@ std::string TagEditor::CapitalizeFirstLetters(const std::string &s)
void TagEditor::CapitalizeFirstLetters(MPD::Song &s)
{
for (const Info::Metadata *m = Info::Tags; m->Name; ++m)
for (const SongInfo::Metadata *m = SongInfo::Tags; m->Name; ++m)
{
unsigned i = 0;
for (std::string tag; !(tag = (s.*m->Get)(i)).empty(); ++i)
@@ -1079,7 +1079,7 @@ void TagEditor::CapitalizeFirstLetters(MPD::Song &s)
void TagEditor::LowerAllLetters(MPD::Song &s)
{
for (const Info::Metadata *m = Info::Tags; m->Name; ++m)
for (const SongInfo::Metadata *m = SongInfo::Tags; m->Name; ++m)
{
unsigned i = 0;
for (std::string tag; !(tag = (s.*m->Get)(i)).empty(); ++i)
@@ -1103,7 +1103,7 @@ std::string TagEditor::TagToString(const MPD::Song &s, void *data)
std::string result;
size_t i = static_cast<Menu<std::string> *>(data)->Choice();
if (i < 11)
result = (s.*Info::Tags[i].Get)(0);
result = (s.*SongInfo::Tags[i].Get)(0);
else if (i == 12)
result = s.GetNewName().empty() ? s.GetName() : s.GetName() + " -> " + s.GetNewName();
return result.empty() ? Config.empty_tag : result;

View File

@@ -31,7 +31,7 @@
#include "charset.h"
#include "display.h"
#include "global.h"
#include "info.h"
#include "song_info.h"
#include "playlist.h"
#include "search_engine.h"
#include "tag_editor.h"
@@ -101,11 +101,11 @@ void TinyTagEditor::EnterPressed()
if (option < 19) // separator after comment
{
size_t pos = option-8;
Statusbar() << fmtBold << Info::Tags[pos].Name << ": " << fmtBoldEnd;
s.SetTags(Info::Tags[pos].Set, Global::wFooter->GetString(s.GetTags(Info::Tags[pos].Get)));
Statusbar() << fmtBold << SongInfo::Tags[pos].Name << ": " << fmtBoldEnd;
s.SetTags(SongInfo::Tags[pos].Set, Global::wFooter->GetString(s.GetTags(SongInfo::Tags[pos].Get)));
w->at(option).Clear();
w->at(option) << fmtBold << Info::Tags[pos].Name << ':' << fmtBoldEnd << ' ';
ShowTag(w->at(option), s.GetTags(Info::Tags[pos].Get));
w->at(option) << fmtBold << SongInfo::Tags[pos].Name << ':' << fmtBoldEnd << ' ';
ShowTag(w->at(option), s.GetTags(SongInfo::Tags[pos].Get));
}
else if (option == 20)
{
@@ -227,7 +227,7 @@ bool TinyTagEditor::GetTags()
w->at(6) << fmtBold << Config.color1 << "Channels: " << fmtBoldEnd << Config.color2 << (f.audioProperties()->channels() == 1 ? "Mono" : "Stereo") << clDefault;
unsigned pos = 8;
for (const Info::Metadata *m = Info::Tags; m->Name; ++m, ++pos)
for (const SongInfo::Metadata *m = SongInfo::Tags; m->Name; ++m, ++pos)
{
w->at(pos) << fmtBold << m->Name << ":" << fmtBoldEnd << ' ';
ShowTag(w->at(pos), s.GetTags(m->Get));