فهرست منبع

Separate saving the settings config file

Instead of having a single save() method with a bool parameter, split it
into 2 separate functions. This doesn't really increase code
duplication, as the code was already pretty much duplicated, and makes
it clearer what is happening on the caller site.

Add a saveSection() function, which decides which config file should be
written depending on which file the section belongs to.
Vincent Lang 9 سال پیش
والد
کامیت
8f19b06385
2فایلهای تغییر یافته به همراه32 افزوده شده و 19 حذف شده
  1. 29 18
      src/settings/SettingsComponent.cpp
  2. 3 1
      src/settings/SettingsComponent.h

+ 29 - 18
src/settings/SettingsComponent.cpp

@@ -146,25 +146,31 @@ void SettingsComponent::loadConf(const QString& path, bool storage)
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-void SettingsComponent::save(bool justStorage)
+void SettingsComponent::saveSettings()
 {
   QVariantMap sections;
-  QVariantMap storage;
 
   foreach(SettingsSection* section, m_sections.values())
   {
-    if (section->isStorage())
-      storage.insert(section->sectionName(), section->allValues());
-    else
+    if (!section->isStorage())
       sections.insert(section->sectionName(), section->allValues());
   }
 
-  if (!justStorage)
+  QJsonObject json;
+  json.insert("sections", QJsonValue::fromVariant(sections));
+  json.insert("version", m_settingsVersion);
+  writeJson(Paths::dataDir("plexmediaplayer.conf"), json);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+void SettingsComponent::saveStorage()
+{
+  QVariantMap storage;
+
+  foreach(SettingsSection* section, m_sections.values())
   {
-    QJsonObject json;
-    json.insert("sections", QJsonValue::fromVariant(sections));
-    json.insert("version", m_settingsVersion);
-    writeJson(Paths::dataDir("plexmediaplayer.conf"), json);
+    if (section->isStorage())
+      storage.insert(section->sectionName(), section->allValues());
   }
 
   QJsonObject storagejson;
@@ -173,6 +179,15 @@ void SettingsComponent::save(bool justStorage)
   writeJson(Paths::dataDir("storage.json"), storagejson, false);
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+void SettingsComponent::saveSection(SettingsSection* section)
+{
+  if (section && section->isStorage())
+    saveStorage();
+  else
+    saveSettings();
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 QVariant SettingsComponent::value(const QString& sectionID, const QString &key)
 {
@@ -195,7 +210,7 @@ void SettingsComponent::setValue(const QString& sectionID, const QString &key, c
     return;
   }
   section->setValue(key, value);
-  save(false);
+  saveSection(section);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -207,8 +222,6 @@ void SettingsComponent::setValues(const QVariantMap& options)
   QString key = options["key"].toString();
   QVariant values = options["value"];
 
-  bool updatedStorageOnly = true;
-
   if (values.type() == QVariant::Map || values.isNull())
   {
     SettingsSection* section = getSection(key);
@@ -226,7 +239,7 @@ void SettingsComponent::setValues(const QVariantMap& options)
     else
       section->setValues(values);
 
-    updatedStorageOnly = section->isStorage();
+    saveSection(section);
   }
   else if (values.type() == QVariant::String)
   {
@@ -239,8 +252,6 @@ void SettingsComponent::setValues(const QVariantMap& options)
     // return so we don't call save()
     return;
   }
-
-  save(updatedStorageOnly);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -253,7 +264,7 @@ void SettingsComponent::removeValue(const QString &sectionOrKey)
     // we want to remove a full section
     // dont remove the section, but remove all keys
     section->removeValues();
-    save(false);
+    saveSection(section);
   }
   else
   {
@@ -261,7 +272,7 @@ void SettingsComponent::removeValue(const QString &sectionOrKey)
     // which is stored in webclient section
     section = m_sections[SETTINGS_SECTION_WEBCLIENT];
     section->removeValue(sectionOrKey);
-    save(false);
+    saveSection(section);
   }
 }
 

+ 3 - 1
src/settings/SettingsComponent.h

@@ -55,7 +55,8 @@ public:
 
   void updatePossibleValues(const QString& sectionID, const QString& key, const QVariantList& possibleValues);
 
-  void save(bool justStorage);
+  void saveSettings();
+  void saveStorage();
   void load();
 
   Q_SIGNAL void groupUpdate(const QString& section, const QVariant& description);
@@ -66,6 +67,7 @@ private:
   void parseSection(const QJsonObject& sectionObject);
   int platformMaskFromObject(const QJsonObject& object);
   Platform platformFromString(const QString& platformString);
+  void saveSection(SettingsSection* section);
 
   QMap<QString, SettingsSection*> m_sections;