From f0f895ad1022b9ba7b5c27306f8c683d92f4c397 Mon Sep 17 00:00:00 2001 From: graffy76 Date: Tue, 25 Jun 2013 19:32:36 -0500 Subject: [PATCH] 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. --- apps/opencs/model/settings/usersettings.cpp | 108 ++++++++++++-------- apps/opencs/model/settings/usersettings.hpp | 6 +- 2 files changed, 69 insertions(+), 45 deletions(-) diff --git a/apps/opencs/model/settings/usersettings.cpp b/apps/opencs/model/settings/usersettings.cpp index c17f28d4e..508942f58 100644 --- a/apps/opencs/model/settings/usersettings.cpp +++ b/apps/opencs/model/settings/usersettings.cpp @@ -61,57 +61,49 @@ QTextStream *CSMSettings::UserSettings::openFileStream (const QString &filePath, else 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("
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; - if (file) + if (file->open(openFlags)) { stream = new QTextStream(file); stream->setCodec(QTextCodec::codecForName("UTF-8")); } + if (!stream) + { + delete file; + file = 0; + } + return stream; } bool CSMSettings::UserSettings::writeSettings(QMap &settings) { - QTextStream *stream = openFileStream(mPaths.back()); + QTextStream *stream = openFileStream(mUserFilePath); - QList keyList = settings.keys(); + if (!stream) + displayFileErrorMessage(mReadWriteMessage, false); - foreach (QString key, keyList) + if (stream) { - SettingList *sectionSettings = settings[key]; + QList keyList = settings.keys(); - *stream << "[" << key << "]" << '\n'; + foreach (QString key, keyList) + { + SettingList *sectionSettings = settings[key]; - foreach (SettingContainer *item, *sectionSettings) - *stream << item->objectName() << " = " << item->getValue() << '\n'; - } + *stream << "[" << key << "]" << '\n'; - stream->device()->close(); + foreach (SettingContainer *item, *sectionSettings) + *stream << item->objectName() << " = " << item->getValue() << '\n'; + } - return true; + stream->device()->close(); + } + + return (stream); } @@ -120,10 +112,10 @@ const CSMSettings::SectionMap &CSMSettings::UserSettings::getSettings() const return mSectionSettings; } -void CSMSettings::UserSettings::loadFromFile(const QString &filePath) +bool CSMSettings::UserSettings::loadFromFile(const QString &filePath) { if (filePath.isEmpty()) - return; + return false; mSectionSettings.clear(); @@ -181,22 +173,37 @@ void CSMSettings::UserSettings::loadFromFile(const QString &filePath) stream->device()->close(); } - return; + return (stream); } void CSMSettings::UserSettings::loadSettings (const QString &fileName) { - if (mPaths.count() == 0) - { - mPaths.append(QString::fromStdString(mCfgMgr.getGlobalPath().string()) + fileName); - mPaths.append(QString::fromStdString(mCfgMgr.getLocalPath().string()) + fileName); - mPaths.append(QString::fromStdString(mCfgMgr.getUserPath().string()) + fileName); - } + bool globalOk; + bool localOk; - 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); - loadFromFile(path); + QString message = QObject::tr("
Could not open user settings files for reading

\ + Global and local settings files could not be read.\ + You may have incorrect file permissions or the OpenCS installation may be corrupted.
"); + + message += QObject::tr("
Global filepath: ") + globalFilePath; + message += QObject::tr("
Local filepath: ") + localFilePath; + + displayFileErrorMessage ( message, true); } } @@ -245,3 +252,18 @@ CSMSettings::UserSettings& CSMSettings::UserSettings::instance() 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(); +} diff --git a/apps/opencs/model/settings/usersettings.hpp b/apps/opencs/model/settings/usersettings.hpp index da8408b3e..49b226df9 100644 --- a/apps/opencs/model/settings/usersettings.hpp +++ b/apps/opencs/model/settings/usersettings.hpp @@ -28,7 +28,7 @@ namespace CSMSettings { SectionMap mSectionSettings; static UserSettings *mUserSettingsInstance; - QStringList mPaths; + QString mUserFilePath; Files::ConfigurationManager mCfgMgr; QString mReadOnlyMessage; QString mReadWriteMessage; @@ -70,7 +70,9 @@ namespace CSMSettings { QTextStream *openFileStream (const QString &filePath, bool isReadOnly = false) const; /// 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: