playlist editor: add data fetch delay after position changes
[ncmpcpp.git] / src / playlist_editor.cpp
blob009eedd93792263a1158eab5681a9940932d0510
1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <algorithm>
22 #include <boost/bind.hpp>
23 #include <cassert>
25 #include "charset.h"
26 #include "display.h"
27 #include "global.h"
28 #include "helpers.h"
29 #include "playlist.h"
30 #include "playlist_editor.h"
31 #include "mpdpp.h"
32 #include "regex_filter.h"
33 #include "status.h"
34 #include "statusbar.h"
35 #include "tag_editor.h"
36 #include "utility/comparators.h"
37 #include "title.h"
38 #include "screen_switcher.h"
40 using Global::MainHeight;
41 using Global::MainStartY;
43 PlaylistEditor *myPlaylistEditor;
45 namespace {
47 const auto fetch_delay = boost::posix_time::milliseconds(500);
49 size_t LeftColumnStartX;
50 size_t LeftColumnWidth;
51 size_t RightColumnStartX;
52 size_t RightColumnWidth;
54 std::string SongToString(const MPD::Song &s);
55 bool PlaylistEntryMatcher(const boost::regex &rx, const std::string &playlist);
56 bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s);
60 PlaylistEditor::PlaylistEditor()
61 : m_timer(boost::posix_time::from_time_t(0))
63 LeftColumnWidth = COLS/3-1;
64 RightColumnStartX = LeftColumnWidth+1;
65 RightColumnWidth = COLS-LeftColumnWidth-1;
67 Playlists = NC::Menu<std::string>(0, MainStartY, LeftColumnWidth, MainHeight, Config.titles_visibility ? "Playlists" : "", Config.main_color, NC::Border::None);
68 Playlists.setHighlightColor(Config.active_column_color);
69 Playlists.cyclicScrolling(Config.use_cyclic_scrolling);
70 Playlists.centeredCursor(Config.centered_cursor);
71 Playlists.setSelectedPrefix(Config.selected_item_prefix);
72 Playlists.setSelectedSuffix(Config.selected_item_suffix);
73 Playlists.setItemDisplayer([](NC::Menu<std::string> &menu) {
74 menu << Charset::utf8ToLocale(menu.drawn()->value());
75 });
77 Content = NC::Menu<MPD::Song>(RightColumnStartX, MainStartY, RightColumnWidth, MainHeight, Config.titles_visibility ? "Playlist content" : "", Config.main_color, NC::Border::None);
78 Content.setHighlightColor(Config.main_highlight_color);
79 Content.cyclicScrolling(Config.use_cyclic_scrolling);
80 Content.centeredCursor(Config.centered_cursor);
81 Content.setSelectedPrefix(Config.selected_item_prefix);
82 Content.setSelectedSuffix(Config.selected_item_suffix);
83 switch (Config.playlist_editor_display_mode)
85 case DisplayMode::Classic:
86 Content.setItemDisplayer(boost::bind(Display::Songs, _1, contentProxyList(), Config.song_list_format));
87 break;
88 case DisplayMode::Columns:
89 Content.setItemDisplayer(boost::bind(Display::SongsInColumns, _1, contentProxyList()));
90 break;
93 w = &Playlists;
96 void PlaylistEditor::resize()
98 size_t x_offset, width;
99 getWindowResizeParams(x_offset, width);
101 LeftColumnStartX = x_offset;
102 LeftColumnWidth = width/3-1;
103 RightColumnStartX = LeftColumnStartX+LeftColumnWidth+1;
104 RightColumnWidth = width-LeftColumnWidth-1;
106 Playlists.resize(LeftColumnWidth, MainHeight);
107 Content.resize(RightColumnWidth, MainHeight);
109 Playlists.moveTo(LeftColumnStartX, MainStartY);
110 Content.moveTo(RightColumnStartX, MainStartY);
112 hasToBeResized = 0;
115 std::wstring PlaylistEditor::title()
117 return L"Playlist editor";
120 void PlaylistEditor::refresh()
122 Playlists.display();
123 mvvline(MainStartY, RightColumnStartX-1, 0, MainHeight);
124 Content.display();
127 void PlaylistEditor::switchTo()
129 SwitchTo::execute(this);
130 markSongsInPlaylist(contentProxyList());
131 drawHeader();
132 refresh();
135 void PlaylistEditor::update()
137 if (Playlists.reallyEmpty() || m_playlists_update_requested)
139 m_playlists_update_requested = false;
140 Playlists.clearSearchResults();
141 withUnfilteredMenuReapplyFilter(Playlists, [this]() {
142 size_t idx = 0;
143 Mpd.GetPlaylists([this, &idx](std::string playlist) {
144 if (idx < Playlists.size())
145 Playlists[idx].value() = playlist;
146 else
147 Playlists.addItem(playlist);
148 ++idx;
150 if (idx < Playlists.size())
151 Playlists.resizeList(idx);
152 std::sort(Playlists.beginV(), Playlists.endV(),
153 LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
155 Playlists.refresh();
158 if (!Playlists.empty()
159 && ((Content.reallyEmpty() && Global::Timer - m_timer > fetch_delay) || m_content_update_requested)
162 m_content_update_requested = false;
163 Content.clearSearchResults();
164 withUnfilteredMenuReapplyFilter(Content, [this]() {
165 size_t idx = 0;
166 Mpd.GetPlaylistContent(Playlists.current().value(), [this, &idx](MPD::Song s) {
167 if (idx < Content.size())
169 Content[idx].value() = s;
170 Content[idx].setBold(myPlaylist->checkForSong(s));
172 else
173 Content.addItem(s, myPlaylist->checkForSong(s));
174 ++idx;
176 if (idx < Content.size())
177 Content.resizeList(idx);
178 std::string title;
179 if (Config.titles_visibility)
181 title = "Playlist content";
182 title += " (";
183 title += boost::lexical_cast<std::string>(Content.size());
184 title += " item";
185 if (Content.size() == 1)
186 title += ")";
187 else
188 title += "s)";
189 title.resize(Content.getWidth());
191 Content.setTitle(title);
193 Content.display();
196 if (isActiveWindow(Content) && Content.reallyEmpty())
198 Content.setHighlightColor(Config.main_highlight_color);
199 Playlists.setHighlightColor(Config.active_column_color);
200 w = &Playlists;
203 if (Playlists.empty() && Content.reallyEmpty())
205 Content.Window::clear();
206 Content.Window::display();
210 bool PlaylistEditor::isContentFiltered()
212 if (Content.isFiltered())
214 Statusbar::print("Function currently unavailable due to filtered playlist content");
215 return true;
217 return false;
220 ProxySongList PlaylistEditor::contentProxyList()
222 return ProxySongList(Content, [](NC::Menu<MPD::Song>::Item &item) {
223 return &item.value();
227 void PlaylistEditor::AddToPlaylist(bool add_n_play)
229 MPD::SongList list;
231 if (isActiveWindow(Playlists) && !Playlists.empty())
233 bool success;
234 withUnfilteredMenu(Content, [&]() {
235 success = addSongsToPlaylist(Content.beginV(), Content.endV(), add_n_play, -1);
237 Statusbar::printf("Playlist \"%1%\" loaded%2%",
238 Playlists.current().value(), withErrors(success)
241 else if (isActiveWindow(Content) && !Content.empty())
242 addSongToPlaylist(Content.current().value(), add_n_play);
244 if (!add_n_play)
245 w->scroll(NC::Scroll::Down);
248 void PlaylistEditor::enterPressed()
250 AddToPlaylist(true);
253 void PlaylistEditor::spacePressed()
255 if (Config.space_selects)
257 if (isActiveWindow(Playlists))
259 if (!Playlists.empty())
261 Playlists.current().setSelected(!Playlists.current().isSelected());
262 Playlists.scroll(NC::Scroll::Down);
265 else if (isActiveWindow(Content))
267 if (!Content.empty())
269 Content.current().setSelected(!Content.current().isSelected());
270 Content.scroll(NC::Scroll::Down);
274 else
275 AddToPlaylist(false);
278 void PlaylistEditor::mouseButtonPressed(MEVENT me)
280 if (!Playlists.empty() && Playlists.hasCoords(me.x, me.y))
282 if (!isActiveWindow(Playlists))
284 if (previousColumnAvailable())
285 previousColumn();
286 else
287 return;
289 if (size_t(me.y) < Playlists.size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
291 Playlists.Goto(me.y);
292 if (me.bstate & BUTTON3_PRESSED)
294 size_t pos = Playlists.choice();
295 spacePressed();
296 if (pos < Playlists.size()-1)
297 Playlists.scroll(NC::Scroll::Up);
300 else
301 Screen<WindowType>::mouseButtonPressed(me);
302 Content.clear();
304 else if (!Content.empty() && Content.hasCoords(me.x, me.y))
306 if (!isActiveWindow(Content))
308 if (nextColumnAvailable())
309 nextColumn();
310 else
311 return;
313 if (size_t(me.y) < Content.size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
315 Content.Goto(me.y);
316 if (me.bstate & BUTTON1_PRESSED)
318 size_t pos = Content.choice();
319 spacePressed();
320 if (pos < Content.size()-1)
321 Content.scroll(NC::Scroll::Up);
323 else
324 enterPressed();
326 else
327 Screen<WindowType>::mouseButtonPressed(me);
331 /***********************************************************************/
333 bool PlaylistEditor::allowsFiltering()
335 return true;
338 std::string PlaylistEditor::currentFilter()
340 std::string filter;
341 if (isActiveWindow(Playlists))
342 filter = RegexFilter<std::string>::currentFilter(Playlists);
343 else if (isActiveWindow(Content))
344 filter = RegexFilter<MPD::Song>::currentFilter(Content);
345 return filter;
348 void PlaylistEditor::applyFilter(const std::string &filter)
350 if (filter.empty())
352 if (isActiveWindow(Playlists))
354 Playlists.clearFilter();
355 Playlists.clearFilterResults();
357 else if (isActiveWindow(Content))
359 Content.clearFilter();
360 Content.clearFilterResults();
362 return;
366 if (isActiveWindow(Playlists))
368 Playlists.showAll();
369 auto rx = RegexFilter<std::string>(
370 boost::regex(filter, Config.regex_type), PlaylistEntryMatcher);
371 Playlists.filter(Playlists.begin(), Playlists.end(), rx);
373 else if (isActiveWindow(Content))
375 Content.showAll();
376 auto rx = RegexFilter<MPD::Song>(
377 boost::regex(filter, Config.regex_type), SongEntryMatcher);
378 Content.filter(Content.begin(), Content.end(), rx);
381 catch (boost::bad_expression &) { }
384 /***********************************************************************/
386 bool PlaylistEditor::allowsSearching()
388 return true;
391 bool PlaylistEditor::search(const std::string &constraint)
393 if (constraint.empty())
395 if (isActiveWindow(Playlists))
396 Playlists.clearSearchResults();
397 else if (isActiveWindow(Content))
398 Content.clearSearchResults();
399 return false;
403 bool result = false;
404 if (isActiveWindow(Playlists))
406 auto rx = RegexFilter<std::string>(
407 boost::regex(constraint, Config.regex_type), PlaylistEntryMatcher);
408 result = Playlists.search(Playlists.begin(), Playlists.end(), rx);
410 else if (isActiveWindow(Content))
412 auto rx = RegexFilter<MPD::Song>(
413 boost::regex(constraint, Config.regex_type), SongEntryMatcher);
414 result = Content.search(Content.begin(), Content.end(), rx);
416 return result;
418 catch (boost::bad_expression &)
420 return false;
424 void PlaylistEditor::nextFound(bool wrap)
426 if (isActiveWindow(Playlists))
427 Playlists.nextFound(wrap);
428 else if (isActiveWindow(Content))
429 Content.nextFound(wrap);
432 void PlaylistEditor::prevFound(bool wrap)
434 if (isActiveWindow(Playlists))
435 Playlists.prevFound(wrap);
436 else if (isActiveWindow(Content))
437 Content.prevFound(wrap);
440 /***********************************************************************/
442 ProxySongList PlaylistEditor::proxySongList()
444 auto ptr = ProxySongList();
445 if (isActiveWindow(Content))
446 ptr = contentProxyList();
447 return ptr;
450 bool PlaylistEditor::allowsSelection()
452 return true;
455 void PlaylistEditor::reverseSelection()
457 if (isActiveWindow(Playlists))
458 reverseSelectionHelper(Playlists.begin(), Playlists.end());
459 else if (isActiveWindow(Content))
460 reverseSelectionHelper(Content.begin(), Content.end());
463 MPD::SongList PlaylistEditor::getSelectedSongs()
465 MPD::SongList result;
466 if (isActiveWindow(Playlists))
468 bool any_selected = false;
469 for (auto &e : Playlists)
471 if (e.isSelected())
473 any_selected = true;
474 Mpd.GetPlaylistContent(e.value(), vectorMoveInserter(result));
477 // if no item is selected, add songs from right column
478 if (!any_selected && !Content.empty())
480 withUnfilteredMenu(Content, [this, &result]() {
481 result.insert(result.end(), Content.beginV(), Content.endV());
485 else if (isActiveWindow(Content))
487 for (auto &e : Content)
488 if (e.isSelected())
489 result.push_back(e.value());
490 // if no item is selected, add current one
491 if (result.empty() && !Content.empty())
492 result.push_back(Content.current().value());
494 return result;
497 /***********************************************************************/
499 bool PlaylistEditor::previousColumnAvailable()
501 if (isActiveWindow(Content))
503 if (!Playlists.reallyEmpty())
504 return true;
506 return false;
509 void PlaylistEditor::previousColumn()
511 if (isActiveWindow(Content))
513 Content.setHighlightColor(Config.main_highlight_color);
514 w->refresh();
515 w = &Playlists;
516 Playlists.setHighlightColor(Config.active_column_color);
520 bool PlaylistEditor::nextColumnAvailable()
522 if (isActiveWindow(Playlists))
524 if (!Content.reallyEmpty())
525 return true;
527 return false;
530 void PlaylistEditor::nextColumn()
532 if (isActiveWindow(Playlists))
534 Playlists.setHighlightColor(Config.main_highlight_color);
535 w->refresh();
536 w = &Content;
537 Content.setHighlightColor(Config.active_column_color);
541 /***********************************************************************/
543 void PlaylistEditor::updateTimer()
545 m_timer = Global::Timer;
548 void PlaylistEditor::Locate(const std::string &name)
550 update();
551 for (size_t i = 0; i < Playlists.size(); ++i)
553 if (name == Playlists[i].value())
555 Playlists.highlight(i);
556 Content.clear();
557 break;
560 switchTo();
563 namespace {//
565 std::string SongToString(const MPD::Song &s)
567 std::string result;
568 switch (Config.playlist_display_mode)
570 case DisplayMode::Classic:
571 result = s.toString(Config.song_list_format_dollar_free, Config.tags_separator);
572 break;
573 case DisplayMode::Columns:
574 result = s.toString(Config.song_in_columns_to_string_format, Config.tags_separator);
575 break;
577 return result;
580 bool PlaylistEntryMatcher(const boost::regex &rx, const std::string &playlist)
582 return boost::regex_search(playlist, rx);
585 bool SongEntryMatcher(const boost::regex &rx, const MPD::Song &s)
587 return boost::regex_search(SongToString(s), rx);