1 /* GUIRemoteControl.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  * created by Michael Lugmair (Lucio Carreras),
21  * Sep 3, 2012
22  *
23  */
24 
25 #include "GUI_RemoteControlPreferences.h"
26 #include "Gui/Preferences/ui_GUI_RemoteControlPreferences.h"
27 
28 #include "Utils/Utils.h"
29 #include "Utils/Language/Language.h"
30 #include "Utils/Settings/Settings.h"
31 
GUI_RemoteControlPreferences(const QString & identifier)32 GUI_RemoteControlPreferences::GUI_RemoteControlPreferences(const QString& identifier) :
33 	Base(identifier) {}
34 
~GUI_RemoteControlPreferences()35 GUI_RemoteControlPreferences::~GUI_RemoteControlPreferences()
36 {
37 	if(ui)
38 	{
39 		delete ui; ui=nullptr;
40 	}
41 }
42 
initUi()43 void GUI_RemoteControlPreferences::initUi()
44 {
45 	setupParent(this, &ui);
46 
47 	connect(ui->cbActivate, &QCheckBox::toggled, this, &GUI_RemoteControlPreferences::activeToggled);
48 	connect(ui->sbPort, spinbox_value_changed_int, this, &GUI_RemoteControlPreferences::portChanged);
49 	connect(ui->sbDiscover, spinbox_value_changed_int, this, &GUI_RemoteControlPreferences::discoverPortChanged);
50 
51 	revert();
52 }
53 
retranslate()54 void GUI_RemoteControlPreferences::retranslate()
55 {
56 	ui->retranslateUi(this);
57 	ui->labActive->setText(Lang::get(Lang::Active));
58 
59 	const QString tooltip = tr("If activated, Sayonara will answer an UDP request that it is remote controllable");
60 	ui->cbDiscover->setToolTip(tooltip);
61 	ui->labDiscover->setToolTip(tooltip);
62 }
63 
64 
commit()65 bool GUI_RemoteControlPreferences::commit()
66 {
67 	SetSetting(Set::Remote_Active, ui->cbActivate->isChecked());
68 	SetSetting(Set::Remote_Port, ui->sbPort->value());
69 	SetSetting(Set::Remote_Discoverable, ui->cbDiscover->isChecked());
70 	SetSetting(Set::Remote_DiscoverPort, ui->sbDiscover->value());
71 
72 	return true;
73 }
74 
revert()75 void GUI_RemoteControlPreferences::revert()
76 {
77 	bool active = GetSetting(Set::Remote_Active);
78 	ui->cbActivate->setChecked(active);
79 
80 	ui->sbPort->setEnabled(active);
81 	ui->sbPort->setValue(GetSetting(Set::Remote_Port));
82 
83 	ui->cbDiscover->setEnabled(active);
84 	ui->cbDiscover->setChecked(GetSetting(Set::Remote_Discoverable));
85 
86 	ui->sbDiscover->setEnabled(active);
87 	ui->sbDiscover->setValue(GetSetting(Set::Remote_DiscoverPort));
88 
89 	refreshUrl();
90 }
91 
92 
actionName() const93 QString GUI_RemoteControlPreferences::actionName() const
94 {
95 	return tr("Remote control");
96 }
97 
98 
activeToggled(bool b)99 void GUI_RemoteControlPreferences::activeToggled(bool b)
100 {
101 	Q_UNUSED(b)
102 	refreshUrl();
103 
104 	bool active = GetSetting(Set::Remote_Active);
105 
106 	ui->sbPort->setEnabled(active);
107 	ui->cbDiscover->setEnabled(active);
108 	ui->sbDiscover->setEnabled(active);
109 }
110 
portChanged(int port)111 void GUI_RemoteControlPreferences::portChanged(int port)
112 {
113 	Q_UNUSED(port)
114 
115 	ui->sbDiscover->setValue(port + 1);
116 	refreshUrl();
117 }
118 
discoverPortChanged(int port)119 void GUI_RemoteControlPreferences::discoverPortChanged(int port)
120 {
121 	if(port == ui->sbPort->value())
122 	{
123 		ui->sbDiscover->setValue(ui->sbPort->value() + 1);
124 	}
125 }
126 
127 
getUrlString()128 QString GUI_RemoteControlPreferences::getUrlString()
129 {
130 	int port = ui->sbPort->value();
131 	const QStringList ips = Util::ipAddresses();
132 
133 	QStringList ret;
134 	for(const QString& ip : ips)
135 	{
136 		ret << QString("%1:%2")
137 					.arg(ip)
138 					.arg(port);
139 	}
140 
141 	return ret.join("\n");
142 }
143 
refreshUrl()144 void GUI_RemoteControlPreferences::refreshUrl()
145 {
146 	bool active = ui->cbActivate->isChecked();
147 
148 	ui->labUrl->setVisible(active);
149 	ui->labUrls->setText(getUrlString());
150 }
151 
hasError() const152 bool GUI_RemoteControlPreferences::hasError() const
153 {
154 	int broadcast_port = GetSetting(Set::Broadcast_Port);
155 
156 	return
157 		(ui->sbPort->value() == broadcast_port) ||
158 		(ui->sbDiscover->value() == broadcast_port);
159 }
160 
errorString() const161 QString GUI_RemoteControlPreferences::errorString() const
162 {
163 	int broadcast_port = GetSetting(Set::Broadcast_Port);
164 	return tr("Port %1 already in use").arg(broadcast_port);
165 }
166