2
0
Эх сурвалжийг харах

Some small changes to the hwmon handling and indexing of hwmons and sensors

Malte Veerman 5 жил өмнө
parent
commit
5133187a96

+ 2 - 1
import/src/hwmon.cpp

@@ -27,6 +27,7 @@
 
 #include <QtCore/QDir>
 #include <QtCore/QTextStream>
+
 #include <KI18n/KLocalizedString>
 
 
@@ -54,7 +55,7 @@ Hwmon::Hwmon(const QString &path, Loader *parent) : QObject(parent),
         }
 
         auto success = false;
-        m_index = path.split('/').last().remove(QStringLiteral("hwmon")).toInt(&success);
+        m_index = path.split('/').last().remove(QStringLiteral("hwmon")).toUInt(&success);
 
         if (!success)
         {

+ 9 - 9
import/src/hwmon.h

@@ -22,7 +22,7 @@
 #define HWMON_H
 
 #include <QtCore/QObject>
-#include <QtCore/QHash>
+#include <QtCore/QMap>
 
 
 namespace Fancontrol
@@ -51,10 +51,10 @@ public:
     void initialize();
     QString name() const { return m_name; }
     QString path() const { return m_path; }
-    int index() const { return m_index; }
-    QHash<uint, Fan *> fans() const { return m_fans; }
-    QHash<uint, PwmFan *> pwmFans() const { return m_pwmFans; }
-    QHash<uint, Temp *> temps() const { return m_temps; }
+    uint index() const { return m_index; }
+    QMap<uint, Fan *> fans() const { return m_fans; }
+    QMap<uint, PwmFan *> pwmFans() const { return m_pwmFans; }
+    QMap<uint, Temp *> temps() const { return m_temps; }
     QList<QObject *> fansAsObjects() const;
     QList<QObject *> pwmFansAsObjects() const;
     QList<QObject *> tempsAsObjects() const;
@@ -80,12 +80,12 @@ signals:
 protected:
 
     QString m_name;
-    int m_index;
+    uint m_index;
     Loader *const m_parent;
     bool m_valid;
-    QHash<uint, Fan *> m_fans;
-    QHash<uint, PwmFan *> m_pwmFans;
-    QHash<uint, Temp *> m_temps;
+    QMap<uint, Fan *> m_fans;
+    QMap<uint, PwmFan *> m_pwmFans;
+    QMap<uint, Temp *> m_temps;
 
 private:
 

+ 16 - 26
import/src/loader.cpp

@@ -93,42 +93,32 @@ void Loader::parseHwmons(QString path)
     while (!list.isEmpty())
         dereferencedList << QFile::symLinkTarget(hwmonDir.absoluteFilePath(list.takeFirst()));
 
-    for (auto &hwmon : m_hwmons)
+    for (auto &hwmon : m_hwmons.values())
     {
-        if (!dereferencedList.contains(hwmon->path()))
-        {
-            hwmon->deleteLater();
-            m_hwmons.removeOne(hwmon);
-            emit hwmonsChanged();
-        }
-        else
-            hwmon->initialize();
+        hwmon->deleteLater();
     }
+    m_hwmons.clear();
 
     for (const auto &hwmonPath : qAsConst(dereferencedList))
     {
-        auto hwmonExists = false;
+        auto newHwmon = new Hwmon(hwmonPath, this);
 
-        for (const auto &hwmon : qAsConst(m_hwmons))
+        if (m_hwmons.contains(newHwmon->index()))
         {
-            if (hwmon->path() == hwmonPath)
-            {
-                hwmonExists = true;
-                break;
-            }
+            emit error(i18n("An Hwmon with index %1 already exists.", newHwmon->index()));
+            continue;
         }
 
-        if (!hwmonExists)
+        if (newHwmon->isValid())
         {
-            auto newHwmon = new Hwmon(hwmonPath, this);
-            if (newHwmon->isValid())
-            {
-                connect(this, &Loader::sensorsUpdateNeeded, newHwmon, &Hwmon::sensorsUpdateNeeded);
-                m_hwmons << newHwmon;
-                emit hwmonsChanged();
-            }
-            else
-                delete newHwmon;
+            connect(this, &Loader::sensorsUpdateNeeded, newHwmon, &Hwmon::sensorsUpdateNeeded);
+            m_hwmons.insert(newHwmon->index(), newHwmon);
+            emit hwmonsChanged();
+        }
+        else
+        {
+            delete newHwmon;
+            emit error(i18n("Invalid hwmon found at: %1", hwmonPath));
         }
     }
 

+ 3 - 2
import/src/loader.h

@@ -21,6 +21,7 @@
 #ifndef LOADER_H
 #define LOADER_H
 
+#include <QtCore/QMap>
 #include <QtCore/QObject>
 #include <QtCore/QUrl>
 
@@ -65,7 +66,7 @@ public:
     QUrl configUrl() const { return m_configUrl; }
     QString configPath() const { return m_configUrl.path(); }
     QString config() const { return m_config; }
-    QList<Hwmon *> hwmons() const { return m_hwmons; }
+    QList<Hwmon *> hwmons() const { return m_hwmons.values(); }
     bool sensorsDetected() const { return m_sensorsDetected; }
     bool restartServiceAfterTesting() const { return m_reactivateAfterTesting; }
     void setRestartServiceAfterTesting(bool restart);
@@ -92,7 +93,7 @@ protected:
     QString createConfig() const;
     bool watchPath(const QString &path);
 
-    QList<Hwmon *> m_hwmons;
+    QMap<uint, Hwmon *> m_hwmons;
 
 
 private:

+ 1 - 1
import/tests/loadertest.h

@@ -102,7 +102,7 @@ public:
     void addHwmon(Hwmon *hwmon)
     {
         connect(this, &Loader::sensorsUpdateNeeded, hwmon, &Hwmon::sensorsUpdateNeeded);
-        m_hwmons << hwmon;
+        m_hwmons.insert(hwmon->index(), hwmon);
         emit hwmonsChanged();
     }
     void parse(const QString &string)