summaryrefslogtreecommitdiffstats
path: root/src/client/connectwindow/connectwindow.cpp
blob: c77a5e67852debf58e1cf22f8f602f02bd48bb5d (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
/*
 * connectwindow.cpp
 *
 *  Created on: 28.01.2013
 *      Author: sr
 */

#include <QNetworkInterface>
#include "../../shared/settings.h"
#include "../../shared/network.h"
#include "../../shared/util.h"
#include "../net/serverconnection.h"
#include "../clientapp/clientapp.h"
#include "connectwindow.h"
#include "ui_connect.h"

#define UDPBUFSIZ 9000
#define SALT_LEN 18

/***************************************************************************//**
 * Initialize Connection Window.
 * @param parent
 */
ConnectWindow::ConnectWindow(QWidget *parent) :	QWidget(parent)
{
	_ui = new Ui::ConnectWindow;
	_timerHide = 0;
	_state = Idle;
	_hashSslErrorCount = 0;
	_pendingConnection = nullptr;

	// Initialize the GUI
	_ui->setupUi(this);

	// Set window properties
	setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool);

	// Set page 0 as default
	_ui->stackedWidget->setCurrentIndex(0);

	// Set actions of buttons
	connect(_ui->btn_connection, SIGNAL(clicked()), this, SLOT(onBtnConnection()));
	connect(_ui->btn_hide, SIGNAL(clicked()), this, SLOT(onBtnCancel()));

	connect(_ui->comboBox_rooms, SIGNAL(currentIndexChanged(int)), this, SLOT(onRoomSelection(int)));

	// React on discovery signal
	connect(&_serverDiscovery, SIGNAL(serverDetected(QString, quint16, QByteArray, QByteArray, bool)),
	        this, SLOT(onServerDetected(QString, quint16, QByteArray, QByteArray, bool)));

	/* finally the most requested feature: connect on press of the enter key */
	connect(_ui->lineEditName, SIGNAL(returnPressed()), _ui->btn_connection, SIGNAL(clicked()));

	/* by default don't show the manual connection box */
	_ui->box_manual->setVisible(false);

	this->updateUserInterface();
}

/***************************************************************************//**
 * @brief ConnectWindow::~ConnectWindow
 */
ConnectWindow::~ConnectWindow() {}



/***************************************************************************//**
 * Handle changes in state and update window.
 * Also changing TextLabel which shows the user what the program is doing.
 */
void ConnectWindow::updateUserInterface()
{
	_ui->lineEditName->setEnabled(_state == Idle);

	if (_state == Connected) {
		_ui->btn_connection->setEnabled(true);
		_ui->btn_connection->setText(tr("&Disconnect"));
		_ui->lblStatus->setText(tr("Connected to %1").arg(clientApp->connection()->getPeerAdress()));
		_ui->lineEditName->setEnabled(false);
		_ui->stackedWidget->setCurrentIndex(1);
		return;
	}

	if (_state != Idle)
		_ui->btn_connection->setText(tr("&Stop"));
	else
		_ui->btn_connection->setText(tr("&Connect"));

	switch (_state) {
	case Idle:
		_ui->lblStatus->setText(tr("Ready to connect."));
		break;
	case Scanning:
		_ui->lblStatus->setText(tr("Scanning for session %1.").arg(_ui->lineEditName->text()));
		break;
	case Connecting:
		_ui->lblStatus->setText(tr("Found session, connecting..."));
		break;
	case AwaitingChallenge:
		_ui->lblStatus->setText(tr("Waiting for server challenge..."));
		break;
	case AwaitingChallengeResponse:
		_ui->lblStatus->setText(tr("Replied to challenge, sent own..."));
		break;
	case LoggingIn:
		_ui->lblStatus->setText(tr("Logging in..."));
		break;
	case Connected:
		_ui->lblStatus->setText(tr("Connection established!"));
		break;
	case InvalidCert:
		_ui->lblStatus->setText(tr("Invalid certificate."));
		break;
	case InvalidSslHash:
		_ui->lblStatus->setText(tr("Invalid TLS hash: %1.").arg(_hashSslErrorCount));
		break;
	default:
		_ui->lblStatus->setText(tr("Unknown state :-("));
		break;
	}
}

/*
 * Overrides
 */

/***************************************************************************//**
 * Called when a Qt timer fires; used for server discovery and
 * auto-hiding the connect dialog.
 */
void ConnectWindow::timerEvent(QTimerEvent* event)
{
	if (event->timerId() == _timerHide) {
		killTimer(_timerHide);
		_timerHide = 0;
		this->hide();
		_ui->stackedWidget->setCurrentIndex(0);
	} else
		// Unknown/Old timer id, kill it ??? PALM -> FACE
		killTimer(event->timerId());
}

/***************************************************************************//**
 * Handle incoming closeEvent and hide window.
 * @param e
 */
void ConnectWindow::closeEvent(QCloseEvent *e)
{
	e->ignore();
	this->hide();
}

void ConnectWindow::doShow()
{
	/* reset to automatic connect window */
	_ui->stackedWidget->setCurrentIndex(0);
	_ui->comboBox_rooms->setCurrentIndex(0);
	show();
	showNormal();
	activateWindow();
	raise();
}

/***************************************************************************//**
 * Gives the keyboard input focus to the input line.
 * @param event
 */
void ConnectWindow::showEvent(QShowEvent* /* event */ )
{
	activateWindow();
	raise();
	_ui->lineEditName->setFocus();
}

/***************************************************************************//**
 * Public function connect to session.
 * Check if currently connected to server,
 *							if not --> connect to given sessionName.
 * @param sessionName
 */
void ConnectWindow::connectToSession(const QByteArray sessionName, QString mgrIP)
{
	if (_state != Idle)
		return;
	_currentSession = sessionName;
	_currentIp = mgrIP;
	_state = Scanning;
	this->updateUserInterface();
	_tryReconnect = true;
	_serverDiscovery.start(sessionName, mgrIP);
}

/*
 * Slots
 */



void ConnectWindow::DoConnect()
{
	qDebug() << "DoConnect()";
	//  Connect (scan for session)
	// qDebug() << _ui->lineEditName->text().toUtf8();
	int index = _ui->comboBox_rooms->currentIndex();
	QString selectedMgrIP = _ui->comboBox_rooms->itemData(index).toString();

	if (selectedMgrIP == "manual_connection") {
		qDebug() << "connect to sessionName by manual connection";
		QByteArray sessionName = _ui->lineEditName->text().toUtf8();
		connectToSession(sessionName, "");
	} else {
		qDebug() << "connect to mgrIP (through room selection) " << selectedMgrIP;
		connectToSession("", selectedMgrIP);
	}
}

void ConnectWindow::DoDisconnect()
{
	qDebug() << "DoDisconnect()";
	_tryReconnect = false;
	// Stop or disconnect
	emit disconnect();
	_state = Idle;
}


/***************************************************************************//**
 * Handle click on Connect/Disconnect button.
 * If already connected --> Stop/disconnect.
 * Else scanning for given sessionId.
 */
void ConnectWindow::onBtnConnection()
{
	if (_timerHide) {
		killTimer(_timerHide);
		_timerHide = 0;
		_ui->stackedWidget->setCurrentIndex(0);
	}

	if (_serverDiscovery.isActive())
		_serverDiscovery.stop();

	if (_state != Idle) {
		DoDisconnect();
	} else {
		DoConnect();
	}
	this->updateUserInterface();
}

/** set the available rooms.
 *  If the list of rooms is empty, switches automatically to the "manual
 *  connection" page */
void ConnectWindow::setAvailableRooms(QList<Room> m)
{
	_ui->comboBox_rooms->clear();
	foreach (Room r, m) {
		_ui->comboBox_rooms->addItem(r.name, r.mgr);
	}
	/* also add a pseudo-room "manual choice" */
	_ui->comboBox_rooms->addItem(tr("Session Name..."), "manual_connection");
}
/***************************************************************************//**
 * Handle click on Cancel/Hide Button.
 * Just hide the window.
 */
void ConnectWindow::onBtnCancel()
{
	this->hide();
}

/** check if "manual_connection" is selected, then switch to manual
 * connection page */
void ConnectWindow::onRoomSelection(int index)
{
	QString sessionName = _ui->comboBox_rooms->itemData(index).toString();
	if (sessionName == "manual_connection") {
		qDebug() << "switch to manual connection";
		_ui->box_manual->setVisible(true);
		//this->setSize(QSize(300,200));
		this->resize(300, 200);
	} else {
		_ui->box_manual->setVisible(false);
		this->resize(300, 140);
	}
}

/***************************************************************************//**
 * @brief ConnectWindow::onServerDetected
 * @param host
 * @param port
 * @param sessionName
 * @param certHash
 */
void ConnectWindow::onServerDetected(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect)
{
	_pendingConnection = new ServerConnection(host, port, sessionName, certHash, autoConnect);
	connect(_pendingConnection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState)));
	connect(_pendingConnection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*)));
	connect(_pendingConnection, SIGNAL(disconnected(ServerConnection*)), this, SLOT(onConnectionDisconnected(ServerConnection*)));
}


/***************************************************************************//**
 * Handle connection state changes and update member variables describing state.
 * @param state
 */
void ConnectWindow::onConnectionStateChange(ConnectWindow::ConnectionState state)
{
	bool reset = (_state == Scanning);
	if (state == InvalidSslHash)
		++_hashSslErrorCount;

	_state = state;

	if (reset) {
		_state = Scanning;
	}
	if (state == Connected) {
		QObject::disconnect(_pendingConnection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState)));
		QObject::disconnect(_pendingConnection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*)));
		emit connected(_pendingConnection);
		_pendingConnection = nullptr;

		this->updateUserInterface();

		_timerHide = startTimer(2000);
	} else {
		this->updateUserInterface();
	}

}

void ConnectWindow::onComboBox_keyPressed(QKeyEvent* e)
{
	qDebug() << "key released";
	if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return ) {
		qDebug() << "enter pressed";
	}

}

/***************************************************************************//**
 * If connection is closed set _pendingConnection = nullptr.
 * @param connection
 */
void ConnectWindow::onConnectionClosed(QObject* /* connection */ )
{
	_pendingConnection = nullptr;
}

/***************************************************************************//**
 * @brief ConnectWindow::onConnectionDisconnected
 */
void ConnectWindow::onConnectionDisconnected(ServerConnection*)
{
	_state = Idle;
	this->updateUserInterface();
	if (_tryReconnect) {
		this->updateUserInterface();
		connectToSession(_currentSession, _currentIp);
	}
}