Config file handling changes to use the right priority and behave like openmw

pull/21/head
Pieter van der Kloet 14 years ago
parent b60bb408fd
commit 889e7d6c2e

@ -50,7 +50,7 @@ MainDialog::MainDialog()
connect(buttonBox, SIGNAL(rejected()), this, SLOT(close())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(play())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(play()));
setupConfig(); //setupConfig();
createIcons(); createIcons();
createPages(); 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 // We can't use QSettings directly because it
// does not support multiple keys with the same name // does not support multiple keys with the same name
QFile file(mGameConfig->fileName()); QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setWindowTitle("Error opening OpenMW configuration file"); msgBox.setWindowTitle("Error opening OpenMW configuration file");
msgBox.setIcon(QMessageBox::Critical); msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(QMessageBox::Ok); msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText(tr("<br><b>Could not open %0</b><br><br> \ msgBox.setText(tr("<br><b>Could not open %0</b><br><br> \
Please make sure you have the right permissions and try again.<br>").arg(file.fileName())); Please make sure you have the right permissions and try again.<br>").arg(file.fileName()));
msgBox.exec(); msgBox.exec();
QApplication::exit(); // File cannot be opened or created QApplication::exit(); // File cannot be opened or created
} }
QTextStream in(&file); QTextStream in(&file);
QStringList dataDirs; QStringList dataDirs;
QString dataLocal;
// Add each data= value
// Read the config line by line
while (!in.atEnd()) { while (!in.atEnd()) {
QString line = in.readLine(); QString line = in.readLine();
if (line.startsWith("data=")) { if (line.startsWith("data=")) {
dataDirs.append(line.remove("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 // Add the data-local= key to the end of the dataDirs for priority reasons
QString dataLocal = mGameConfig->value("data-local").toString();
if (!dataLocal.isEmpty()) { if (!dataLocal.isEmpty()) {
dataDirs.append(dataLocal); 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()) { if (!dataDirs.isEmpty()) {
// Now pass the datadirs on to the DataFilesPage // Now pass the datadirs on to the DataFilesPage
mDataFilesPage->setupDataFiles(dataDirs, mGameConfig->value("fs-strict").toBool()); mDataFilesPage->setupDataFiles(dataDirs, mStrict);
} else { } else {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setWindowTitle("Error reading OpenMW configuration file"); 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("<br><b>Could not open %0</b><br><br> \
Please make sure you have the right permissions and try again.<br>").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() void MainDialog::writeConfig()
{ {
// Write the profiles // Write the profiles

@ -6,8 +6,9 @@
class QListWidget; class QListWidget;
class QListWidgetItem; class QListWidgetItem;
class QStackedWidget; class QStackedWidget;
class QStringList;
class QStringListModel; class QStringListModel;
class QSettings; class QString;
class PlayPage; class PlayPage;
class GraphicsPage; class GraphicsPage;
@ -32,7 +33,9 @@ private:
void setupConfig(); void setupConfig();
void writeConfig(); void writeConfig();
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
QStringList readConfig(const QString &fileName);
QListWidget *mIconWidget; QListWidget *mIconWidget;
QStackedWidget *mPagesWidget; QStackedWidget *mPagesWidget;
@ -40,7 +43,8 @@ private:
GraphicsPage *mGraphicsPage; GraphicsPage *mGraphicsPage;
DataFilesPage *mDataFilesPage; DataFilesPage *mDataFilesPage;
QSettings *mGameConfig; QStringList mDataDirs;
bool mStrict;
}; };
#endif #endif

Loading…
Cancel
Save