playlist editor: hold MPD::PlaylistS instead of std::stringS

This commit is contained in:
Andrzej Rybczak
2014-11-01 23:16:06 +01:00
parent fabd24c6a5
commit 30d57afcac
8 changed files with 57 additions and 46 deletions

View File

@@ -642,7 +642,7 @@ void DeletePlaylistItems::run()
} }
else if (myScreen->isActiveWindow(myPlaylistEditor->Content)) else if (myScreen->isActiveWindow(myPlaylistEditor->Content))
{ {
std::string playlist = myPlaylistEditor->Playlists.current().value(); std::string playlist = myPlaylistEditor->Playlists.current().value().path();
auto delete_fun = boost::bind(&MPD::Connection::PlaylistDelete, _1, playlist, _2); auto delete_fun = boost::bind(&MPD::Connection::PlaylistDelete, _1, playlist, _2);
Statusbar::print("Deleting items..."); Statusbar::print("Deleting items...");
deleteSelectedSongs(myPlaylistEditor->Content, delete_fun); deleteSelectedSongs(myPlaylistEditor->Content, delete_fun);
@@ -726,7 +726,7 @@ void DeleteStoredPlaylist::run()
question = boost::format("Delete selected playlists?"); question = boost::format("Delete selected playlists?");
else else
question = boost::format("Delete playlist \"%1%\"?") question = boost::format("Delete playlist \"%1%\"?")
% wideShorten(myPlaylistEditor->Playlists.current().value(), COLS-question.size()-10); % wideShorten(myPlaylistEditor->Playlists.current().value().path(), COLS-question.size()-10);
confirmAction(question); confirmAction(question);
auto list = getSelectedOrCurrent( auto list = getSelectedOrCurrent(
myPlaylistEditor->Playlists.begin(), myPlaylistEditor->Playlists.begin(),
@@ -734,7 +734,7 @@ void DeleteStoredPlaylist::run()
myPlaylistEditor->Playlists.currentI() myPlaylistEditor->Playlists.currentI()
); );
for (const auto &item : list) for (const auto &item : list)
Mpd.DeletePlaylist(item->value()); Mpd.DeletePlaylist(item->value().path());
Statusbar::printf("%1% deleted", list.size() == 1 ? "Playlist" : "Playlists"); Statusbar::printf("%1% deleted", list.size() == 1 ? "Playlist" : "Playlists");
// force playlists update. this happens automatically, but only after call // force playlists update. this happens automatically, but only after call
// to Key::read, therefore when we call PlaylistEditor::Update, it won't // to Key::read, therefore when we call PlaylistEditor::Update, it won't
@@ -880,7 +880,7 @@ void MoveSelectedItemsUp::run()
else if (myScreen == myPlaylistEditor) else if (myScreen == myPlaylistEditor)
{ {
assert(!myPlaylistEditor->Playlists.empty()); assert(!myPlaylistEditor->Playlists.empty());
std::string playlist = myPlaylistEditor->Playlists.current().value(); std::string playlist = myPlaylistEditor->Playlists.current().value().path();
auto move_fun = boost::bind(&MPD::Connection::PlaylistMove, _1, playlist, _2, _3); auto move_fun = boost::bind(&MPD::Connection::PlaylistMove, _1, playlist, _2, _3);
moveSelectedItemsUp(myPlaylistEditor->Content, move_fun); moveSelectedItemsUp(myPlaylistEditor->Content, move_fun);
} }
@@ -905,7 +905,7 @@ void MoveSelectedItemsDown::run()
else if (myScreen == myPlaylistEditor) else if (myScreen == myPlaylistEditor)
{ {
assert(!myPlaylistEditor->Playlists.empty()); assert(!myPlaylistEditor->Playlists.empty());
std::string playlist = myPlaylistEditor->Playlists.current().value(); std::string playlist = myPlaylistEditor->Playlists.current().value().path();
auto move_fun = boost::bind(&MPD::Connection::PlaylistMove, _1, playlist, _2, _3); auto move_fun = boost::bind(&MPD::Connection::PlaylistMove, _1, playlist, _2, _3);
moveSelectedItemsDown(myPlaylistEditor->Content, move_fun); moveSelectedItemsDown(myPlaylistEditor->Content, move_fun);
} }
@@ -927,7 +927,7 @@ void MoveSelectedItemsTo::run()
else else
{ {
assert(!myPlaylistEditor->Playlists.empty()); assert(!myPlaylistEditor->Playlists.empty());
std::string playlist = myPlaylistEditor->Playlists.current().value(); std::string playlist = myPlaylistEditor->Playlists.current().value().path();
auto move_fun = boost::bind(&MPD::Connection::PlaylistMove, _1, playlist, _2, _3); auto move_fun = boost::bind(&MPD::Connection::PlaylistMove, _1, playlist, _2, _3);
moveSelectedItemsTo(myPlaylistEditor->Content, move_fun); moveSelectedItemsTo(myPlaylistEditor->Content, move_fun);
} }
@@ -953,7 +953,7 @@ void Add::run()
Statusbar::put() << "Adding..."; Statusbar::put() << "Adding...";
wFooter->refresh(); wFooter->refresh();
if (myScreen == myPlaylistEditor) if (myScreen == myPlaylistEditor)
Mpd.AddToPlaylist(myPlaylistEditor->Playlists.current().value(), path); Mpd.AddToPlaylist(myPlaylistEditor->Playlists.current().value().path(), path);
else else
{ {
const char lastfm_url[] = "lastfm://"; const char lastfm_url[] = "lastfm://";
@@ -1504,7 +1504,7 @@ void EditPlaylistName::run()
// FIXME: support local browser more generally // FIXME: support local browser more generally
std::string old_name, new_name; std::string old_name, new_name;
if (myScreen->isActiveWindow(myPlaylistEditor->Playlists)) if (myScreen->isActiveWindow(myPlaylistEditor->Playlists))
old_name = myPlaylistEditor->Playlists.current().value(); old_name = myPlaylistEditor->Playlists.current().value().path();
else else
old_name = myBrowser->main().current().value().name; old_name = myBrowser->main().current().value().name;
{ {
@@ -1759,7 +1759,7 @@ void CropPlaylist::run()
if (w.size() <= 1) if (w.size() <= 1)
return; return;
assert(!myPlaylistEditor->Playlists.empty()); assert(!myPlaylistEditor->Playlists.empty());
std::string playlist = myPlaylistEditor->Playlists.current().value(); std::string playlist = myPlaylistEditor->Playlists.current().value().path();
if (Config.ask_before_clearing_playlists) if (Config.ask_before_clearing_playlists)
confirmAction(boost::format("Do you really want to crop playlist \"%1%\"?") % playlist); confirmAction(boost::format("Do you really want to crop playlist \"%1%\"?") % playlist);
selectCurrentIfNoneSelected(w); selectCurrentIfNoneSelected(w);
@@ -1789,7 +1789,7 @@ void ClearPlaylist::run()
{ {
if (myPlaylistEditor->Playlists.empty()) if (myPlaylistEditor->Playlists.empty())
return; return;
std::string playlist = myPlaylistEditor->Playlists.current().value(); std::string playlist = myPlaylistEditor->Playlists.current().value().path();
if (Config.ask_before_clearing_playlists) if (Config.ask_before_clearing_playlists)
confirmAction(boost::format("Do you really want to clear playlist \"%1%\"?") % playlist); confirmAction(boost::format("Do you really want to clear playlist \"%1%\"?") % playlist);
auto delete_fun = boost::bind(&MPD::Connection::PlaylistDelete, _1, playlist, _2); auto delete_fun = boost::bind(&MPD::Connection::PlaylistDelete, _1, playlist, _2);

View File

@@ -604,12 +604,12 @@ void Connection::SavePlaylist(const std::string &name)
checkErrors(); checkErrors();
} }
void Connection::GetPlaylists(StringConsumer f) PlaylistIterator Connection::GetPlaylists()
{ {
GetDirectory("/", [&f](Item &&item) { prechecksNoCommandsList();
if (item.type == MPD::Item::Type::Playlist) mpd_send_list_playlists(m_connection.get());
f(std::move(item.name)); checkErrors();
}); return PlaylistIterator(m_connection.get(), mpd_recv_playlist);
} }
void Connection::GetList(mpd_tag_type type, StringConsumer f) void Connection::GetList(mpd_tag_type type, StringConsumer f)

View File

@@ -305,8 +305,9 @@ private:
DestT m_object; DestT m_object;
}; };
typedef Iterator<Song, mpd_song> SongIterator;
typedef Iterator<Output, mpd_output> OutputIterator; typedef Iterator<Output, mpd_output> OutputIterator;
typedef Iterator<Playlist, mpd_playlist> PlaylistIterator;
typedef Iterator<Song, mpd_song> SongIterator;
class Connection class Connection
{ {
@@ -402,7 +403,7 @@ public:
SongIterator CommitSearchSongs(); SongIterator CommitSearchSongs();
void CommitSearchTags(StringConsumer f); void CommitSearchTags(StringConsumer f);
void GetPlaylists(StringConsumer f); PlaylistIterator GetPlaylists();
void GetList(mpd_tag_type type, StringConsumer f); void GetList(mpd_tag_type type, StringConsumer f);
void GetDirectory(const std::string &directory, ItemConsumer f); void GetDirectory(const std::string &directory, ItemConsumer f);
void GetDirectoryRecursive(const std::string &directory, SongConsumer f); void GetDirectoryRecursive(const std::string &directory, SongConsumer f);

View File

@@ -51,7 +51,7 @@ size_t RightColumnStartX;
size_t RightColumnWidth; size_t RightColumnWidth;
std::string SongToString(const MPD::Song &s); std::string SongToString(const MPD::Song &s);
bool PlaylistEntryMatcher(const boost::regex &rx, const std::string &playlist); bool PlaylistEntryMatcher(const boost::regex &rx, const MPD::Playlist &playlist);
bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s); bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s);
} }
@@ -65,14 +65,14 @@ PlaylistEditor::PlaylistEditor()
RightColumnStartX = LeftColumnWidth+1; RightColumnStartX = LeftColumnWidth+1;
RightColumnWidth = COLS-LeftColumnWidth-1; RightColumnWidth = COLS-LeftColumnWidth-1;
Playlists = NC::Menu<std::string>(0, MainStartY, LeftColumnWidth, MainHeight, Config.titles_visibility ? "Playlists" : "", Config.main_color, NC::Border::None); Playlists = NC::Menu<MPD::Playlist>(0, MainStartY, LeftColumnWidth, MainHeight, Config.titles_visibility ? "Playlists" : "", Config.main_color, NC::Border::None);
Playlists.setHighlightColor(Config.active_column_color); Playlists.setHighlightColor(Config.active_column_color);
Playlists.cyclicScrolling(Config.use_cyclic_scrolling); Playlists.cyclicScrolling(Config.use_cyclic_scrolling);
Playlists.centeredCursor(Config.centered_cursor); Playlists.centeredCursor(Config.centered_cursor);
Playlists.setSelectedPrefix(Config.selected_item_prefix); Playlists.setSelectedPrefix(Config.selected_item_prefix);
Playlists.setSelectedSuffix(Config.selected_item_suffix); Playlists.setSelectedSuffix(Config.selected_item_suffix);
Playlists.setItemDisplayer([](NC::Menu<std::string> &menu) { Playlists.setItemDisplayer([](NC::Menu<MPD::Playlist> &menu) {
menu << Charset::utf8ToLocale(menu.drawn()->value()); menu << Charset::utf8ToLocale(menu.drawn()->value().path());
}); });
Content = NC::Menu<MPD::Song>(RightColumnStartX, MainStartY, RightColumnWidth, MainHeight, Config.titles_visibility ? "Playlist content" : "", Config.main_color, NC::Border::None); Content = NC::Menu<MPD::Song>(RightColumnStartX, MainStartY, RightColumnWidth, MainHeight, Config.titles_visibility ? "Playlist content" : "", Config.main_color, NC::Border::None);
@@ -141,13 +141,13 @@ void PlaylistEditor::update()
Playlists.clearSearchResults(); Playlists.clearSearchResults();
withUnfilteredMenuReapplyFilter(Playlists, [this]() { withUnfilteredMenuReapplyFilter(Playlists, [this]() {
size_t idx = 0; size_t idx = 0;
Mpd.GetPlaylists([this, &idx](std::string playlist) { for (MPD::PlaylistIterator it = Mpd.GetPlaylists(), end; it != end; ++it, ++idx)
{
if (idx < Playlists.size()) if (idx < Playlists.size())
Playlists[idx].value() = playlist; Playlists[idx].value() = std::move(*it);
else else
Playlists.addItem(playlist); Playlists.addItem(std::move(*it));
++idx; };
});
if (idx < Playlists.size()) if (idx < Playlists.size())
Playlists.resizeList(idx); Playlists.resizeList(idx);
std::sort(Playlists.beginV(), Playlists.endV(), std::sort(Playlists.beginV(), Playlists.endV(),
@@ -167,7 +167,7 @@ void PlaylistEditor::update()
Content.clearSearchResults(); Content.clearSearchResults();
withUnfilteredMenuReapplyFilter(Content, [this]() { withUnfilteredMenuReapplyFilter(Content, [this]() {
size_t idx = 0; size_t idx = 0;
MPD::SongIterator s = Mpd.GetPlaylistContent(Playlists.current().value()), end; MPD::SongIterator s = Mpd.GetPlaylistContent(Playlists.current().value().path()), end;
for (; s != end; ++s, ++idx) for (; s != end; ++s, ++idx)
{ {
bool is_bold = myPlaylist->checkForSong(*s); bool is_bold = myPlaylist->checkForSong(*s);
@@ -250,7 +250,7 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play)
success = addSongsToPlaylist(Content.beginV(), Content.endV(), add_n_play, -1); success = addSongsToPlaylist(Content.beginV(), Content.endV(), add_n_play, -1);
}); });
Statusbar::printf("Playlist \"%1%\" loaded%2%", Statusbar::printf("Playlist \"%1%\" loaded%2%",
Playlists.current().value(), withErrors(success) Playlists.current().value().path(), withErrors(success)
); );
} }
else if (isActiveWindow(Content) && !Content.empty()) else if (isActiveWindow(Content) && !Content.empty())
@@ -354,7 +354,7 @@ std::string PlaylistEditor::currentFilter()
{ {
std::string filter; std::string filter;
if (isActiveWindow(Playlists)) if (isActiveWindow(Playlists))
filter = RegexFilter<std::string>::currentFilter(Playlists); filter = RegexFilter<MPD::Playlist>::currentFilter(Playlists);
else if (isActiveWindow(Content)) else if (isActiveWindow(Content))
filter = RegexFilter<MPD::Song>::currentFilter(Content); filter = RegexFilter<MPD::Song>::currentFilter(Content);
return filter; return filter;
@@ -381,7 +381,7 @@ void PlaylistEditor::applyFilter(const std::string &filter)
if (isActiveWindow(Playlists)) if (isActiveWindow(Playlists))
{ {
Playlists.showAll(); Playlists.showAll();
auto rx = RegexFilter<std::string>( auto rx = RegexFilter<MPD::Playlist>(
boost::regex(filter, Config.regex_type), PlaylistEntryMatcher); boost::regex(filter, Config.regex_type), PlaylistEntryMatcher);
Playlists.filter(Playlists.begin(), Playlists.end(), rx); Playlists.filter(Playlists.begin(), Playlists.end(), rx);
} }
@@ -418,7 +418,7 @@ bool PlaylistEditor::search(const std::string &constraint)
bool result = false; bool result = false;
if (isActiveWindow(Playlists)) if (isActiveWindow(Playlists))
{ {
auto rx = RegexFilter<std::string>( auto rx = RegexFilter<MPD::Playlist>(
boost::regex(constraint, Config.regex_type), PlaylistEntryMatcher); boost::regex(constraint, Config.regex_type), PlaylistEntryMatcher);
result = Playlists.search(Playlists.begin(), Playlists.end(), rx); result = Playlists.search(Playlists.begin(), Playlists.end(), rx);
} }
@@ -487,7 +487,7 @@ MPD::SongList PlaylistEditor::getSelectedSongs()
{ {
any_selected = true; any_selected = true;
std::copy( std::copy(
std::make_move_iterator(Mpd.GetPlaylistContent(e.value())), std::make_move_iterator(Mpd.GetPlaylistContent(e.value().path())),
std::make_move_iterator(MPD::SongIterator()), std::make_move_iterator(MPD::SongIterator()),
std::back_inserter(result) std::back_inserter(result)
); );
@@ -569,7 +569,7 @@ void PlaylistEditor::Locate(const std::string &name)
update(); update();
for (size_t i = 0; i < Playlists.size(); ++i) for (size_t i = 0; i < Playlists.size(); ++i)
{ {
if (name == Playlists[i].value()) if (name == Playlists[i].value().path())
{ {
Playlists.highlight(i); Playlists.highlight(i);
Content.clear(); Content.clear();
@@ -596,9 +596,9 @@ std::string SongToString(const MPD::Song &s)
return result; return result;
} }
bool PlaylistEntryMatcher(const boost::regex &rx, const std::string &playlist) bool PlaylistEntryMatcher(const boost::regex &rx, const MPD::Playlist &playlist)
{ {
return boost::regex_search(playlist, rx); return boost::regex_search(playlist.path(), rx);
} }
bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s) bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s)

View File

@@ -82,7 +82,7 @@ struct PlaylistEditor: Screen<NC::Window *>, Filterable, HasColumns, HasSongs, S
bool isContentFiltered(); bool isContentFiltered();
ProxySongList contentProxyList(); ProxySongList contentProxyList();
NC::Menu<std::string> Playlists; NC::Menu<MPD::Playlist> Playlists;
NC::Menu<MPD::Song> Content; NC::Menu<MPD::Song> Content;
protected: protected:

View File

@@ -200,11 +200,12 @@ void SelectedItemsAdder::populatePlaylistSelector(BaseScreen *old_screen)
if (!in_local_browser) if (!in_local_browser)
{ {
size_t begin = m_playlist_selector.size(); size_t begin = m_playlist_selector.size();
Mpd.GetPlaylists([this](std::string playlist) { for (MPD::PlaylistIterator it = Mpd.GetPlaylists(), end; it != end; ++it)
m_playlist_selector.addItem(Entry(playlist, {
boost::bind(&Self::addToExistingPlaylist, this, playlist) m_playlist_selector.addItem(Entry(it->path(),
boost::bind(&Self::addToExistingPlaylist, this, it->path())
)); ));
}); };
std::sort(m_playlist_selector.beginV()+begin, m_playlist_selector.endV(), std::sort(m_playlist_selector.beginV()+begin, m_playlist_selector.endV(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the)); LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
if (begin < m_playlist_selector.size()) if (begin < m_playlist_selector.size())

View File

@@ -35,10 +35,8 @@ bool hasTheWord(const std::string &s)
} }
int LocaleStringComparison::operator()(const std::string &a, const std::string &b) const int LocaleStringComparison::compare(const char *a, size_t a_len, const char *b, size_t b_len) const
{ {
const char *ac = a.c_str();
const char *bc = b.c_str();
size_t ac_off = 0, bc_off = 0; size_t ac_off = 0, bc_off = 0;
if (m_ignore_the) if (m_ignore_the)
{ {
@@ -48,7 +46,7 @@ int LocaleStringComparison::operator()(const std::string &a, const std::string &
bc_off += 4; bc_off += 4;
} }
return std::use_facet<std::collate<char>>(m_locale).compare( return std::use_facet<std::collate<char>>(m_locale).compare(
ac+ac_off, ac+a.length(), bc+bc_off, bc+b.length() a+ac_off, a+a_len, b+bc_off, b+b_len
); );
} }

View File

@@ -35,8 +35,15 @@ class LocaleStringComparison
public: public:
LocaleStringComparison(const std::locale &loc, bool ignore_the) LocaleStringComparison(const std::locale &loc, bool ignore_the)
: m_locale(loc), m_ignore_the(ignore_the) { } : m_locale(loc), m_ignore_the(ignore_the) { }
int operator()(const std::string &a, const std::string &b) const; int operator()(const char *a, const char *b) const {
return compare(a, strlen(a), b, strlen(b));
}
int operator()(const std::string &a, const std::string &b) const {
return compare(a.c_str(), a.length(), b.c_str(), b.length());
}
int compare(const char *a, size_t a_len, const char *b, size_t b_len) const;
}; };
class LocaleBasedSorting class LocaleBasedSorting
@@ -50,6 +57,10 @@ public:
return m_cmp(a, b) < 0; return m_cmp(a, b) < 0;
} }
bool operator()(const MPD::Playlist &a, const MPD::Playlist &b) const {
return m_cmp(a.path(), b.path()) < 0;
}
bool operator()(const MPD::Song &a, const MPD::Song &b) const { bool operator()(const MPD::Song &a, const MPD::Song &b) const {
return m_cmp(a.getName(), b.getName()) < 0; return m_cmp(a.getName(), b.getName()) < 0;
} }