From 8a231f0b0b70e66e6c348b049560c7db9b2e103d Mon Sep 17 00:00:00 2001 From: Jonathan Young Date: Fri, 9 May 2014 12:21:42 +1000 Subject: [PATCH] Save Quake 3 path to settings on Windows. QSettings is wrapped to keep error-prone stringly typed code in one place (QSettings uses string keys). --- launch.pro | 6 ++++-- mainwindow.cpp | 27 ++++++++++++++++++++++++--- mainwindow.h | 5 ++--- settings.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ settings.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 settings.cpp create mode 100644 settings.h diff --git a/launch.pro b/launch.pro index 502ce84..59f0d32 100644 --- a/launch.pro +++ b/launch.pro @@ -13,9 +13,11 @@ TEMPLATE = app SOURCES += main.cpp\ - mainwindow.cpp + mainwindow.cpp \ + settings.cpp -HEADERS += mainwindow.h +HEADERS += mainwindow.h \ + settings.h FORMS += mainwindow.ui diff --git a/mainwindow.cpp b/mainwindow.cpp index 17ccca3..e5f4c8c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,14 +20,35 @@ ioLaunch::~ioLaunch() void ioLaunch::on_btnLaunch_clicked() { + QString ioq3; + #ifdef Q_OS_WIN32 - if(ioq3 == NULL) + // Prompt the user to set the ioq3 path if the settings value either doesn't exist or is invalid. + bool promptForPath = true; + + if (settings.containsQuakePath()) + { + const QString path = settings.getQuakePath(); + const QDir dir(path); + + if (!path.isEmpty() && dir.exists()) + promptForPath = false; + } + + if(promptForPath) { msg.setText("Please select your Quake3 directory"); msg.exec(); - QString path = QFileDialog::getExistingDirectory (this, tr("Directory"), directory.path()); - ioq3 = QString("\"") + path + QDir::separator() + "ioquake3.x86.exe\" +set r_mode -1"; + + const QString path = QFileDialog::getExistingDirectory (this, tr("Directory")); + + if (path.isEmpty()) + return; + + settings.setQuakePath(path); } + + ioq3 = QString("\"") + settings.getQuakePath() + QDir::separator() + "ioquake3.x86.exe\" +set r_mode -1"; #elif defined Q_OS_MAC ioq3 = "open -a ioquake3 --args +set r_mode -1"; #elif defined Q_OS_UNIX diff --git a/mainwindow.h b/mainwindow.h index 5d1bc8a..ad924d6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -8,6 +8,7 @@ #include #include #include +#include "settings.h" namespace Ui { class ioLaunch; @@ -41,17 +42,15 @@ private slots: private: Ui::ioLaunch *ui; - QString ioq3; QString resOption; QString screenOption; QMessageBox msg; QMessageBox ioq3Failed; - QDir directory; int ioWidth; int ioHeight; bool ioWedited; bool ioHedited; - + Settings settings; }; #endif // MAINWINDOW_H diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..c91f20f --- /dev/null +++ b/settings.cpp @@ -0,0 +1,44 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013 The ioquake Group + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +#include +#include "settings.h" + +Settings::Settings() : + settings("ioquake", "launch") +{ +} + +QString Settings::getQuakePath() const +{ + return settings.value("quakePath").toString(); +} + +bool Settings::containsQuakePath() const +{ + return settings.contains("quakePath"); +} + +void Settings::setQuakePath(const QString &path) +{ + settings.setValue("quakePath", path); +} diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..7a6099c --- /dev/null +++ b/settings.h @@ -0,0 +1,43 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013 The ioquake Group + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include + +class QString; + +class Settings +{ +public: + Settings(); + + QString getQuakePath() const; + bool containsQuakePath() const; + void setQuakePath(const QString &path); + +private: + QSettings settings; +}; + +#endif // SETTINGS_H