浏览代码

Reset now truly resets all changes made instead of reloading the possibly changed config file

Malte Veerman 6 年之前
父节点
当前提交
07d8dd104e
共有 3 个文件被更改,包括 36 次插入24 次删除
  1. 1 2
      import/src/guibase.cpp
  2. 31 19
      import/src/loader.cpp
  3. 4 3
      import/src/loader.h

+ 1 - 2
import/src/guibase.cpp

@@ -244,8 +244,7 @@ void GUIBase::reset()
 {
     qInfo() << i18n("Resetting changes");
 
-    if (m_loader->needsSave() || configUrl() != m_loader->configUrl())
-        m_loader->load(configUrl());
+    m_loader->reset();
 
 #ifndef NO_SYSTEMD
     m_com->setServiceName(serviceName());

+ 31 - 19
import/src/loader.cpp

@@ -433,11 +433,12 @@ bool Loader::load(const QUrl &url)
 
     QTextStream stream;
     QFile file(filePath);
+    QString fileContent;
 
     if (file.open(QFile::ReadOnly | QFile::Text))
     {
         stream.setDevice(&file);
-        m_configFileContent = stream.readAll();
+        fileContent = stream.readAll();
     }
     else if (file.exists())
     {
@@ -462,7 +463,7 @@ bool Loader::load(const QUrl &url)
                 return false;
             }
             else
-                m_configFileContent = job->data().value(QStringLiteral("content")).toString();
+                fileContent = job->data().value(QStringLiteral("content")).toString();
         }
         else
             emit error(i18n("Action not supported! Try running the application as root."), true);
@@ -473,36 +474,39 @@ bool Loader::load(const QUrl &url)
         return false;
     }
 
-    watchPath(filePath);
+    bool success = load(fileContent);
 
-    if (!url.isEmpty())
+    if (success)
     {
-        m_configUrl = url;
-        emit configUrlChanged();
-    }
+        watchPath(filePath);
 
-    if (m_config == m_configFileContent)
-        return true;
+        if (!url.isEmpty())
+        {
+            m_configUrl = url;
+            emit configUrlChanged();
+        }
+    }
 
-    return parseConfig(m_configFileContent);
+    return success;
 }
 
-void Loader::load(const QString& config)
+bool Loader::load(const QString& config)
 {
     if (m_config == config)
-        return;
+        return true;
 
     if (config.isEmpty())
     {
-        emit error(i18n("Cannot load empty config."));
-        return;
+        emit error(i18n("Cannot load empty config."), true);
+        return false;
     }
 
-    m_config = config;
-    parseConfig(config);
+    bool success = parseConfig(config);
 
-    emit configChanged();
-    emit needsSaveChanged();
+    if (success)
+        m_loadedConfig = config;
+
+    return success;
 }
 
 bool Loader::save(const QUrl &url)
@@ -579,12 +583,20 @@ bool Loader::save(const QUrl &url)
         }
     }
 
-    m_configFileContent = m_config;
+    m_loadedConfig = m_config;
     emit configChanged();
 
     return true;
 }
 
+void Loader::reset()
+{
+    if (m_config == m_loadedConfig)
+        return;
+
+    load(m_loadedConfig);
+}
+
 void Loader::updateConfig()
 {
     const auto config = createConfig();

+ 4 - 3
import/src/loader.h

@@ -57,10 +57,11 @@ public:
     Q_INVOKABLE void parseHwmons();
     Q_INVOKABLE bool load(const QUrl & = QUrl());
     Q_INVOKABLE bool save(const QUrl & = QUrl());
+    Q_INVOKABLE void reset();
     Q_INVOKABLE void testFans();
     Q_INVOKABLE void abortTestingFans();
 
-    void load(const QString &config);
+    bool load(const QString &config);
     QUrl configUrl() const { return m_configUrl; }
     QString configPath() const { return m_configUrl.path(); }
     QString config() const { return m_config; }
@@ -78,7 +79,7 @@ public:
     Temp *temp(int hwmonIndex, int tempIndex) const;
     Fan *fan(int hwmonIndex, int fanIndex) const;
     void toDefault();
-    bool needsSave() const { return m_config != m_configFileContent; }
+    bool needsSave() const { return m_config != m_loadedConfig; }
     void updateConfig();
     void handleTestStatusChanged();
 
@@ -100,7 +101,7 @@ private:
     int m_interval;
     QUrl m_configUrl;
     QString m_config;
-    QString m_configFileContent;
+    QString m_loadedConfig;
     QTimer *m_timer;
     QFileSystemWatcher *m_watcher;
     bool m_sensorsDetected;