configure: explicitly link with boost.system
[ncmpcpp/stream.git] / src / sort_playlist.cpp
blobf2c75cd8c5d165631b98a0f2f4c8101c4716d959
1 /***************************************************************************
2 * Copyright (C) 2008-2013 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 "charset.h"
22 #include "display.h"
23 #include "global.h"
24 #include "helpers.h"
25 #include "playlist.h"
26 #include "settings.h"
27 #include "sort_playlist.h"
28 #include "statusbar.h"
29 #include "utility/comparators.h"
30 #include "screen_switcher.h"
32 SortPlaylistDialog *mySortPlaylistDialog;
34 SortPlaylistDialog::SortPlaylistDialog()
36 typedef WindowType::Item::Type Entry;
38 using Global::MainHeight;
39 using Global::MainStartY;
41 setDimensions();
42 w = WindowType((COLS-m_width)/2, (MainHeight-m_height)/2+MainStartY, m_width, m_height, "Sort songs by...", Config.main_color, Config.window_border);
43 w.cyclicScrolling(Config.use_cyclic_scrolling);
44 w.centeredCursor(Config.centered_cursor);
45 w.setItemDisplayer([](Self::WindowType &menu) {
46 menu << Charset::utf8ToLocale(menu.drawn()->value().item().first);
47 });
49 w.addItem(Entry(std::make_pair("Artist", &MPD::Song::getArtist),
50 std::bind(&Self::moveSortOrderHint, this)
51 ));
52 w.addItem(Entry(std::make_pair("Album", &MPD::Song::getAlbum),
53 std::bind(&Self::moveSortOrderHint, this)
54 ));
55 w.addItem(Entry(std::make_pair("Disc", &MPD::Song::getDisc),
56 std::bind(&Self::moveSortOrderHint, this)
57 ));
58 w.addItem(Entry(std::make_pair("Track", &MPD::Song::getTrack),
59 std::bind(&Self::moveSortOrderHint, this)
60 ));
61 w.addItem(Entry(std::make_pair("Genre", &MPD::Song::getGenre),
62 std::bind(&Self::moveSortOrderHint, this)
63 ));
64 w.addItem(Entry(std::make_pair("Date", &MPD::Song::getDate),
65 std::bind(&Self::moveSortOrderHint, this)
66 ));
67 w.addItem(Entry(std::make_pair("Composer", &MPD::Song::getComposer),
68 std::bind(&Self::moveSortOrderHint, this)
69 ));
70 w.addItem(Entry(std::make_pair("Performer", &MPD::Song::getPerformer),
71 std::bind(&Self::moveSortOrderHint, this)
72 ));
73 w.addItem(Entry(std::make_pair("Title", &MPD::Song::getTitle),
74 std::bind(&Self::moveSortOrderHint, this)
75 ));
76 w.addItem(Entry(std::make_pair("Filename", &MPD::Song::getURI),
77 std::bind(&Self::moveSortOrderHint, this)
78 ));
79 w.addSeparator();
80 w.addItem(Entry(std::make_pair("Sort", static_cast<MPD::Song::GetFunction>(0)),
81 std::bind(&Self::sort, this)
82 ));
83 w.addItem(Entry(std::make_pair("Cancel", static_cast<MPD::Song::GetFunction>(0)),
84 std::bind(&Self::cancel, this)
85 ));
88 void SortPlaylistDialog::switchTo()
90 SwitchTo::execute(this);
91 w.reset();
94 void SortPlaylistDialog::resize()
96 using Global::MainHeight;
97 using Global::MainStartY;
98 setDimensions();
99 w.resize(m_width, m_height);
100 w.moveTo((COLS-m_width)/2, (MainHeight-m_height)/2+MainStartY);
101 hasToBeResized = false;
104 std::wstring SortPlaylistDialog::title()
106 return previousScreen()->title();
109 void SortPlaylistDialog::enterPressed()
111 w.current().value().exec()();
114 void SortPlaylistDialog::mouseButtonPressed(MEVENT me)
116 if (w.hasCoords(me.x, me.y))
118 if (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED))
120 w.Goto(me.y);
121 if (me.bstate & BUTTON3_PRESSED)
122 enterPressed();
124 else
125 Screen<WindowType>::mouseButtonPressed(me);
129 void SortPlaylistDialog::moveSortOrderDown()
131 auto cur = w.currentVI();
132 if ((cur+1)->item().second)
134 std::iter_swap(cur, cur+1);
135 w.scroll(NC::Scroll::Down);
139 void SortPlaylistDialog::moveSortOrderUp()
141 auto cur = w.currentVI();
142 if (cur > w.beginV() && cur->item().second)
144 std::iter_swap(cur, cur-1);
145 w.scroll(NC::Scroll::Up);
149 void SortPlaylistDialog::moveSortOrderHint() const
151 Statusbar::msg("Move tag types up and down to adjust sort order");
154 void SortPlaylistDialog::sort() const
156 auto &pl = myPlaylist->main();
157 auto begin = pl.begin(), end = pl.end();
158 std::tie(begin, end) = getSelectedRange(begin, end);
160 size_t start_pos = begin - pl.begin();
161 MPD::SongList playlist;
162 playlist.reserve(end - begin);
163 for (; begin != end; ++begin)
164 playlist.push_back(begin->value());
166 typedef MPD::SongList::iterator Iterator;
167 LocaleStringComparison cmp(std::locale(), Config.ignore_leading_the);
168 std::function<void(Iterator, Iterator)> iter_swap, quick_sort;
169 auto song_cmp = [this, &cmp](const MPD::Song &a, const MPD::Song &b) -> bool {
170 for (auto it = w.beginV(); it->item().second; ++it)
172 int res = cmp(a.getTags(it->item().second, Config.tags_separator),
173 b.getTags(it->item().second, Config.tags_separator));
174 if (res != 0)
175 return res < 0;
177 return a.getPosition() < b.getPosition();
179 iter_swap = [&playlist, &start_pos](Iterator a, Iterator b) {
180 std::iter_swap(a, b);
181 Mpd.Swap(start_pos+a-playlist.begin(), start_pos+b-playlist.begin());
183 quick_sort = [this, &song_cmp, &quick_sort, &iter_swap](Iterator first, Iterator last) {
184 if (last-first > 1)
186 Iterator pivot = first+rand()%(last-first);
187 iter_swap(pivot, last-1);
188 pivot = last-1;
190 Iterator tmp = first;
191 for (Iterator i = first; i != pivot; ++i)
192 if (song_cmp(*i, *pivot))
193 iter_swap(i, tmp++);
194 iter_swap(tmp, pivot);
196 quick_sort(first, tmp);
197 quick_sort(tmp+1, last);
201 Statusbar::msg("Sorting...");
202 Mpd.StartCommandsList();
203 quick_sort(playlist.begin(), playlist.end());
204 Mpd.CommitCommandsList();
205 Statusbar::msg("Playlist sorted");
206 switchToPreviousScreen();
209 void SortPlaylistDialog::cancel() const
211 switchToPreviousScreen();
214 void SortPlaylistDialog::setDimensions()
216 m_height = std::min(size_t(17), Global::MainHeight);
217 m_width = 30;