From 20a40ae90f6c0e4cac0345c57ac54f1bd671f633 Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Fri, 16 May 2014 00:44:00 +0200 Subject: Several changes: * Change the messagebox text * Add button stub for exclusiveStudentToTutor * Split prepareProjection in broacast, unicast and exclusiveunicast (stub) * Make multiple used, hardcoded strings static const member of mainwindow * Replace multiple loops asking for selection and tutor by membervariable * references to the connectionframe of the latter * Complete missing error messages --- src/server/mainwindow/mainwindow.cpp | 349 ++++++++++++++++++++++------------- src/server/mainwindow/mainwindow.h | 73 ++++---- 2 files changed, 255 insertions(+), 167 deletions(-) (limited to 'src') diff --git a/src/server/mainwindow/mainwindow.cpp b/src/server/mainwindow/mainwindow.cpp index 47f9610..ca9ecca 100644 --- a/src/server/mainwindow/mainwindow.cpp +++ b/src/server/mainwindow/mainwindow.cpp @@ -36,6 +36,14 @@ // Auto-generated ui class #include "ui_mainwindow.h" +const QString MainWindow::sStrTutorNdef = tr("No tutor defined."); +const QString MainWindow::sStrTutorOffline = tr("Tutor is offline."); +const QString MainWindow::sStrSourceNdef = tr("Please select a projection source."); +const QString MainWindow::sStrSourceOffline = tr("The projection source is offline."); +const QString MainWindow::sStrDestNdef = tr("Please select a projection destination."); +const QString MainWindow::sStrDestOffline = tr("The projection destination is offline."); +const QString MainWindow::sStrSourceDestSame = tr("Selected projection target is tutor."); + /***************************************************************************//** * Initialize MainWindow and ListenServer. * @param ipListUrl @@ -44,6 +52,10 @@ MainWindow::MainWindow(QString ipListUrl, QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow), _tbIconSize(24), _tbArea(Qt::LeftToolBarArea) { + + _tutorFrame = NULL; + _selectedFrame = NULL; + _sessionNameWindow = new SessionNameWindow(this); ui->setupUi(this); @@ -76,6 +88,7 @@ MainWindow::MainWindow(QString ipListUrl, QWidget* parent) : connect(ui->action_BroadcastScreen, SIGNAL(triggered()), this, SLOT(onButtonStudentToAll())); connect(ui->action_TutorToAll, SIGNAL(triggered()), this, SLOT(onButtonTutorToAll())); connect(ui->action_StudentToTutor, SIGNAL(triggered()), this, SLOT(onButtonStudentToTutor())); + connect(ui->action_StudentToTutorExclusive, SIGNAL(triggered()), this, SLOT(onButtonStudentToTutorExclusive())); connect(ui->action_TutorToStudent, SIGNAL(triggered()), this, SLOT(onButtonTutorToStudent())); connect(ui->action_StopProjection, SIGNAL(triggered()), this, SLOT(onButtonStopProjection())); connect(ui->action_SetAsTutor, SIGNAL(triggered()), this, SLOT(onButtonSetAsTutor())); @@ -301,9 +314,9 @@ void MainWindow::mouseReleaseEvent(QMouseEvent* e) const QSize frame(ui->frmRoom->size()); if (frame.width() > pos.x() && frame.height() > pos.y()) { - for (QList::iterator it = _clientFrames.begin(); it != _clientFrames.end(); ++it) - { - (**it).setSelection(false); + if (_selectedFrame != NULL) { + _selectedFrame->setSelection(false); + _selectedFrame = NULL; } } } @@ -424,13 +437,16 @@ void MainWindow::onPlaceFrame(ConnectionFrame* frame) */ void MainWindow::onFrameClicked(ConnectionFrame* frame) { - for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - { - if (*it == frame) - frame->setSelection(true); - else - (**it).setSelection(false); - } + // If same frame is clicked again,, do nothing + if (_selectedFrame == frame) + return; + + // If another frame has been selected, unselect it + // Set the ui selected and set a new reference + if (_selectedFrame != NULL) + _selectedFrame->setSelection(false); + _selectedFrame = frame; + _selectedFrame->setSelection(true); } /***************************************************************************//** @@ -466,6 +482,113 @@ bool MainWindow::isValidClient(Client* client) return false; } + +/***************************************************************************//** + * @brief broadcast + * @param from + */ +void MainWindow::broadcast(Client *from) +{ + // Projection source is never allowed to be an active VNC viewer + if (from->isActiveVncClient()) + { + qDebug("From is active client, stopping..."); + from->stopVncClient(); + } + from->setDesiredProjectionSource(0); + + qDebug("Broadcast requested..."); + from->setProjectionSource(true); + + // Set desired projection source on all clients + for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) + { + Client *c = (**it).client(); + if (c == NULL || c->id() == from->id()) + continue; // Self or offline + c->setDesiredProjectionSource(from->id()); + } + + if (from->isActiveVncServer()) + { + // From is already active, if there is at least one active client, assume it is not + // shutting down, so we can directly tell the new client to connect to it + qDebug("Source is already running a VNC server...."); + for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) + { + Client *c = (**it).client(); + if (c == NULL || c->id() == from->id()) + continue; // Self or offline + if (c->currentProjectionSource() != from->id()) + continue; // Other client + // Yep :-) + qDebug("Reusing because of active client"); + this->onVncServerStateChange(from); + return; + } + qDebug("But no active client found...."); + } + // Could not take shortcut, (re)start VNC server on source + qDebug("Starting VNC server on source machine"); + from->startVncServer(); + return; +} + +/***************************************************************************//** + * @brief unicast + * @param from + * @param to + */ +void MainWindow::unicast(Client *from, Client *to) +{ + // Projection source is never allowed to be an active VNC viewer + if (from->isActiveVncClient()) + { + qDebug("From is active client, stopping..."); + from->stopVncClient(); + } + from->setDesiredProjectionSource(0); + + to->setProjectionSource(false); + + if (to->isActiveVncServer()) // TODO aint this not alway the + { + if (to->currentProjectionSource() == from->id()) // TODO SHALL to->VNCSERVER NOT BE CLOSED? + return; // Nothing to do + to->stopVncServer(); + } + to->setDesiredProjectionSource(from->id()); + + if (from->isActiveVncServer()) + { + // From is already active, if there is at least one active client, assume it is not + // shutting down, so we can directly tell the new client to connect to it + for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) + { + Client *c = (**it).client(); + if (c == NULL || c->id() == from->id()) + continue; + if (c->currentProjectionSource() != from->id()) + continue; + // Yep :-) + this->onVncServerStateChange(from); + return; + } + } + // Could not take shortcut, (re)start VNC server on source + from->startVncServer(); +} + +/***************************************************************************//** + * @brief unicastExlusive + * @param from + * @param to + */ +void MainWindow::unicastExlusive(Client *from, Client *to) +{ + +} + /***************************************************************************//** * Handle VNC settings for a projection from "from" to "to". * Check if projection source is active vnc client. @@ -554,6 +677,26 @@ void MainWindow::prepareForProjection(Client * const from, Client * const to) from->startVncServer(); } +/***************************************************************************//** + * Handle projection from tutor to all. + * Get the client who is tutor and set the projectionSource of all + * clients, except the tutor ones, to false. + * Call prepareForProjection(from, to). + */ +void MainWindow::onButtonTutorToAll() +{ + for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) + if ((*it)->client() != NULL && !(**it).isTutor()) + (*it)->client()->setProjectionSource(false); + + if (_tutorFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); + else if (_tutorFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); + else + broadcast(_tutorFrame->client()); +} + /***************************************************************************//** * Button projection from one student to all the others. * First get which client is projectionSource and set this one as from. @@ -562,131 +705,81 @@ void MainWindow::prepareForProjection(Client * const from, Client * const to) */ void MainWindow::onButtonStudentToAll() { - Client *from = NULL; for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - { - Client *c = (**it).client(); - if (c == NULL) - continue; - if ((**it).selected()) - from = c; - else - c->setProjectionSource(false); - } - if (from == NULL) - QMessageBox::critical(this, - tr("Projection"), - tr("No projection source selected.") - ); + if ((*it)->client() != NULL && !(*it)->selected()) + (*it)->client()->setProjectionSource(false); + + if (_selectedFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrSourceNdef); + if (_selectedFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrSourceOffline); else - prepareForProjection(from, NULL); + broadcast(_selectedFrame->client()); } /***************************************************************************//** - * Handle projection from one student to tutor. - * Get the client to project from and get client, who is tutor, as to. + * Handle the projection from Tutor to specific student. + * Set the client who is tutor as from and the selected client as to. * Call prepareForProjection(from, to). */ -void MainWindow::onButtonStudentToTutor() +void MainWindow::onButtonTutorToStudent() { - Client *from = NULL; - Client *to = NULL; - for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - { - Client *c = (**it).client(); - if (c == NULL) - continue; - if ((**it).selected()) - from = c; - else if ((**it).isTutor()) - to = c; - } - if (from == NULL) - QMessageBox::critical(this, - tr("Projection"), - tr("No projection source selected.") - ); - else if (to == NULL) - QMessageBox::critical(this, - tr("Projection"), - tr("No tutor defined, or tutor is offline.") - ); - else if (to == from) - QMessageBox::critical(this, - tr("Projection"), - tr("Selected projection source is tutor.") - ); + if (_selectedFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrDestNdef); + else if (_tutorFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); + else if (_selectedFrame == _tutorFrame) + QMessageBox::critical(this, tr("Projection"), sStrSourceDestSame); + else if (_selectedFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrDestOffline); + else if (_tutorFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else - prepareForProjection(from, to); + unicast(_tutorFrame->client(), _selectedFrame->client()); } /***************************************************************************//** - * Handle projection from tutor to all. - * Get the client who is tutor and set the projectionSource of all - * clients, except the tutor ones, to false. + * Handle projection from one student to tutor. + * Get the client to project from and get client, who is tutor, as to. * Call prepareForProjection(from, to). */ -void MainWindow::onButtonTutorToAll() +void MainWindow::onButtonStudentToTutor() { - // - Client *from = NULL; - for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - { - Client *c = (**it).client(); - if (c == NULL) - continue; - if ((**it).isTutor()) - from = c; - else - c->setProjectionSource(false); - } - if (from == NULL) - QMessageBox::critical(this, - tr("Projection"), - tr("No tutor defined, or tutor is offline.") - ); + if (_selectedFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrSourceNdef); + else if (_tutorFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); + else if (_tutorFrame == _selectedFrame) + QMessageBox::critical(this, tr("Projection"), sStrSourceDestSame); + else if (_selectedFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrSourceOffline); + else if (_tutorFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else - prepareForProjection(from, NULL); + unicast(_selectedFrame->client(), _tutorFrame->client()); } + /***************************************************************************//** - * Handle the projection from Tutor to specific student. - * Set the client who is tutor as from and the selected client as to. - * Call prepareForProjection(from, to). + * @brief MainWindow::onButtonStudentToTutorExclusive */ -void MainWindow::onButtonTutorToStudent() +void MainWindow::onButtonStudentToTutorExclusive() { - Client *from = NULL; - Client *to = NULL; - for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - { - Client *c = (**it).client(); - if (c == NULL) - continue; - if ((**it).selected()) - to = c; - if ((**it).isTutor()) - from = c; - } - if (to == NULL) - QMessageBox::critical(this, - tr("Projection"), - tr("No projection source selected.") - ); - else if (from == NULL) - QMessageBox::critical(this, - tr("Projection"), - tr("No tutor defined, or tutor is offline.") - ); - else if (to == from) - QMessageBox::critical(this, - tr("Projection"), - tr("Selected projection target is tutor.") - ); + if (_selectedFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrSourceNdef); + else if (_tutorFrame == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); + else if (_tutorFrame == _selectedFrame) + QMessageBox::critical(this, tr("Projection"), sStrSourceDestSame); + else if (_selectedFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrSourceOffline); + else if (_tutorFrame->client() == NULL) + QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else - prepareForProjection(from, to); + unicastExlusive(_tutorFrame->client(), _selectedFrame->client()); } + /***************************************************************************//** * Handle Button StopProjection. * Set ProjectionSource of each client to false and create a NetworkMessage to @@ -761,27 +854,21 @@ void MainWindow::onButtonExit() */ void MainWindow::onButtonSetAsTutor() { - bool selected = false; - ConnectionFrame *oldTutor = NULL; - bool changedTutor = false; - for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - { - selected = (*it)->selected(); - if (!selected && (*it)->isTutor()) - { - oldTutor = (*it); - (*it)->setTutor(false); - } - if (selected && ((*it)->client() != NULL)) - { - // This frame is marked to be Tutor. - (*it)->setTutor(true); - changedTutor = true; - } - } - if (!changedTutor && oldTutor != NULL) { - oldTutor->setTutor(true); + // If same frame is already tutor, do nothing + if (_selectedFrame == _tutorFrame) + return; + + // If a frame has been selected unselect it + if (_selectedFrame->client() == NULL){ + QMessageBox::critical(this, tr("Selection"), tr("The selected client is not connected.")); + return; } + + // Else unset the old and set the new tutor + if (_tutorFrame != NULL) + _tutorFrame->setTutor(false); + _tutorFrame = _selectedFrame; + _tutorFrame->setTutor(true); } /***************************************************************************//** diff --git a/src/server/mainwindow/mainwindow.h b/src/server/mainwindow/mainwindow.h index 4b9bf68..5dd177d 100644 --- a/src/server/mainwindow/mainwindow.h +++ b/src/server/mainwindow/mainwindow.h @@ -23,40 +23,43 @@ class MainWindow : public QMainWindow { Q_OBJECT +public: + + MainWindow(QString ipListUrl, QWidget *parent = 0); + ~MainWindow(); + private: - Ui::MainWindow *ui; + // Ui stuff + Ui::MainWindow *ui; SessionNameWindow *_sessionNameWindow; - QLabel *_sessionNameLabel; - - /** - * List of current clients in session. - */ - QList _clientFrames; - int _tbIconSize; - Qt::ToolBarArea _tbArea; - int _tileWidth, _tileHeight; - int _timerId, _timerTimeout; - ListenServer *_listenServer; - DiscoveryListener *_discoveryListener; + QLabel *_sessionNameLabel; + int _tbIconSize; + Qt::ToolBarArea _tbArea; + int _tileWidth, _tileHeight; + ConnectionFrame *_tutorFrame, *_selectedFrame; + static const int _tilesX = 9; + static const int _tilesY = 7; // Button block stuff - QTimer * _buttonLockTimer; - QList _lockingButtons; + QTimer *_buttonLockTimer; + QList _lockingButtons; static const qint64 _buttonBlockTime = 600; - /** - * Downloader for IP - List of possible tutors. - */ - FileDownloader _fileDownloader; - - /** - * Stored List of possible tutor IPs. - */ - QStringList _tutorList; + // Internal stuff + QList _clientFrames; + int _timerId, _timerTimeout; + ListenServer *_listenServer; + DiscoveryListener *_discoveryListener; + FileDownloader _fileDownloader; + QStringList _tutorList; - // Magic numbers - static const int _tilesX = 9; - static const int _tilesY = 7; + static const QString sStrTutorNdef; + static const QString sStrTutorOffline; + static const QString sStrSourceNdef; + static const QString sStrSourceOffline; + static const QString sStrDestNdef; + static const QString sStrDestOffline; + static const QString sStrSourceDestSame; void placeFrameInFreeSlot(ConnectionFrame* frame); ConnectionFrame* createFrame(); @@ -64,31 +67,32 @@ private: void savePosition(ConnectionFrame *cf); void prepareForProjection(Client * const from, Client * const to); bool isValidClient(Client* client); + void broadcast(Client *from); + void unicast(Client *from, Client *to); + void unicastExlusive(Client *from, Client *to); -public: - MainWindow(QString ipListUrl, QWidget *parent = 0); - ~MainWindow(); - -protected: void closeEvent(QCloseEvent *e); void changeEvent(QEvent *e); void resizeEvent(QResizeEvent *e); void mouseReleaseEvent(QMouseEvent* e); void timerEvent(QTimerEvent* event); + protected slots: - // This void onTutorListDownloaded(QByteArray& tutorList); void onSessionNameClick(); void onSessionNameUpdate(); void onButtonStudentToAll(); void onButtonStudentToTutor(); + void onButtonStudentToTutorExclusive(); void onButtonTutorToAll(); void onButtonTutorToStudent(); void onButtonStopProjection(); void onButtonLock(bool checked); void onButtonExit(); void onButtonSetAsTutor(); + void DisableButtons(); + void EnableButtons(); // connection frame void onPlaceFrame(ConnectionFrame* frame); void onFrameClicked(ConnectionFrame* frame); @@ -98,8 +102,5 @@ protected slots: void onClientAuthenticated(Client* client); void onVncServerStateChange(Client* client); void onVncClientStateChange(Client* client, int lastProjectionSource); - //Buttons - void DisableButtons(); - void EnableButtons(); }; #endif -- cgit v1.2.3-55-g7522