media library: locate song: clean up

This commit is contained in:
Andrzej Rybczak
2015-04-18 22:18:33 +02:00
parent b8645d7647
commit a26aa6c295

View File

@@ -56,6 +56,7 @@ size_t itsMiddleColStartX;
size_t itsRightColWidth; size_t itsRightColWidth;
size_t itsRightColStartX; size_t itsRightColStartX;
typedef MediaLibrary::PrimaryTag PrimaryTag;
typedef MediaLibrary::AlbumEntry AlbumEntry; typedef MediaLibrary::AlbumEntry AlbumEntry;
MPD::SongIterator getSongsFromAlbum(const AlbumEntry &album) MPD::SongIterator getSongsFromAlbum(const AlbumEntry &album)
@@ -77,8 +78,8 @@ bool TagEntryMatcher(const boost::regex &rx, const MediaLibrary::PrimaryTag &tag
bool AlbumEntryMatcher(const boost::regex &rx, const NC::Menu<AlbumEntry>::Item &item, bool filter); bool AlbumEntryMatcher(const boost::regex &rx, const NC::Menu<AlbumEntry>::Item &item, bool filter);
bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s); bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s);
bool MoveToTag(MediaLibrary &ml, const std::string &primary_tag, bool tags_changed); bool MoveToTag(NC::Menu<PrimaryTag> &tags, const std::string &primary_tag);
bool MoveToAlbum(MediaLibrary &ml, const std::string &primary_tag, const MPD::Song &s, bool albums_changed); bool MoveToAlbum(NC::Menu<AlbumEntry> &albums, const std::string &primary_tag, const MPD::Song &s);
struct SortSongs { struct SortSongs {
typedef NC::Menu<MPD::Song>::Item SongItem; typedef NC::Menu<MPD::Song>::Item SongItem;
@@ -142,8 +143,6 @@ public:
}; };
class SortPrimaryTags { class SortPrimaryTags {
typedef MediaLibrary::PrimaryTag PrimaryTag;
LocaleStringComparison m_cmp; LocaleStringComparison m_cmp;
public: public:
@@ -895,6 +894,7 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
Statusbar::printf("Can't use this function because the song has no %s tag", item_type); Statusbar::printf("Can't use this function because the song has no %s tag", item_type);
return; return;
} }
if (!s.isFromDatabase()) if (!s.isFromDatabase())
{ {
Statusbar::print("Song is not from the database"); Statusbar::print("Song is not from the database");
@@ -905,13 +905,13 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
switchTo(); switchTo();
Statusbar::put() << "Jumping to song..."; Statusbar::put() << "Jumping to song...";
Global::wFooter->refresh(); Global::wFooter->refresh();
// FIXME: use std::find
if (!hasTwoColumns) if (!hasTwoColumns)
{ {
if (Tags.empty()) if (Tags.empty())
update(); update();
if (!MoveToTag(*this, primary_tag, false)) if (!MoveToTag(Tags, primary_tag))
{ {
// The tag could not be found. Since this was called from an existing // The tag could not be found. Since this was called from an existing
// song, the tag should exist in the library, but it was not listed by // song, the tag should exist in the library, but it was not listed by
@@ -921,8 +921,9 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
Tags.addItem(PrimaryTag(primary_tag, s.getMTime())); Tags.addItem(PrimaryTag(primary_tag, s.getMTime()));
std::sort(Tags.beginV(), Tags.endV(), SortPrimaryTags()); std::sort(Tags.beginV(), Tags.endV(), SortPrimaryTags());
Tags.refresh(); Tags.refresh();
MoveToTag(*this, primary_tag, true); MoveToTag(Tags, primary_tag);
} }
Albums.clear();
} }
if (Albums.empty()) if (Albums.empty())
@@ -947,56 +948,41 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
// Note: We don't want to return when no albums are found in two column // Note: We don't want to return when no albums are found in two column
// mode. In this case, we try to insert the album, as we do with tags when // mode. In this case, we try to insert the album, as we do with tags when
// they are not found. // they are not found.
if (!hasTwoColumns && Albums.empty()) if (hasTwoColumns || !Albums.empty())
{ {
Tags.setHighlightColor(Config.active_column_color); if (!MoveToAlbum(Albums, primary_tag, s))
Albums.setHighlightColor(Config.main_highlight_color); {
Songs.setHighlightColor(Config.main_highlight_color); // The album could not be found, insert it if in two column mode.
w = &Tags; // See comment about tags not found above. This is the equivalent
refresh(); // for two column mode.
return; Albums.addItem(AlbumEntry(
} Album(primary_tag, s.getAlbum(), s.getDate(), s.getMTime())
));
std::sort(Albums.beginV(), Albums.endV(), SortAlbumEntries());
Albums.refresh();
MoveToAlbum(Albums, primary_tag, s);
}
if (hasTwoColumns && !MoveToAlbum(*this, primary_tag, s, false)) Songs.clear();
{
// The album could not be found, insert it if in two column mode.
// See comment about tags not found above. This is the equivalent
// for two column mode.
Albums.addItem(AlbumEntry(Album(primary_tag, s.getAlbum(), s.getDate(), s.getMTime())));
std::sort(Albums.beginV(), Albums.endV(), SortAlbumEntries());
Albums.refresh();
MoveToAlbum(*this, primary_tag, s, true);
}
if (Songs.empty())
update(); update();
if (Songs.empty()) if (!Songs.empty())
{
Tags.setHighlightColor(Config.main_highlight_color);
Albums.setHighlightColor(Config.active_column_color);
Songs.setHighlightColor(Config.main_highlight_color);
w = &Albums;
refresh();
return;
}
if (s != Songs.current()->value())
{
for (size_t i = 0; i < Songs.size(); ++i)
{ {
if (s == Songs[i].value()) if (s != Songs.current()->value())
{ {
Songs.highlight(i); auto begin = Songs.beginV(), end = Songs.endV();
break; auto it = std::find(begin, end, s);
if (it != end)
Songs.highlight(it-begin);
} }
nextColumn();
nextColumn();
} }
else // invalid album was added, clear the list
Albums.clear();
} }
else // invalid tag was added, clear the list
Tags.setHighlightColor(Config.main_highlight_color); Tags.clear();
Albums.setHighlightColor(Config.main_highlight_color);
Songs.setHighlightColor(Config.active_column_color);
w = &Songs;
refresh(); refresh();
} }
@@ -1079,7 +1065,7 @@ std::string SongToString(const MPD::Song &s)
); );
} }
bool TagEntryMatcher(const boost::regex &rx, const MediaLibrary::PrimaryTag &pt) bool TagEntryMatcher(const boost::regex &rx, const PrimaryTag &pt)
{ {
return boost::regex_search(pt.tag(), rx); return boost::regex_search(pt.tag(), rx);
} }
@@ -1096,56 +1082,52 @@ bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s)
return boost::regex_search(SongToString(s), rx); return boost::regex_search(SongToString(s), rx);
} }
bool MoveToTag(MediaLibrary &ml, const std::string &primary_tag, bool tags_changed) bool MoveToTag(NC::Menu<PrimaryTag> &tags, const std::string &primary_tag)
{ {
if (primary_tag == ml.Tags.current()->value().tag()) if (tags.empty())
{ return false;
if (tags_changed)
{
ml.Albums.clear();
ml.Songs.clear();
}
return true;
}
for (size_t i = 0; i < ml.Tags.size(); ++i) auto equals_fun_argument = [&](PrimaryTag &e) {
return e.tag() == primary_tag;
};
if (equals_fun_argument(*tags.currentV()))
return true;
auto begin = tags.beginV(), end = tags.endV();
auto it = std::find_if(begin, end, equals_fun_argument);
if (it != end)
{ {
if (primary_tag == ml.Tags[i].value().tag()) tags.highlight(it-begin);
{ return true;
ml.Tags.highlight(i);
ml.Albums.clear();
ml.Songs.clear();
return true;
}
} }
return false; return false;
} }
bool MoveToAlbum(MediaLibrary &ml, const std::string &primary_tag, const MPD::Song &s, bool albums_changed) bool MoveToAlbum(NC::Menu<AlbumEntry> &albums, const std::string &primary_tag, const MPD::Song &s)
{ {
if (albums.empty())
return false;
std::string album = s.getAlbum(); std::string album = s.getAlbum();
std::string date = s.getDate(); std::string date = s.getDate();
if ((!hasTwoColumns || ml.Albums.current()->value().entry().tag() == primary_tag) auto equals_fun_argument = [&](AlbumEntry &e) {
&& album == ml.Albums.current()->value().entry().album() return (!hasTwoColumns || e.entry().tag() == primary_tag)
&& date == ml.Albums.current()->value().entry().date()) && e.entry().album() == album
{ && e.entry().date() == date;
if (albums_changed) };
ml.Songs.clear();
return true;
}
for (size_t i = 0; i < ml.Albums.size(); ++i) if (equals_fun_argument(*albums.currentV()))
return true;
auto begin = albums.beginV(), end = albums.endV();
auto it = std::find_if(begin, end, equals_fun_argument);
if (it != end)
{ {
if ((!hasTwoColumns || ml.Albums[i].value().entry().tag() == primary_tag) albums.highlight(it-begin);
&& album == ml.Albums[i].value().entry().album() return true;
&& date == ml.Albums[i].value().entry().date())
{
ml.Albums.highlight(i);
ml.Songs.clear();
return true;
}
} }
return false; return false;