Pool Video Switch v2
Software video switch for distributed remote display in a lecture environment
connectwindow.cpp
Go to the documentation of this file.
1 /*
2  * connectwindow.cpp
3  *
4  * Created on: 28.01.2013
5  * Author: sr
6  */
7 
8 #include <QNetworkInterface>
9 #include "../../shared/settings.h"
10 #include "../../shared/util.h"
11 #include "../net/serverconnection.h"
12 #include "../clientapp/clientapp.h"
13 #include "connectwindow.h"
14 #include "ui_connectwindow.h"
15 
16 #include <QCloseEvent>
17 
18 #define UDPBUFSIZ 9000
19 #define SALT_LEN 18
20 
21 namespace Ui {
22  class ConnectWindow;
23 }
24 
30 {
31  _ui = new Ui::ConnectWindow;
32  _timerHide = 0;
33  _state = Idle;
35  _pendingConnection = nullptr;
36 
37  // Initialize the GUI
38  _ui->setupUi(this);
39 
40  // Set window properties
41  setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool);
42 
43  // Set page 0 as default
44  _ui->stackedWidget->setCurrentIndex(0);
45 
46  // Set actions of buttons
47  connect(_ui->btn_connection, &QPushButton::clicked, this, &ConnectWindow::onBtnConnection);
48  connect(_ui->btn_hide, &QPushButton::clicked, this, &ConnectWindow::onBtnHide);
49 
50  connect(_ui->comboBox_rooms, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConnectWindow::onRoomSelection);
51 
52  // React on discovery signal
54 
55  /* finally the most requested feature: connect on press of the enter key */
56  connect(_ui->lineEditName, &QLineEdit::returnPressed, this, &ConnectWindow::onBtnConnection);
57 
58  /* by default don't show the manual connection box */
59  _ui->box_manual->setVisible(false);
60 
61  this->updateUserInterface();
62  this->adjustSize();
63 }
64 
69 
70 
71 
77 {
78  _ui->lineEditName->setEnabled(_state == Idle);
79 
80  if (_state == Connected) {
81  _ui->btn_connection->setEnabled(true);
82  _ui->btn_connection->setText(tr("&Disconnect"));
83  _ui->lblStatus->setText(tr("Connected to %1").arg(_currentSession.isEmpty() ? _currentIp : _currentSession));
84  _ui->lineEditName->setEnabled(false);
85  _ui->stackedWidget->setCurrentIndex(1);
86  return;
87  }
88 
89  if (_state != Idle)
90  _ui->btn_connection->setText(tr("&Stop"));
91  else
92  _ui->btn_connection->setText(tr("&Connect"));
93 
94  switch (_state) {
95  case Idle:
96  _ui->lblStatus->setText(tr("Ready to connect."));
97  break;
98  case Scanning:
99  _ui->lblStatus->setText(tr("Scanning for session %1.").arg(_currentSession.isEmpty() ? _currentIp : _currentSession));
100  break;
101  case Connecting:
102  _ui->lblStatus->setText(tr("Found session, connecting..."));
103  break;
104  case AwaitingChallenge:
105  _ui->lblStatus->setText(tr("Waiting for server challenge..."));
106  break;
108  _ui->lblStatus->setText(tr("Replied to challenge, sent own..."));
109  break;
110  case LoggingIn:
111  _ui->lblStatus->setText(tr("Logging in..."));
112  break;
113  case Connected:
114  break;
115  case InvalidCert:
116  _ui->lblStatus->setText(tr("Invalid certificate."));
117  break;
118  case InvalidSslHash:
119  _ui->lblStatus->setText(tr("Invalid TLS hash: %1.").arg(_hashSslErrorCount));
120  break;
121  default:
122  _ui->lblStatus->setText(tr("Unknown state :-("));
123  break;
124  }
125 }
126 
127 /*
128  * Overrides
129  */
130 
135 void ConnectWindow::timerEvent(QTimerEvent* event)
136 {
137  if (event->timerId() == _timerHide) {
138  killTimer(_timerHide);
139  _timerHide = 0;
140  this->hide();
141  _ui->stackedWidget->setCurrentIndex(0);
142  } else
143  // Unknown/Old timer id, kill it ??? PALM -> FACE
144  killTimer(event->timerId());
145 }
146 
151 void ConnectWindow::closeEvent(QCloseEvent *e)
152 {
153  e->ignore();
154  this->hide();
155 }
156 
158 {
159  /* reset to automatic connect window */
160  _ui->stackedWidget->setCurrentIndex(0);
161  _ui->comboBox_rooms->setCurrentIndex(0);
162  show();
163  showNormal();
164  activateWindow();
165  raise();
166 }
167 
172 void ConnectWindow::showEvent(QShowEvent* /* event */ )
173 {
174  activateWindow();
175  raise();
176  _ui->lineEditName->setFocus();
177  this->adjustSize();
178 }
179 
186 void ConnectWindow::connectToSession(const QByteArray& sessionName, const QString &mgrIP)
187 {
188  if (_state != Idle)
189  return;
190  qDebug() << "Connection attempt started: session" << sessionName << "manager IP" << mgrIP;
191  _currentSession = sessionName;
192  _currentIp = mgrIP;
193  _state = Scanning;
194  this->updateUserInterface();
195  _tryReconnect = true;
196  _serverDiscovery.start(sessionName, mgrIP);
197 }
198 
199 /*
200  * Slots
201  */
202 
203 
204 
206 {
207  qDebug() << "DoConnect()";
208  // Connect (scan for session)
209  // qDebug() << _ui->lineEditName->text().toUtf8();
210  int index = _ui->comboBox_rooms->currentIndex();
211  QString selectedMgrIP = _ui->comboBox_rooms->itemData(index).toString();
212 
213  if (selectedMgrIP == "manual_connection") {
214  qDebug() << "connect to sessionName by manual connection";
215  QByteArray sessionName = _ui->lineEditName->text().toUtf8();
216  connectToSession(sessionName, "");
217  } else {
218  qDebug() << "connect to mgrIP (through room selection) " << selectedMgrIP;
219  connectToSession("", selectedMgrIP);
220  }
221 }
222 
224 {
225  qDebug() << "Stopping connection attempt or disconnecting from session" << _currentSession << _currentIp
226  << "state" << int(_state);
227  _tryReconnect = false;
228  // Stop or disconnect
229  emit disconnect();
230  _state = Idle;
231 }
232 
233 
240 {
241  if (_timerHide) {
242  killTimer(_timerHide);
243  _timerHide = 0;
244  _ui->stackedWidget->setCurrentIndex(0);
245  }
246 
249 
250  if (_state != Idle) {
251  DoDisconnect();
252  } else {
253  DoConnect();
254  }
255  this->updateUserInterface();
256 }
257 
261 void ConnectWindow::setAvailableRooms(const QList<Room>& m)
262 {
263  _ui->comboBox_rooms->clear();
264  foreach (Room r, m) {
265  _ui->comboBox_rooms->addItem(r.name, r.mgr);
266  }
267  /* also add a pseudo-room "manual choice" */
268  _ui->comboBox_rooms->addItem(tr("Session Name..."), "manual_connection");
269 }
275 {
276  this->hide();
277 }
278 
282 {
283  QString sessionName = _ui->comboBox_rooms->itemData(index).toString();
284  if (sessionName == "manual_connection") {
285  qDebug() << "switch to manual connection";
286  _ui->box_manual->setVisible(true);
287  } else {
288  _ui->box_manual->setVisible(false);
289  }
290  this->adjustSize();
291 }
292 
300 void ConnectWindow::onServerDetected(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect)
301 {
302  qDebug() << "ConnectWindow: server selected for connection" << host << "port" << port
303  << "session" << sessionName << "certificate SHA-1" << certHash.toHex()
304  << "auto-connect" << autoConnect;
305  _pendingConnection = new ServerConnection(host, port, sessionName, certHash, autoConnect);
307  connect(_pendingConnection, &ServerConnection::destroyed, this, &ConnectWindow::onConnectionClosed);
309 }
310 
311 
317 {
318  qDebug() << "ConnectWindow: connection state changed from" << int(_state) << "to" << int(state);
319  bool reset = (_state == Scanning);
320  if (state == InvalidSslHash)
322 
323  _state = state;
324  this->updateUserInterface();
325 
326  if (reset) {
327  _state = Scanning;
328  }
329  if (state == Connected) {
331  QObject::disconnect(_pendingConnection, &ServerConnection::destroyed, this, &ConnectWindow::onConnectionClosed);
333  _pendingConnection = nullptr;
334  _timerHide = startTimer(2000);
335  }
336 
337 }
338 
344 {
345  _pendingConnection = nullptr;
346 }
347 
352 {
353  qDebug() << "ConnectWindow: connection disconnected"
354  << (connection ? connection->getPeerAdress() : QStringLiteral("unknown peer"))
355  << "reconnect enabled" << _tryReconnect;
356  _state = Idle;
357  this->updateUserInterface();
358  if (_tryReconnect) {
359  qDebug() << "ConnectWindow: retrying discovery for session" << _currentSession << "manager IP" << _currentIp;
360  this->updateUserInterface();
361  connectToSession(_currentSession, _currentIp);
362  }
363 }
QString getPeerAdress() const
Class for handling the client connection.
Definition: connectwindow.h:41
void onConnectionClosed(QObject *connection)
If connection is closed set _pendingConnection = nullptr.
int _hashSslErrorCount
Definition: connectwindow.h:66
void DoDisconnect()
actually disconnects the connection
QString mgr
Definition: room.h:7
void updateUserInterface()
Handle changes in state and update window.
QByteArray _currentSession
Definition: connectwindow.h:70
ConnectWindow(QWidget *parent=nullptr)
Initialize Connection Window.
void onBtnConnection()
Handle click on Connect/Disconnect button.
void stateChange(ConnectWindow::ConnectionState state)
void onServerDetected(const QString &host, quint16 port, const QByteArray &sessionName, const QByteArray &certHash, bool autoConnect)
ConnectWindow::onServerDetected.
void onRoomSelection(int index)
check if "manual_connection" is selected, then switch to manual connection page
void start(const QByteArray &sessionName, const QString &mgrIP)
start
ServerDiscovery _serverDiscovery
Definition: connectwindow.h:67
void disconnected(ServerConnection *connection)
~ConnectWindow() override
ConnectWindow::~ConnectWindow.
ConnectionState _state
Definition: connectwindow.h:69
ServerConnection * _pendingConnection
Definition: connectwindow.h:68
void setAvailableRooms(const QList< Room > &m)
set the available rooms.
void DoConnect()
actually connects the connection
QString name
Definition: room.h:8
void disconnect()
void onBtnHide()
Handle click on Cancel/Hide Button.
void closeEvent(QCloseEvent *e) override
Handle incoming closeEvent and hide window.
void connectToSession(const QByteArray &sessionName, const QString &mgrIP)
Public function connect to session.
void onConnectionDisconnected(ServerConnection *connection)
ConnectWindow::onConnectionDisconnected.
void connected(ServerConnection *connection)
void timerEvent(QTimerEvent *event) override
Called when a Qt timer fires; used for server discovery and auto-hiding the connect dialog...
void serverDetected(const QString &host, const quint16 port, const QByteArray &sessionName, const QByteArray &certHash, bool autoConnect)
Definition: room.h:6
void onConnectionStateChange(ConnectWindow::ConnectionState state)
Handle connection state changes and update member variables describing state.
void showEvent(QShowEvent *event) override
Gives the keyboard input focus to the input line.
Ui::ConnectWindow * _ui
Definition: connectwindow.h:65
QString _currentIp
Definition: connectwindow.h:71