11#include " Settings.h"
22
33#include < QApplication>
4+ #include < QDebug>
45#include < QDir>
56#include < QSettings>
67#include < QColor>
910#include < QStandardPaths>
1011#include < QPalette>
1112
13+ QString Settings::userConfigurationFile;
14+ QSettings* Settings::settings;
1215std::unordered_map<std::string, QVariant> Settings::m_hCache;
1316int 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+
2081QVariant 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
496557void 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
505566void Settings::restoreDefaults ()
506567{
507- QSettings settings ( QApplication::organizationName (), QApplication::organizationName () );
508- settings. clear ();
568+ setSettingsObject ( );
569+ settings-> clear ();
509570 m_hCache.clear ();
510571}
0 commit comments