1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-21 10:23:52 +00:00

Fixed settings file error message

Errors only occur if both global and local settings files are not found.
All other file read errors fail silently.
This commit is contained in:
graffy76 2013-06-25 19:32:36 -05:00
parent 8f4fd8202e
commit f0f895ad10
2 changed files with 69 additions and 45 deletions

View file

@ -61,57 +61,49 @@ QTextStream *CSMSettings::UserSettings::openFileStream (const QString &filePath,
else else
openFlags = QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate; openFlags = QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate;
if (!(file->open(openFlags)))
{
// File cannot be opened or created
QMessageBox msgBox;
msgBox.setWindowTitle(QObject::tr("OpenCS configuration file I/O error"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(QMessageBox::Ok);
QString fileMessage = QObject::tr("<br> File: %0").arg(file->fileName());
if (!isReadOnly)
msgBox.setText (mReadWriteMessage + fileMessage);
else
msgBox.setText (mReadOnlyMessage + fileMessage);
msgBox.exec();
delete file;
file = 0;
}
QTextStream *stream = 0; QTextStream *stream = 0;
if (file) if (file->open(openFlags))
{ {
stream = new QTextStream(file); stream = new QTextStream(file);
stream->setCodec(QTextCodec::codecForName("UTF-8")); stream->setCodec(QTextCodec::codecForName("UTF-8"));
} }
if (!stream)
{
delete file;
file = 0;
}
return stream; return stream;
} }
bool CSMSettings::UserSettings::writeSettings(QMap<QString, CSMSettings::SettingList *> &settings) bool CSMSettings::UserSettings::writeSettings(QMap<QString, CSMSettings::SettingList *> &settings)
{ {
QTextStream *stream = openFileStream(mPaths.back()); QTextStream *stream = openFileStream(mUserFilePath);
QList<QString> keyList = settings.keys(); if (!stream)
displayFileErrorMessage(mReadWriteMessage, false);
foreach (QString key, keyList) if (stream)
{ {
SettingList *sectionSettings = settings[key]; QList<QString> keyList = settings.keys();
*stream << "[" << key << "]" << '\n'; foreach (QString key, keyList)
{
SettingList *sectionSettings = settings[key];
foreach (SettingContainer *item, *sectionSettings) *stream << "[" << key << "]" << '\n';
*stream << item->objectName() << " = " << item->getValue() << '\n';
foreach (SettingContainer *item, *sectionSettings)
*stream << item->objectName() << " = " << item->getValue() << '\n';
}
stream->device()->close();
} }
stream->device()->close(); return (stream);
return true;
} }
@ -120,10 +112,10 @@ const CSMSettings::SectionMap &CSMSettings::UserSettings::getSettings() const
return mSectionSettings; return mSectionSettings;
} }
void CSMSettings::UserSettings::loadFromFile(const QString &filePath) bool CSMSettings::UserSettings::loadFromFile(const QString &filePath)
{ {
if (filePath.isEmpty()) if (filePath.isEmpty())
return; return false;
mSectionSettings.clear(); mSectionSettings.clear();
@ -181,22 +173,37 @@ void CSMSettings::UserSettings::loadFromFile(const QString &filePath)
stream->device()->close(); stream->device()->close();
} }
return; return (stream);
} }
void CSMSettings::UserSettings::loadSettings (const QString &fileName) void CSMSettings::UserSettings::loadSettings (const QString &fileName)
{ {
if (mPaths.count() == 0) bool globalOk;
{ bool localOk;
mPaths.append(QString::fromStdString(mCfgMgr.getGlobalPath().string()) + fileName);
mPaths.append(QString::fromStdString(mCfgMgr.getLocalPath().string()) + fileName);
mPaths.append(QString::fromStdString(mCfgMgr.getUserPath().string()) + fileName);
}
foreach (const QString &path, mPaths) //global
QString globalFilePath = QString::fromStdString(mCfgMgr.getGlobalPath().string()) + fileName;
globalOk = loadFromFile(globalFilePath);
//local
QString localFilePath = QString::fromStdString(mCfgMgr.getLocalPath().string()) + fileName;
localOk = loadFromFile(localFilePath);
//user
mUserFilePath = QString::fromStdString(mCfgMgr.getUserPath().string()) + fileName;
loadFromFile(mUserFilePath);
if (!(localOk || globalOk))
{ {
qDebug() << "Loading config file:" << qPrintable(path); QString message = QObject::tr("<br><b>Could not open user settings files for reading</b><br><br> \
loadFromFile(path); Global and local settings files could not be read.\
You may have incorrect file permissions or the OpenCS installation may be corrupted.<br>");
message += QObject::tr("<br>Global filepath: ") + globalFilePath;
message += QObject::tr("<br>Local filepath: ") + localFilePath;
displayFileErrorMessage ( message, true);
} }
} }
@ -245,3 +252,18 @@ CSMSettings::UserSettings& CSMSettings::UserSettings::instance()
return *mUserSettingsInstance; return *mUserSettingsInstance;
} }
void CSMSettings::UserSettings::displayFileErrorMessage(const QString &message, bool isReadOnly)
{
// File cannot be opened or created
QMessageBox msgBox;
msgBox.setWindowTitle(QObject::tr("OpenCS configuration file I/O error"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(QMessageBox::Ok);
if (!isReadOnly)
msgBox.setText (mReadWriteMessage + message);
else
msgBox.setText (message);
msgBox.exec();
}

View file

@ -28,7 +28,7 @@ namespace CSMSettings {
SectionMap mSectionSettings; SectionMap mSectionSettings;
static UserSettings *mUserSettingsInstance; static UserSettings *mUserSettingsInstance;
QStringList mPaths; QString mUserFilePath;
Files::ConfigurationManager mCfgMgr; Files::ConfigurationManager mCfgMgr;
QString mReadOnlyMessage; QString mReadOnlyMessage;
QString mReadWriteMessage; QString mReadWriteMessage;
@ -70,7 +70,9 @@ namespace CSMSettings {
QTextStream *openFileStream (const QString &filePath, bool isReadOnly = false) const; QTextStream *openFileStream (const QString &filePath, bool isReadOnly = false) const;
/// Parses a setting file specified in filePath from the provided text stream. /// Parses a setting file specified in filePath from the provided text stream.
void loadFromFile (const QString &filePath = ""); bool loadFromFile (const QString &filePath = "");
void displayFileErrorMessage(const QString &message, bool isReadOnly);
signals: signals: