1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 <boost/bind.hpp>
29 #include "sort_playlist.h"
30 #include "statusbar.h"
31 #include "utility/comparators.h"
32 #include "screen_switcher.h"
34 SortPlaylistDialog
*mySortPlaylistDialog
;
36 SortPlaylistDialog::SortPlaylistDialog()
38 typedef WindowType::Item::Type Entry
;
40 using Global::MainHeight
;
41 using Global::MainStartY
;
44 w
= WindowType((COLS
-m_width
)/2, (MainHeight
-m_height
)/2+MainStartY
, m_width
, m_height
, "Sort songs by...", Config
.main_color
, Config
.window_border
);
45 w
.cyclicScrolling(Config
.use_cyclic_scrolling
);
46 w
.centeredCursor(Config
.centered_cursor
);
47 w
.setItemDisplayer([](Self::WindowType
&menu
) {
48 menu
<< Charset::utf8ToLocale(menu
.drawn()->value().item().first
);
51 w
.addItem(Entry(std::make_pair("Artist", &MPD::Song::getArtist
),
52 boost::bind(&Self::moveSortOrderHint
, this)
54 w
.addItem(Entry(std::make_pair("Album", &MPD::Song::getAlbum
),
55 boost::bind(&Self::moveSortOrderHint
, this)
57 w
.addItem(Entry(std::make_pair("Disc", &MPD::Song::getDisc
),
58 boost::bind(&Self::moveSortOrderHint
, this)
60 w
.addItem(Entry(std::make_pair("Track", &MPD::Song::getTrack
),
61 boost::bind(&Self::moveSortOrderHint
, this)
63 w
.addItem(Entry(std::make_pair("Genre", &MPD::Song::getGenre
),
64 boost::bind(&Self::moveSortOrderHint
, this)
66 w
.addItem(Entry(std::make_pair("Date", &MPD::Song::getDate
),
67 boost::bind(&Self::moveSortOrderHint
, this)
69 w
.addItem(Entry(std::make_pair("Composer", &MPD::Song::getComposer
),
70 boost::bind(&Self::moveSortOrderHint
, this)
72 w
.addItem(Entry(std::make_pair("Performer", &MPD::Song::getPerformer
),
73 boost::bind(&Self::moveSortOrderHint
, this)
75 w
.addItem(Entry(std::make_pair("Title", &MPD::Song::getTitle
),
76 boost::bind(&Self::moveSortOrderHint
, this)
78 w
.addItem(Entry(std::make_pair("Filename", &MPD::Song::getURI
),
79 boost::bind(&Self::moveSortOrderHint
, this)
82 w
.addItem(Entry(std::make_pair("Sort", static_cast<MPD::Song::GetFunction
>(0)),
83 boost::bind(&Self::sort
, this)
85 w
.addItem(Entry(std::make_pair("Cancel", static_cast<MPD::Song::GetFunction
>(0)),
86 boost::bind(&Self::cancel
, this)
90 void SortPlaylistDialog::switchTo()
92 SwitchTo::execute(this);
96 void SortPlaylistDialog::resize()
98 using Global::MainHeight
;
99 using Global::MainStartY
;
101 w
.resize(m_width
, m_height
);
102 w
.moveTo((COLS
-m_width
)/2, (MainHeight
-m_height
)/2+MainStartY
);
103 hasToBeResized
= false;
106 std::wstring
SortPlaylistDialog::title()
108 return previousScreen()->title();
111 void SortPlaylistDialog::enterPressed()
113 w
.current().value().run();
116 void SortPlaylistDialog::mouseButtonPressed(MEVENT me
)
118 if (w
.hasCoords(me
.x
, me
.y
))
120 if (me
.bstate
& (BUTTON1_PRESSED
| BUTTON3_PRESSED
))
123 if (me
.bstate
& BUTTON3_PRESSED
)
127 Screen
<WindowType
>::mouseButtonPressed(me
);
131 void SortPlaylistDialog::moveSortOrderDown()
133 auto cur
= w
.currentVI();
134 if ((cur
+1)->item().second
)
136 std::iter_swap(cur
, cur
+1);
137 w
.scroll(NC::Scroll::Down
);
141 void SortPlaylistDialog::moveSortOrderUp()
143 auto cur
= w
.currentVI();
144 if (cur
> w
.beginV() && cur
->item().second
)
146 std::iter_swap(cur
, cur
-1);
147 w
.scroll(NC::Scroll::Up
);
151 void SortPlaylistDialog::moveSortOrderHint() const
153 Statusbar::print("Move tag types up and down to adjust sort order");
156 void SortPlaylistDialog::sort() const
158 auto &pl
= myPlaylist
->main();
159 auto begin
= pl
.begin(), end
= pl
.end();
160 std::tie(begin
, end
) = getSelectedRange(begin
, end
);
162 size_t start_pos
= begin
- pl
.begin();
163 MPD::SongList playlist
;
164 playlist
.reserve(end
- begin
);
165 for (; begin
!= end
; ++begin
)
166 playlist
.push_back(begin
->value());
168 typedef MPD::SongList::iterator Iterator
;
169 LocaleStringComparison
cmp(std::locale(), Config
.ignore_leading_the
);
170 std::function
<void(Iterator
, Iterator
)> iter_swap
, quick_sort
;
171 auto song_cmp
= [this, &cmp
](const MPD::Song
&a
, const MPD::Song
&b
) -> bool {
172 for (auto it
= w
.beginV(); it
->item().second
; ++it
)
174 int res
= cmp(a
.getTags(it
->item().second
, Config
.tags_separator
),
175 b
.getTags(it
->item().second
, Config
.tags_separator
));
179 return a
.getPosition() < b
.getPosition();
181 iter_swap
= [&playlist
, &start_pos
](Iterator a
, Iterator b
) {
182 std::iter_swap(a
, b
);
183 Mpd
.Swap(start_pos
+a
-playlist
.begin(), start_pos
+b
-playlist
.begin());
185 quick_sort
= [this, &song_cmp
, &quick_sort
, &iter_swap
](Iterator first
, Iterator last
) {
188 Iterator pivot
= first
+rand()%(last
-first
);
189 iter_swap(pivot
, last
-1);
192 Iterator tmp
= first
;
193 for (Iterator i
= first
; i
!= pivot
; ++i
)
194 if (song_cmp(*i
, *pivot
))
196 iter_swap(tmp
, pivot
);
198 quick_sort(first
, tmp
);
199 quick_sort(tmp
+1, last
);
203 Statusbar::print("Sorting...");
204 Mpd
.StartCommandsList();
205 quick_sort(playlist
.begin(), playlist
.end());
206 Mpd
.CommitCommandsList();
207 Statusbar::print("Playlist sorted");
208 switchToPreviousScreen();
211 void SortPlaylistDialog::cancel() const
213 switchToPreviousScreen();
216 void SortPlaylistDialog::setDimensions()
218 m_height
= std::min(size_t(17), Global::MainHeight
);