Allow users to export or import user-settings file in a GUI environme… · amtech/sqlitebrowser@27c2f64 · GitHub
Skip to content

Commit 27c2f64

Browse files
author
SeongTae Jeong
authored
Allow users to export or import user-settings file in a GUI environment (sqlitebrowser#2394)
* Change 'Preferences' to 'Settings' for natural expression. See the issue sqlitebrowser#2393 * Add filter for INI file * Add 'Export Settings' and 'Import Settings' buttons In Preferences dialog * Add validation of whether it is a normal settings file
1 parent 81af52a commit 27c2f64

6 files changed

Lines changed: 132 additions & 38 deletions

File tree

‎src/Application.cpp‎

Lines changed: 12 additions & 12 deletions

‎src/FileDialog.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ static const QString FILE_FILTER_HEX(QObject::tr("Hex Dump Files (*.dat *.bin)")
5656
// Dynamic/Shared Objects File Extension Filter
5757
static const QString FILE_FILTER_DYN(QObject::tr("Extensions (*.so *.dylib *.dll)"));
5858

59+
// Initialization File Extension Filter
60+
static const QString FILE_FILTER_INI(QObject::tr("Initialization File (*.ini)"));
61+
5962
enum FileDialogTypes {
6063
NoSpecificType,
6164

@@ -74,7 +77,10 @@ enum FileDialogTypes {
7477
OpenDataFile,
7578

7679
OpenExtensionFile,
77-
OpenCertificateFile
80+
OpenCertificateFile,
81+
82+
CreateSettingsFile,
83+
OpenSettingsFile
7884
};
7985

8086
class FileDialog : public QFileDialog

‎src/PreferencesDialog.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ PreferencesDialog::PreferencesDialog(QWidget* parent, Tabs tab)
4949

5050
// Set current tab
5151
ui->tabWidget->setCurrentIndex(tab);
52+
53+
// Add 'Export Settings' and 'Import Settings' buttons
54+
const QPushButton* exportSettings = ui->buttonBox->addButton(tr("Export Settings"), QDialogButtonBox::ActionRole);
55+
connect(exportSettings, &QPushButton::clicked, this, &PreferencesDialog::exportSettings);
56+
const QPushButton* importSettings = ui->buttonBox->addButton(tr("Import Settings"), QDialogButtonBox::ActionRole);
57+
connect(importSettings, &QPushButton::clicked, this, &PreferencesDialog::importSettings);
5258
}
5359

5460
/*
@@ -692,3 +698,27 @@ void PreferencesDialog::configureProxy()
692698
{
693699
m_proxyDialog->show();
694700
}
701+
702+
void PreferencesDialog::exportSettings()
703+
{
704+
const QString fileName = FileDialog::getSaveFileName(CreateSettingsFile, this, tr("Save Settings File"), tr("Initialization File (*.ini)"));
705+
if(!fileName.isEmpty())
706+
{
707+
Settings::exportSettings(fileName);
708+
QMessageBox::information(this, QApplication::applicationName(), (tr("The settings file has been saved in location :\n") + fileName));
709+
}
710+
}
711+
712+
void PreferencesDialog::importSettings()
713+
{
714+
const QString fileName = FileDialog::getOpenFileName(OpenSettingsFile, this, tr("Open Settings File"), tr("Initialization File (*.ini)"));
715+
if(!fileName.isEmpty())
716+
{
717+
if(Settings::importSettings(fileName))
718+
QMessageBox::warning(this, QApplication::applicationName(), tr("Rerun the program to apply the settings."));
719+
else
720+
QMessageBox::critical(this, QApplication::applicationName(), tr("The selected settings file is not a normal settings file.\nPlease check again."));
721+
722+
PreferencesDialog::close();
723+
}
724+
}

‎src/PreferencesDialog.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ private slots:
6363
void setColorSetting(QFrame* frame, const QColor &color);
6464
void saveColorSetting(QFrame* frame, const std::string& name);
6565
void addClientCertToTable(const QString& path, const QSslCertificate& cert);
66+
void exportSettings();
67+
void importSettings();
6668

6769
protected:
6870
bool eventFilter(QObject *obj, QEvent *event) override;

‎src/Settings.cpp‎

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <QStandardPaths>
1111
#include <QPalette>
1212

13-
QString Settings::userPreferencesFile;
13+
QString Settings::userSettingsFile;
1414
QSettings* Settings::settings;
1515
std::unordered_map<std::string, QVariant> Settings::m_hCache;
1616
int Settings::m_defaultFontSize;
@@ -20,59 +20,67 @@ static bool ends_with(const std::string& str, const std::string& with)
2020
return str.rfind(with) == str.size() - with.size();
2121
}
2222

23-
void Settings::setUserPreferencesFile(const QString userPreferencesFileArg)
23+
void Settings::setUserSettingsFile(const QString userSettingsFileArg)
2424
{
25-
userPreferencesFile = userPreferencesFileArg;
25+
userSettingsFile = userSettingsFileArg;
2626
}
2727

28-
void Settings::setSettingsObject()
28+
bool Settings::isVaildSettingsFile(const QString userSettingsFile)
2929
{
30-
// If an object has already been created, it is terminated to reduce overhead
31-
if(settings)
32-
return;
33-
3430
/*
35-
Variable that stores whether or not the preferences file requested by the user is a normal preferences file
31+
Variable that stores whether or not the settings file requested by the user is a normal settings file
3632
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
3733
*/
38-
bool isNormalUserPreferencesFile = true;
3934

40-
// Code that verifies that the preferences file requested by the user is a normal preferences file
41-
if(userPreferencesFile != nullptr)
35+
bool isNormalUserSettingsFile = true;
36+
37+
// Code that verifies that the settings file requested by the user is a normal settings file
38+
if(userSettingsFile != nullptr)
4239
{
4340
QFile *file = new QFile;
44-
file->setFileName(userPreferencesFile);
41+
file->setFileName(userSettingsFile);
4542

4643
if(file->open(QIODevice::ReadOnly))
4744
{
4845
if(file->exists() &&
4946
QString::compare(QString::fromStdString("[%General]\n"), file->readLine(), Qt::CaseInsensitive) != 0)
50-
isNormalUserPreferencesFile = false;
47+
isNormalUserSettingsFile = false;
5148
}
5249

5350
file->close();
5451
}
5552

56-
if(userPreferencesFile == nullptr)
53+
return isNormalUserSettingsFile;
54+
}
55+
56+
void Settings::setSettingsObject()
57+
{
58+
// If an object has already been created, it is terminated to reduce overhead
59+
if(settings)
60+
return;
61+
62+
const bool isNormalUserSettingsFile = isVaildSettingsFile(userSettingsFile);
63+
64+
if(userSettingsFile == nullptr)
5765
{
5866
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
5967
} else {
60-
if(isNormalUserPreferencesFile)
68+
if(isNormalUserSettingsFile)
6169
{
62-
settings = new QSettings(userPreferencesFile, QSettings::IniFormat);
70+
settings = new QSettings(userSettingsFile, QSettings::IniFormat);
6371

64-
// Code to verify that the user does not have access to the requested preferences file
72+
// Code to verify that the user does not have access to the requested settings file
6573
if(settings->status() == QSettings::AccessError) {
66-
qWarning() << qPrintable("The given preferences file can NOT access. Please check the permission for the file.");
67-
qWarning() << qPrintable("So, the -p/--preferences option is ignored.");
74+
qWarning() << qPrintable("The given settings file can NOT access. Please check the permission for the file.");
75+
qWarning() << qPrintable("So, the -S/--settings option is ignored.");
6876

6977
// Since you do not have permission to the file, delete the existing assignment and assign the standard
7078
delete settings;
7179
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
7280
}
7381
} else {
74-
qWarning() << qPrintable("The given preferences file is not a normal preferences file. Please check again.");
75-
qWarning() << qPrintable("So, the -p/--preferences option is ignored.");
82+
qWarning() << qPrintable("The given settings file is not a normal settings file. Please check again.");
83+
qWarning() << qPrintable("So, the -S/--settings option is ignored.");
7684
settings = new QSettings(QCoreApplication::organizationName(), QCoreApplication::organizationName());
7785
}
7886
}
@@ -569,3 +577,46 @@ void Settings::restoreDefaults ()
569577
settings->clear();
570578
m_hCache.clear();
571579
}
580+
581+
void Settings::exportSettings(const QString fileName)
582+
{
583+
QSettings* exportSettings = new QSettings(fileName, QSettings::IniFormat);
584+
585+
const QStringList groups = settings->childGroups();
586+
foreach(QString currentGroup, groups)
587+
{
588+
settings->beginGroup(currentGroup);
589+
const QStringList keys = settings->childKeys();
590+
foreach(QString currentKey, keys)
591+
{
592+
exportSettings->beginGroup(currentGroup);
593+
exportSettings->setValue(currentKey, getValue((currentGroup.toStdString()), (currentKey.toStdString())));
594+
exportSettings->endGroup();
595+
}
596+
settings->endGroup();
597+
}
598+
}
599+
600+
bool Settings::importSettings(const QString fileName)
601+
{
602+
if(!isVaildSettingsFile(fileName))
603+
return false;
604+
605+
QSettings* importSettings = new QSettings(fileName, QSettings::IniFormat);
606+
607+
const QStringList groups = importSettings->childGroups();
608+
for(const QString currentGroup : groups)
609+
{
610+
importSettings->beginGroup(currentGroup);
611+
const QStringList keys = importSettings->childKeys();
612+
for(const QString currentKey : keys)
613+
{
614+
settings->beginGroup(currentGroup);
615+
settings->setValue(currentKey, importSettings->value(currentKey));
616+
settings->endGroup();
617+
}
618+
importSettings->endGroup();
619+
}
620+
621+
return true;
622+
}

‎src/Settings.h‎

Lines changed: 8 additions & 3 deletions

0 commit comments

Comments
 (0)