From de5f3b22e06460b3a2a3846ed07ad61235bf9b92 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Tue, 10 Aug 2010 23:46:57 +0200 Subject: [PATCH] separate 'song info' functionality from Info class --- src/Makefile.am | 8 +-- src/display.cpp | 4 +- src/info.cpp | 84 --------------------------- src/info.h | 12 ---- src/ncmpcpp.cpp | 3 +- src/song_info.cpp | 126 ++++++++++++++++++++++++++++++++++++++++ src/song_info.h | 61 +++++++++++++++++++ src/tag_editor.cpp | 14 ++--- src/tiny_tag_editor.cpp | 12 ++-- 9 files changed, 208 insertions(+), 116 deletions(-) create mode 100644 src/song_info.cpp create mode 100644 src/song_info.h diff --git a/src/Makefile.am b/src/Makefile.am index 5312d9b8..4b3b3f19 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/display.cpp b/src/display.cpp index 4884ce02..1462fb7b 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -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 *menu) size_t i = static_cast *>(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) { diff --git a/src/info.cpp b/src/info.cpp index 3f7086b6..ab840f6e 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -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)); - } -} - diff --git a/src/info.h b/src/info.h index 1b1ca6c5..9b68d798 100644 --- a/src/info.h +++ b/src/info.h @@ -28,13 +28,6 @@ class Info : public Screen { 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 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 std::string itsTitle; std::string itsFilenamePath; - void PrepareSong(MPD::Song &); - # ifdef HAVE_CURL_CURL_H static void *PrepareArtist(void *); diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 75168ff6..0d67f57b 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -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)) diff --git a/src/song_info.cpp b/src/song_info.cpp new file mode 100644 index 00000000..3f7cfb8d --- /dev/null +++ b/src/song_info.cpp @@ -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 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)); + } +} + diff --git a/src/song_info.h b/src/song_info.h new file mode 100644 index 00000000..92d3e3a7 --- /dev/null +++ b/src/song_info.h @@ -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 +{ + public: + struct Metadata + { + const char *Name; + MPD::Song::GetFunction Get; + MPD::Song::SetFunction Set; + }; + + virtual void SwitchTo(); + virtual void Resize(); + + virtual std::basic_string 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 + diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index c66cea00..978979e4 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -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 *>(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; diff --git a/src/tiny_tag_editor.cpp b/src/tiny_tag_editor.cpp index 66c220b0..41c6dfb1 100644 --- a/src/tiny_tag_editor.cpp +++ b/src/tiny_tag_editor.cpp @@ -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));