From ba0cd9ba655c6543d2f9c2a46a3ecf0b6da98f1d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 26 Jul 2018 17:15:55 +0200 Subject: Cleanup code style (C++11, casts, etc) --- src/choosersettings.cpp | 2 +- src/command_line_options.cpp | 56 ++++++++++++++++++++++---------------------- src/dialog.cpp | 43 ++++++++++++++++------------------ src/filedownloader.cpp | 14 +++++------ src/globals.h | 2 ++ src/main.cpp | 6 ++--- src/session.h | 2 +- src/sessionsiconholder.cpp | 2 +- src/sessionsiconholder.h | 2 +- src/sessiontreeitem.cpp | 6 ++--- src/sessiontreeitem.h | 6 ++--- src/sessiontreemodel.cpp | 15 +++++------- src/sessiontreemodel.h | 2 +- src/vsession.cpp | 2 +- src/vsession.h | 1 + 15 files changed, 79 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/choosersettings.cpp b/src/choosersettings.cpp index 28cbdb1..439c9ea 100644 --- a/src/choosersettings.cpp +++ b/src/choosersettings.cpp @@ -9,7 +9,7 @@ #include "globals.h" bool ChooserSettings::settingsLoaded = false; -ChooserSettings* ChooserSettings::chooserSettings = NULL; +ChooserSettings* ChooserSettings::chooserSettings = nullptr; ChooserSettings::ChooserSettings() { // test (and make) directory diff --git a/src/command_line_options.cpp b/src/command_line_options.cpp index 2018760..0f26edf 100644 --- a/src/command_line_options.cpp +++ b/src/command_line_options.cpp @@ -6,39 +6,39 @@ CommandLineOptions::CommandLineOptions(int argc, char * const argv[]) { // parse command line arguments (please sort by short option for easier handling) static const struct option longOptions[] = { - {"allow-vm-edit", no_argument, NULL, 'vmed'}, - {"autoquit", required_argument, NULL, 'aqit'}, - {"base", required_argument, NULL, 'b'}, - {"path", required_argument, NULL, 'b'}, // Compatibility to v1.0 - {"config", required_argument, NULL, 'c'}, - {"debug", no_argument, NULL, 'D'}, - {"default", required_argument, NULL, 'd'}, - {"exam-mode", no_argument, NULL, 'exms'}, - {"fullscreen", no_argument, NULL, 'F'}, - {"file", required_argument, NULL, 'f'}, - {"help", no_argument, NULL, 'h'}, - {"locations", required_argument, NULL, 'l'}, - {"pool", required_argument, NULL, 'P'}, - {"pvs", no_argument, NULL, 'p'}, - {"pvs-checked", no_argument, NULL, 'pvck'}, - {"runscript", no_argument, NULL, 'S'}, - {"size", required_argument, NULL, 's'}, - {"tab", required_argument, NULL, 'T'}, - {"theme", required_argument, NULL, 't'}, - {"url", required_argument, NULL, 'u'}, - {"version", no_argument, NULL, 'v'}, - {"xpath", required_argument, NULL, 'x'}, - {"location-mode", required_argument, NULL, 'locm'}, - {"template-mode", required_argument, NULL, 'tmpm'}, - {"start-uuid", required_argument, NULL, 'uuid'}, - {"no-vtx", no_argument, NULL, 'nvtx'}, - {0, 0, 0, 0} + {"allow-vm-edit", no_argument, nullptr, 'vmed'}, + {"autoquit", required_argument, nullptr, 'aqit'}, + {"base", required_argument, nullptr, 'b'}, + {"path", required_argument, nullptr, 'b'}, // Compatibility to v1.0 + {"config", required_argument, nullptr, 'c'}, + {"debug", no_argument, nullptr, 'D'}, + {"default", required_argument, nullptr, 'd'}, + {"exam-mode", no_argument, nullptr, 'exms'}, + {"fullscreen", no_argument, nullptr, 'F'}, + {"file", required_argument, nullptr, 'f'}, + {"help", no_argument, nullptr, 'h'}, + {"locations", required_argument, nullptr, 'l'}, + {"pool", required_argument, nullptr, 'P'}, + {"pvs", no_argument, nullptr, 'p'}, + {"pvs-checked", no_argument, nullptr, 'pvck'}, + {"runscript", no_argument, nullptr, 'S'}, + {"size", required_argument, nullptr, 's'}, + {"tab", required_argument, nullptr, 'T'}, + {"theme", required_argument, nullptr, 't'}, + {"url", required_argument, nullptr, 'u'}, + {"version", no_argument, nullptr, 'v'}, + {"xpath", required_argument, nullptr, 'x'}, + {"location-mode", required_argument, nullptr, 'locm'}, + {"template-mode", required_argument, nullptr, 'tmpm'}, + {"start-uuid", required_argument, nullptr, 'uuid'}, + {"no-vtx", no_argument, nullptr, 'nvtx'}, + {nullptr, 0, nullptr, 0} }; int c; // Again, please sort alphabetically in getopt_long call and switch statement - while ((c = getopt_long(argc, argv, "b:c:Dd:Ff:hl:P:pSs:t:T:u:vx:?", longOptions, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "b:c:Dd:Ff:hl:P:pSs:t:T:u:vx:?", longOptions, nullptr)) != -1) { switch (c) { case 'aqit': options.insert("autoquit", optarg); diff --git a/src/dialog.cpp b/src/dialog.cpp index 6250114..8399e62 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -100,12 +100,8 @@ Dialog::~Dialog() { void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); - switch (e->type()) { - case QEvent::LanguageChange: + if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(this); - break; - default: - break; } } @@ -117,11 +113,11 @@ void Dialog::on_treeView_doubleClicked(const QModelIndex& index) SessionTreeItem* item = static_cast(index.internalPointer()); - if (item == NULL) + if (item == nullptr) return; // do nothing if cast failed const Session* s(item->session()); - if (s == NULL) // no valid session has been selected, do nothing + if (s == nullptr) // no valid session has been selected, do nothing return; // These two are up here in case run-virt cares... @@ -173,13 +169,13 @@ void Dialog::on_treeView_doubleClicked(const QModelIndex& index) if (pid == 0) { // At this point we are executing as the 2nd child process // Replace this clone with the process we'd like to start - char const * const args[] = {VMCHOOSER_SESSION_START_SCRIPT, NULL}; - execv(VMCHOOSER_SESSION_START_SCRIPT, (char**)args); + char * const args[] = { strdup(VMCHOOSER_SESSION_START_SCRIPT), nullptr }; + execv(VMCHOOSER_SESSION_START_SCRIPT, args); } _exit(0); // Dont use exit hooks } // Wait and cleanup the intermediate process - waitpid(pid, NULL, 0); + waitpid(pid, nullptr, 0); } ChooserSettings::setSetting("last-session", (s->shortDescription())); ChooserSettings::setSetting("last-tab", QString::number(activeTab_)); @@ -195,7 +191,7 @@ void Dialog::on_treeView_doubleClicked(const QModelIndex& index) void Dialog::on_treeView_expanded(const QModelIndex& index) { SessionTreeItem* item = static_cast(index.internalPointer()); - if (item->session() == NULL && item->sectionType() != SECTION_FOR_LOCATION) { + if (item->session() == nullptr && item->sectionType() != SECTION_FOR_LOCATION) { genericExpandedOnce_ = true; } } @@ -262,7 +258,7 @@ bool Dialog::selectSession(const QString& name) { continue; SessionTreeItem* item = static_cast(index.internalPointer()); const Session* s = item->session(); - if (s == NULL) { + if (s == nullptr) { continue; } if (s->shortDescription() == name) { @@ -296,7 +292,7 @@ bool Dialog::selectSessionByUuid(const QString& name) { QModelIndex index(model_[tab]->index(j, 0, section)); SessionTreeItem* item = static_cast(index.internalPointer()); const Session* s(item->session()); - if (s == NULL) { + if (s == nullptr) { continue; } /* TODO: implement this here */ @@ -457,7 +453,7 @@ void Dialog::addSessionsAfterDownload(QNetworkReply* reply) { // TODO: Filter user's classes and add to tab[TAB_MY_COURSES] bool showEdit = false; for (QList::const_iterator it = sessions.begin(); it != sessions.end(); ++it) { - if (((VSession*)(*it))->canEdit()) { + if (reinterpret_cast(*it)->canEdit()) { showEdit = true; break; } @@ -485,7 +481,7 @@ void Dialog::mousePressEvent(QMouseEvent * event) { void Dialog::treeView_selectionChanged(const QModelIndex& current, const QModelIndex&) { SessionTreeItem* item = static_cast(current.internalPointer()); - if (item == NULL) { + if (item == nullptr) { ui->chkAdminMode->setEnabled(false); return; } @@ -500,7 +496,7 @@ void Dialog::treeView_selectionChanged(const QModelIndex& current, const QModelI QString description; if (s->type() == Session::VSESSION) { - const VSession* vs = (VSession*) s; + const VSession* vs = reinterpret_cast(s); ui->label_creator->setText(vs->getAttribute("creator", "param")); ui->label_creator->setToolTip(vs->getAttribute("creator", "param")); @@ -582,25 +578,25 @@ void Dialog::on_filterEdit_textEdited() { void Dialog::on_filterEdit_textChanged() { if (activeTab_ < 0 || activeTab_ >= TAB_COUNT) return; - SessionTreeModel *newModel = NULL; + SessionTreeModel *newModel = nullptr; // filter the current model if (!ui->filterEdit->text().isEmpty() && ui->filterEdit->text().replace(" ", "").length() > 2) { - if (model_[activeTab_] != NULL) { + if (model_[activeTab_] != nullptr) { newModel = new SessionTreeModel(this); newModel->addItems(this->model_[activeTab_]->lookForItem(ui->filterEdit->text())); } } - if (newModel == NULL) { + if (newModel == nullptr) { newModel = model_[activeTab_]; } - if (newModel != NULL) { + if (newModel != nullptr) { setListModel(newModel); } } void Dialog::setListModel(SessionTreeModel *model) { - QAbstractItemModel *old = NULL; + QAbstractItemModel *old = nullptr; if (ui->treeView->model() == model_[0] || ui->treeView->model() == model_[1] || ui->treeView->model() == model_[2]) { } else { old = ui->treeView->model(); @@ -617,7 +613,7 @@ void Dialog::setListModel(SessionTreeModel *model) { ui->treeView->collapse(index); } } - if (old != NULL) { + if (old != nullptr) { old->deleteLater(); } @@ -674,7 +670,7 @@ void Dialog::addNewsAfterDownload(QNetworkReply* reply) { QDomElement timeNode = newsNode.firstChildElement("date"); QDomElement infoNode = newsNode.firstChildElement("info"); QDateTime timestamp; - timestamp.setTime_t(timeNode.text().toInt()); + timestamp.setTime_t(timeNode.text().toUInt()); if (timeNode.isNull() || infoNode.isNull()) { ui->newsTextBrowser->setText(QCoreApplication::instance()->translate("Dialog", "Could not get news. (//news/date or //news/info missing)")); @@ -778,6 +774,7 @@ void Dialog::keyPressEvent(QKeyEvent* event) { case Qt::Key_Return: this->on_pushButtonStart_clicked(); break; case Qt::Key_Escape: this->on_pushButtonAbort_clicked(); break; case Qt::Key_H: this->on_helpNewsButton_clicked(); break; + default: break; } QDialog::keyPressEvent(event); } diff --git a/src/filedownloader.cpp b/src/filedownloader.cpp index 7e2574b..4da3586 100644 --- a/src/filedownloader.cpp +++ b/src/filedownloader.cpp @@ -26,7 +26,7 @@ bool FileDownloader::downloadFile() { if (this->started) return true; QNetworkReply *reply = m_WebCtrl.get(QNetworkRequest(this->url)); - if (reply == NULL) + if (reply == nullptr) return false; this->started = true; connect(reply, SIGNAL(finished()), SLOT(fileDownloaded())); @@ -39,14 +39,14 @@ bool FileDownloader::downloadFile() { */ void FileDownloader::downloadFailed(QNetworkReply::NetworkError) { - QNetworkReply *reply = (QNetworkReply*)this->sender(); + QNetworkReply *reply = reinterpret_cast(this->sender()); killReply(reply); emit downloaded(this->url, QByteArray()); } void FileDownloader::fileDownloaded() { - QNetworkReply *reply = (QNetworkReply*)this->sender(); - if (reply == NULL) + QNetworkReply *reply = reinterpret_cast(this->sender()); + if (reply == nullptr) return; QByteArray downloadedData(reply->readAll()); killReply(reply); @@ -55,8 +55,8 @@ void FileDownloader::fileDownloaded() { } void FileDownloader::downloadProgress(qint64 received, qint64 totalSize) { - QNetworkReply *reply = (QNetworkReply*)this->sender(); - if (reply == NULL) + QNetworkReply *reply = reinterpret_cast(this->sender()); + if (reply == nullptr) return; if (received > MAXSIZE || totalSize > MAXSIZE) { killReply(reply); @@ -65,7 +65,7 @@ void FileDownloader::downloadProgress(qint64 received, qint64 totalSize) { } void FileDownloader::killReply(QNetworkReply *reply) { - if (reply == NULL) + if (reply == nullptr) return; reply->blockSignals(true); reply->abort(); diff --git a/src/globals.h b/src/globals.h index d0f1c75..707182e 100644 --- a/src/globals.h +++ b/src/globals.h @@ -9,6 +9,8 @@ #define VMCHOOSER_DEFAULT_WIDTH 800 #define VMCHOOSER_DEFAULT_HEIGHT 600 +#include + class Session; extern bool g_debugMode; diff --git a/src/main.cpp b/src/main.cpp index 41600a6..46d8bc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -234,7 +234,7 @@ int main(int argc, char *argv[]) { g_forLocationHandling = LOCATION_EXCLUSIVE; } else { qDebug() << "Invalid location mode: " << mode; - QMessageBox::critical(NULL, "Error", "Invalid location mode: " + mode); + QMessageBox::critical(nullptr, "Error", "Invalid location mode: " + mode); return 1; } } @@ -247,7 +247,7 @@ int main(int argc, char *argv[]) { g_templateHandling = LOCATION_BUMP; } else { qDebug() << "Invalid template mode: " << mode; - QMessageBox::critical(NULL, "Error", "Invalid template mode: " + mode); + QMessageBox::critical(nullptr, "Error", "Invalid template mode: " + mode); return 1; } } @@ -339,7 +339,7 @@ int main(int argc, char *argv[]) { // center dialog on primary screen QPoint center = desktopRect.center(); - w.move(center.x() - w.width() * 0.5, center.y() - w.height() * 0.5); + w.move(center.x() - w.width() / 2, center.y() - w.height() / 2); a.setActiveWindow(&w); return a.exec(); diff --git a/src/session.h b/src/session.h index ec7ae7a..2b50a56 100644 --- a/src/session.h +++ b/src/session.h @@ -7,7 +7,7 @@ class QString; class QIcon; enum SectionType { - SECTION_NULL, + SECTION_nullptr, SECTION_XSESSION, SECTION_FOR_LOCATION, SECTION_TEMPLATES, diff --git a/src/sessionsiconholder.cpp b/src/sessionsiconholder.cpp index 5a1824e..5180658 100644 --- a/src/sessionsiconholder.cpp +++ b/src/sessionsiconholder.cpp @@ -19,7 +19,7 @@ #include "sessiontreemodel.h" #include "filedownloader.h" -SessionsIconHolder* SessionsIconHolder::instance = NULL; +SessionsIconHolder* SessionsIconHolder::instance = nullptr; static inline QString url2filename(const QString& url) { return TEMP_PATH_ICONS + QString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex()); diff --git a/src/sessionsiconholder.h b/src/sessionsiconholder.h index 8935606..06ca73c 100644 --- a/src/sessionsiconholder.h +++ b/src/sessionsiconholder.h @@ -35,7 +35,7 @@ signals: public: QIcon getIcon(const QString& name); QIcon getIcon(const QUrl& url); - static SessionsIconHolder* get() { if (instance == NULL) instance = new SessionsIconHolder(); return instance; } + static SessionsIconHolder* get() { if (instance == nullptr) instance = new SessionsIconHolder(); return instance; } public slots: void afterDownload(const QUrl& url, const QByteArray& downloadedData); diff --git a/src/sessiontreeitem.cpp b/src/sessiontreeitem.cpp index 5f3c28e..c95b5cf 100644 --- a/src/sessiontreeitem.cpp +++ b/src/sessiontreeitem.cpp @@ -11,7 +11,7 @@ QString SessionTreeItem::typeToName(const SectionType type) { return QCoreApplication::instance()->translate("Dialog", "Working environments"); case SECTION_XSESSION: return QCoreApplication::instance()->translate("Dialog", "X-Sessions"); - case SECTION_NULL: + case SECTION_nullptr: return ""; default: break; @@ -24,11 +24,11 @@ SessionTreeItem::SessionTreeItem(const Session* session, SessionTreeItem *parent } SessionTreeItem::SessionTreeItem(const SectionType type, SessionTreeItem *parent) - : parent_(parent), session_(NULL), sectionType_(type), text_(typeToName(type)) { + : parent_(parent), session_(nullptr), sectionType_(type), text_(typeToName(type)) { } SessionTreeItem::SessionTreeItem(const QString& text, SessionTreeItem *parent) - : parent_(parent), session_(NULL), sectionType_(SECTION_NULL), text_(text) { + : parent_(parent), session_(nullptr), sectionType_(SECTION_nullptr), text_(text) { } SessionTreeItem::~SessionTreeItem() { diff --git a/src/sessiontreeitem.h b/src/sessiontreeitem.h index 6202b62..7c1a58f 100644 --- a/src/sessiontreeitem.h +++ b/src/sessiontreeitem.h @@ -10,9 +10,9 @@ class SessionTreeItem { public: - SessionTreeItem(const Session* session, SessionTreeItem *parent = 0); - SessionTreeItem(const SectionType type, SessionTreeItem *parent = 0); - SessionTreeItem(const QString& text, SessionTreeItem *parent = 0); + SessionTreeItem(const Session* session, SessionTreeItem *parent = nullptr); + SessionTreeItem(const SectionType type, SessionTreeItem *parent = nullptr); + SessionTreeItem(const QString& text, SessionTreeItem *parent = nullptr); ~SessionTreeItem(); void appendChild(SessionTreeItem *child); diff --git a/src/sessiontreemodel.cpp b/src/sessiontreemodel.cpp index ac60ee9..bb96f0e 100644 --- a/src/sessiontreemodel.cpp +++ b/src/sessiontreemodel.cpp @@ -13,7 +13,7 @@ SessionTreeModel::SessionTreeModel(QObject *parent) : QAbstractItemModel(parent) { - root_ = new SessionTreeItem(SECTION_NULL); + root_ = new SessionTreeItem(SECTION_nullptr); } SessionTreeModel::~SessionTreeModel() { @@ -70,7 +70,7 @@ QVariant SessionTreeModel::data(const QModelIndex &index, int role) const { Qt::ItemFlags SessionTreeModel::flags(const QModelIndex &index) const { if (!index.isValid()) { - return 0; + return Qt::NoItemFlags; } SessionTreeItem* item = @@ -128,22 +128,19 @@ QModelIndex SessionTreeModel::parent(const QModelIndex &index) const { } void SessionTreeModel::addItems(const QList& sessions) { - SessionTreeItem* parentItem; - foreach (Session* s, sessions) { - bool sectionExists = false; + SessionTreeItem* parentItem = nullptr; SectionType section = s->section(); for (int i = 0; i < root_->childCount(); ++i) { SessionTreeItem* item = root_->child(i); if (item->sectionType() == section) { parentItem = item; - sectionExists = true; break; } } - if (!sectionExists) { + if (parentItem == nullptr) { parentItem = new SessionTreeItem(section, root_); root_->appendChild(parentItem); } @@ -168,7 +165,7 @@ void SessionTreeModel::removeItem(const QString& name) { static void lookDeeper(QList& result, QList& items, SessionTreeItem* root) { for (int i = 0; i < root->childCount(); ++i) { SessionTreeItem* item = root->child(i); - if (item->session() != NULL && item->session()->containsKeywords(items)) { + if (item->session() != nullptr && item->session()->containsKeywords(items)) { result.append(const_cast(item->session())); } lookDeeper(result, items, item); @@ -198,7 +195,7 @@ QList SessionTreeModel::lookForItem(const QString& label) { QModelIndex SessionTreeModel::getSection(const SectionType type) { for (int i = 0; i < root_->childCount(); ++i) { SessionTreeItem* item = root_->child(i); - if (item->sectionType() == type && item->session() == NULL) { + if (item->sectionType() == type && item->session() == nullptr) { return createIndex(i, 0, item); } } diff --git a/src/sessiontreemodel.h b/src/sessiontreemodel.h index 6a55eed..febd0aa 100644 --- a/src/sessiontreemodel.h +++ b/src/sessiontreemodel.h @@ -13,7 +13,7 @@ class SessionTreeModel : public QAbstractItemModel { Q_OBJECT public: - explicit SessionTreeModel(QObject *parent = 0); + explicit SessionTreeModel(QObject *parent = nullptr); ~SessionTreeModel(); QVariant data(const QModelIndex &index, int role) const; diff --git a/src/vsession.cpp b/src/vsession.cpp index fc58927..e35c1af 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -261,7 +261,7 @@ bool VSession::run() const { } if (g_noVtx && is64Bit()) { - QMessageBox::warning(NULL, QObject::trUtf8("Warning"), + QMessageBox::warning(nullptr, QObject::trUtf8("Warning"), QObject::trUtf8("The selected session is based on a 64 bit operating system," " but this computer doesn't seem to support this (VT-x/AMD-V not" " supported by CPU, or disabled in BIOS). You will probably get an" diff --git a/src/vsession.h b/src/vsession.h index bd0f7a2..08885cc 100644 --- a/src/vsession.h +++ b/src/vsession.h @@ -7,6 +7,7 @@ #include #include "session.h" #include "httpxmldownloader.h" +#include "globals.h" enum ImgType { VMWARE, -- cgit v1.2.3-55-g7522