Răsfoiți Sursa

Fixed not being able to save to not yet existent config file.

Malte Veerman 6 ani în urmă
părinte
comite
c6067b9915
1 a modificat fișierele cu 46 adăugiri și 34 ștergeri
  1. 46 34
      import/src/loader.cpp

+ 46 - 34
import/src/loader.cpp

@@ -436,63 +436,75 @@ bool Loader::load(const QUrl &url)
     }
     emit info(i18n("Loading config file: \'%1\'", filePath));
 
+    watchPath(filePath);
+
     QTextStream stream;
     QFile file(filePath);
     QString fileContent;
 
-    if (file.open(QFile::ReadOnly | QFile::Text))
+    if (file.exists())
     {
-        stream.setDevice(&file);
-        fileContent = stream.readAll();
-    }
-    else if (file.exists())
-    {
-        auto action = newFancontrolAction();
-
-        if (action.isValid())
+        if (file.open(QFile::ReadOnly | QFile::Text))
         {
-            auto map = QVariantMap();
-            map[QStringLiteral("action")] = QVariant("read");
-            map[QStringLiteral("filename")] = filePath;
-            action.setArguments(map);
-            auto job = action.execute();
-            if (!job->exec())
+            stream.setDevice(&file);
+            fileContent = stream.readAll();
+        }
+        else
+        {
+            auto action = newFancontrolAction();
+
+            if (action.isValid())
             {
-                if (job->error() == 4)
+                auto map = QVariantMap();
+                map[QStringLiteral("action")] = QVariant("read");
+                map[QStringLiteral("filename")] = filePath;
+                action.setArguments(map);
+                auto job = action.execute();
+                if (!job->exec())
                 {
-                    emit info(i18n("Loading of file aborted by user"));
+                    if (job->error() == 4)
+                    {
+                        emit info(i18n("Loading of file aborted by user"));
+                        return false;
+                    }
+
+                    emit error(i18n("KAuth::ExecuteJob error! Code: %1\nAdditional Info: %2", job->error(), job->errorString()), true);
                     return false;
                 }
-
-                emit error(i18n("KAuth::ExecuteJob error! Code: %1\nAdditional Info: %2", job->error(), job->errorString()), true);
-                return false;
+                else
+                    fileContent = job->data().value(QStringLiteral("content")).toString();
             }
             else
-                fileContent = job->data().value(QStringLiteral("content")).toString();
+                emit error(i18n("Action not supported! Try running the application as root."), true);
         }
-        else
-            emit error(i18n("Action not supported! Try running the application as root."), true);
+
+        bool success = load(fileContent);
+
+        if (!url.isEmpty())
+        {
+            m_configUrl = url;
+            emit configUrlChanged();
+        }
+
+        return success;
     }
     else
     {
-        emit error(i18n("File does not exist: \'%1\'" ,filePath));
-        return false;
-    }
-
-    bool success = load(fileContent);
+        emit error(i18n("File does not yet exist: \'%1\'" ,filePath));
 
-    if (success)
-    {
-        watchPath(filePath);
+        m_loadedConfig = QString();
+        emit needsSaveChanged();
 
         if (!url.isEmpty())
         {
             m_configUrl = url;
             emit configUrlChanged();
         }
+
+        return false;
     }
 
-    return success;
+    return false;
 }
 
 bool Loader::load(const QString& config)
@@ -508,8 +520,8 @@ bool Loader::load(const QString& config)
 
     bool success = parseConfig(config);
 
-    if (success)
-        m_loadedConfig = config;
+    m_loadedConfig = config;
+    emit needsSaveChanged();
 
     return success;
 }