From 889e7d6c2eec10ee2949d85ae8e2907187cd832d Mon Sep 17 00:00:00 2001 From: Pieter van der Kloet Date: Tue, 6 Sep 2011 03:50:40 +0200 Subject: [PATCH] Config file handling changes to use the right priority and behave like openmw --- apps/launcher/maindialog.cpp | 129 ++++++++++++++++++++--------------- apps/launcher/maindialog.hpp | 10 ++- 2 files changed, 81 insertions(+), 58 deletions(-) diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index 81acb8bbf..94e90bddb 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -50,7 +50,7 @@ MainDialog::MainDialog() connect(buttonBox, SIGNAL(rejected()), this, SLOT(close())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(play())); - setupConfig(); + //setupConfig(); createIcons(); createPages(); } @@ -88,52 +88,105 @@ void MainDialog::createIcons() } -void MainDialog::createPages() +QStringList MainDialog::readConfig(const QString &fileName) { - // Various pages - mPlayPage = new PlayPage(this); - mGraphicsPage = new GraphicsPage(this); - mDataFilesPage = new DataFilesPage(this); - - // First we retrieve all data= keys from the config // We can't use QSettings directly because it // does not support multiple keys with the same name - QFile file(mGameConfig->fileName()); - + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox msgBox; msgBox.setWindowTitle("Error opening OpenMW configuration file"); msgBox.setIcon(QMessageBox::Critical); msgBox.setStandardButtons(QMessageBox::Ok); msgBox.setText(tr("
Could not open %0

\ - Please make sure you have the right permissions and try again.
").arg(file.fileName())); + Please make sure you have the right permissions and try again.
").arg(file.fileName())); msgBox.exec(); - + QApplication::exit(); // File cannot be opened or created } - + QTextStream in(&file); - QStringList dataDirs; - - // Add each data= value + QString dataLocal; + + // Read the config line by line while (!in.atEnd()) { QString line = in.readLine(); - + if (line.startsWith("data=")) { dataDirs.append(line.remove("data=")); } + + // Read the data-local key, if more than one are found only the last is used + if (line.startsWith("data-local=")) { + dataLocal = line.remove("data-local="); + } + + // Read fs-strict key + if (line.startsWith("fs-strict=")) { + QString value = line.remove("fs-strict="); + + (value.toLower() == QLatin1String("true")) + ? mStrict = true + : mStrict = false; + + } + } - - // Add the data-local= key - QString dataLocal = mGameConfig->value("data-local").toString(); + + // Add the data-local= key to the end of the dataDirs for priority reasons if (!dataLocal.isEmpty()) { dataDirs.append(dataLocal); } + + if (!dataDirs.isEmpty()) + { + // Reset the global datadirs to the newly read entries + // Else return the previous dataDirs because nothing was found in this file; + mDataDirs = dataDirs; + } + + file.close(); + + return mDataDirs; +} + +void MainDialog::createPages() +{ + mPlayPage = new PlayPage(this); + mGraphicsPage = new GraphicsPage(this); + mDataFilesPage = new DataFilesPage(this); + + // Retrieve all data entries from the configs + QStringList dataDirs; + + // Global location + QFile file(QString::fromStdString(Files::getPath(Files::Path_ConfigGlobal, + "openmw", "openmw.cfg"))); + if (file.exists()) { + dataDirs = readConfig(file.fileName()); + } + + // User location + file.setFileName(QString::fromStdString(Files::getPath(Files::Path_ConfigUser, + "openmw", "openmw.cfg"))); + if (file.exists()) { + dataDirs = readConfig(file.fileName()); + } + + // Local location + file.setFileName("./openmw.cfg"); + + if (file.exists()) { + dataDirs = readConfig(file.fileName()); + } + + file.close(); if (!dataDirs.isEmpty()) { // Now pass the datadirs on to the DataFilesPage - mDataFilesPage->setupDataFiles(dataDirs, mGameConfig->value("fs-strict").toBool()); + mDataFilesPage->setupDataFiles(dataDirs, mStrict); } else { QMessageBox msgBox; msgBox.setWindowTitle("Error reading OpenMW configuration file"); @@ -266,40 +319,6 @@ void MainDialog::play() } } -void MainDialog::setupConfig() -{ - // First we read the OpenMW config - QString config = "./openmw.cfg"; - QFile file(config); - - if (!file.exists()) { - file.setFileName(QString::fromStdString(Files::getPath(Files::Path_ConfigUser, - "openmw", "openmw.cfg"))); - } - - if (!file.exists()) { - file.setFileName(QString::fromStdString(Files::getPath(Files::Path_ConfigGlobal, - "openmw", "openmw.cfg"))); - } - - if (!file.exists()) { - QMessageBox msgBox; - msgBox.setWindowTitle("Error opening OpenMW configuration file"); - msgBox.setIcon(QMessageBox::Critical); - msgBox.setStandardButtons(QMessageBox::Ok); - msgBox.setText(tr("
Could not open %0

\ - Please make sure you have the right permissions and try again.
").arg(file.fileName())); - msgBox.exec(); - - file.close(); - QApplication::exit(); // No config file available - } - - // Open our config file - mGameConfig = new QSettings(file.fileName(), QSettings::IniFormat); - file.close(); -} - void MainDialog::writeConfig() { // Write the profiles diff --git a/apps/launcher/maindialog.hpp b/apps/launcher/maindialog.hpp index c6f22e336..3ed72e0fd 100644 --- a/apps/launcher/maindialog.hpp +++ b/apps/launcher/maindialog.hpp @@ -6,8 +6,9 @@ class QListWidget; class QListWidgetItem; class QStackedWidget; +class QStringList; class QStringListModel; -class QSettings; +class QString; class PlayPage; class GraphicsPage; @@ -32,7 +33,9 @@ private: void setupConfig(); void writeConfig(); void closeEvent(QCloseEvent *event); - + + QStringList readConfig(const QString &fileName); + QListWidget *mIconWidget; QStackedWidget *mPagesWidget; @@ -40,7 +43,8 @@ private: GraphicsPage *mGraphicsPage; DataFilesPage *mDataFilesPage; - QSettings *mGameConfig; + QStringList mDataDirs; + bool mStrict; }; #endif