From b77466cc38e23de3d66e8d869d16dc0fc824d639 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Wed, 29 Aug 2012 18:45:58 +0200 Subject: [PATCH] browser: read tags from local songs --- src/actions.cpp | 4 ++-- src/browser.cpp | 47 ++++++++++++++++----------------------------- src/browser.h | 2 +- src/display.cpp | 9 ++------- src/mpdpp.cpp | 2 +- src/mpdpp.h | 2 +- src/mutable_song.cpp | 14 ++++++++++++++ src/mutable_song.h | 8 ++++++-- src/song.cpp | 2 +- src/tag_editor.cpp | 3 ++- src/utility/comparators.cpp | 6 +++--- 11 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index fcc7c16..b21ea15 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -819,7 +819,7 @@ void Delete::Run() if (myBrowser->isParentDir(myBrowser->Main()->Choice())) return; - std::string name = item.type == itSong ? item.song.getName() : item.name; + std::string name = item.type == itSong ? item.song->getName() : item.name; std::string question; if (myBrowser->Main()->hasSelected()) question = "Delete selected items?"; @@ -842,7 +842,7 @@ void Delete::Run() for (size_t i = 0; i < list.size(); ++i) { const MPD::Item &it = (*myBrowser->Main())[list[i]]; - name = it.type == itSong ? it.song.getName() : it.name; + name = it.type == itSong ? it.song->getName() : it.name; if (myBrowser->DeleteItem(it)) { const char msg[] = "\"%s\" deleted"; diff --git a/src/browser.cpp b/src/browser.cpp index 98c408a..375498b 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -144,7 +144,7 @@ void Browser::EnterPressed() } case itSong: { - bool res = myPlaylist->Add(item.song, w->isBold(), 1); + bool res = myPlaylist->Add(*item.song, w->isBold(), 1); w->Bold(w->Choice(), res); break; } @@ -189,7 +189,7 @@ void Browser::SpacePressed() myBrowser->GetLocalDirectory(items, item.name, 1); list.reserve(items.size()); for (MPD::ItemList::const_iterator it = items.begin(); it != items.end(); ++it) - list.push_back(it->song); + list.push_back(*it->song); result = myPlaylist->Add(list, 0); } else @@ -201,7 +201,7 @@ void Browser::SpacePressed() } case itSong: { - bool res = myPlaylist->Add(item.song, w->isBold(), 0); + bool res = myPlaylist->Add(*item.song, w->isBold(), 0); w->Bold(w->Choice(), res); break; } @@ -258,7 +258,7 @@ void Browser::MouseButtonPressed(MEVENT me) MPD::Song *Browser::CurrentSong() { - return !w->Empty() && w->Current().type == itSong ? &w->Current().song : 0; + return !w->Empty() && w->Current().type == itSong ? w->Current().song.get() : 0; } void Browser::ReverseSelection() @@ -287,7 +287,7 @@ void Browser::GetSelectedSongs(MPD::SongList &v) MPD::ItemList list; GetLocalDirectory(list, item.name, 1); for (auto j = list.begin(); j != list.end(); ++j) - v.push_back(j->song); + v.push_back(*j->song); } else # endif // !WIN32 @@ -300,7 +300,7 @@ void Browser::GetSelectedSongs(MPD::SongList &v) } case itSong: { - v.push_back(item.song); + v.push_back(*item.song); break; } case itPlaylist: @@ -344,7 +344,7 @@ void Browser::LocateSong(const MPD::Song &s) GetDirectory(s.getDirectory()); for (size_t i = 0; i < w->Size(); ++i) { - if ((*w)[i].type == itSong && s.getHash() == (*w)[i].song.getHash()) + if ((*w)[i].type == itSong && s.getHash() == (*w)[i].song->getHash()) { w->Highlight(i); break; @@ -416,7 +416,7 @@ void Browser::GetDirectory(std::string dir, std::string subdir) bool bold = 0; for (size_t i = 0; i < myPlaylist->Items->Size(); ++i) { - if (myPlaylist->Items->at(i).getHash() == it->song.getHash()) + if (myPlaylist->Items->at(i).getHash() == it->song->getHash()) { bold = 1; break; @@ -477,11 +477,12 @@ void Browser::GetLocalDirectory(MPD::ItemList &v, const std::string &directory, { new_item.type = itSong; mpd_pair file_pair = { "file", full_path.c_str() }; - new_item.song = MPD::Song(mpd_song_begin(&file_pair)); + MPD::MutableSong *s = new MPD::MutableSong(mpd_song_begin(&file_pair)); + new_item.song = std::shared_ptr(s); # ifdef HAVE_TAGLIB_H // FIXME - //if (!recursively) - // TagEditor::ReadTags(*new_item.song); + if (!recursively) + TagEditor::ReadTags(*s); # endif // HAVE_TAGLIB_H v.push_back(new_item); } @@ -558,7 +559,7 @@ bool Browser::DeleteItem(const MPD::Item &item) std::string path; if (!isLocal()) path = Config.mpd_music_dir; - path += item.type == itSong ? item.song.getURI() : item.name; + path += item.type == itSong ? item.song->getURI() : item.name; if (item.type == itDirectory) ClearDirectory(path); @@ -569,23 +570,9 @@ bool Browser::DeleteItem(const MPD::Item &item) void Browser::UpdateItemList() { - bool bold = 0; for (size_t i = 0; i < w->Size(); ++i) - { - if (w->at(i).type == itSong) - { - for (size_t j = 0; j < myPlaylist->Items->Size(); ++j) - { - if (myPlaylist->Items->at(j).getHash() == w->at(i).song.getHash()) - { - bold = 1; - break; - } - } - w->Bold(i, bold); - bold = 0; - } - } + if ((*w)[i].type == itSong) + w->Bold(i, myPlaylist->checkForSong(*(*w)[i].song)); w->Refresh(); } @@ -600,9 +587,9 @@ std::string Browser::ItemToString(const MPD::Item &item, void *) case MPD::itSong: { if (!Config.columns_in_browser) - return item.song.toString(Config.song_list_format_dollar_free); + return item.song->toString(Config.song_list_format_dollar_free); else - return Playlist::SongInColumnsToString(item.song, 0); + return Playlist::SongInColumnsToString(*item.song, 0); } case MPD::itPlaylist: { diff --git a/src/browser.h b/src/browser.h index 718ee6f..5fe0b90 100644 --- a/src/browser.h +++ b/src/browser.h @@ -40,7 +40,7 @@ class Browser : public Screen< Menu > virtual bool isTabbable() { return true; } virtual MPD::Song *CurrentSong(); - virtual MPD::Song *GetSong(size_t pos) { return w->at(pos).type == MPD::itSong ? &(*w)[pos].song : 0; } + virtual MPD::Song *GetSong(size_t pos) { return w->at(pos).type == MPD::itSong ? (*w)[pos].song.get() : 0; } virtual bool allowsSelection() { return true; } virtual void ReverseSelection(); diff --git a/src/display.cpp b/src/display.cpp index 31478c4..57db5fc 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -364,19 +364,14 @@ void Display::Items(const MPD::Item &item, void *data, Menu *menu) { case MPD::itDirectory: { - if (!item.song.empty()) - { - *menu << "[..]"; - return; - } *menu << "[" << getBasename(item.name) << "]"; return; } case MPD::itSong: if (!Config.columns_in_browser) - Display::Songs(item.song, data, reinterpret_cast *>(menu)); + Display::Songs(*item.song, data, reinterpret_cast *>(menu)); else - Display::SongsInColumns(item.song, data, reinterpret_cast *>(menu)); + Display::SongsInColumns(*item.song, data, reinterpret_cast *>(menu)); return; case MPD::itPlaylist: *menu << Config.browser_playlist_prefix << getBasename(item.name); diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index 9d9aba5..3b46c79 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -1186,7 +1186,7 @@ void MPD::Connection::GetDirectory(const std::string &path, std::function(new Song(mpd_song_dup(mpd_entity_get_song(item)))); it.type = itSong; break; case MPD_ENTITY_TYPE_PLAYLIST: diff --git a/src/mpdpp.h b/src/mpdpp.h index 9bf8c5d..0225269 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -35,7 +35,7 @@ namespace MPD struct Item { - Song song; + std::shared_ptr song; ItemType type; std::string name; }; diff --git a/src/mutable_song.cpp b/src/mutable_song.cpp index 6012633..ebe72c0 100644 --- a/src/mutable_song.cpp +++ b/src/mutable_song.cpp @@ -152,6 +152,20 @@ void MutableSong::setNewURI(const std::string &value) m_uri = value; } +unsigned MutableSong::getDuration() const +{ + if (m_duration > 0) + return m_duration; + else + return Song::getDuration(); +} + + +void MutableSong::setDuration(unsigned int duration) +{ + m_duration = duration; +} + void MutableSong::setTag(SetFunction set, const std::string &value, const std::string &delimiter) { auto tags = split(value, delimiter); diff --git a/src/mutable_song.h b/src/mutable_song.h index 014be9f..95ee77e 100644 --- a/src/mutable_song.h +++ b/src/mutable_song.h @@ -30,8 +30,8 @@ struct MutableSong : public Song { typedef void (MutableSong::*SetFunction)(const std::string &, unsigned); - MutableSong() { } - MutableSong(Song s) : Song(s) { } + MutableSong() : m_duration(0) { } + MutableSong(Song s) : Song(s), m_duration(0) { } virtual std::string getArtist(unsigned idx = 0) const; virtual std::string getTitle(unsigned idx = 0) const; @@ -60,6 +60,9 @@ struct MutableSong : public Song const std::string &getNewURI() const; void setNewURI(const std::string &value); + virtual unsigned getDuration() const; + void setDuration(unsigned duration); + void setTag(SetFunction set, const std::string &value, const std::string &delimiter = ""); bool isModified() const; @@ -89,6 +92,7 @@ private: void replaceTag(mpd_tag_type tag_type, std::string &&orig_value, const std::string &value, unsigned idx); std::string m_uri; + unsigned m_duration; std::map m_tags; }; diff --git a/src/song.cpp b/src/song.cpp index b105965..46ce360 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -178,7 +178,7 @@ std::string Song::getLength(unsigned idx) const assert(m_song); if (idx > 0) return ""; - unsigned len = mpd_song_get_duration(m_song.get()); + unsigned len = getDuration(); if (len > 0) return ShowTime(len); else diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index 7849217..4f229d9 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -962,7 +962,8 @@ void TagEditor::ReadTags(MPD::MutableSong &s) return; TagLib::MPEG::File *mpegf = dynamic_cast(f.file()); - + s.setDuration(f.audioProperties()->length()); + s.setArtist(f.tag()->artist().to8Bit(1)); s.setTitle(f.tag()->title().to8Bit(1)); s.setAlbum(f.tag()->album().to8Bit(1)); diff --git a/src/utility/comparators.cpp b/src/utility/comparators.cpp index f434639..65ed263 100644 --- a/src/utility/comparators.cpp +++ b/src/utility/comparators.cpp @@ -55,13 +55,13 @@ bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b) switch (Config.browser_sort_mode) { case smName: - result = operator()(a.song, b.song); + result = operator()(*a.song, *b.song); break; case smMTime: - result = a.song.getMTime() > b.song.getMTime(); + result = a.song->getMTime() > b.song->getMTime(); break; case smCustomFormat: - result = cmp(a.song.toString(Config.browser_sort_format), b.song.toString(Config.browser_sort_format)) < 0; + result = cmp(a.song->toString(Config.browser_sort_format), b.song->toString(Config.browser_sort_format)) < 0; break; } break; -- 2.11.4.GIT