Explorar el Código

add support for detecting sensors

Malte Veerman hace 10 años
padre
commit
af48c1d4b0
Se han modificado 6 ficheros con 158 adiciones y 37 borrados
  1. 29 3
      helper/src/helper.cpp
  2. 1 0
      lib/src/guibase.cpp
  3. 71 16
      lib/src/hwmon.cpp
  4. 1 0
      lib/src/hwmon.h
  5. 55 18
      lib/src/loader.cpp
  6. 1 0
      lib/src/loader.h

+ 29 - 3
helper/src/helper.cpp

@@ -21,6 +21,7 @@
 
 #include <QFile>
 #include <QTextStream>
+#include <QProcess>
 
 #ifndef NO_SYSTEMD
 #include <QtDBus>
@@ -77,10 +78,10 @@ ActionReply Helper::action(const QVariantMap &arguments)
         QTextStream stream(&file);
         QString content = stream.readAll();
 
-        QVariantMap retdata;
-        retdata["content"] = content;
+        QVariantMap returnData;
+        returnData["content"] = content;
 
-        reply.setData(retdata);
+        reply.setData(returnData);
     }
 
     else if (arguments["action"] == "write")
@@ -100,6 +101,31 @@ ActionReply Helper::action(const QVariantMap &arguments)
         stream << arguments["content"].toString();
     }
 
+    else if (arguments["action"] == "detectSensors")
+    {
+        QString program = "sensors-detect";
+        QStringList arguments = QStringList() << "--auto";
+        
+        QProcess process;
+        process.start(program, arguments);
+        
+        if (!process.waitForStarted(), 10000)
+        {
+            reply = ActionReply::HelperErrorType;
+            reply.addData("errorDescription", process.errorString());
+            
+            return reply;
+        }
+        
+        if (!process.waitForFinished(), 10000)
+        {
+            reply = ActionReply::HelperErrorType;
+            reply.addData("errorDescription", process.errorString());
+            
+            return reply;
+        }
+    }
+    
     return reply;
 }
 

+ 1 - 0
lib/src/guibase.cpp

@@ -28,6 +28,7 @@
 #include <QtQml>
 #include <QLocale>
 
+
 GUIBase::GUIBase(QObject *parent) : QObject(parent),    
     m_config(Config::instance()),
 

+ 71 - 16
lib/src/hwmon.cpp

@@ -35,42 +35,97 @@ Hwmon::Hwmon(const QString &path, Loader *parent) : QObject(parent),
         m_name = QTextStream(&nameFile).readLine();
     else
         m_name = path.split('/').last();
+    
+    connect(this, SIGNAL(configUpdateNeeded()), parent, SLOT(createConfigFile()));
+    connect(this, SIGNAL(pwmFansChanged()), parent, SLOT(emitAllPwmFansChanged()));
+    connect(this, SIGNAL(tempsChanged()), parent, SLOT(emitAllTempsChanged()));
 
+    initialize();
+}
+
+void Hwmon::initialize()
+{
     QDir dir(m_path);
     QStringList entrys = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
     foreach (QString entry, entrys)
     {
         QString str = entry;
-        int index = str.remove(QRegExp("\\D+")).toInt();
+        uint index = str.remove(QRegExp("\\D+")).toUInt();
         if (entry.contains("fan") && entry.contains("input"))
         {
             if (QFile::exists(m_path + "/pwm" + QString::number(index)))
             {
-                PwmFan *newPwmFan = new PwmFan(this, index);
-                connect(this, SIGNAL(sensorsUpdateNeeded()), newPwmFan, SLOT(update()));
-                m_pwmFans << newPwmFan;
-                emit pwmFansChanged();
-                m_fans << qobject_cast<Fan *>(newPwmFan);
-                emit fansChanged();
+                PwmFan *newPwmFan = Q_NULLPTR;
+                
+                foreach (PwmFan *pwmFan, m_pwmFans)
+                {
+                    if (pwmFan->index() == index)
+                    {
+                        newPwmFan = pwmFan;
+                        break;
+                    }
+                }
+                
+                if (!newPwmFan)
+                {
+                    newPwmFan = new PwmFan(this, index);
+                    connect(this, SIGNAL(sensorsUpdateNeeded()), newPwmFan, SLOT(update()));
+                    m_pwmFans << newPwmFan;
+                    emit pwmFansChanged();
+                }
+                
+                Fan *newFan = qobject_cast<Fan *>(newPwmFan);
+                if (!m_fans.contains(newFan))
+                {
+                    m_fans << newFan;
+                    emit fansChanged();
+                }
             }
             else
             {
-                Fan *newFan = new Fan(this, index);
-                connect(this, SIGNAL(sensorsUpdateNeeded()), newFan, SLOT(update()));
-                m_fans << newFan;
-                emit fansChanged();
+                Fan *newFan = Q_NULLPTR;
+                
+                foreach (Fan *fan, m_fans)
+                {
+                    if (fan->index() == index)
+                    {
+                        newFan = fan;
+                        break;
+                    }
+                }
+                
+                if (!newFan)
+                {
+                    newFan = new Fan(this, index);
+                    connect(this, SIGNAL(sensorsUpdateNeeded()), newFan, SLOT(update()));
+                    m_fans << newFan;
+                    emit fansChanged();
+                }
             }
         }
 
         if (entry.contains("temp") && entry.contains("input"))
         {
-            Temp *newTemp = new Temp(this, index);
-            connect(this, SIGNAL(sensorsUpdateNeeded()), newTemp, SLOT(update()));
-            m_temps << newTemp;
-            emit tempsChanged();
+            Temp *newTemp = Q_NULLPTR;
+                
+            foreach (Temp *temp, m_temps)
+            {
+                if (temp->index() == index)
+                {
+                    newTemp = temp;
+                    break;
+                }
+            }
+            
+            if (!newTemp)
+            {
+                newTemp = new Temp(this, index);
+                connect(this, SIGNAL(sensorsUpdateNeeded()), newTemp, SLOT(update()));
+                m_temps << newTemp;
+                emit tempsChanged();
+            }
         }
     }
-//    qDebug() << "New Hwmon" << m_temps.size() << m_pwmFans.size();
 }
 
 QList<QObject *> Hwmon::fans() const

+ 1 - 0
lib/src/hwmon.h

@@ -46,6 +46,7 @@ public:
 
     explicit Hwmon(const QString &, Loader *parent);
 
+    void initialize();
     QString name() const { return m_name; }
     QString path() const { return m_path; }
     int index() const { return m_index; }

+ 55 - 18
lib/src/loader.cpp

@@ -47,36 +47,54 @@ Loader::Loader(QObject *parent) : QObject(parent),
 
 void Loader::parseHwmons()
 {
-    foreach (Hwmon *hwmon, m_hwmons)
-    {
-        hwmon->deleteLater();
-    }
-    m_hwmons.clear();
-
     QDir hwmonDir(HWMON_PATH);
     QStringList list;
     if (hwmonDir.isReadable())
-    {
         list = hwmonDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
-    }
+
     else
     {
         qDebug() << HWMON_PATH << " is not readable!";
         return;
     }
+    
+    QStringList dereferencedList;
+    while (!list.isEmpty())
+        dereferencedList << QFile::symLinkTarget(hwmonDir.absoluteFilePath(list.takeFirst()));
+    
+    foreach (Hwmon *hwmon, m_hwmons)
+    {
+        if (!dereferencedList.contains(hwmon->path()))
+        {
+            m_hwmons.removeOne(hwmon);
+            emit hwmonsChanged();
+            hwmon->deleteLater();
+        }
+        else
+            hwmon->initialize();
+    }
 
-    foreach (QString hwmonPath, list)
+    foreach (const QString &hwmonPath, dereferencedList)
     {
-        Hwmon *hwmon = new Hwmon(QFile::symLinkTarget(hwmonDir.absoluteFilePath(hwmonPath)), this);
-        connect(hwmon, SIGNAL(configUpdateNeeded()), this, SLOT(createConfigFile()));
-        connect(hwmon, SIGNAL(pwmFansChanged()), this, SLOT(emitAllPwmFansChanged()));
-        connect(hwmon, SIGNAL(tempsChanged()), this, SLOT(emitAllTempsChanged()));
-        connect(this, SIGNAL(sensorsUpdateNeeded()), hwmon, SLOT(updateSensors()));
-        m_hwmons << hwmon;
+        bool hwmonExists = false;
+        
+        foreach (const Hwmon *hwmon, m_hwmons)
+        {
+            if (hwmon->path() == hwmonPath)
+            {
+                hwmonExists = true;
+                break;
+            }
+        }
+
+        if (!hwmonExists)
+        {
+            Hwmon *newHwmon = new Hwmon(hwmonPath, this);
+            connect(this, SIGNAL(sensorsUpdateNeeded()), newHwmon, SLOT(updateSensors()));
+            m_hwmons << newHwmon;
+            emit hwmonsChanged();
+        }
     }
-    emit hwmonsChanged();
-    emit allPwmFansChanged();
-    emit allTempsChanged();
 }
 
 bool Loader::load(const QUrl &url)
@@ -534,6 +552,25 @@ void Loader::testFans()
     }
 }
 
+void Loader::detectSensors()
+{
+    KAuth::Action action("fancontrol.gui.helper.action");
+    action.setHelperId("fancontrol.gui.helper");
+    QVariantMap map;
+    map["action"] = "detectSensors";
+    
+    action.setArguments(map);
+    KAuth::ExecuteJob *reply = action.execute();
+
+    if (!reply->exec())
+    {
+        setError(reply->errorString());
+        return;
+    }    
+    
+    parseHwmons();
+}
+
 QList<QObject *> Loader::hwmons() const
 {
     QList<QObject *> list;

+ 1 - 0
lib/src/loader.h

@@ -50,6 +50,7 @@ public:
     Q_INVOKABLE bool load(const QUrl & = QUrl());
     Q_INVOKABLE bool save(const QUrl & = QUrl());
     Q_INVOKABLE void testFans();
+    Q_INVOKABLE void detectSensors();
     QUrl configUrl() const { return m_configUrl; }
     QString configFile() const { return m_configFile; }
     QList<QObject *> hwmons() const;