Move ncurses related files to curses directory
[ncmpcpp.git] / src / playlist.cpp
blobd143cbee3267b3c4cf75e03cbffb5ae29c42ecf5
1 /***************************************************************************
2 * Copyright (C) 2008-2016 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/date_time/posix_time/posix_time.hpp>
23 #include <sstream>
25 #include "curses/menu_impl.h"
26 #include "display.h"
27 #include "global.h"
28 #include "helpers.h"
29 #include "playlist.h"
30 #include "screen_switcher.h"
31 #include "song.h"
32 #include "status.h"
33 #include "statusbar.h"
34 #include "helpers/song_iterator_maker.h"
35 #include "utility/comparators.h"
36 #include "utility/functional.h"
37 #include "title.h"
39 using Global::MainHeight;
40 using Global::MainStartY;
42 namespace ph = std::placeholders;
44 Playlist *myPlaylist;
46 namespace {
48 std::string songToString(const MPD::Song &s);
49 bool playlistEntryMatcher(const Regex::Regex &rx, const MPD::Song &s);
53 Playlist::Playlist()
54 : m_total_length(0), m_remaining_time(0), m_scroll_begin(0)
55 , m_timer(boost::posix_time::from_time_t(0))
56 , m_reload_total_length(false), m_reload_remaining(false)
58 w = NC::Menu<MPD::Song>(0, MainStartY, COLS, MainHeight, Config.playlist_display_mode == DisplayMode::Columns && Config.titles_visibility ? Display::Columns(COLS) : "", Config.main_color, NC::Border());
59 w.cyclicScrolling(Config.use_cyclic_scrolling);
60 w.centeredCursor(Config.centered_cursor);
61 w.setHighlightColor(Config.main_highlight_color);
62 w.setSelectedPrefix(Config.selected_item_prefix);
63 w.setSelectedSuffix(Config.selected_item_suffix);
64 switch (Config.playlist_display_mode)
66 case DisplayMode::Classic:
67 w.setItemDisplayer(std::bind(
68 Display::Songs, ph::_1, std::cref(w), std::cref(Config.song_list_format)
69 ));
70 break;
71 case DisplayMode::Columns:
72 w.setItemDisplayer(std::bind(
73 Display::SongsInColumns, ph::_1, std::cref(w)
74 ));
75 break;
79 void Playlist::switchTo()
81 SwitchTo::execute(this);
82 m_scroll_begin = 0;
83 drawHeader();
86 void Playlist::resize()
88 size_t x_offset, width;
89 getWindowResizeParams(x_offset, width);
90 w.resize(width, MainHeight);
91 w.moveTo(x_offset, MainStartY);
93 switch (Config.playlist_display_mode)
95 case DisplayMode::Columns:
96 if (Config.titles_visibility)
98 w.setTitle(Display::Columns(w.getWidth()));
99 break;
101 case DisplayMode::Classic:
102 w.setTitle("");
105 hasToBeResized = 0;
108 std::wstring Playlist::title()
110 std::wstring result = L"Playlist ";
111 if (Config.playlist_show_mpd_host)
113 result += L"on ";
114 result += ToWString(Mpd.GetHostname());
115 result += L" ";
117 if (m_reload_total_length || m_reload_remaining)
118 m_stats = getTotalLength();
119 result += Scroller(ToWString(m_stats), m_scroll_begin, COLS-result.length()-(Config.design == Design::Alternative ? 2 : Global::VolumeState.length()));
120 return result;
123 void Playlist::update()
125 if (w.isHighlighted()
126 && Config.playlist_disable_highlight_delay.time_duration::seconds() > 0
127 && Global::Timer - m_timer > Config.playlist_disable_highlight_delay)
129 w.setHighlighting(false);
130 w.refresh();
134 void Playlist::mouseButtonPressed(MEVENT me)
136 if (!w.empty() && w.hasCoords(me.x, me.y))
138 if (size_t(me.y) < w.size() && (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED)))
140 w.Goto(me.y);
141 if (me.bstate & BUTTON3_PRESSED)
142 addItemToPlaylist(true);
144 else
145 Screen<WindowType>::mouseButtonPressed(me);
149 /***********************************************************************/
151 bool Playlist::allowsSearching()
153 return true;
156 const std::string &Playlist::searchConstraint()
158 return m_search_predicate.constraint();
161 void Playlist::setSearchConstraint(const std::string &constraint)
163 m_search_predicate = Regex::Filter<MPD::Song>(
164 constraint,
165 Config.regex_type,
166 playlistEntryMatcher);
169 void Playlist::clearSearchConstraint()
171 m_search_predicate.clear();
174 bool Playlist::search(SearchDirection direction, bool wrap, bool skip_current)
176 return ::search(w, m_search_predicate, direction, wrap, skip_current);
179 /***********************************************************************/
181 bool Playlist::allowsFiltering()
183 return allowsSearching();
186 std::string Playlist::currentFilter()
188 std::string result;
189 if (auto pred = w.filterPredicate<Regex::Filter<MPD::Song>>())
190 result = pred->constraint();
191 return result;
194 void Playlist::applyFilter(const std::string &constraint)
196 if (!constraint.empty())
198 w.applyFilter(Regex::Filter<MPD::Song>(
199 constraint,
200 Config.regex_type,
201 playlistEntryMatcher));
203 else
204 w.clearFilter();
207 /***********************************************************************/
209 bool Playlist::itemAvailable()
211 return !w.empty();
214 bool Playlist::addItemToPlaylist(bool play)
216 if (play)
217 Mpd.PlayID(w.currentV()->getID());
218 return true;
221 std::vector<MPD::Song> Playlist::getSelectedSongs()
223 return w.getSelectedSongs();
226 /***********************************************************************/
228 MPD::Song Playlist::nowPlayingSong()
230 MPD::Song s;
231 if (Status::State::player() != MPD::psUnknown)
233 ScopedUnfilteredMenu<MPD::Song> sunfilter(ReapplyFilter::No, w);
234 auto sp = Status::State::currentSongPosition();
235 if (sp >= 0 && size_t(sp) < w.size())
236 s = w.at(sp).value();
238 return s;
241 void Playlist::locateSong(const MPD::Song &s)
243 if (!w.isFiltered())
244 w.highlight(s.getPosition());
245 else
247 auto cmp = [](const MPD::Song &a, const MPD::Song &b) {
248 return a.getPosition() < b.getPosition();
250 auto first = w.beginV(), last = w.endV();
251 auto it = std::lower_bound(first, last, s, cmp);
252 if (it != last && it->getPosition() == s.getPosition())
253 w.highlight(it - first);
254 else
255 Statusbar::print("Song is filtered out");
259 void Playlist::enableHighlighting()
261 w.setHighlighting(true);
262 m_timer = Global::Timer;
265 std::string Playlist::getTotalLength()
267 std::ostringstream result;
269 if (m_reload_total_length)
271 m_total_length = 0;
272 for (const auto &s : w)
273 m_total_length += s.value().getDuration();
274 m_reload_total_length = false;
276 if (Config.playlist_show_remaining_time && m_reload_remaining)
278 ScopedUnfilteredMenu<MPD::Song> sunfilter(ReapplyFilter::No, w);
279 m_remaining_time = 0;
280 for (size_t i = Status::State::currentSongPosition(); i < w.size(); ++i)
281 m_remaining_time += w[i].value().getDuration();
282 m_reload_remaining = false;
285 result << '(' << w.size() << (w.size() == 1 ? " item" : " items");
287 if (w.isFiltered())
289 ScopedUnfilteredMenu<MPD::Song> sunfilter(ReapplyFilter::No, w);
290 result << " (out of " << w.size() << ")";
293 if (m_total_length)
295 result << ", length: ";
296 ShowTime(result, m_total_length, Config.playlist_shorten_total_times);
298 if (Config.playlist_show_remaining_time && m_remaining_time && w.size() > 1)
300 result << ", remaining: ";
301 ShowTime(result, m_remaining_time, Config.playlist_shorten_total_times);
303 result << ')';
304 return result.str();
307 void Playlist::setSelectedItemsPriority(int prio)
309 auto list = getSelectedOrCurrent(w.begin(), w.end(), w.current());
310 Mpd.StartCommandsList();
311 for (auto it = list.begin(); it != list.end(); ++it)
312 Mpd.SetPriority((*it)->value(), prio);
313 Mpd.CommitCommandsList();
314 Statusbar::print("Priority set");
317 bool Playlist::checkForSong(const MPD::Song &s)
319 return m_song_refs.find(s) != m_song_refs.end();
322 void Playlist::registerSong(const MPD::Song &s)
324 ++m_song_refs[s];
327 void Playlist::unregisterSong(const MPD::Song &s)
329 auto it = m_song_refs.find(s);
330 assert(it != m_song_refs.end());
331 if (it->second == 1)
332 m_song_refs.erase(it);
333 else
334 --it->second;
337 namespace {
339 std::string songToString(const MPD::Song &s)
341 std::string result;
342 switch (Config.playlist_display_mode)
344 case DisplayMode::Classic:
345 result = Format::stringify<char>(Config.song_list_format, &s);
346 break;
347 case DisplayMode::Columns:
348 result = Format::stringify<char>(Config.song_columns_mode_format, &s);
350 return result;
353 bool playlistEntryMatcher(const Regex::Regex &rx, const MPD::Song &s)
355 return Regex::search(songToString(s), rx);