Some tweaking of settings infrastructure.

This commit is contained in:
Martin Rotter 2015-03-22 08:37:30 +01:00
parent 23c1e118b0
commit c11a104959
6 changed files with 78 additions and 44 deletions

View File

@ -71,8 +71,8 @@ project(rssguard)
set(APP_NAME "RSS Guard") set(APP_NAME "RSS Guard")
set(APP_LOW_NAME "rssguard") set(APP_LOW_NAME "rssguard")
set(APP_VERSION "2.2.0") set(APP_VERSION "2.3.0")
set(FILE_VERSION "2,2,0,0") set(FILE_VERSION "2,3,0,0")
set(APP_AUTHOR "Martin Rotter") set(APP_AUTHOR "Martin Rotter")
set(APP_URL "http://bitbucket.org/skunkos/rssguard") set(APP_URL "http://bitbucket.org/skunkos/rssguard")
set(APP_URL_ISSUES "http://bitbucket.org/skunkos/rssguard/issues") set(APP_URL_ISSUES "http://bitbucket.org/skunkos/rssguard/issues")

View File

@ -104,7 +104,7 @@ FormAbout::FormAbout(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormAbout)
APP_NAME)); APP_NAME));
// Load additional paths information. // Load additional paths information.
if (qApp->settings()->type() == Settings::Portable) { if (qApp->settings()->type() == SettingsType::Portable) {
m_ui->m_txtPathsSettingsType->setText(tr("FULLY portable")); m_ui->m_txtPathsSettingsType->setText(tr("FULLY portable"));
m_ui->m_txtPathsDatabaseRoot->setText(QDir::toNativeSeparators(qApp->applicationDirPath() + QDir::separator() + QString(APP_DB_SQLITE_PATH))); m_ui->m_txtPathsDatabaseRoot->setText(QDir::toNativeSeparators(qApp->applicationDirPath() + QDir::separator() + QString(APP_DB_SQLITE_PATH)));
} }

View File

@ -115,7 +115,7 @@ void DatabaseFactory::finishRestoration() {
} }
void DatabaseFactory::sqliteAssemblyDatabaseFilePath() { void DatabaseFactory::sqliteAssemblyDatabaseFilePath() {
if (qApp->settings()->type() == Settings::Portable) { if (qApp->settings()->type() == SettingsType::Portable) {
m_sqliteDatabaseFilePath = qApp->applicationDirPath() + QDir::separator() + QString(APP_DB_SQLITE_PATH); m_sqliteDatabaseFilePath = qApp->applicationDirPath() + QDir::separator() + QString(APP_DB_SQLITE_PATH);
} }
else { else {

View File

@ -238,8 +238,7 @@ DVALUE(bool) Browser::QueueTabsDef = true;
// Categories. // Categories.
DKEY Categories::ID = "categories_expand_states"; DKEY Categories::ID = "categories_expand_states";
Settings::Settings(const QString &file_name, Format format, Settings::Settings(const QString &file_name, Format format, const SettingsType &status, QObject *parent)
const Type &status, QObject *parent)
: QSettings(file_name, format, parent), m_initializationStatus(status) { : QSettings(file_name, format, parent), m_initializationStatus(status) {
} }
@ -248,6 +247,10 @@ Settings::~Settings() {
qDebug("Deleting Settings instance."); qDebug("Deleting Settings instance.");
} }
QString Settings::pathName() const {
return QFileInfo(fileName()).absolutePath();
}
QSettings::Status Settings::checkSettings() { QSettings::Status Settings::checkSettings() {
qDebug("Syncing settings."); qDebug("Syncing settings.");
@ -281,14 +284,40 @@ void Settings::finishRestoration(const QString &desired_settings_file_path) {
Settings *Settings::setupSettings(QObject *parent) { Settings *Settings::setupSettings(QObject *parent) {
Settings *new_settings; Settings *new_settings;
// If settings file exists in executable file working directory // If settings file exists (and is writable) in executable file working directory
// (in subdirectory APP_CFG_PATH), then use it (portable settings). // (in subdirectory APP_CFG_PATH), then use it (portable settings).
// Otherwise use settings file stored in home path. // Otherwise use settings file stored in home path.
QString relative_path = QDir::separator() + QString(APP_CFG_PATH) + QDir::separator() + QString(APP_CFG_FILE); SettingsProperties properties = determineProperties();
finishRestoration(properties.m_absoluteSettingsFileName);
// Portable settings are available, use them.
new_settings = new Settings(properties.m_absoluteSettingsFileName, QSettings::IniFormat, properties.m_type, parent);
// Construct icon cache in the same path.
QString web_path = properties.m_baseDirectory + QDir::separator() + QString(APP_DB_WEB_PATH);
QDir(web_path).mkpath(web_path);
QWebSettings::setIconDatabasePath(web_path);
// Check if portable settings are available.
if (properties.m_type == SettingsType::Portable) {
qDebug("Initializing settings in '%s' (portable way).", qPrintable(QDir::toNativeSeparators(properties.m_absoluteSettingsFileName)));
}
else {
qDebug("Initializing settings in '%s' (non-portable way).", qPrintable(QDir::toNativeSeparators(properties.m_absoluteSettingsFileName)));
}
return new_settings;
}
SettingsProperties Settings::determineProperties() {
SettingsProperties properties;
properties.m_settingsSuffix = QDir::separator() + QString(APP_CFG_PATH) + QDir::separator() + QString(APP_CFG_FILE);
QString app_path = qApp->applicationDirPath(); QString app_path = qApp->applicationDirPath();
QString app_path_file = app_path + relative_path;
QString home_path = qApp->homeFolderPath() + QDir::separator() + QString(APP_LOW_H_NAME); QString home_path = qApp->homeFolderPath() + QDir::separator() + QString(APP_LOW_H_NAME);
QString home_path_file = home_path + relative_path; QString home_path_file = home_path + properties.m_settingsSuffix;
bool portable_settings_available = QFileInfo(app_path).isWritable(); bool portable_settings_available = QFileInfo(app_path).isWritable();
bool non_portable_settings_exist = QFile::exists(home_path_file); bool non_portable_settings_exist = QFile::exists(home_path_file);
@ -297,34 +326,16 @@ Settings *Settings::setupSettings(QObject *parent) {
// settings was not initialized before. // settings was not initialized before.
bool will_we_use_portable_settings = portable_settings_available && !non_portable_settings_exist; bool will_we_use_portable_settings = portable_settings_available && !non_portable_settings_exist;
// Check if portable settings are available.
if (will_we_use_portable_settings) { if (will_we_use_portable_settings) {
finishRestoration(app_path_file); properties.m_type = SettingsType::Portable;
properties.m_baseDirectory = app_path;
// Portable settings are available, use them.
new_settings = new Settings(app_path_file, QSettings::IniFormat, Settings::Portable, parent);
// Construct icon cache in the same path.
QString web_path = app_path + QDir::separator() + QString(APP_DB_WEB_PATH);
QDir(web_path).mkpath(web_path);
QWebSettings::setIconDatabasePath(web_path);
qDebug("Initializing settings in '%s' (portable way).", qPrintable(QDir::toNativeSeparators(app_path_file)));
} }
else { else {
finishRestoration(home_path_file); properties.m_type = SettingsType::NonPortable;
properties.m_baseDirectory = home_path;
// Portable settings are NOT available, store them in
// user's home directory.
new_settings = new Settings(home_path_file, QSettings::IniFormat, Settings::NonPortable, parent);
// Construct icon cache in the same path.
QString web_path = home_path + QDir::separator() + QString(APP_DB_WEB_PATH);
QDir(web_path).mkpath(web_path);
QWebSettings::setIconDatabasePath(web_path);
qDebug("Initializing settings in '%s' (non-portable way).", qPrintable(QDir::toNativeSeparators(home_path_file)));
} }
return new_settings; properties.m_absoluteSettingsFileName = properties.m_baseDirectory + properties.m_settingsSuffix;
return properties;
} }

View File

@ -22,6 +22,9 @@
#include "definitions/definitions.h" #include "definitions/definitions.h"
#include "miscellaneous/settingsproperties.h"
#include <QPointer> #include <QPointer>
#include <QNetworkProxy> #include <QNetworkProxy>
@ -267,17 +270,11 @@ class Settings : public QSettings {
Q_OBJECT Q_OBJECT
public: public:
// Describes possible types of loaded settings.
enum Type {
Portable,
NonPortable
};
// Destructor. // Destructor.
virtual ~Settings(); virtual ~Settings();
// Type of used settings. // Type of used settings.
inline Type type() const { inline SettingsType type() const {
return m_initializationStatus; return m_initializationStatus;
} }
@ -302,6 +299,9 @@ class Settings : public QSettings {
QSettings::remove(QString("%1/%2").arg(section, key)); QSettings::remove(QString("%1/%2").arg(section, key));
} }
// Returns the path which contains the settings.
QString pathName() const;
// Synchronizes settings. // Synchronizes settings.
QSettings::Status checkSettings(); QSettings::Status checkSettings();
@ -311,11 +311,13 @@ class Settings : public QSettings {
// Creates settings file in correct location. // Creates settings file in correct location.
static Settings *setupSettings(QObject *parent); static Settings *setupSettings(QObject *parent);
static SettingsProperties determineProperties();
private: private:
// Constructor. // Constructor.
explicit Settings(const QString &file_name, Format format, const Type &type, QObject *parent = 0); explicit Settings(const QString &file_name, Format format, const SettingsType &type, QObject *parent = 0);
Type m_initializationStatus; SettingsType m_initializationStatus;
}; };
#endif // SETTINGS_H #endif // SETTINGS_H

View File

@ -0,0 +1,21 @@
#ifndef SETTINGSPROPERTIES_H
#define SETTINGSPROPERTIES_H
#include <QString>
// Describes possible types of loaded settings.
enum SettingsType {
Portable,
NonPortable
};
// Describes characteristics of settings.
struct SettingsProperties {
SettingsType m_type;
QString m_baseDirectory;
QString m_settingsSuffix;
QString m_absoluteSettingsFileName;
};
#endif // SETTINGSPROPERTIES_H