瀏覽代碼

restructured lib

Malte Veerman 10 年之前
父節點
當前提交
141bb649a6
共有 4 個文件被更改,包括 77 次插入47 次删除
  1. 0 1
      lib/src/hwmon.cpp
  2. 4 0
      lib/src/hwmon.h
  3. 62 39
      lib/src/sensors.cpp
  4. 11 7
      lib/src/sensors.h

+ 0 - 1
lib/src/hwmon.cpp

@@ -18,7 +18,6 @@
  */
 
 #include "hwmon.h"
-#include "sensors.h"
 #include "loader.h"
 
 #include <QDir>

+ 4 - 0
lib/src/hwmon.h

@@ -21,6 +21,10 @@
 #define HWMON_H
 
 #include <QObject>
+#include <QString>
+#include <QList>
+
+#include "sensors.h"
 
 class Fan;
 class PwmFan;

+ 62 - 39
lib/src/sensors.cpp

@@ -20,6 +20,8 @@
 #include "sensors.h"
 #include <QFile>
 #include <QDir>
+#include <QTextStream>
+#include <QTimer>
 #include <QDebug>
 #include <KConfigGroup>
 #include <KSharedConfig>
@@ -36,7 +38,8 @@ Sensor::Sensor(Hwmon *parent, uint index, const QString &path) : QObject(parent)
 
 
 Fan::Fan(Hwmon *parent, uint index) : 
-    Sensor(parent, index, QString(parent->name() + QString("/fan") + QString::number(index)))
+    Sensor(parent, index, QString(parent->name() + QString("/fan") + QString::number(index))),
+    m_rpmStream(new QTextStream)
 {
     if (QDir(parent->path()).isReadable())
     {
@@ -44,8 +47,8 @@ Fan::Fan(Hwmon *parent, uint index) :
 
         if (rpmFile->open(QFile::ReadOnly))
         {
-            m_rpmStream.setDevice(rpmFile);
-            m_rpmStream >> m_rpm;
+            m_rpmStream->setDevice(rpmFile);
+            *m_rpmStream >> m_rpm;
         }
         else
         {
@@ -54,6 +57,11 @@ Fan::Fan(Hwmon *parent, uint index) :
     }
 }
 
+Fan::~Fan()
+{
+    delete m_rpmStream;
+}
+
 QString Fan::name() const
 {
     KConfigGroup names = KSharedConfig::openConfig("fancontrol-gui")->group("names");
@@ -78,13 +86,16 @@ void Fan::setName(const QString &name)
 
 void Fan::update()
 {
-    m_rpmStream.seek(0);
-    m_rpmStream >> m_rpm;
+    m_rpmStream->seek(0);
+    *m_rpmStream >> m_rpm;
     emit rpmChanged();
 }
 
 
 PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index),
+    m_pwmStream(new QTextStream),
+    m_modeStream(new QTextStream),
+    m_testTimer(new QTimer(this)),
     m_temp(Q_NULLPTR),
     m_hasTemp(false),
     m_testing(false),
@@ -97,7 +108,7 @@ PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index),
     m_zeroRpm(0),
     m_testStatus(notTesting)
 {
-    m_testTimer.setSingleShot(true);
+    m_testTimer->setSingleShot(true);
 
     connect(this, SIGNAL(tempChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(hasTempChanged()), parent, SLOT(updateConfig()));
@@ -107,20 +118,20 @@ PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index),
     connect(this, SIGNAL(maxPwmChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(minStartChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(minStopChanged()), parent, SLOT(updateConfig()));
-    connect(&m_testTimer, SIGNAL(timeout()), this, SLOT(continueTest()));
+    connect(m_testTimer, SIGNAL(timeout()), this, SLOT(continueTest()));
 
     if (QDir(parent->path()).isReadable())
     {
         QFile *pwmFile = new QFile(parent->path() + "/pwm" + QString::number(index), this);
         if (pwmFile->open(QFile::ReadWrite))
         {
-            m_pwmStream.setDevice(pwmFile);
-            m_pwmStream >> m_pwm;
+            m_pwmStream->setDevice(pwmFile);
+            *m_pwmStream >> m_pwm;
         }
         else if (pwmFile->open(QFile::ReadOnly))
         {
-            m_pwmStream.setDevice(pwmFile);
-            m_pwmStream >> m_pwm;
+            m_pwmStream->setDevice(pwmFile);
+            *m_pwmStream >> m_pwm;
         }
         else
             qDebug() << "Can't open pwmFile " << pwmFile->fileName();
@@ -128,28 +139,34 @@ PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index),
         QFile *pwmModeFile = new QFile(parent->path() + "/pwm" + QString::number(index) + "_mode", this);
         if (pwmModeFile->open(QFile::ReadWrite))
         {
-            m_modeStream.setDevice(pwmModeFile);
-            m_modeStream >> m_pwmMode;
+            m_modeStream->setDevice(pwmModeFile);
+            *m_modeStream >> m_pwmMode;
         }
         else if (pwmModeFile->open(QFile::ReadOnly))
         {
-            m_modeStream.setDevice(pwmModeFile);
-            m_modeStream >> m_pwmMode;
+            m_modeStream->setDevice(pwmModeFile);
+            *m_modeStream >> m_pwmMode;
         }
         else
             qDebug() << "Can't open pwmModeFile " << pwmModeFile->fileName();
     }
 }
 
+PwmFan::~PwmFan()
+{
+    delete m_pwmStream;
+    delete m_modeStream;
+}
+
 void PwmFan::update()
 {
     Fan::update();
 
-    m_pwmStream.seek(0);
-    setPwm(m_pwmStream.readAll().toInt(), false);
+    m_pwmStream->seek(0);
+    setPwm(m_pwmStream->readAll().toInt(), false);
 
-    m_modeStream.seek(0);
-    setPwmMode(m_modeStream.readAll().toInt(), false);
+    m_modeStream->seek(0);
+    setPwmMode(m_modeStream->readAll().toInt(), false);
 }
 
 void PwmFan::setPwm(int pwm, bool write)
@@ -161,15 +178,15 @@ void PwmFan::setPwm(int pwm, bool write)
 
         if (write)
         {
-            if (m_pwmStream.device()->isWritable())
-                m_pwmStream << pwm;
+            if (m_pwmStream->device()->isWritable())
+                *m_pwmStream << pwm;
             else
             {
                 KAuth::Action action("fancontrol.gui.helper.action");
                 action.setHelperId("fancontrol.gui.helper");
                 QVariantMap map;
                 map["action"] = "write";
-                map["filename"] = qobject_cast<QFile *>(m_pwmStream.device())->fileName();
+                map["filename"] = qobject_cast<QFile *>(m_pwmStream->device())->fileName();
                 map["content"] = QString::number(pwm);
                 action.setArguments(map);
                 KAuth::ExecuteJob *reply = action.execute();
@@ -190,15 +207,15 @@ void PwmFan::setPwmMode(int pwmMode, bool write)
 
         if (write)
         {
-            if (m_modeStream.device()->isWritable())
-                m_modeStream << pwmMode;
+            if (m_modeStream->device()->isWritable())
+                *m_modeStream << pwmMode;
             else
             {
                 KAuth::Action action("fancontrol.gui.helper.action");
                 action.setHelperId("fancontrol.gui.helper");
                 QVariantMap map;
                 map["action"] = "write";
-                map["filename"] = qobject_cast<QFile *>(m_modeStream.device())->fileName();
+                map["filename"] = qobject_cast<QFile *>(m_modeStream->device())->fileName();
                 map["content"] = QString::number(pwmMode);
                 action.setArguments(map);
                 KAuth::ExecuteJob *reply = action.execute();
@@ -218,15 +235,15 @@ void PwmFan::test()
     m_testStatus = findingStop1;
     setPwmMode(1);
     setPwm(255);
-    m_testTimer.setInterval(500);
-    m_testTimer.start();
+    m_testTimer->setInterval(500);
+    m_testTimer->start();
     qDebug() << "Start testing...";
 }
 
 void PwmFan::abortTest()
 {
     setPwm(255);
-    m_testTimer.stop();
+    m_testTimer->stop();
 
     m_testing = false;
     emit testingChanged();
@@ -253,11 +270,11 @@ void PwmFan::continueTest()
             {
                 m_testStatus = findingStart;
                 m_zeroRpm = 0;
-                m_testTimer.setInterval(500);
+                m_testTimer->setInterval(500);
                 qDebug() << "Start finding start value...";
             }
         }
-        m_testTimer.start();
+        m_testTimer->start();
         break;
 
     case findingStart:
@@ -266,11 +283,11 @@ void PwmFan::continueTest()
         else
         {
             m_testStatus = findingStop2;
-            m_testTimer.setInterval(1000);
+            m_testTimer->setInterval(1000);
             setMinStart(m_pwm);
             qDebug() << "Start finding stop value...";
         }
-        m_testTimer.start();
+        m_testTimer->start();
         break;
 
     case findingStop2:
@@ -278,14 +295,14 @@ void PwmFan::continueTest()
         {
             setPwm(m_pwm - 1);
             m_zeroRpm = 0;
-            m_testTimer.start();
+            m_testTimer->start();
         }
         else
         {
             if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
             {
                 m_zeroRpm++;
-                m_testTimer.start();
+                m_testTimer->start();
             }
             else
             {
@@ -340,7 +357,8 @@ void PwmFan::setActive(bool a)
 
 
 Temp::Temp(Hwmon *parent, uint index) : 
-    Sensor(parent, index, QString(parent->name() + QString("/temp") + QString::number(index)))
+    Sensor(parent, index, QString(parent->name() + QString("/temp") + QString::number(index))),
+    m_valueStream(new QTextStream)
 {
     if (QDir(parent->path()).isReadable())
     {
@@ -349,8 +367,8 @@ Temp::Temp(Hwmon *parent, uint index) :
 
         if (valueFile->open(QFile::ReadOnly))
         {
-            m_valueStream.setDevice(valueFile);
-            m_valueStream >> m_value;
+            m_valueStream->setDevice(valueFile);
+            *m_valueStream >> m_value;
             m_value /= 1000;
         }
         else
@@ -367,6 +385,11 @@ Temp::Temp(Hwmon *parent, uint index) :
     }
 }
 
+Temp::~Temp()
+{
+    delete m_valueStream;
+}
+
 QString Temp::name() const
 {
     KConfigGroup names = KSharedConfig::openConfig("fancontrol-gui")->group("names");
@@ -395,8 +418,8 @@ void Temp::setName(const QString &name)
 
 void Temp::update()
 {
-    m_valueStream.seek(0);
-    m_valueStream >> m_value;
+    m_valueStream->seek(0);
+    *m_valueStream >> m_value;
     m_value /= 1000;
     emit valueChanged();
 }

+ 11 - 7
lib/src/sensors.h

@@ -21,12 +21,13 @@
 #define SENSORS_H
 
 #include <QObject>
-#include <QTextStream>
-#include <QTimer>
+#include <QString>
 
 #include "hwmon.h"
 
 class Hwmon;
+class QTextStream;
+class QTimer;
 
 class Sensor : public QObject
 {
@@ -74,6 +75,7 @@ class Temp : public Sensor
 public:
 
     explicit Temp(Hwmon *parent, uint index);
+    ~Temp();
 
     QString label() const { return m_label; }
     int value() const { return m_value; }
@@ -96,7 +98,7 @@ protected:
 
     QString m_label;
     int m_value;
-    QTextStream m_valueStream;
+    QTextStream *m_valueStream;
 };
 
 
@@ -108,6 +110,7 @@ class Fan : public Sensor
 public:
 
     explicit Fan(Hwmon *parent, uint index);
+    virtual ~Fan();
 
     int rpm() const { return m_rpm; }
     QString name() const;
@@ -130,7 +133,7 @@ public slots:
 protected:
 
     int m_rpm;
-    QTextStream m_rpmStream;
+    QTextStream *m_rpmStream;
 };
 
 
@@ -153,6 +156,7 @@ class PwmFan : public Fan
 public:
 
     explicit PwmFan(Hwmon *parent, uint index);
+    ~PwmFan();
 
     int pwm() const { return m_pwm; }
     Temp * temp() const { return m_temp; }
@@ -207,9 +211,9 @@ protected slots:
 protected:
 
     int m_pwm;
-    QTextStream m_pwmStream;
-    QTextStream m_modeStream;
-    QTimer m_testTimer;
+    QTextStream *m_pwmStream;
+    QTextStream *m_modeStream;
+    QTimer *m_testTimer;
     Temp *m_temp;
     bool m_hasTemp;
     bool m_testing;