소스 검색

Add explicit check for default being boolean when trying to cycle setting

- Expose defaultValue(key) from SettingsSection
- Check defaultValue return type against QMetaType::Bool
  It's necessary to cast the return value of QVariant.type() before comparing it against QMetaType.
  QVariant.type() casts the type to QVariant::Type.

Plex-CLA-1.0-signed-off-by: Lukas Pitschl lukas@leftandleaving.com
Lukas Pitschl 8 년 전
부모
커밋
5fbe051d22
3개의 변경된 파일28개의 추가작업 그리고 8개의 파일을 삭제
  1. 17 8
      src/settings/SettingsComponent.cpp
  2. 10 0
      src/settings/SettingsSection.cpp
  3. 1 0
      src/settings/SettingsSection.h

+ 17 - 8
src/settings/SettingsComponent.cpp

@@ -48,16 +48,25 @@ void SettingsComponent::cycleSetting(const QString& args)
     return;
   }
   QVariantList values = section->possibleValues(valueName);
-  // If no possible values are set, assume that the setting is a simple
-  // checkbox and cycle through the boolean value.
+  // If no possible values are defined, check the type of the default value.
+  // In the case it's a boolean simply negate the current value to cycle through.
+  // Otherwise log an error message, that it's not possible to cycle through the value.
   if (values.size() == 0)
   {
-    QVariant currentValue = section->value(valueName);
-    auto nextValue = currentValue.toBool() ? false : true;
-    setValue(sectionID, valueName, nextValue);
-    QLOG_DEBUG() << "Setting" << settingName << "to " << (nextValue ? "Enabled" : "Disabled");
-    emit SystemComponent::Get().settingsMessage(valueName, nextValue ? "Enabled" : "Disabled");
-    return;
+    if (static_cast<QMetaType::Type>(section->defaultValue(valueName).type()) == QMetaType::Bool)
+    {
+      QVariant currentValue = section->value(valueName);
+      auto nextValue = currentValue.toBool() ? false : true;
+      setValue(sectionID, valueName, nextValue);
+      QLOG_DEBUG() << "Setting" << settingName << "to " << (nextValue ? "Enabled" : "Disabled");
+      emit SystemComponent::Get().settingsMessage(valueName, nextValue ? "Enabled" : "Disabled");
+      return;
+    }
+    else
+    {
+      QLOG_ERROR() << "Setting" << settingName << "is unknown or is not cycleable.";
+      return;
+    }
   }
   QVariant currentValue = section->value(valueName);
   int nextValueIndex = 0;

+ 10 - 0
src/settings/SettingsSection.cpp

@@ -60,6 +60,16 @@ void SettingsSection::setValues(const QVariant& values)
     emit valuesUpdated(updatedValues);
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+QVariant SettingsSection::defaultValue(const QString& key)
+{
+  if (m_values.contains(key))
+    return m_values[key]->defaultValue();
+
+  QLOG_WARN() << "Looking for defaultValue:" << key << "in section:" << m_sectionID << "but it can't be found";
+  return QVariant();
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 QVariant SettingsSection::value(const QString& key)
 {

+ 1 - 0
src/settings/SettingsSection.h

@@ -27,6 +27,7 @@ public:
   bool isHidden() const;
 
   QVariant value(const QString& key);
+  QVariant defaultValue(const QString& key);
   QString sectionName() const { return m_sectionID; }
 
   const QVariantMap allValues() const;