From 9ba63d1460db41c219b638212b42476164fcfdff Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Jul 2018 13:08:25 +0200 Subject: Update code style, fix compiler warnings - Use nullptr instead of NULL for better warnings in case of mistakes - Get rid of VLAs which are not in C++11 actually - Fix implicit signed <-> unsigned mismatches by adding checks and casts --- src/server/mainwindow/mainwindow.cpp | 137 ++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 67 deletions(-) (limited to 'src/server/mainwindow/mainwindow.cpp') diff --git a/src/server/mainwindow/mainwindow.cpp b/src/server/mainwindow/mainwindow.cpp index 5daade4..dcf8973 100644 --- a/src/server/mainwindow/mainwindow.cpp +++ b/src/server/mainwindow/mainwindow.cpp @@ -190,7 +190,7 @@ void MainWindow::clientCountChanged() for (auto it = _clientFrames.begin(); it != _clientFrames.end(); ++it) { Client* c = (*it)->client(); - if (c != NULL) { + if (c != nullptr) { bool b = c->isExamMode(); examClientCount += b ? 1 : 0; clientCount++; @@ -262,7 +262,7 @@ int distance(QPointF a, QPointF b) QPoint MainWindow::closestFreeSlot(QPoint preferredPixels, ConnectionFrame* toIgnore) { const bool pickFirstOne = ( preferredPixels == QPoint(-1, -1) ); - if (!pickFirstOne && toIgnore != NULL) { + if (!pickFirstOne && toIgnore != nullptr) { // Check if we're already in the desired location and skip all the checks QPoint desired = QPoint(preferredPixels.x() / getTileWidthPx(), preferredPixels.y() / getTileHeightPx()); if (desired == toIgnore->getGridPosition()) { @@ -270,8 +270,9 @@ QPoint MainWindow::closestFreeSlot(QPoint preferredPixels, ConnectionFrame* toIg } } const QSize& clientSize = serverApp->getCurrentRoom()->clientSize; - bool grid[_tilesX][_tilesY]; - memset(grid, 0, sizeof(bool) * _tilesX * _tilesY); /* set everything to false */ +#define GRID(X,Y) (grid[X * _tilesX + Y]) + bool *grid = new bool[_tilesX * _tilesY]; + memset(grid, 0, sizeof(bool) * size_t(_tilesX * _tilesY)); /* set everything to false */ /* fill grid */ for (auto it = _clientFrames.begin(); it != _clientFrames.end(); ++it) { @@ -282,7 +283,7 @@ QPoint MainWindow::closestFreeSlot(QPoint preferredPixels, ConnectionFrame* toIg for (int x = p.x(); x < p.x() + clientSize.width(); x++) { for (int y = p.y(); y < p.y() + clientSize.height(); y++) { - grid[x][y] = true; + GRID(x, y) = true; } } } @@ -295,7 +296,7 @@ QPoint MainWindow::closestFreeSlot(QPoint preferredPixels, ConnectionFrame* toIg bool isFree = true; for (int dx = 0; dx < clientSize.width(); dx++) { for (int dy = 0; dy < clientSize.height(); dy++) { - if (grid[x + dx][y + dy]) { + if (GRID(x + dx, y + dy)) { isFree = false; goto endLoop; // double-break } @@ -311,6 +312,8 @@ endLoop: ; } } endSearch: ; +#undef GRID + delete[] grid; /* among all the free positions, find the closest */ int min_distance = 1000000; QPoint bestPosition = QPoint(0, 0); @@ -342,7 +345,7 @@ void MainWindow::placeFrameInFreeSlot(ConnectionFrame* frame, QPoint preferredPi ConnectionFrame* MainWindow::createFrame(const QString &computerId, const QPoint* gridPosition, bool fromRoomplan) { ConnectionFrame *cf = new ConnectionFrame(this, ui->frmRoom, fromRoomplan); - if (gridPosition == NULL) { + if (gridPosition == nullptr) { placeFrameInFreeSlot(cf); } else { cf->setGridPosition(*gridPosition); @@ -378,47 +381,47 @@ void MainWindow::tellClientCurrentSituation(Client* client) * Returns connected client which belongs to given id. * Iterating over ConnectionFrames and comparing id to given id. * @param id - * @return Client with given id, if not NULL. + * @return Client with given id, if not nullptr. */ Client* MainWindow::getClientFromId(int id) { for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { - if ((*it)->client() != NULL) { + if ((*it)->client() != nullptr) { if ((*it)->client()->id() == id) return (*it)->client(); } } - return NULL; + return nullptr; } /***************************************************************************//** * Return the Frame, which is currently beeing Tutor. * Iterating over all ConnectionFrames, and looking for flag _isTutor. * @return Frame with flag _isTutor = true, - * else NULL if no Tutor is available. + * else nullptr if no Tutor is available. */ ConnectionFrame* MainWindow::getTutorFrame() { for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { - if (((*it) != NULL) && ((*it)->isTutor())) + if (((*it) != nullptr) && ((*it)->isTutor())) return (*it); } - return NULL; + return nullptr; } /***************************************************************************//** * Return the Frame, which is currently selected by user. * Iterating over all ConnectionFrame and looking for flag _isSelected. * @return Frame with flag _isSelected = true, - * else NULL if no frame is selected. + * else nullptr if no frame is selected. */ ConnectionFrame* MainWindow::getSelectedFrame() { for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { - if (((*it) != NULL) && ((*it)->isSelected())) + if (((*it) != nullptr) && ((*it)->isSelected())) return (*it); } - return NULL; + return nullptr; } /* @@ -527,7 +530,7 @@ void MainWindow::resizeEvent(QResizeEvent* /* e */ ) } /* update background image label */ - if (_backgroundImage != NULL) { + if (_backgroundImage != nullptr) { int w = ui->frmRoom->width() - 5; /* to make sure that we don't enlarge the window */ int h = ui->frmRoom->height() - 5; ui->imageLabel->hide(); @@ -553,10 +556,10 @@ void MainWindow::unlockContextButtons() (*it)->setEnabled(true); } /* and disable some again based on special rules */ - if (getSelectedFrame()->client() != NULL) { + if (getSelectedFrame()->client() != nullptr) { ui->action_DeleteClient->setEnabled(false); } - if (getSelectedFrame()->client() == NULL) { + if (getSelectedFrame()->client() == nullptr) { ui->action_SetAsTutor->setEnabled(false); ui->action_StudentToTutorExclusive->setEnabled(false); ui->action_StudentToTutor->setEnabled(false); @@ -583,7 +586,7 @@ void MainWindow::mouseReleaseEvent(QMouseEvent* e) const QSize frame(ui->frmRoom->size()); if (frame.width() > pos.x() && frame.height() > pos.y()) { lockContextButtons(); - if (getSelectedFrame() != NULL) { + if (getSelectedFrame() != nullptr) { getSelectedFrame()->setSelection(false); } } @@ -598,13 +601,13 @@ void MainWindow::reset(bool lock) // Unlock all clients for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ((*it)->client() != NULL) { + if ((*it)->client() != nullptr) { (*it)->client()->lockScreen(lock); (*it)->client()->removeAttention(); } // Stop server (Clients get stopped on ACK) - if (getClientFromId(_streamingSource) != NULL) + if (getClientFromId(_streamingSource) != nullptr) getClientFromId(_streamingSource)->stopVncServer(); } @@ -623,7 +626,7 @@ void MainWindow::onPlaceFrame(ConnectionFrame* connectionFrame) const QPoint preferredPixels = connectionFrame->pos(); placeFrameInFreeSlot(connectionFrame, preferredPixels); - //resizeEvent(NULL); + //resizeEvent(nullptr); } /***************************************************************************//** @@ -638,7 +641,7 @@ void MainWindow::onFrameClicked(ConnectionFrame* frame) // If another frame has been selected, unselect it // Set the ui selected and set a new reference - if (getSelectedFrame() != NULL) { + if (getSelectedFrame() != nullptr) { getSelectedFrame()->setSelection(false); } frame->setSelection(true); @@ -698,14 +701,14 @@ void MainWindow::startVncServerIfNecessary(int from) Client* ns = getClientFromId(from); // if there is a server running which is not "from" stop it. - if (os != NULL && _streamingSource != from) + if (os != nullptr && _streamingSource != from) os->stopVncServer(); // Set new streaming source _streamingSource = from; // If streaming source is already active avoid a restart - if (ns != NULL) { + if (ns != nullptr) { if (ns->isActiveVncServer()) { this->onVncServerStateChange(ns); } else { // Could not take shortcut, (re)start VNC server on source @@ -737,11 +740,11 @@ void MainWindow::onReloadRoomCancel() void MainWindow::reloadCurrentRoom() { /* delete old image */ - if (_backgroundImage != NULL) {delete _backgroundImage; } - _backgroundImage = NULL; + if (_backgroundImage != nullptr) {delete _backgroundImage; } + _backgroundImage = nullptr; const Room *room = serverApp->getCurrentRoom(); - if (room != NULL) { + if (room != nullptr) { /* set tiles */ _tilesX = room->gridSize.width(); _tilesY = room->gridSize.height(); @@ -756,7 +759,7 @@ void MainWindow::reloadCurrentRoom() onPlaceFrame(cf); if (computerId == room->tutorIP) { qDebug() << "set computer with id " << computerId << " as tutor per configuration"; - if (getTutorFrame() != NULL) { + if (getTutorFrame() != nullptr) { getTutorFrame()->setTutor(false); } cf->setTutor(true); @@ -785,12 +788,12 @@ void MainWindow::reloadCurrentRoom() } /* and force a resize event (this scales the image) */ - resizeEvent(NULL); + resizeEvent(nullptr); } void MainWindow::onReloadRoomOk() { - if (_reloadWindow->ui->roomList->currentItem() == NULL) { + if (_reloadWindow->ui->roomList->currentItem() == nullptr) { QMessageBox::critical(this, "Warning", tr("No item selected, please select room!"), 0, 1); return; } @@ -855,9 +858,9 @@ void MainWindow::onButtonTutorToAll() { ui->action_Lock->setChecked(false); - if (getTutorFrame() == NULL) + if (getTutorFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); - else if (getTutorFrame()->client() == NULL) + else if (getTutorFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else if (_clientFrames.size() == 1) QMessageBox::critical(this, tr("Projection"), sStrNoDestAv); @@ -870,7 +873,7 @@ void MainWindow::onButtonTutorToAll() // Set all clients as watchers of tutor. Except for the tutors desired source, which hase to be none for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ((*it)->client() != NULL) + if ((*it)->client() != nullptr) (*it)->client()->setDesiredProjectionSource((*it)->client() == getTutorFrame()->client() ? NO_SOURCE : getTutorFrame()->client()->id()); disableButtons(); @@ -887,21 +890,21 @@ void MainWindow::onButtonTutorToStudent() { ui->action_Lock->setChecked(false); - if (getSelectedFrame() == NULL) + if (getSelectedFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrDestNdef); - else if (getTutorFrame() == NULL) + else if (getTutorFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); else if (getSelectedFrame() == getTutorFrame()) QMessageBox::critical(this, tr("Projection"), sStrSourceDestSame); - else if (getSelectedFrame()->client() == NULL) + else if (getSelectedFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrDestOffline); - else if (getTutorFrame()->client() == NULL) + else if (getTutorFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else { // If this is the first call in this mode clear the watchers if (_mode != Mode::Multicast) { for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ((*it)->client() != NULL) + if ((*it)->client() != nullptr) (*it)->client()->setDesiredProjectionSource(NO_SOURCE); } @@ -925,15 +928,15 @@ void MainWindow::onButtonStudentToTutor() { ui->action_Lock->setChecked(false); - if (getSelectedFrame() == NULL) + if (getSelectedFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrSourceNdef); - else if (getTutorFrame() == NULL) + else if (getTutorFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); else if (getTutorFrame() == getSelectedFrame()) QMessageBox::critical(this, tr("Projection"), sStrSourceDestSame); - else if (getSelectedFrame()->client() == NULL) + else if (getSelectedFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrSourceOffline); - else if (getTutorFrame()->client() == NULL) + else if (getTutorFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else { // If this is not the first run in this mode and the current source is selected, stop the streaming. @@ -946,7 +949,7 @@ void MainWindow::onButtonStudentToTutor() // Unset all clients desired sources. Except the tutors desired source, this has to be the selecteds frame for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ((*it)->client() != NULL) + if ((*it)->client() != nullptr) (*it)->client()->setDesiredProjectionSource(getTutorFrame()->client()->id() == (*it)->client()->id() ? getSelectedFrame()->client()->id() : NO_SOURCE); disableButtons(); @@ -963,15 +966,15 @@ void MainWindow::onButtonStudentToTutorExclusive() { ui->action_Lock->setChecked(false); - if (getSelectedFrame() == NULL) + if (getSelectedFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrSourceNdef); - else if (getTutorFrame() == NULL) + else if (getTutorFrame() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorNdef); else if (getTutorFrame() == getSelectedFrame()) QMessageBox::critical(this, tr("Projection"), sStrSourceDestSame); - else if (getSelectedFrame()->client() == NULL) + else if (getSelectedFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrSourceOffline); - else if (getTutorFrame()->client() == NULL) + else if (getTutorFrame()->client() == nullptr) QMessageBox::critical(this, tr("Projection"), sStrTutorOffline); else { Client *selectedClient = getSelectedFrame()->client(); @@ -986,7 +989,7 @@ void MainWindow::onButtonStudentToTutorExclusive() // Unset all clients desired sources. Except the tutors desired source, this has to be the selecteds frame for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ((*it)->client() != NULL) + if ((*it)->client() != nullptr) (*it)->client()->setDesiredProjectionSource(tutorClient->id() == (*it)->client()->id() ? selectedClient->id() : NO_SOURCE); disableButtons(); @@ -1022,14 +1025,14 @@ void MainWindow::onButtonLock(bool checked) void MainWindow::onButtonLockSingle() { // If no frame is selected, warning. - if (getSelectedFrame() == NULL) { + if (getSelectedFrame() == nullptr) { QMessageBox::critical(this, tr("Selection"), tr("No client is selected.")); return; } Client *client = getSelectedFrame()->client(); // If frame of inactive client has been selected unselect it - if (client == NULL) { + if (client == nullptr) { QMessageBox::critical(this, tr("Selection"), tr("The selected client is not connected.")); return; } else { // If selected client is locked, first unlock @@ -1038,7 +1041,7 @@ void MainWindow::onButtonLockSingle() if (!newState) { // If no more clients are locked, reset button for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { - if ((*it)->client() == NULL) + if ((*it)->client() == nullptr) continue; if ((*it)->client()->isLocked()) return; @@ -1063,13 +1066,13 @@ void MainWindow::onButtonExit() void MainWindow::onButtonSetAsTutor() { // If no frame is selected, warning. - if (getSelectedFrame() == NULL) { + if (getSelectedFrame() == nullptr) { QMessageBox::critical(this, tr("Selection"), tr("No client is selected.")); return; } // If frame of inactive client has been selected unselect it - if (getSelectedFrame()->client() == NULL) { + if (getSelectedFrame()->client() == nullptr) { QMessageBox::critical(this, tr("Selection"), tr("The selected client is not connected.")); return; } else { // If selected client is locked, first unlock @@ -1081,7 +1084,7 @@ void MainWindow::onButtonSetAsTutor() return; // Else unset the old and set the new tutor - if (getTutorFrame() != NULL) { + if (getTutorFrame() != nullptr) { getTutorFrame()->setTutor(false); } getSelectedFrame()->setTutor(true); @@ -1122,7 +1125,7 @@ void MainWindow::onClientAuthenticating(Client* client, ClientLogin* request) inuse = false; for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { Client *c = (**it).client(); - if (c == NULL) + if (c == nullptr) continue; if (!c->isAuthed()) continue; @@ -1158,8 +1161,8 @@ void MainWindow::onClientAuthenticated(Client* client) disconnect(client, SIGNAL(authenticated(Client*)), this, SLOT(onClientAuthenticated(Client*))); connect(client, SIGNAL(vncServerStateChange(Client*)), this, SLOT(onVncServerStateChange(Client*))); connect(client, SIGNAL(vncClientStateChange(Client*)), this, SLOT(onVncClientStateChange(Client*))); - ConnectionFrame *existing = NULL; - ConnectionFrame *cf = NULL; + ConnectionFrame *existing = nullptr; + ConnectionFrame *cf = nullptr; for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { if ((*it)->computerId() == client->ip()) { existing = *it; @@ -1167,7 +1170,7 @@ void MainWindow::onClientAuthenticated(Client* client) } // Clients ip already exists, but was not active. - if (existing != NULL) { + if (existing != nullptr) { cf = existing; } else { cf = createFrame(); @@ -1193,7 +1196,7 @@ void MainWindow::onVncServerStateChange(Client* client) if (client->isActiveVncServer()) { // apply the desired projection sources for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ( (*it)->client() != NULL) { // Ignore offline clients + if ( (*it)->client() != nullptr) { // Ignore offline clients if ( (*it)->client()->desiredProjectionSource() == client->id() ) (*it)->client()->startVncClient(client); else @@ -1206,7 +1209,7 @@ void MainWindow::onVncServerStateChange(Client* client) } else { // VNC server stopped on some client or failed to start - reset local pending projection information for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) { - if ((*it)->client() != NULL) { + if ((*it)->client() != nullptr) { if ((*it)->client()->desiredProjectionSource() == client->id()) { (*it)->client()->setDesiredProjectionSource(NO_SOURCE); (*it)->client()->stopVncClient(); @@ -1234,7 +1237,7 @@ void MainWindow::onVncServerStateChange(Client* client) */ void MainWindow::onVncClientStateChange(Client* client) { - if (client != NULL) { + if (client != nullptr) { // VNC Client stopped -> remove from watchers if (!client->isActiveVncClient()) { // Only unset a desired Projection source if it has not changed meanwhile @@ -1252,7 +1255,7 @@ void MainWindow::onVncClientStateChange(Client* client) */ bool serverHasWatchers = false; for (QList::iterator it(_clientFrames.begin()); it != _clientFrames.end(); ++it) - if ((*it)->client() != NULL) + if ((*it)->client() != nullptr) if ((*it)->client()->desiredProjectionSource() == client->projectionSource()) { serverHasWatchers = true; break; @@ -1260,7 +1263,7 @@ void MainWindow::onVncClientStateChange(Client* client) if ( !serverHasWatchers ) { Client* c = getClientFromId(client->projectionSource()); - if (c != NULL) + if (c != nullptr) c->stopVncServer(); } } @@ -1296,11 +1299,11 @@ void MainWindow::onDeleteClient() { // If no frame is selected, warning. ConnectionFrame* frame = getSelectedFrame(); - if (frame == NULL) { + if (frame == nullptr) { QMessageBox::critical(this, tr("Selection"), tr("No client is selected.")); return; } - if (frame->client() != NULL) { + if (frame->client() != nullptr) { QMessageBox::critical(this, tr("Selection"), tr("This client is still connected.")); return; } -- cgit v1.2.3-55-g7522