Просмотр исходного кода

added option in kcm to change the config file path and some minor improvements

Malte Veerman 9 лет назад
Родитель
Сommit
00acb3f63c

+ 1 - 1
kcm/src/fancontrolkcm.cpp

@@ -79,7 +79,7 @@ void FancontrolKCM::save()
 void FancontrolKCM::load()
 {
     m_base->load();
-    setManualControl(m_base->systemdCommunicator()->serviceEnabled());
+    setManualControl(m_base->systemdCommunicator()->serviceEnabled() || m_base->systemdCommunicator()->serviceActive());
     setNeedsSave(false);
 }
 

+ 10 - 1
lib/src/config.cpp

@@ -26,6 +26,14 @@
 
 #define CONFIG_NAME "fancontrol-gui"
 
+#ifndef STANDARD_CONFIG_FILE
+#define STANDARD_CONFIG_FILE "/etc/fancontrol"
+#endif
+
+#ifndef STANDARD_SERVICE_NAME
+#define STANDARD_SERVICE_NAME "fancontrol"
+#endif
+
 
 namespace Fancontrol
 {
@@ -37,7 +45,8 @@ Config::Config(QObject *parent) : KCoreConfigSkeleton(KSharedConfig::openConfig(
     setCurrentGroup("preferences");
     addItemDouble("MinTemp", m_minTemp, 30.0);
     addItemDouble("MaxTemp", m_maxTemp, 90.0);
-    addItemString("ServiceName", m_serviceName, "fancontrol");
+    addItemString("ServiceName", m_serviceName, QString(STANDARD_SERVICE_NAME));
+    addItemPath("ConfigUrl", m_configUrl, QString("file://") + QString(STANDARD_CONFIG_FILE));
     
     load();
 }

+ 1 - 0
lib/src/config.h

@@ -51,6 +51,7 @@ private:
     double m_minTemp;
     double m_maxTemp;
     QString m_serviceName;
+    QString m_configUrl;
 };
 
 }

+ 23 - 5
lib/src/guibase.cpp

@@ -37,10 +37,11 @@ GUIBase::GUIBase(QObject *parent) : QObject(parent),
     m_config(Config::instance()),
 
 #ifndef NO_SYSTEMD
-    m_com(new SystemdCommunicator(m_config->findItem("ServiceName")->property().toString(), this)),
+    m_com(new SystemdCommunicator(this)),
 #endif
 
-    m_loader(new Loader(this))
+    m_loader(new Loader(this)),
+    m_configValid(false)
 {
     connect(m_config, &Config::configChanged, this, &GUIBase::emitConfigChanged);
         
@@ -63,12 +64,13 @@ GUIBase::GUIBase(QObject *parent) : QObject(parent),
 void GUIBase::load()
 {
     m_config->load();
-    emitConfigChanged();
-    m_loader->load();
+    m_configValid = m_loader->load(configUrl());
     
 #ifndef NO_SYSTEMD
-    m_com->setServiceName(m_config->findItem("ServiceName")->property().toString());
+    m_com->setServiceName(serviceName());
 #endif
+
+    emitConfigChanged();
 }
 
 void GUIBase::save(bool saveLoader)
@@ -94,6 +96,11 @@ QString GUIBase::serviceName() const
     return m_config->findItem("ServiceName")->property().toString();
 }
 
+QUrl GUIBase::configUrl() const
+{
+    return QUrl::fromLocalFile(m_config->findItem("ConfigUrl")->property().toString());
+}
+
 void GUIBase::setMaxTemp(qreal temp)
 {
     if (temp != maxTemp())
@@ -126,11 +133,22 @@ void GUIBase::setServiceName(const QString& name)
     }
 }
 
+void GUIBase::setConfigUrl(const QUrl &url)
+{
+    if (url != configUrl())
+    {
+        m_config->findItem("ConfigUrl")->setProperty(url.toString(QUrl::PreferLocalFile));
+        m_configValid = m_loader->load(url);
+        emit configUrlChanged();
+    }
+}
+
 void GUIBase::emitConfigChanged()
 {
     emit serviceNameChanged();
     emit minTempChanged();
     emit maxTempChanged();
+    emit configUrlChanged();
 }
 
 }

+ 7 - 0
lib/src/guibase.h

@@ -58,6 +58,8 @@ class FANCONTROL_GUI_LIB_EXPORT GUIBase : public QObject
     Q_PROPERTY(qreal maxTemp READ maxTemp WRITE setMaxTemp NOTIFY maxTempChanged)
     Q_PROPERTY(int unit READ unit WRITE setUnit NOTIFY unitChanged)
     Q_PROPERTY(QString serviceName READ serviceName WRITE setServiceName NOTIFY serviceNameChanged)
+    Q_PROPERTY(QUrl configUrl READ configUrl WRITE setConfigUrl NOTIFY configUrlChanged)
+    Q_PROPERTY(bool configValid READ configValid NOTIFY configUrlChanged)
 
 public:
     
@@ -72,10 +74,13 @@ public:
     qreal minTemp() const;
     qreal maxTemp() const;
     QString serviceName() const;
+    QUrl configUrl() const;
+    bool configValid() const { return m_configValid; }
     int unit() const { return m_unit; }
     void setMinTemp(qreal minTemp);
     void setMaxTemp(qreal maxTemp);
     void setServiceName(const QString &name);
+    void setConfigUrl(const QUrl &url);
     void setUnit(int unit) { if (unit != m_unit) { m_unit = unit; emit unitChanged(); } }
     void load();
     void save(bool saveLoader = false);
@@ -88,6 +93,7 @@ signals:
     void minTempChanged();
     void maxTempChanged();
     void serviceNameChanged();
+    void configUrlChanged();
     void unitChanged();
     
     
@@ -106,6 +112,7 @@ private:
     
     Loader *const m_loader; 
     int m_unit;
+    bool m_configValid;
 };
 
 }

+ 11 - 11
lib/src/hwmon.cpp

@@ -37,7 +37,7 @@ Hwmon::Hwmon(const QString &path, QObject *parent) : QObject(parent),
         emit errorChanged(path + " is not readable!");
         m_valid = false;
     }
-    
+
     bool success;
     m_index = path.split('/').last().remove("hwmon").toInt(&success);
     if (!success)
@@ -45,13 +45,13 @@ Hwmon::Hwmon(const QString &path, QObject *parent) : QObject(parent),
         emit errorChanged(path + "is invalid!");
         m_valid = false;
     }
-    
+
     QFile nameFile(path + "/name");
     if (nameFile.open(QFile::ReadOnly))
         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()));
@@ -74,7 +74,7 @@ void Hwmon::initialize()
             if (QFile::exists(m_path + "/pwm" + QString::number(index)))
             {
                 PwmFan *newPwmFan = Q_NULLPTR;
-                
+
                 foreach (PwmFan *pwmFan, m_pwmFans)
                 {
                     if (pwmFan->index() == index)
@@ -84,7 +84,7 @@ void Hwmon::initialize()
                         break;
                     }
                 }
-                
+
                 if (!newPwmFan)
                 {
                     newPwmFan = new PwmFan(this, index);
@@ -92,7 +92,7 @@ void Hwmon::initialize()
                     m_pwmFans << newPwmFan;
                     emit pwmFansChanged();
                 }
-                
+
                 Fan *newFan = qobject_cast<Fan *>(newPwmFan);
                 if (!m_fans.contains(newFan))
                 {
@@ -103,7 +103,7 @@ void Hwmon::initialize()
             else
             {
                 Fan *newFan = Q_NULLPTR;
-                
+
                 foreach (Fan *fan, m_fans)
                 {
                     if (fan->index() == index)
@@ -113,7 +113,7 @@ void Hwmon::initialize()
                         break;
                     }
                 }
-                
+
                 if (!newFan)
                 {
                     newFan = new Fan(this, index);
@@ -127,7 +127,7 @@ void Hwmon::initialize()
         if (entry.contains("temp") && entry.contains("input"))
         {
             Temp *newTemp = Q_NULLPTR;
-                
+
             foreach (Temp *temp, m_temps)
             {
                 if (temp->index() == index)
@@ -137,7 +137,7 @@ void Hwmon::initialize()
                     break;
                 }
             }
-            
+
             if (!newTemp)
             {
                 newTemp = new Temp(this, index);
@@ -215,4 +215,4 @@ void Hwmon::setError(const QString &error)
     emit errorChanged(error);
 }
 
-}
+}

+ 1 - 1
lib/src/hwmon.h

@@ -46,7 +46,7 @@ class Hwmon : public QObject
 
 public:
 
-    explicit Hwmon(const QString &, QObject *parent = Q_NULLPTR);
+    explicit Hwmon(const QString &path, QObject *parent = Q_NULLPTR);
 
     void initialize();
     QString name() const { return m_name; }

+ 30 - 13
lib/src/loader.cpp

@@ -65,12 +65,12 @@ void Loader::parseHwmons()
 
     else if (hwmonDir.exists())
     {
-        setError(QString(HWMON_PATH) + " is not readable!");
+        setError(QString(HWMON_PATH) + " is not readable!", true);
         return;
     }
     else
     {
-        qCritical() << HWMON_PATH << " is not readable!";
+        setError(QString(HWMON_PATH) + " does not exist!", true);
         return;
     }
 
@@ -230,12 +230,20 @@ bool Loader::load(const QUrl &url)
         qDebug() << "Given empty url. Fallback to " << m_configUrl;
         fileName = m_configUrl.toLocalFile();
     }
-    else if (url.isLocalFile())
-        fileName = url.toLocalFile();
+    else if (url.isValid())
+    {
+        if (url.isLocalFile())
+            fileName = url.toLocalFile();
 
+        else
+        {
+            setError(url.toDisplayString() + " is not a local file!");
+            return false;
+        }
+    }
     else
     {
-        setError(url.toDisplayString() + " is not a local file!");
+        setError(url.toDisplayString() + " is not a valid url!");
         return false;
     }
 
@@ -265,7 +273,7 @@ bool Loader::load(const QUrl &url)
         if (!reply->exec())
         {
             qDebug() << reply->error();
-            setError(reply->errorString() + reply->errorText());
+            setError(reply->errorString() + reply->errorText(), true);
             return false;
         }
         else
@@ -300,6 +308,8 @@ bool Loader::load(const QUrl &url)
             qobject_cast<PwmFan *>(pwmFan)->reset();
         }
     }
+    
+    createConfigFile();
 
     stream.setString(&fileContent);
     QStringList lines;
@@ -329,7 +339,7 @@ bool Loader::load(const QUrl &url)
             }
             else
             {
-                setError("Unable to parse interval line");
+                setError("Unable to parse interval line", true);
                 return false;
             }
         }
@@ -346,7 +356,7 @@ bool Loader::load(const QUrl &url)
                     QString temp = nameValuePair.at(1);
                     PwmFan *pwmPointer = getPwmFan(getEntryNumbers(pwm));
                     Temp *tempPointer = getTemp(getEntryNumbers(temp));
-
+                    
                     if (pwmPointer && tempPointer)
                     {
                         pwmPointer->setTemp(tempPointer);
@@ -421,7 +431,7 @@ bool Loader::save(const QUrl &url)
 
     else
     {
-        setError(url.toDisplayString() + " is not a local file!");
+        setError(url.toDisplayString() + " is not a local file!", true);
         return false;
     }
 
@@ -446,7 +456,7 @@ bool Loader::save(const QUrl &url)
         if (!reply->exec())
         {
             qDebug() << reply->error();
-            setError(reply->errorString() + reply->errorText());
+            setError(reply->errorString() + reply->errorText(), true);
             return false;
         }
     }
@@ -629,7 +639,7 @@ void Loader::handleDetectSensorsResult(KJob *job)
     if (job->error())
     {
         qDebug() << job->error();
-        setError(job->errorString() + job->errorText());
+        setError(job->errorString() + job->errorText(), true);
     }
     else
         parseHwmons();
@@ -665,11 +675,18 @@ QList<QObject *> Loader::allTemps() const
     return list;
 }
 
-void Loader::setError (const QString &error)
+void Loader::setError (const QString &error, bool critical)
 {
     m_error = error;
     emit errorChanged();
-    qCritical() << error;
+    
+    if (critical)
+    {
+        qCritical() << error;
+        emit criticalError();
+    }
+    else
+        qWarning() << error;
 }
 
 }

+ 2 - 1
lib/src/loader.h

@@ -84,7 +84,7 @@ protected slots:
     void createConfigFile();
     void emitAllPwmFansChanged() { emit allPwmFansChanged(); }
     void emitAllTempsChanged() { emit allTempsChanged(); }
-    void setError(const QString &error);
+    void setError(const QString &error, bool critical = false);
     void handleDetectSensorsResult(KJob *job);
 
 
@@ -117,6 +117,7 @@ signals:
     void allPwmFansChanged();
     void allTempsChanged();
     void invalidConfigUrl();
+    void criticalError();
 };
 
 }

+ 3 - 0
lib/src/pwmfan.cpp

@@ -121,6 +121,9 @@ void PwmFan::reset()
 {
     Fan::reset();
     
+    setHasTemp(false);
+    setTemp(Q_NULLPTR);
+    
     QIODevice *oldFile = m_pwmStream->device();
     delete m_pwmStream;
     delete oldFile;

+ 19 - 19
lib/src/systemdcommunicator.cpp

@@ -66,7 +66,7 @@ const QDBusArgument& operator >>(const QDBusArgument &argument, SystemdUnitFile
 namespace Fancontrol
 {
 
-SystemdCommunicator::SystemdCommunicator(const QString &serviceName, QObject *parent) : QObject(parent),
+SystemdCommunicator::SystemdCommunicator(QObject *parent, const QString &serviceName) : QObject(parent),
     m_error(""),
     m_managerInterface(new QDBusInterface("org.freedesktop.systemd1",
                                           "/org/freedesktop/systemd1",
@@ -77,7 +77,7 @@ SystemdCommunicator::SystemdCommunicator(const QString &serviceName, QObject *pa
 {
     if (serviceName.isEmpty())
         setServiceName(STANDARD_SERVICE_NAME);
-    else 
+    else
         setServiceName(serviceName);
 }
 
@@ -96,7 +96,7 @@ void SystemdCommunicator::setServiceName(const QString &name)
             m_serviceInterface->deleteLater();
             m_serviceInterface = Q_NULLPTR;
         }
-        
+
         m_serviceName = name;
 
         if (serviceExists())
@@ -127,7 +127,7 @@ void SystemdCommunicator::setServiceName(const QString &name)
                                                      SLOT(updateServiceProperties(QString, QVariantMap, QStringList)));
             }
         }
-    
+
         emit serviceNameChanged();
         emit serviceEnabledChanged();
         emit serviceActiveChanged();
@@ -141,7 +141,7 @@ bool SystemdCommunicator::serviceExists()
         if (m_serviceInterface->isValid())
             return true;
     }
-    
+
     QDBusMessage dbusreply;
 
     if (m_managerInterface && m_managerInterface->isValid())
@@ -219,21 +219,21 @@ bool SystemdCommunicator::setServiceEnabled(bool enabled)
 bool SystemdCommunicator::setServiceActive(bool active)
 {
     qDebug() << "Set service active:" << active;
-       
+
     if (serviceExists())
     {
         if (active != serviceActive())
         {
             QVariantList args = QVariantList() << m_serviceName + ".service" << "replace";
             QString action = active ? "ReloadOrRestartUnit" : "StopUnit";
-            
+
             if (dbusAction(action, args))
             {
                 emit serviceActiveChanged();
                 return true;
             }
         }
-        
+
         return true;
     }
     return false;
@@ -257,7 +257,7 @@ bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
     }
 
     if (dbusreply.type() == QDBusMessage::ErrorMessage)
-    {      
+    {
         if (dbusreply.errorMessage() == "Interactive authentication required.")
         {
             KAuth::Action action = newFancontrolAction();
@@ -270,35 +270,35 @@ bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
             KAuth::ExecuteJob *job = action.execute();
             connect(job, SIGNAL(result(KJob*)), this, SLOT(handleDbusActionResult(KJob*)));
             job->start();
-            
+
             return true;
         }
         setError(dbusreply.errorMessage());
         return false;
     }
-    
+
     return true;
 }
 
 void SystemdCommunicator::handleDbusActionResult(KJob *job)
-{   
+{
     if (job->error())
     {
         if (job->error() == KAuth::ActionReply::HelperBusyError)
         {
             qDebug() << "Helper busy...";
-            
+
             KAuth::ExecuteJob *executeJob = static_cast<KAuth::ExecuteJob *>(job);
             if (executeJob)
             {
                 KAuth::ExecuteJob *newJob = executeJob->action().execute();
                 connect(newJob, SIGNAL(result(KJob*)), this, SLOT(handleDbusActionResult(KJob*)));
-                
+
                 QTimer::singleShot(50, this, [newJob] (){ newJob->start(); });
                 return;
             }
         }
-        
+
         setError(job->errorText());
     }
 }
@@ -311,7 +311,7 @@ bool SystemdCommunicator::restartService()
         args << m_serviceName + ".service" << "replace";
         return dbusAction("ReloadOrRestartUnit", args);
     }
-    
+
     setError("Service doesn't exist");
     return false;
 }
@@ -328,12 +328,12 @@ void SystemdCommunicator::updateServiceProperties(QString, QVariantMap propchang
 void SystemdCommunicator::setError(const QString &error)
 {
     qCritical() << error;
-    
+
     if (error != m_error)
     {
-        m_error = error; 
+        m_error = error;
         emit errorChanged();
     }
 }
 
-}
+}

+ 1 - 1
lib/src/systemdcommunicator.h

@@ -42,7 +42,7 @@ class FANCONTROL_GUI_LIB_EXPORT SystemdCommunicator : public QObject
 
 public:
 
-    explicit SystemdCommunicator(const QString &serviceName = QString(), QObject *parent = Q_NULLPTR);
+    explicit SystemdCommunicator(QObject *parent = Q_NULLPTR, const QString &serviceName = QString());
 
     QString serviceName() const { return m_serviceName; }
     void setServiceName(const QString &name);

+ 44 - 14
package/contents/ui/KCM.qml

@@ -229,26 +229,56 @@ Item {
                 onTextChanged: base.serviceName = text
             }
         }
+        RowLayout {
+            visible: expand.expanded
+            
+            Label {
+                Layout.preferredWidth: root.textWidth
+                clip: true
+                text: i18n("Path to the fancontrol config file:")
+                horizontalAlignment: Text.AlignRight
+                Component.onCompleted: root.textWidth = Math.max(root.textWidth, contentWidth)
+            }
+            OptionInput {
+                Layout.minimumWidth: implicitWidth
+                Layout.fillWidth: true
+                text: base.configUrl.toString().replace("file://", "")
+                color: base.configValid ? "green" : "red"
+                onTextChanged: base.configUrl = text
+            }
+            Button {
+                action: loadAction 
+            }
+        }
     }
     
-//     FileDialog {
-//         id: openFileDialog
-//         title: i18n("Please choose a configuration file")
-//         folder: "file:///etc"
-//         selectExisting: true
-//         selectMultiple: false
-//         modality: Qt.NonModal
-//         
-//         onAccepted: {
-//             base.loader.load(fileUrl);
-//         }
-//     }
+    Action {
+        id: loadAction
+        iconName: "document-open"
+        onTriggered: openFileDialog.open()
+        tooltip: i18n("Load configuration file")
+        shortcut: StandardKey.Open
+    }
+    
+    FileDialog {
+        id: openFileDialog
+        title: i18n("Please choose a configuration file")
+        folder: "file:///etc"
+        selectExisting: true
+        selectMultiple: false
+        
+        onAccepted: {
+            base.configUrl = fileUrl;
+        }
+    }
     
     ErrorDialog {
         id: errorDialog
-        visible: !!base.loader.error
         modality: Qt.ApplicationModal
         text: base.loader.error
-        onTextChanged: show()
+    }
+    Connections {
+        target: base.loader
+        onCriticalError: errorDialog.open()
     }
 }