summaryrefslogtreecommitdiff
path: root/src/sel_items_adder.cpp
blob: eced10113275aa02d436888b9ae37ece9d3099ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
 *   Copyright (C) 2008-2012 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include <algorithm>
#include "charset.h"
#include "browser.h"
#include "display.h"
#include "global.h"
#include "mpdpp.h"
#include "playlist.h"
#include "playlist_editor.h"
#include "sel_items_adder.h"
#include "settings.h"
#include "status.h"
#include "utility/comparators.h"

using Global::MainHeight;
using Global::MainStartY;
using Global::myScreen;
using Global::myOldScreen;

SelectedItemsAdder *mySelectedItemsAdder = new SelectedItemsAdder;

void SelectedItemsAdder::Init()
{
	SetDimensions();
	itsPlaylistSelector = new NC::Menu<std::string>((COLS-itsWidth)/2, (MainHeight-itsHeight)/2+MainStartY, itsWidth, itsHeight, "Add selected item(s) to...", Config.main_color, Config.window_border);
	itsPlaylistSelector->CyclicScrolling(Config.use_cyclic_scrolling);
	itsPlaylistSelector->CenteredCursor(Config.centered_cursor);
	itsPlaylistSelector->HighlightColor(Config.main_highlight_color);
	itsPlaylistSelector->setItemDisplayer(Display::Default<std::string>);
	
	itsPositionSelector = new NC::Menu<std::string>((COLS-itsPSWidth)/2, (MainHeight-itsPSHeight)/2+MainStartY, itsPSWidth, itsPSHeight, "Where?", Config.main_color, Config.window_border);
	itsPositionSelector->CyclicScrolling(Config.use_cyclic_scrolling);
	itsPositionSelector->CenteredCursor(Config.centered_cursor);
	itsPositionSelector->HighlightColor(Config.main_highlight_color);
	itsPositionSelector->setItemDisplayer(Display::Default<std::string>);
	itsPositionSelector->AddItem("At the end of playlist");
	itsPositionSelector->AddItem("At the beginning of playlist");
	itsPositionSelector->AddItem("After current track");
	itsPositionSelector->AddItem("After current album");
	itsPositionSelector->AddItem("After highlighted item");
	itsPositionSelector->AddSeparator();
	itsPositionSelector->AddItem("Cancel");
	
	w = itsPlaylistSelector;
	isInitialized = 1;
}

void SelectedItemsAdder::SwitchTo()
{
	if (myScreen == this)
	{
		myOldScreen->SwitchTo();
		return;
	}
	auto hs = dynamic_cast<HasSongs *>(myScreen);
	if (!hs || !hs->allowsSelection())
		return;
	
	if (MainHeight < 5)
	{
		ShowMessage("Screen is too small to display this window");
		return;
	}
	
	if (!isInitialized)
		Init();
	
	// default to main window
	w = itsPlaylistSelector;
	
	// Resize() can fall back to old screen, so we need it updated
	myOldScreen = myScreen;
	
	if (hasToBeResized)
		Resize();
	
	bool playlists_not_active = myScreen == myBrowser && myBrowser->isLocal();
	if (playlists_not_active)
		ShowMessage("Local items can't be added to stored playlists");
	
	w->Clear();
	w->Reset();
	if (myOldScreen != myPlaylist)
		w->AddItem("Current MPD playlist", 0, 0);
	w->AddItem("New playlist", 0, playlists_not_active);
	w->AddSeparator();
	
	auto playlists = Mpd.GetPlaylists();
	std::sort(playlists.begin(), playlists.end(), CaseInsensitiveSorting());
	for (auto it = playlists.begin(); it != playlists.end(); ++it)
		w->AddItem(*it, 0, playlists_not_active);
	w->AddSeparator();
	w->AddItem("Cancel");
	
	myScreen = this;
}

void SelectedItemsAdder::Resize()
{
	SetDimensions();
	if (itsHeight < 5) // screen too low to display this window
		return myOldScreen->SwitchTo();
	itsPlaylistSelector->Resize(itsWidth, itsHeight);
	itsPlaylistSelector->MoveTo((COLS-itsWidth)/2, (MainHeight-itsHeight)/2+MainStartY);
	size_t poss_width = std::min(itsPSWidth, size_t(COLS));
	size_t poss_height = std::min(itsPSHeight, size_t(MainHeight));
	itsPositionSelector->Resize(poss_width, poss_height);
	itsPositionSelector->MoveTo((COLS-poss_width)/2, (MainHeight-poss_height)/2+MainStartY);
	if (myOldScreen && myOldScreen->hasToBeResized) // resize background window
	{
		myOldScreen->Resize();
		myOldScreen->Refresh();
	}
	hasToBeResized = 0;
}

void SelectedItemsAdder::Refresh()
{
	if (w == itsPositionSelector)
	{
		itsPlaylistSelector->Display();
		itsPositionSelector->Display();
	}
	else
		itsPlaylistSelector->Display();
}

std::basic_string<my_char_t> SelectedItemsAdder::Title()
{
	return myOldScreen->Title();
}

void SelectedItemsAdder::EnterPressed()
{
	size_t pos = w->Choice();

	// adding to current playlist is disabled when playlist is active
	if (w == itsPlaylistSelector && myOldScreen == myPlaylist && pos == 0)
		pos++;
	
	MPD::SongList list;
	if ((w != itsPlaylistSelector || pos != 0) && pos != w->Size()-1)
		list = dynamic_cast<HasSongs &>(*myOldScreen).getSelectedSongs();
	
	if (w == itsPlaylistSelector)
	{
		if (pos == 0) // add to mpd playlist
		{
			w = itsPositionSelector;
			itsPositionSelector->Reset();
			return;
		}
		else if (pos == 1) // create new playlist
		{
			LockStatusbar();
			Statusbar() << "Save playlist as: ";
			std::string playlist = Global::wFooter->GetString();
			UnlockStatusbar();
			if (!playlist.empty())
			{
				std::string utf_playlist = locale_to_utf_cpy(playlist);
				Mpd.StartCommandsList();
				for (auto it = list.begin(); it != list.end(); ++it)
					Mpd.AddToPlaylist(utf_playlist, *it);
				if (Mpd.CommitCommandsList())
					ShowMessage("Selected item(s) added to playlist \"%s\"", playlist.c_str());
			}
		}
		else if (pos > 1 && pos < w->Size()-1) // add items to existing playlist
		{
			std::string playlist = locale_to_utf_cpy(w->Current().value());
			Mpd.StartCommandsList();
			for (auto it = list.begin(); it != list.end(); ++it)
				Mpd.AddToPlaylist(playlist, *it);
			if (Mpd.CommitCommandsList())
				ShowMessage("Selected item(s) added to playlist \"%s\"", w->Current().value().c_str());
		}
		if (pos != w->Size()-1)
		{
			// refresh playlist's lists
			if (myBrowser->Main() && !myBrowser->isLocal() && myBrowser->CurrentDir() == "/")
				myBrowser->GetDirectory("/");
			if (myPlaylistEditor->Main())
				myPlaylistEditor->Playlists->Clear(); // make playlist editor update itself
		}
	}
	else
	{
		// disable adding after current track/album when stopped
		if (pos > 1 && pos < 4 && !Mpd.isPlaying())
		{
			ShowMessage("Player is stopped");
			return;
		}
		
		bool successful_operation;
		if (pos == 0) // end of playlist
		{
			successful_operation = myPlaylist->Add(list, 0);
		}
		else if (pos == 1) // beginning of playlist
		{
			successful_operation = myPlaylist->Add(list, 0, 0);
		}
		else if (pos == 2) // after currently playing track
		{
			successful_operation = myPlaylist->Add(list, 0, Mpd.GetCurrentlyPlayingSongPos()+1);
		}
		else if (pos == 3) // after currently playing album
		{
			std::string album = myPlaylist->NowPlayingSong()->getAlbum();
			int i;
			for (i = Mpd.GetCurrentlyPlayingSongPos()+1; i < int(myPlaylist->Items->Size()); ++i)
				if ((*myPlaylist->Items)[i].value().getAlbum() != album)
					break;
			successful_operation = myPlaylist->Add(list, 0, i);
		}
		else if (pos == 4) // after highlighted item
		{
			successful_operation = myPlaylist->Add(list, 0, std::min(myPlaylist->Items->Choice()+1, myPlaylist->Items->Size()));
		}
		else
		{
			w = itsPlaylistSelector;
			return;
		}
		
		if (successful_operation)
			ShowMessage("Selected item(s) added");
	}
	SwitchTo();
}

void SelectedItemsAdder::MouseButtonPressed(MEVENT me)
{
	if (w->Empty() || !w->hasCoords(me.x, me.y) || size_t(me.y) >= w->Size())
		return;
	if (me.bstate & (BUTTON1_PRESSED | BUTTON3_PRESSED))
	{
		w->Goto(me.y);
		if (me.bstate & BUTTON3_PRESSED)
			EnterPressed();
	}
	else
		Screen< NC::Menu<std::string> >::MouseButtonPressed(me);
}

void SelectedItemsAdder::SetDimensions()
{
	itsWidth = COLS*0.6;
	itsHeight = std::min(size_t(LINES*0.6), MainHeight);
}