1 /* GUI_PlaylistChooser.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "GUI_PlaylistChooser.h"
22 #include "GUI_TargetPlaylistDialog.h"
23 #include "Gui/Plugins/ui_GUI_PlaylistChooser.h"
24 #include "Gui/Utils/InputDialog/LineInputDialog.h"
25 
26 #include "Components/Playlist/PlaylistChooser.h"
27 #include "Components/Playlist/PlaylistHandler.h"
28 
29 #include "Utils/Algorithm.h"
30 #include "Utils/Language/Language.h"
31 #include "Utils/Message/Message.h"
32 #include "Utils/Playlist/CustomPlaylist.h"
33 #include "Utils/Language/Language.h"
34 
35 using Gui::ContextMenu;
36 using Gui::ContextMenuEntries;
37 using Gui::MenuToolButton;
38 
39 using Playlist::Chooser;
40 
41 struct GUI_PlaylistChooser::Private
42 {
43 	Chooser* playlistChooser = nullptr;
44 
PrivateGUI_PlaylistChooser::Private45 	Private(Playlist::Chooser* playlistChooser) :
46 		playlistChooser(playlistChooser) {}
47 };
48 
GUI_PlaylistChooser(Playlist::Chooser * playlistChooser,QWidget * parent)49 GUI_PlaylistChooser::GUI_PlaylistChooser(Playlist::Chooser* playlistChooser, QWidget* parent) :
50 	PlayerPlugin::Base(parent)
51 {
52 	m = Pimpl::make<Private>(playlistChooser);
53 }
54 
~GUI_PlaylistChooser()55 GUI_PlaylistChooser::~GUI_PlaylistChooser()
56 {
57 	if(ui)
58 	{
59 		delete ui;
60 		ui = nullptr;
61 	}
62 }
63 
initUi()64 void GUI_PlaylistChooser::initUi()
65 {
66 	setupParent(this, &ui);
67 
68 	ui->btnActions->showAction(ContextMenu::EntryRename, true);
69 	ui->btnActions->showAction(ContextMenu::EntryDelete, true);
70 
71 	connect(ui->btnActions, &MenuToolButton::sigRename, this, &GUI_PlaylistChooser::renameTriggered);
72 	connect(ui->btnActions, &MenuToolButton::sigDelete, this, &GUI_PlaylistChooser::deleteTriggered);
73 	connect(ui->comboPlaylists, combo_activated_int, this, &GUI_PlaylistChooser::playlistSelected);
74 	connect(m->playlistChooser, &Chooser::sigPlaylistsChanged, this, &GUI_PlaylistChooser::playlistsChanged);
75 
76 	playlistsChanged();
77 }
78 
retranslate()79 void GUI_PlaylistChooser::retranslate()
80 {
81 	ui->retranslateUi(this);
82 
83 	const CustomPlaylistSkeletons& skeletons =
84 		m->playlistChooser->playlists();
85 
86 	if(skeletons.isEmpty())
87 	{
88 		ui->comboPlaylists->clear();
89 		ui->comboPlaylists->addItem(tr("No playlists found"), -1);
90 	}
91 }
92 
name() const93 QString GUI_PlaylistChooser::name() const
94 {
95 	return "Playlists";
96 }
97 
displayName() const98 QString GUI_PlaylistChooser::displayName() const
99 {
100 	return Lang::get(Lang::Playlists);
101 }
102 
playlistsChanged()103 void GUI_PlaylistChooser::playlistsChanged()
104 {
105 	if(!isUiInitialized())
106 	{
107 		return;
108 	}
109 
110 	QString old_text = ui->comboPlaylists->currentText();
111 
112 	const CustomPlaylistSkeletons& skeletons =
113 		m->playlistChooser->playlists();
114 
115 	ui->comboPlaylists->clear();
116 
117 	for(const CustomPlaylistSkeleton& skeleton : skeletons)
118 	{
119 		ui->comboPlaylists->addItem(skeleton.name(), skeleton.id());
120 	}
121 
122 	ui->btnActions->setEnabled(!skeletons.isEmpty());
123 	if(skeletons.isEmpty())
124 	{
125 		ui->comboPlaylists->addItem(tr("No playlists found"), -1);
126 	}
127 
128 	int cur_idx = std::max(ui->comboPlaylists->findText(old_text), 0);
129 	ui->comboPlaylists->setCurrentIndex(cur_idx);
130 }
131 
renameTriggered()132 void GUI_PlaylistChooser::renameTriggered()
133 {
134 	auto* dialog = new Gui::LineInputDialog
135 		(
136 			Lang::get(Lang::Rename),
137 			Lang::get(Lang::Rename),
138 			ui->comboPlaylists->currentText(),
139 			this
140 		);
141 
142 	connect(dialog, &Gui::LineInputDialog::sigClosed, this, &GUI_PlaylistChooser::renameDialogClosed);
143 	dialog->show();
144 }
145 
renameDialogClosed()146 void GUI_PlaylistChooser::renameDialogClosed()
147 {
148 	using Util::SaveAsAnswer;
149 	auto* dialog = static_cast<Gui::LineInputDialog*>(sender());
150 
151 	Gui::LineInputDialog::ReturnValue val = dialog->returnValue();
152 	if(val == Gui::LineInputDialog::ReturnValue::Ok)
153 	{
154 		int id = ui->comboPlaylists->currentData().toInt();
155 		QString new_name = dialog->text();
156 
157 		SaveAsAnswer answer = m->playlistChooser->renamePlaylist(id, new_name);
158 		if(answer != SaveAsAnswer::Success)
159 		{
160 			QString error_msg = tr("Could not rename playlist");
161 			if(answer == SaveAsAnswer::InvalidName || answer == SaveAsAnswer::NameAlreadyThere)
162 			{
163 				error_msg += "<br>" + tr("Name is invalid");
164 			}
165 
166 			Message::error(error_msg);
167 		}
168 	}
169 }
170 
deleteTriggered()171 void GUI_PlaylistChooser::deleteTriggered()
172 {
173 	int id = ui->comboPlaylists->currentData().toInt();
174 	QString name = ui->comboPlaylists->currentText();
175 
176 	Message::Answer answer = Message::question_yn(tr("Do you really want to delete %1?").arg(name));
177 	if(answer == Message::Answer::Yes)
178 	{
179 		bool success = m->playlistChooser->deletePlaylist(id);
180 		if(!success)
181 		{
182 			Message::error(tr("Could not delete playlist %1").arg(name));
183 		}
184 	}
185 }
186 
playlistSelected(int idx)187 void GUI_PlaylistChooser::playlistSelected(int idx)
188 {
189 	int id = m->playlistChooser->findPlaylist(ui->comboPlaylists->currentText());
190 	int data = ui->comboPlaylists->itemData(idx).toInt();
191 
192 	if(data < 0)
193 	{
194 		return;
195 	}
196 
197 	if(id >= 0)
198 	{
199 		m->playlistChooser->loadSinglePlaylist(id);
200 	}
201 }
202 
203