Skip to content

Commit 0644bc0

Browse files
author
SeongTae Jeong
authored
Add the ability to support custom config files (#2390)
Two options are added for the command line: -c [config_file] --config [config_file] We can also use environment variables without using options on the command line. export DB4S_CONFIG_FILE=[config_file] If there is an environment variable and use the -c or --config option, the environment variable is ignored. See issue #2317.
1 parent 2ef7d56 commit 0644bc0

3 files changed

Lines changed: 115 additions & 13 deletions

File tree

src/Application.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@
1818
Application::Application(int& argc, char** argv) :
1919
QApplication(argc, argv)
2020
{
21+
// Get 'DB4S_CONFIG_FILE' environment variable
22+
const char* env = getenv("DB4S_CONFIG_FILE");
23+
24+
// If 'DB4S_CONFIG_FILE' environment variable exists
25+
if(env)
26+
Settings::setUserConfigurationFile(QString::fromStdString(env));
27+
28+
for(int i=1;i<arguments().size();i++)
29+
{
30+
if(arguments().at(i) == "-c" || arguments().at(i) == "--config")
31+
{
32+
if(++i < arguments().size())
33+
{
34+
if(env)
35+
{
36+
qWarning() << qPrintable(tr("The user configuration file location is replaced with the argument value instead of the environment variable value."));
37+
qWarning() << qPrintable(tr("Ignored environment variable(DB4S_CONFIG_FILE) value : ") + QString::fromStdString(env));
38+
}
39+
Settings::setUserConfigurationFile(arguments().at(i));
40+
}
41+
}
42+
}
43+
2144
// Set organisation and application names
2245
setOrganizationName("sqlitebrowser");
2346
setApplicationName("DB Browser for SQLite");
@@ -98,6 +121,8 @@ Application::Application(int& argc, char** argv) :
98121
qWarning() << qPrintable(tr(" -s, --sql <file> Execute this SQL file after opening the DB"));
99122
qWarning() << qPrintable(tr(" -t, --table <table> Browse this table after opening the DB"));
100123
qWarning() << qPrintable(tr(" -R, --read-only Open database in read-only mode"));
124+
qWarning() << qPrintable(tr(" -c, --config <config_file>"));
125+
qWarning() << qPrintable(tr(" Run application based on this configuration file"));
101126
qWarning() << qPrintable(tr(" -o, --option <group>/<setting>=<value>"));
102127
qWarning() << qPrintable(tr(" Run application with this setting temporarily set to value"));
103128
qWarning() << qPrintable(tr(" -O, --save-option <group>/<setting>=<value>"));
@@ -126,6 +151,11 @@ Application::Application(int& argc, char** argv) :
126151
m_dontShowMainWindow = true;
127152
} else if(arguments().at(i) == "-R" || arguments().at(i) == "--read-only") {
128153
readOnly = true;
154+
} else if(arguments().at(i) == "-c" || arguments().at(i) == "--config") {
155+
// This option has already been handled above
156+
// For here, only print the error when no parameter value is given
157+
if(++i >= arguments().size())
158+
qWarning() << qPrintable(tr("The -c/--config option requires an argument. The option is ignored."));
129159
} else if(arguments().at(i) == "-o" || arguments().at(i) == "--option" ||
130160
arguments().at(i) == "-O" || arguments().at(i) == "--save-option") {
131161
const QString optionWarning = tr("The -o/--option and -O/--save-option options require an argument in the form group/setting=value");

src/Settings.cpp

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Settings.h"
22

33
#include <QApplication>
4+
#include <QDebug>
45
#include <QDir>
56
#include <QSettings>
67
#include <QColor>
@@ -9,6 +10,8 @@
910
#include <QStandardPaths>
1011
#include <QPalette>
1112

13+
QString Settings::userConfigurationFile;
14+
QSettings* Settings::settings;
1215
std::unordered_map<std::string, QVariant> Settings::m_hCache;
1316
int Settings::m_defaultFontSize;
1417

@@ -17,6 +20,64 @@ static bool ends_with(const std::string& str, const std::string& with)
1720
return str.rfind(with) == str.size() - with.size();
1821
}
1922

23+
void Settings::setUserConfigurationFile(const QString userConfigurationFileArg)
24+
{
25+
userConfigurationFile = userConfigurationFileArg;
26+
}
27+
28+
void Settings::setSettingsObject()
29+
{
30+
// If an object has already been created, it is terminated to reduce overhead
31+
if(settings)
32+
return;
33+
34+
/*
35+
Variable that stores whether or not the configuration file requested by the user is a normal configuration file
36+
If the file does not exist and is newly created, the if statement below is not executed, so the default value is set to true
37+
*/
38+
bool isNormalUserConfigurationFile = true;
39+
40+
// Code that verifies that the configuration file requested by the user is a normal configuration file
41+
if(userConfigurationFile != nullptr)
42+
{
43+
QFile *file = new QFile;
44+
file->setFileName(userConfigurationFile);
45+
46+
if(file->open(QIODevice::ReadOnly))
47+
{
48+
if(file->exists() &&
49+
QString::compare(QString::fromStdString("[%General]\n"), file->readLine(), Qt::CaseInsensitive) != 0)
50+
isNormalUserConfigurationFile = false;
51+
}
52+
53+
file->close();
54+
}
55+
56+
if(userConfigurationFile == nullptr)
57+
{
58+
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
59+
} else {
60+
if(isNormalUserConfigurationFile)
61+
{
62+
settings = new QSettings(userConfigurationFile, QSettings::IniFormat);
63+
64+
// Code to verify that the user does not have access to the requested configuration file
65+
if(settings->status() == QSettings::AccessError) {
66+
qWarning() << qPrintable("The given configuration file can NOT access. Please check the permission for the file.");
67+
qWarning() << qPrintable("So, the -c/--config option is ignored.");
68+
69+
// Since you do not have permission to the file, delete the existing assignment and assign the standard
70+
delete settings;
71+
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
72+
}
73+
} else {
74+
qWarning() << qPrintable("The given configuration file is not a normal configuration file. Please check again.");
75+
qWarning() << qPrintable("So, the -c/--config option is ignored.");
76+
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
77+
}
78+
}
79+
}
80+
2081
QVariant Settings::getValue(const std::string& group, const std::string& name)
2182
{
2283
// Have a look in the cache first
@@ -26,8 +87,8 @@ QVariant Settings::getValue(const std::string& group, const std::string& name)
2687
return cacheIndex->second;
2788
} else {
2889
// Nothing found in the cache, so get the value from the settings file or get the default value if there is no value set yet
29-
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
30-
QVariant value = settings.value(QString::fromStdString(group + "/" + name), getDefaultValue(group, name));
90+
setSettingsObject();
91+
QVariant value = settings->value(QString::fromStdString(group + "/" + name), getDefaultValue(group, name));
3192

3293
// Store this value in the cache for further usage and return it afterwards
3394
m_hCache.insert({group + name, value});
@@ -41,11 +102,11 @@ void Settings::setValue(const std::string& group, const std::string& name, const
41102
// In order to achieve this this flag can be set which disables the save to disk mechanism and only leaves the save to cache part active.
42103
if(dont_save_to_disk == false)
43104
{
105+
setSettingsObject();
44106
// Set the group and save the given value
45-
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
46-
settings.beginGroup(QString::fromStdString(group));
47-
settings.setValue(QString::fromStdString(name), value);
48-
settings.endGroup();
107+
settings->beginGroup(QString::fromStdString(group));
108+
settings->setValue(QString::fromStdString(name), value);
109+
settings->endGroup();
49110
}
50111

51112
// Also change it in the cache
@@ -495,16 +556,16 @@ QColor Settings::getDefaultColorValue(const std::string& group, const std::strin
495556

496557
void Settings::clearValue(const std::string& group, const std::string& name)
497558
{
498-
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
499-
settings.beginGroup(QString::fromStdString(group));
500-
settings.remove(QString::fromStdString(name));
501-
settings.endGroup();
559+
setSettingsObject();
560+
settings->beginGroup(QString::fromStdString(group));
561+
settings->remove(QString::fromStdString(name));
562+
settings->endGroup();
502563
m_hCache.clear();
503564
}
504565

505566
void Settings::restoreDefaults ()
506567
{
507-
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
508-
settings.clear();
568+
setSettingsObject();
569+
settings->clear();
509570
m_hCache.clear();
510571
}

src/Settings.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
#define SETTINGS_H
33

44
#include <unordered_map>
5+
#include <QSettings>
56
#include <QVariant>
67

78
class Settings
89
{
910
friend class PreferencesDialog;
1011

1112
public:
12-
1313
enum AppStyle {
1414
FollowDesktopStyle,
1515
DarkStyle
1616
};
17+
18+
static void setUserConfigurationFile(const QString userConfigurationFileArg);
1719
static QVariant getValue(const std::string& group, const std::string& name);
1820
static void setValue(const std::string& group, const std::string& name, const QVariant& value, bool dont_save_to_disk = false);
1921
static void clearValue(const std::string& group, const std::string& name);
@@ -31,6 +33,15 @@ class Settings
3133
// instead of the current palette.
3234
static QColor getDefaultColorValue(const std::string& group, const std::string& name, AppStyle style);
3335

36+
// User configuration file path
37+
static QString userConfigurationFile;
38+
39+
// QSettings object
40+
static QSettings* settings;
41+
42+
// This works initialize QSettings object
43+
static void setSettingsObject();
44+
3445
// Cache for storing the settings to avoid repeatedly reading the settings file all the time
3546
static std::unordered_map<std::string, QVariant> m_hCache;
3647

0 commit comments

Comments
 (0)