1010#include < QStandardPaths>
1111#include < QPalette>
1212
13- QString Settings::userPreferencesFile ;
13+ QString Settings::userSettingsFile ;
1414QSettings* Settings::settings;
1515std::unordered_map<std::string, QVariant> Settings::m_hCache;
1616int 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+ }
0 commit comments