Bladeren bron

added support to detect sensors and fans

Malte Veerman 10 jaren geleden
bovenliggende
commit
f89a032987

+ 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;

+ 52 - 36
package/contents/ui/KCM.qml

@@ -24,47 +24,63 @@ import QtQuick.Layouts 1.1
 import org.kde.kcm 1.0
 import "../scripts/arrayfunctions.js" as ArrayFunctions
 
-ColumnLayout {
-    id: root
+Item {
+    implicitWidth: 800
+    implicitHeight: 600
     
-    CheckBox {
-        id: enabledBox
-        Layout.alignment: Qt.AlignLeft | Qt.AlignTop
-        text: i18n("Control fans manually")
-        checked: kcm.manualControl
-        onCheckedChanged: {
-            kcm.manualControl = checked;
-            fanRow.visible = checked;
-            fan.visible = checked;
-        }
-    }
-    
-    RowLayout {  
-        id: fanRow
+    ColumnLayout {
+        anchors.fill: parent    
         
         Label {
-            text: i18n("Fan:")
-            Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
-            renderType: Text.NativeRendering
+            visible: kcm.loader.allPwmFans.length == 0
+            text: i18n("There are no pwm capable fans in your system.")
+            anchors.top: enabledBox.bottom
+            anchors.margins: 20
+        }
+        
+        Button {
+            text: i18n("Detect fans")
+            visible: kcm.loader.allPwmFans.length == 0
+            onClicked: kcm.loader.detectSensors()
+        }
+        
+        CheckBox {
+            id: enabledBox
+            visible: kcm.loader.allPwmFans.length > 0
+            Layout.alignment: Qt.AlignLeft | Qt.AlignTop
+            text: i18n("Control fans manually")
+            checked: kcm.manualControl
+        }
+        
+        RowLayout {  
+            visible: enabledBox.checked && kcm.loader.allPwmFans.length > 0
+            
+            Label {
+                text: i18n("Fan:")
+                Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
+                renderType: Text.NativeRendering
+            }
+            ComboBox {
+                id: fanCombobox
+                model: ArrayFunctions.namesWithPaths(kcm.loader.allPwmFans)
+                Layout.fillWidth: true
+                Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
+            }
         }
-        ComboBox {
-            id: fanCombobox
-            model: ArrayFunctions.namesWithPaths(kcm.loader.allPwmFans)
+        
+        Loader {
             Layout.fillWidth: true
-            Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
+            Layout.fillHeight: true
+            active: enabledBox.checked
+            sourceComponent: PwmFan {
+                minimizable: false
+                unit: kcm.unit
+                fan: kcm.loader.allPwmFans[fanCombobox.currentIndex]
+                loader: kcm.loader
+                systemdCom: kcm.systemdCom
+                minTemp: kcm.minTemp
+                maxTemp: kcm.maxTemp
+            }
         }
     }
-    
-    PwmFan {
-        id: fan
-        minimizable: false
-        unit: kcm.unit
-        fan: kcm.loader.allPwmFans[fanCombobox.currentIndex]
-        loader: kcm.loader
-        systemdCom: kcm.systemdCom
-        minTemp: kcm.minTemp
-        maxTemp: kcm.maxTemp
-        Layout.fillWidth: true
-        Layout.fillHeight: true
-    }
 }

+ 0 - 2
package/contents/ui/PwmFan.qml

@@ -37,8 +37,6 @@ Rectangle {
     property bool minimizable: true
 
     id: root
-    implicitWidth: 800
-    implicitHeight: 600
     color: "transparent"
     border.color: "black"
     border.width: 2

+ 6 - 0
package/contents/ui/PwmFansTab.qml

@@ -67,4 +67,10 @@ ScrollView {
         text: i18n("There are no pwm capable fans in your system.")
         font.bold: true
     }
+    Button {
+        anchors.centerIn: parent
+        visible: repeater.fans.length == 0
+        text: i18n("Detect fans")
+        onClicked: loader.detectSensors()
+    }
 }

+ 8 - 1
package/contents/ui/SettingsTab.qml

@@ -27,6 +27,7 @@ import "../scripts/units.js" as Units
 Item {
     property QtObject gui
     property QtObject systemdCom: gui && gui.hasSystemdCommunicator() ? gui.systemdCom : null
+    property QtObject loader : gui ? gui.loader : null
     property int padding: 10
     property real textWidth: 0
     property var locale: Qt.locale()
@@ -107,7 +108,7 @@ Item {
                     onUnitChanged: maxTempValue.text = Units.fromCelsius(gui.maxTemp, gui.unit)
                 }
             }
-        }
+        }        
         Loader {
             active: systemdCom
             sourceComponent: RowLayout {
@@ -151,5 +152,11 @@ Item {
                 }
             }
         }
+        
+        Button {
+            x: maxTempValue.x
+            text: i18n("Detect fans")
+            onClicked: loader.detectSensors()
+        }
     }
 }