Ver código fonte

improved loading and saving of config files

Maldela 8 anos atrás
pai
commit
e1d2e7587b

+ 1 - 1
fancontrol-gui/package/contents/ui/Application.qml

@@ -139,7 +139,7 @@ ApplicationWindow {
         text: i18n("Save configuration file")
         onTriggered: Fancontrol.base.save(true)
         iconName: "document-save"
-        tooltip: i18n("Save configuration file") + " (" + Fancontrol.base.loader.configUrl.toString() + ")"
+        tooltip: i18n("Save configuration file") + " (" + Fancontrol.base.loader.configPath + ")"
         shortcut: StandardKey.Save
     }
 

+ 2 - 2
fancontrol-gui/package/contents/ui/ConfigfileTab.qml

@@ -32,7 +32,7 @@ ColumnLayout {
 
     Label {
         anchors.top: parent.top
-        text: !!loader ? decodeURIComponent(loader.configPath) : ""
+        text: !!loader && loader.configEqualToLoadedFile ? loader.configPath : i18n("New config")
     }
 
     Rectangle {
@@ -48,7 +48,7 @@ ColumnLayout {
             anchors.margins: 5
 
             TextEdit {
-                text: !!loader ? loader.configFile : ""
+                text: !!loader ? loader.config : ""
                 readOnly: true
                 color: palette.text
             }

+ 26 - 13
import/src/loader.cpp

@@ -435,12 +435,12 @@ bool Loader::load(const QUrl &url)
 
     QTextStream stream;
     QFile file(fileName);
-    QString fileContent;
 
     if (file.open(QFile::ReadOnly | QFile::Text))
     {
         stream.setDevice(&file);
-        fileContent = stream.readAll();
+        m_configFileContent = stream.readAll();
+        emit configChanged();
     }
     else if (file.exists())
     {
@@ -465,7 +465,10 @@ bool Loader::load(const QUrl &url)
                 return false;
             }
             else
-                fileContent = reply->data().value(QStringLiteral("content")).toString();
+            {
+                m_configFileContent = reply->data().value(QStringLiteral("content")).toString();
+                emit configChanged();
+            }
         }
         else
             emit error(i18n("Action not supported! Try running the application as root."), true);
@@ -476,15 +479,16 @@ bool Loader::load(const QUrl &url)
         return false;
     }
 
-    auto success = parseConfig(fileContent);
-
     if (!url.isEmpty())
     {
         m_configUrl = url;
         emit configUrlChanged();
     }
 
-    return success;
+    if (m_config == m_configFileContent)
+        return true;
+
+    return parseConfig(m_configFileContent);
 }
 
 bool Loader::save(const QUrl &url)
@@ -496,8 +500,11 @@ bool Loader::save(const QUrl &url)
         fileName = m_configUrl.toLocalFile();
     }
     else if (url.isLocalFile())
+    {
         fileName = url.toLocalFile();
-
+        m_configUrl = url;
+        emit configUrlChanged();
+    }
     else
     {
         emit error(i18n("%1 is not a local file!", url.toDisplayString()), true);
@@ -509,7 +516,7 @@ bool Loader::save(const QUrl &url)
     if (file.open(QFile::WriteOnly | QFile::Text))
     {
         QTextStream stream(&file);
-        stream << m_configFile;
+        stream << m_config;
     }
     else
     {
@@ -520,7 +527,7 @@ bool Loader::save(const QUrl &url)
             QVariantMap map;
             map[QStringLiteral("action")] = QVariant("write");
             map[QStringLiteral("filename")] = fileName;
-            map[QStringLiteral("content")] = m_configFile;
+            map[QStringLiteral("content")] = m_config;
 
             action.setArguments(map);
             auto reply = action.execute();
@@ -538,20 +545,26 @@ bool Loader::save(const QUrl &url)
             }
         }
         else
+        {
             emit error(i18n("Action not supported! Try running the application as root."), true);
+            return false;
+        }
     }
 
+    m_configFileContent = m_config;
+    emit configChanged();
+
     return true;
 }
 
 void Loader::updateConfig()
 {
-    const auto configFile = createConfig();
+    const auto config = createConfig();
 
-    if (configFile != m_configFile)
+    if (config != m_config)
     {
-        m_configFile = configFile;
-        emit configFileChanged();
+        m_config = config;
+        emit configChanged();
     }
 }
 

+ 7 - 4
import/src/loader.h

@@ -46,11 +46,12 @@ class Loader : public QObject
     Q_OBJECT
     Q_PROPERTY(QUrl configUrl READ configUrl NOTIFY configUrlChanged)
     Q_PROPERTY(QString configPath READ configPath NOTIFY configUrlChanged)
-    Q_PROPERTY(QString configFile READ configFile NOTIFY configFileChanged)
+    Q_PROPERTY(QString config READ config NOTIFY configChanged)
     Q_PROPERTY(QList<QObject *> hwmons READ hwmonsAsObjects NOTIFY hwmonsChanged)
     Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
     Q_PROPERTY(bool sensorsDetected READ sensorsDetected NOTIFY sensorsDetectedChanged)
     Q_PROPERTY(bool restartServiceAfterTesting READ restartServiceAfterTesting WRITE setRestartServiceAfterTesting NOTIFY restartServiceAfterTestingChanged)
+    Q_PROPERTY(bool configEqualToLoadedFile READ configEqualToLoadedFile NOTIFY configChanged)
 
 
 public:
@@ -65,7 +66,7 @@ public:
     Q_INVOKABLE void detectSensors();
     QUrl configUrl() const { return m_configUrl; }
     QString configPath() const { return m_configUrl.path(); }
-    QString configFile() const { return m_configFile; }
+    QString config() const { return m_config; }
     QList<Hwmon *> hwmons() const { return m_hwmons; }
     bool sensorsDetected() const { return m_sensorsDetected; }
     bool restartServiceAfterTesting() const { return m_reactivateAfterTesting; }
@@ -80,6 +81,7 @@ public:
     Temp *temp(int hwmonIndex, int tempIndex) const;
     Fan *fan(int hwmonIndex, int fanIndex) const;
     void reset() const;
+    bool configEqualToLoadedFile() const { return m_config == m_configFileContent; }
 
 
 public slots:
@@ -105,7 +107,8 @@ private:
     bool m_reactivateAfterTesting;
     int m_interval;
     QUrl m_configUrl;
-    QString m_configFile;
+    QString m_config;
+    QString m_configFileContent;
     QTimer *m_timer;
     bool m_sensorsDetected;
 
@@ -113,7 +116,7 @@ private:
 signals:
 
     void configUrlChanged();
-    void configFileChanged();
+    void configChanged();
     void hwmonsChanged();
     void intervalChanged();
     void error(QString, bool = false);