From 73def1cfcc13db082ece95913147d88cc4b1e6fe Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 14 Nov 2013 20:54:55 +0100 Subject: [PATCH] Portable settings, now including local database. --- src/core/databasefactory.cpp | 10 ++++++++-- src/core/settings.cpp | 37 ++++++++++++++---------------------- src/core/settings.h | 30 +++++++++++++++++++---------- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/core/databasefactory.cpp b/src/core/databasefactory.cpp index eae54d9ef..afd5bc3d8 100644 --- a/src/core/databasefactory.cpp +++ b/src/core/databasefactory.cpp @@ -6,6 +6,7 @@ #include "core/defs.h" #include "core/databasefactory.h" +#include "core/settings.h" QPointer DatabaseFactory::s_instance; @@ -27,8 +28,13 @@ DatabaseFactory *DatabaseFactory::getInstance() { } void DatabaseFactory::assemblyDatabaseFilePath() { - m_databasePath = QDir::homePath() + QDir::separator() + APP_LOW_H_NAME + - QDir::separator() + APP_DB_PATH; + if (Settings::getInstance()->type() == Settings::Portable) { + m_databasePath = qApp->applicationDirPath() + QDir::separator() + APP_DB_PATH; + } + else { + m_databasePath = QDir::homePath() + QDir::separator() + APP_LOW_H_NAME + + QDir::separator() + APP_DB_PATH; + } } QString DatabaseFactory::getDatabasePath() { diff --git a/src/core/settings.cpp b/src/core/settings.cpp index e425394e6..b90e2c1ea 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -1,22 +1,3 @@ -/* - This file is part of Qonverter. - - Qonverter is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Qonverter is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Qonverter. If not, see . - - Copyright 2012 - 2013 Martin Rotter -*/ - #include #include #include @@ -29,8 +10,9 @@ QPointer Settings::s_instance; -Settings::Settings(const QString &file_name, Format format, QObject *parent) - : QSettings(file_name, format, parent) { +Settings::Settings(const QString &file_name, Format format, + const Type &status, QObject *parent) + : QSettings(file_name, format, parent), m_initializationStatus(status) { } Settings::~Settings() { @@ -38,6 +20,11 @@ Settings::~Settings() { qDebug("Deleting Settings instance."); } +Settings::Type Settings::type() const { + return m_initializationStatus; +} + + QSettings::Status Settings::checkSettings() { qDebug("Syncing settings."); sync(); @@ -77,8 +64,10 @@ QSettings::Status Settings::setupSettings() { // Check if portable settings are available. if (QFile(app_path_file).exists()) { // Portable settings are available, use them. - s_instance = new Settings(app_path_file, QSettings::IniFormat, qApp); + s_instance = new Settings(app_path_file, QSettings::IniFormat, + Settings::Portable, qApp); + // TODO: Separate web settings into another unit. // Construct icon cache in the same path. QString web_path = app_path + QDir::separator() + APP_CFG_WEB_PATH; QDir(web_path).mkpath(web_path); @@ -94,8 +83,10 @@ QSettings::Status Settings::setupSettings() { APP_LOW_H_NAME; QString home_path_file = home_path + relative_path; - s_instance = new Settings(home_path_file, QSettings::IniFormat, qApp); + s_instance = new Settings(home_path_file, QSettings::IniFormat, + Settings::NonPortable, qApp); + // Construct icon cache in the same path. QString web_path = home_path + QDir::separator() + APP_CFG_WEB_PATH; QDir(web_path).mkpath(web_path); QWebSettings::setIconDatabasePath(web_path); diff --git a/src/core/settings.h b/src/core/settings.h index 7bffc2702..779d380d6 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -8,23 +8,20 @@ class Settings : public QSettings { Q_OBJECT - private: - // Constructor. - Settings(const QString & file_name, Format format, QObject * parent = 0); - - // Creates settings file in correct location. - static QSettings::Status setupSettings(); - - // Private singleton value. - static QPointer s_instance; - public: + enum Type { + Portable, + NonPortable + }; + // Singleton getter. static Settings *getInstance(); // Destructor. virtual ~Settings(); + Type type() const; + // Getter/setter for settings values. QVariant value(const QString §ion, const QString &key, @@ -36,6 +33,19 @@ class Settings : public QSettings { // Synchronises settings. QSettings::Status checkSettings(); + + private: + // Constructor. + Settings(const QString & file_name, Format format, + const Type &type, QObject * parent = 0); + + Type m_initializationStatus; + + // Creates settings file in correct location. + static QSettings::Status setupSettings(); + + // Private singleton value. + static QPointer s_instance; }; #endif // SETTINGS_H