summaryrefslogtreecommitdiffstats
path: root/pica-client/dialogs/settingsdialog.cpp
blob: 536d141b307b54fce28e0a74792e69573d59027a (plain) (blame)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
	(c) Copyright  2012 - 2018 Anton Sviridenko
	https://picapica.im

	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, version 3.

	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 "settingsdialog.h"
#include "../settings.h"
#include "../globals.h"
#include "../audiodevice.h"
#include "../videodevice.h"
#include "../../PICA_netconf.h"
#include "../../PICA_proto.h"

#include <QGroupBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QVariant>
#include <QListWidget>
#include <QNetworkInterface>
#include <QHostAddress>
#include <QTabWidget>

SettingsDialog::SettingsDialog(QWidget *parent) :
	QDialog(parent)
{
	QVBoxLayout *settingsLayout = new QVBoxLayout();

	QTabWidget *tabW = new QTabWidget(this);
	QWidget* directc2ctab = new QWidget(0);
	QWidget* soundstab = new QWidget(0);
	QWidget* multilogintab = new QWidget(0);
	QWidget* audiodevtab = new QWidget(0);
	QWidget* videodevtab = new QWidget(0);

	QVBoxLayout *directc2cLayout = new QVBoxLayout();
	QVBoxLayout *multiloginlayout = new QVBoxLayout();
	QVBoxLayout *audiodevlayout = new QVBoxLayout();
	QVBoxLayout *videodevLayout = new QVBoxLayout();

//Direct connections
	rbDisableDirectConns = new QRadioButton(tr("Disable direct connections"), this);
	rbEnableOutgoingConns = new QRadioButton(tr("Connect to the remote peer directly if possible"), this);
	rbEnableIncomingConns = new QRadioButton(tr("Enable incoming direct connections"), this);

#ifdef HAVE_LIBMINIUPNPC
	cbEnableUPnP = new QCheckBox(tr("Enable UPnP"));
	cbEnableUPnP->setChecked(true);
#endif

	rbEnableIncomingConns->setChecked(true);
	connect(rbEnableIncomingConns, SIGNAL(toggled(bool)), this, SLOT(toggleIncomingConnections(bool)));

	QLabel *lbAddr = new QLabel(tr("Public address for incoming connections"), this);
	addr = new QComboBox(this);
	QLabel *lbPubPort = new QLabel(tr("External TCP port for incoming connections"), this);
	publicPort = new QSpinBox(this);
	QLabel *lbLocPort = new QLabel(tr("Local TCP port for incoming connections"), this);
	localPort = new QSpinBox(this);

	addr->setEditable(true);

	publicPort->setRange(1, 65535);
	localPort->setRange(1, 65535);

	publicPort->setValue(2298);
	localPort->setValue(2298);

	QList<QHostAddress> hostIfAddrs = QNetworkInterface::allAddresses();
	QHostAddress ifAddr;

	addr->addItem(QString("autoconfigure"));

	foreach (ifAddr, hostIfAddrs)
		if (ifAddr.protocol() == QAbstractSocket::IPv4Protocol)
			addr->addItem(ifAddr.toString());


	directc2cLayout->addWidget(rbDisableDirectConns);
	directc2cLayout->addWidget(rbEnableOutgoingConns);
	directc2cLayout->addWidget(rbEnableIncomingConns);
#ifdef HAVE_LIBMINIUPNPC
	directc2cLayout->addWidget(cbEnableUPnP);
#endif
	directc2cLayout->addWidget(lbAddr);
	directc2cLayout->addWidget(addr);
	directc2cLayout->addWidget(lbPubPort);
	directc2cLayout->addWidget(publicPort);
	directc2cLayout->addWidget(lbLocPort);
	directc2cLayout->addWidget(localPort);
	directc2cLayout->addStretch(1);

	directc2ctab->setLayout(directc2cLayout);

//Multiple logins
	QLabel *lbMLP = new QLabel("Policy for same account logins from multiple devices");
	rbMLPProhibit = new QRadioButton(tr("Prohibit new login attempts"), this);
	rbMLPReplace = new QRadioButton(tr("Replace existing connections"), this);
	rbMLPAllowMultiple = new QRadioButton(tr("Allow multiple logins, try to synchronise chat history"));

	rbMLPProhibit->setChecked(true);

	multiloginlayout->addWidget(lbMLP);
	multiloginlayout->addWidget(rbMLPProhibit);
	multiloginlayout->addWidget(rbMLPReplace);
	multiloginlayout->addWidget(rbMLPAllowMultiple);
	multiloginlayout->addStretch(1);

	multilogintab->setLayout(multiloginlayout);

// Audio Devices
	QLabel *lbAudioCaptureDev = new QLabel(tr("Microphone Device 🎙️"));
	QLabel *lbAudioPlaybackDev = new QLabel(tr("Playback device 🎧"));
	QLabel *lbAudioRingDev = new QLabel(tr("Ring device 🔔☎️"));

	audioCaptureDev = new QComboBox(this);
	audioCaptureDev->setMinimumHeight(audioCaptureDev->height() * 2);
	fillAudioCaptureDevices();

	audioPlaybackDev = new QComboBox(this);
	audioPlaybackDev->setMinimumHeight(audioPlaybackDev->height() * 2);
	fillAudioPlaybackDevices();

	audioRingDev = new QComboBox(this);
	audioRingDev->setMinimumHeight(audioRingDev->height() * 2);
	fillAudioRingDevices();

	btAudioTest = new QPushButton(tr("Test"), this);

	audiodevlayout->addWidget(lbAudioCaptureDev);
	audiodevlayout->addWidget(audioCaptureDev);
	audiodevlayout->addWidget(lbAudioPlaybackDev);
	audiodevlayout->addWidget(audioPlaybackDev);
	audiodevlayout->addWidget(lbAudioRingDev);
	audiodevlayout->addWidget(audioRingDev);
	audiodevlayout->addWidget(btAudioTest);
	audiodevlayout->addStretch(1);

	audiodevtab->setLayout(audiodevlayout);

//Video Devices
	videoDev = new QComboBox(this);
	videoDev->setMinimumHeight(videoDev->height() * 2);
	fillVideoDevices();
	videoDevRefresh = new QPushButton(tr("Refresh 🔄"), this);
	connect(videoDevRefresh, SIGNAL(clicked()), this, SLOT(fillVideoDevices()));

	btVideoTest = new QPushButton(tr("Test 📷"), this);
	connect(btVideoTest, SIGNAL(clicked()), this, SLOT(toggleVideoTest()));

	videoPreview = new QLabel(this);
	videoPreview->setAlignment(Qt::AlignCenter);
	videoPreview->setMinimumSize(320, 240);
	videoPreview->setFrameShape(QFrame::StyledPanel);

	videodevLayout->addWidget(videoDev);
	videodevLayout->addWidget(videoDevRefresh);
	videodevLayout->addWidget(btVideoTest);
	videodevLayout->addWidget(videoPreview);
	videodevLayout->addStretch(1);

	videodevtab->setLayout(videodevLayout);

	// Capture+encode and decode ends of the test pipeline, each blocking its
	// own thread while running, exactly as they do during a call. They stay
	// idle until the Test button is pressed.
	videoTestRunning = false;

	testCam = new VideoDevice();
	testCam->moveToThread(&testCamThread);
	connect(testCam, SIGNAL(packetReady(QByteArray,bool)), this, SLOT(videoTestFragment(QByteArray,bool)));
	connect(testCam, SIGNAL(errorOccurred(QString)), this, SLOT(videoTestError(QString)));
	testCamThread.start();

	testDecoder = new VideoDevice();
	testDecoder->moveToThread(&testDecoderThread);
	connect(testDecoder, SIGNAL(frameReady(QImage)), this, SLOT(videoTestFrame(QImage)));
	connect(testDecoder, SIGNAL(errorOccurred(QString)), this, SLOT(videoTestError(QString)));
	testDecoderThread.start();

	tabW->addTab(directc2ctab, tr("Direct Connections"));
	tabW->addTab(multilogintab, tr("Multiple logins"));
	tabW->addTab(audiodevtab, tr("Audio Devices"));
	tabW->addTab(videodevtab, tr("Video Devices"));
	tabW->addTab(soundstab, tr("Sounds"));
	settingsLayout->addWidget(tabW);

	QHBoxLayout *buttonsLayout = new QHBoxLayout;

	btOk = new QPushButton(tr("&OK"), this);
	btCancel = new QPushButton(tr("Cancel"), this);

	buttonsLayout->addStretch(1);
	buttonsLayout->addWidget(btOk);
	buttonsLayout->addWidget(btCancel);

	settingsLayout->addLayout(buttonsLayout);

	setLayout(settingsLayout);

	connect(btOk, SIGNAL(clicked()), this, SLOT(OK()));
	connect(btCancel, SIGNAL(clicked()), this, SLOT(Cancel()));

	loadSettings();

	setWindowTitle(tr("Pica Pica Messenger Settings"));
}

SettingsDialog::~SettingsDialog()
{
	// Stops the loops first - the threads cannot finish while Capture()/Play()
	// are still running inside them.
	testCam->Close();
	testDecoder->Close();

	testCamThread.quit();
	testCamThread.wait();
	delete testCam;

	testDecoderThread.quit();
	testDecoderThread.wait();
	delete testDecoder;
}

void SettingsDialog::toggleVideoTest()
{
	if (videoTestRunning)
	{
		stopVideoTest();
		return;
	}

	QString dev = videoDev->itemData(videoDev->currentIndex()).toString();

	if (dev.isEmpty())
	{
		videoPreview->setText(tr("No camera selected"));
		return;
	}

	testAssembler.reset();
	videoTestRunning = true;
	btVideoTest->setText(tr("Stop ⏹"));
	videoPreview->setText(tr("Starting..."));

	QMetaObject::invokeMethod(testCam, "configureCapture", Qt::QueuedConnection,
	                           Q_ARG(QString, dev), Q_ARG(QString, QStringLiteral("h264")),
	                           Q_ARG(int, 640), Q_ARG(int, 480));
	QMetaObject::invokeMethod(testCam, "Capture", Qt::QueuedConnection);

	QMetaObject::invokeMethod(testDecoder, "configurePlayback", Qt::QueuedConnection,
	                           Q_ARG(QString, QStringLiteral("h264")),
	                           Q_ARG(int, 640), Q_ARG(int, 480));
	QMetaObject::invokeMethod(testDecoder, "Play", Qt::QueuedConnection);
}

void SettingsDialog::stopVideoTest()
{
	// Direct calls: both devices are blocked inside their capture/decode
	// loops, so a queued call would never reach them (see the comments in
	// AudioVideoCallController::stopAudioPipeline()).
	testCam->Close();
	testDecoder->Close();

	testAssembler.reset();
	videoTestRunning = false;
	btVideoTest->setText(tr("Test 📷"));
	videoPreview->clear();
}

void SettingsDialog::videoTestFragment(QByteArray data, bool is_last_fragment)
{
	if (!videoTestRunning)
		return;

	// The encoder emits fragments exactly as they would go onto the wire, so
	// feed them through the same assembler the receiving end of a call uses.
	// The sequence number is rebuilt here the way send_video_packet() builds
	// it, the timestamp being constant since only frame boundaries matter.
	quint16 seq = is_last_fragment ? VideoFrameAssembler::LastFragmentFlag : 0;
	QByteArray frame = testAssembler.addFragment(seq, 0, data);

	if (frame.isEmpty())
		return;

	// Direct call, for the same reason as in the call controller.
	testDecoder->enqueueFrame(frame);
}

void SettingsDialog::videoTestFrame(QImage frame)
{
	if (!videoTestRunning || frame.isNull())
		return;

	videoPreview->setPixmap(QPixmap::fromImage(frame).scaled(videoPreview->size(),
	                                                         Qt::KeepAspectRatio,
	                                                         Qt::SmoothTransformation));
}

void SettingsDialog::videoTestError(QString message)
{
	if (!videoTestRunning)
		return;

	stopVideoTest();
	videoPreview->setText(message);
}

void SettingsDialog::fillDevicesComboBox(QComboBox *cb, MediaDevice *dev, enum MediaDeviceStreamDirection dir)
{
		cb->clear();
		QList<MediaDeviceInfo> md = dev->Enumerate(dir);
		for (int i = 0; i < md.size(); i++)
		{
			QString item = QString(QLatin1String("%2\n(%1)"))
									.arg(md.at(i).device)
									.arg(md.at(i).humanReadable);
			cb->addItem(item, md.at(i).device);
		}
}

void SettingsDialog::fillVideoDevices()
{
		VideoDevice vd;
		fillDevicesComboBox(videoDev, &vd, CAPTURE);
}

void SettingsDialog::fillAudioCaptureDevices()
{
	AudioDevice acd;
	fillDevicesComboBox(audioCaptureDev, &acd, CAPTURE);
}

void SettingsDialog::fillAudioPlaybackDevices()
{
	AudioDevice apd;
	fillDevicesComboBox(audioPlaybackDev, &apd, PLAYBACK);
}

void SettingsDialog::fillAudioRingDevices()
{
	AudioDevice ard;
	fillDevicesComboBox(audioRingDev, &ard, PLAYBACK);
}

void SettingsDialog::toggleIncomingConnections(bool checked)
{
	addr->setEnabled(checked);
	publicPort->setEnabled(checked);
	localPort->setEnabled(checked);
#ifdef HAVE_LIBMINIUPNPC
	cbEnableUPnP->setEnabled(checked);
#endif
}

void SettingsDialog::toggleMultipleLogins(bool checked)
{

}

void SettingsDialog::OK()
{
	storeSettings();
	done(1);
}

void SettingsDialog::Cancel()
{
	done(0);
}

void SettingsDialog::loadSettings()
{
	Settings st(config_dbname);

	//Direct c2c connections
	int c2c_state;

	c2c_state = st.loadValue("direct_c2c.state", 1).toInt();

	switch (c2c_state)
	{
	case 0:
		rbDisableDirectConns->setChecked(true);
		break;

	case 1:
		rbEnableOutgoingConns->setChecked(true);
		break;

	case 2:
		rbEnableIncomingConns->setChecked(true);
		break;

	default:
		break;
	}

#ifdef HAVE_LIBMINIUPNPC
	cbEnableUPnP->setChecked(st.loadValue("direct_c2c.upnp_enabled", 1).toBool());
#endif

	addr->lineEdit()->setText(st.loadValue("direct_c2c.public_addr", "autoconfigure").toString());

	if (addr->lineEdit()->text().contains(QString("autoconfigure")))
	{
		in_addr_t guess;
		struct in_addr in;

		guess = PICA_guess_listening_addr_ipv4();
		in.s_addr = guess;
		addr->lineEdit()->setText(QString("autoconfigured(%1)").arg(inet_ntoa(in)));

#ifdef HAVE_LIBMINIUPNPC
		if (cbEnableUPnP->isChecked() && PICA_is_reserved_addr_ipv4(guess))
		{
			int ret;
			char public_ip[64];
			ret = PICA_upnp_autoconfigure_ipv4(st.loadValue("direct_c2c.public_port", 2298).toInt(),
			                                   st.loadValue("direct_c2c.local_port", 2298).toInt(),
			                                   public_ip);

			if (ret)
			{
				addr->lineEdit()->setText(QString("autoconfigured(%1)").arg(public_ip));
			}
		}
#endif
	}

	publicPort->setValue(st.loadValue("direct_c2c.public_port", 2298).toInt());
	localPort->setValue(st.loadValue("direct_c2c.local_port", 2298).toInt());

	//multiple logins
	int mlpstate;
	mlpstate = c2c_state = st.loadValue("multiple_logins.state", 0).toInt();

	switch(mlpstate)
	{
	case PICA_MULTILOGIN_PROHIBIT:
		rbMLPProhibit->setChecked(true);
		break;

	case PICA_MULTILOGIN_REPLACE:
		rbMLPReplace->setChecked(true);
		break;

	case PICA_MULTILOGIN_ALLOW:
		rbMLPAllowMultiple->setChecked(true);
		break;

	default:
		break;
	}

	QString videoCapDevVal = st.loadValue("video.capture_device", QString()).toString();
	int videoDevItem = videoDev->findData(videoCapDevVal);
	if (videoDevItem >= 0)
		videoDev->setCurrentIndex(videoDevItem);

	QString audioCapDevVal = st.loadValue("audio.capture_device", "default").toString();
	int audioCapDevItem = audioCaptureDev->findData(audioCapDevVal);
	if (audioCapDevItem >= 0)
		audioCaptureDev->setCurrentIndex(audioCapDevItem);

	QString audioPlaybackDevVal = st.loadValue("audio.playback_device", "default").toString();
	int audioPlaybackDevItem = audioPlaybackDev->findData(audioPlaybackDevVal);
	if (audioPlaybackDevItem >= 0)
		audioPlaybackDev->setCurrentIndex(audioPlaybackDevItem);

	QString audioRingDevVal = st.loadValue("audio.ring_device", "default").toString();
	int audioRingDevItem = audioRingDev->findData(audioRingDevVal);
	if (audioRingDevItem >= 0)
		audioRingDev->setCurrentIndex(audioRingDevItem);
}

void SettingsDialog::storeSettings()
{
	Settings st(config_dbname);

	//Direct c2c connections
	int c2c_state;

	if (rbDisableDirectConns->isChecked())
		c2c_state = 0;
	else if (rbEnableOutgoingConns->isChecked())
		c2c_state = 1;
	else if (rbEnableIncomingConns->isChecked())
		c2c_state = 2;

	st.storeValue("direct_c2c.state", QString::number(c2c_state));

#ifdef HAVE_LIBMINIUPNPC
	st.storeValue("direct_c2c.upnp_enabled", cbEnableUPnP->isChecked() ? "1" : "0");
#endif

	st.storeValue("direct_c2c.public_addr", addr->lineEdit()->text());
	st.storeValue("direct_c2c.public_port", QString::number(publicPort->value()));
	st.storeValue("direct_c2c.local_port", QString::number(localPort->value()));

	//multiple logins
	int mlpstate;

	if (rbMLPProhibit->isChecked())
		mlpstate = PICA_MULTILOGIN_PROHIBIT;
	else if (rbMLPReplace->isChecked())
		mlpstate = PICA_MULTILOGIN_REPLACE;
	else if (rbMLPAllowMultiple->isChecked())
		mlpstate = PICA_MULTILOGIN_ALLOW;

	st.storeValue("multiple_logins.state", QString::number(mlpstate));

	st.storeValue("video.capture_device", videoDev->itemData(videoDev->currentIndex()).toString());
	st.storeValue("audio.capture_device", audioCaptureDev->itemData(audioCaptureDev->currentIndex()).toString());
	st.storeValue("audio.playback_device", audioPlaybackDev->itemData(audioPlaybackDev->currentIndex()).toString());
	st.storeValue("audio.ring_device", audioRingDev->itemData(audioRingDev->currentIndex()).toString());
}