-
-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathSettingsDialog.cpp
More file actions
315 lines (264 loc) · 10.7 KB
/
SettingsDialog.cpp
File metadata and controls
315 lines (264 loc) · 10.7 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
obs-websocket
Copyright (C) 2016-2021 Stephane Lepin <stephane.lepin@gmail.com>
Copyright (C) 2020-2021 Kyle Manning <tt2468@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, see <https://www.gnu.org/licenses/>
*/
#include <QtWidgets/QMessageBox>
#include <QDateTime>
#include <QTime>
#include <obs-module.h>
#include <obs.hpp>
#include "SettingsDialog.h"
#include "../obs-websocket.h"
#include "../Config.h"
#include "../websocketserver/WebSocketServer.h"
#include "../utils/Crypto.h"
QString GetToolTipIconHtml()
{
bool lightTheme = QApplication::palette().text().color().redF() < 0.5;
QString iconFile = lightTheme ? ":toolTip/images/help.svg" : ":toolTip/images/help_light.svg";
QString iconTemplate = "<html> <img src='%1' style=' vertical-align: bottom; ' /></html>";
return iconTemplate.arg(iconFile);
}
SettingsDialog::SettingsDialog(QWidget *parent)
: QDialog(parent, Qt::Dialog),
ui(new Ui::SettingsDialog),
connectInfo(new ConnectInfo),
sessionTableTimer(new QTimer),
passwordManuallyEdited(false)
{
ui->setupUi(this);
ui->websocketSessionTable->horizontalHeader()->resizeSection(3, 100); // Resize Session Table column widths
ui->websocketSessionTable->horizontalHeader()->resizeSection(4, 100);
// Remove the ? button on dialogs on Windows
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
// Set the appropriate tooltip icon for the theme
ui->enableDebugLoggingToolTipLabel->setText(GetToolTipIconHtml());
connect(sessionTableTimer, &QTimer::timeout, this, &SettingsDialog::FillSessionTable);
connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &SettingsDialog::DialogButtonClicked);
connect(ui->enableAuthenticationCheckBox, &QCheckBox::stateChanged, this,
&SettingsDialog::EnableAuthenticationCheckBoxChanged);
connect(ui->generatePasswordButton, &QPushButton::clicked, this, &SettingsDialog::GeneratePasswordButtonClicked);
connect(ui->showConnectInfoButton, &QPushButton::clicked, this, &SettingsDialog::ShowConnectInfoButtonClicked);
connect(ui->serverPasswordLineEdit, &QLineEdit::textEdited, this, &SettingsDialog::PasswordEdited);
}
SettingsDialog::~SettingsDialog()
{
delete ui;
delete connectInfo;
delete sessionTableTimer;
}
void SettingsDialog::showEvent(QShowEvent *)
{
auto conf = GetConfig();
if (!conf) {
blog(LOG_ERROR, "[SettingsDialog::showEvent] Unable to retreive config!");
return;
}
if (conf->PortOverridden)
ui->serverPortSpinBox->setEnabled(false);
if (conf->PasswordOverridden) {
ui->enableAuthenticationCheckBox->setEnabled(false);
ui->serverPasswordLineEdit->setEnabled(false);
ui->generatePasswordButton->setEnabled(false);
}
passwordManuallyEdited = false;
RefreshData();
sessionTableTimer->start(1000);
}
void SettingsDialog::hideEvent(QHideEvent *)
{
if (sessionTableTimer->isActive())
sessionTableTimer->stop();
connectInfo->hide();
}
void SettingsDialog::ToggleShowHide()
{
if (!isVisible())
setVisible(true);
else
setVisible(false);
}
void SettingsDialog::RefreshData()
{
auto conf = GetConfig();
if (!conf) {
blog(LOG_ERROR, "[SettingsDialog::RefreshData] Unable to retreive config!");
return;
}
ui->enableWebSocketServerCheckBox->setChecked(conf->ServerEnabled);
ui->enableSystemTrayAlertsCheckBox->setChecked(conf->AlertsEnabled);
ui->enableDebugLoggingCheckBox->setChecked(conf->DebugEnabled);
ui->serverPortSpinBox->setValue(conf->ServerPort);
ui->enableAuthenticationCheckBox->setChecked(conf->AuthRequired);
ui->serverPasswordLineEdit->setText(QString::fromStdString(conf->ServerPassword));
ui->serverPasswordLineEdit->setEnabled(conf->AuthRequired);
ui->generatePasswordButton->setEnabled(conf->AuthRequired);
FillSessionTable();
}
void SettingsDialog::DialogButtonClicked(QAbstractButton *button)
{
if (button == ui->buttonBox->button(QDialogButtonBox::Ok)) {
SaveFormData();
} else if (button == ui->buttonBox->button(QDialogButtonBox::Apply)) {
SaveFormData();
}
}
void SettingsDialog::SaveFormData()
{
auto conf = GetConfig();
if (!conf) {
blog(LOG_ERROR, "[SettingsDialog::SaveFormData] Unable to retreive config!");
return;
}
if (ui->serverPasswordLineEdit->text().length() < 6) {
QMessageBox msgBox;
msgBox.setWindowTitle(obs_module_text("OBSWebSocket.Settings.Save.PasswordInvalidErrorTitle"));
msgBox.setText(obs_module_text("OBSWebSocket.Settings.Save.PasswordInvalidErrorMessage"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
return;
}
// Show a confirmation box to the user if they attempt to provide their own password
if (passwordManuallyEdited && (conf->ServerPassword != ui->serverPasswordLineEdit->text().toStdString())) {
QMessageBox msgBox;
msgBox.setWindowTitle(obs_module_text("OBSWebSocket.Settings.Save.UserPasswordWarningTitle"));
msgBox.setText(obs_module_text("OBSWebSocket.Settings.Save.UserPasswordWarningMessage"));
msgBox.setInformativeText(obs_module_text("OBSWebSocket.Settings.Save.UserPasswordWarningInfoText"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Yes:
break;
case QMessageBox::No:
default:
ui->serverPasswordLineEdit->setText(QString::fromStdString(conf->ServerPassword));
return;
}
}
bool needsRestart = (conf->ServerEnabled != ui->enableWebSocketServerCheckBox->isChecked()) ||
(conf->ServerPort != ui->serverPortSpinBox->value()) ||
(ui->enableAuthenticationCheckBox->isChecked() &&
conf->ServerPassword != ui->serverPasswordLineEdit->text().toStdString());
conf->ServerEnabled = ui->enableWebSocketServerCheckBox->isChecked();
conf->AlertsEnabled = ui->enableSystemTrayAlertsCheckBox->isChecked();
conf->DebugEnabled = ui->enableDebugLoggingCheckBox->isChecked();
conf->ServerPort = ui->serverPortSpinBox->value();
conf->AuthRequired = ui->enableAuthenticationCheckBox->isChecked();
conf->ServerPassword = ui->serverPasswordLineEdit->text().toStdString();
conf->Save();
RefreshData();
connectInfo->RefreshData();
if (needsRestart) {
blog(LOG_INFO, "[SettingsDialog::SaveFormData] A setting was changed which requires a server restart.");
auto server = GetWebSocketServer();
server->Stop();
if (conf->ServerEnabled) {
server->Start();
}
}
}
void SettingsDialog::FillSessionTable()
{
auto webSocketServer = GetWebSocketServer();
if (!webSocketServer) {
blog(LOG_ERROR, "[SettingsDialog::FillSessionTable] Unable to fetch websocket server instance!");
return;
}
auto webSocketSessions = webSocketServer->GetWebSocketSessions();
size_t rowCount = webSocketSessions.size();
// Manually setting the pixmap size *might* break with highdpi. Not sure though
QIcon checkIcon = style()->standardIcon(QStyle::SP_DialogOkButton);
QPixmap checkIconPixmap = checkIcon.pixmap(QSize(25, 25));
QIcon crossIcon = style()->standardIcon(QStyle::SP_DialogCancelButton);
QPixmap crossIconPixmap = crossIcon.pixmap(QSize(25, 25));
// Todo: Make a util for translations so that we don't need to import a bunch of obs libraries in order to use them.
QString kickButtonText = obs_module_text("OBSWebSocket.SessionTable.KickButtonText");
ui->websocketSessionTable->setRowCount(rowCount);
size_t i = 0;
for (auto session : webSocketSessions) {
QTableWidgetItem *addressItem = new QTableWidgetItem(QString::fromStdString(session.remoteAddress));
ui->websocketSessionTable->setItem(i, 0, addressItem);
uint64_t sessionDuration = QDateTime::currentSecsSinceEpoch() - session.connectedAt;
QTableWidgetItem *durationItem = new QTableWidgetItem(QTime(0, 0, sessionDuration).toString("hh:mm:ss"));
ui->websocketSessionTable->setItem(i, 1, durationItem);
QTableWidgetItem *statsItem =
new QTableWidgetItem(QString("%1/%2").arg(session.incomingMessages).arg(session.outgoingMessages));
ui->websocketSessionTable->setItem(i, 2, statsItem);
QLabel *identifiedLabel = new QLabel();
identifiedLabel->setAlignment(Qt::AlignCenter);
if (session.isIdentified) {
identifiedLabel->setPixmap(checkIconPixmap);
} else {
identifiedLabel->setPixmap(crossIconPixmap);
}
ui->websocketSessionTable->setCellWidget(i, 3, identifiedLabel);
QPushButton *invalidateButton = new QPushButton(kickButtonText, this);
QWidget *invalidateButtonWidget = new QWidget();
QHBoxLayout *invalidateButtonLayout = new QHBoxLayout(invalidateButtonWidget);
invalidateButtonLayout->addWidget(invalidateButton);
invalidateButtonLayout->setAlignment(Qt::AlignCenter);
invalidateButtonLayout->setContentsMargins(0, 0, 0, 0);
invalidateButtonWidget->setLayout(invalidateButtonLayout);
ui->websocketSessionTable->setCellWidget(i, 4, invalidateButtonWidget);
connect(invalidateButton, &QPushButton::clicked, [=]() { webSocketServer->InvalidateSession(session.hdl); });
i++;
}
}
void SettingsDialog::EnableAuthenticationCheckBoxChanged()
{
if (ui->enableAuthenticationCheckBox->isChecked()) {
ui->serverPasswordLineEdit->setEnabled(true);
ui->generatePasswordButton->setEnabled(true);
} else {
ui->serverPasswordLineEdit->setEnabled(false);
ui->generatePasswordButton->setEnabled(false);
}
}
void SettingsDialog::GeneratePasswordButtonClicked()
{
QString newPassword = QString::fromStdString(Utils::Crypto::GeneratePassword());
ui->serverPasswordLineEdit->setText(newPassword);
ui->serverPasswordLineEdit->selectAll();
passwordManuallyEdited = false;
}
void SettingsDialog::ShowConnectInfoButtonClicked()
{
if (obs_video_active()) {
QMessageBox msgBox;
msgBox.setWindowTitle(obs_module_text("OBSWebSocket.Settings.ShowConnectInfoWarningTitle"));
msgBox.setText(obs_module_text("OBSWebSocket.Settings.ShowConnectInfoWarningMessage"));
msgBox.setInformativeText(obs_module_text("OBSWebSocket.Settings.ShowConnectInfoWarningInfoText"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Yes:
break;
case QMessageBox::No:
default:
return;
}
}
// show connect info implies apply to prevent outdata data on a missed apply
SaveFormData();
connectInfo->show();
connectInfo->activateWindow();
connectInfo->raise();
connectInfo->setFocus();
}
void SettingsDialog::PasswordEdited()
{
passwordManuallyEdited = true;
}