Răsfoiți Sursa

added setting to enable/disable the fancontrol service

Malte Veerman 10 ani în urmă
părinte
comite
f17e68d716

+ 1 - 1
fancontrol-gui/qml/fancontrol-gui.qml

@@ -160,7 +160,7 @@ ApplicationWindow {
             color: "red"
 
             onSystemdErrorChanged: {
-                if (systemdError !== "Success")
+                if (systemdError !== "Success" && systemdError.search("succeeded") == -1)
                     text = systemdError;
                 else if (loaderError === "Success" || loaderError === "")
                     text = ""

+ 1 - 2
helper/src/helper.cpp

@@ -28,7 +28,7 @@
 ActionReply Helper::dbusaction(const QVariantMap &arguments)
 {
     QString method = arguments["method"].toString();
-    QList<QVariant> argsForCall = arguments["arguments"].toList();
+    QVariantList argsForCall = arguments["arguments"].toList();
 
     ActionReply reply;
 
@@ -59,7 +59,6 @@ ActionReply Helper::dbusaction(const QVariantMap &arguments)
 }
 #endif
 
-
 ActionReply Helper::read(const QVariantMap &args)
 {
     ActionReply reply;

+ 1 - 0
helper/src/helper.h

@@ -18,6 +18,7 @@
  */
 
 #include <KAuth>
+#include <QtDBus/QtDBus>
 
 using namespace KAuth;
 

+ 22 - 0
share/qml/SettingsTab.qml

@@ -158,6 +158,28 @@ Item {
                     onTextChanged: systemdCom.serviceName = text
                 }
             }
+            RowLayout {
+                width: parent.width
+                visible: typeof systemdCom != "undefined"
+
+                Text {
+                    anchors.left: parent.left
+                    anchors.leftMargin: padding
+                    Layout.maximumWidth: parent.width - maxTempValue.width - padding*2
+                    clip: true
+                    text: "Fancontrol systemd service autostart:"
+                }
+                ComboBox {
+                    id: autostartBox
+                    anchors.right: parent.right
+                    anchors.rightMargin: padding
+                    model: ["disabled" , "enabled"]
+                    currentIndex: systemdCom.serviceEnabled ? 1 : 0
+                    onCurrentIndexChanged: {
+                        systemdCom.serviceEnabled = (currentIndex == 1) ? true : false;
+                    }
+                }
+            }
         }
     }
 }

+ 52 - 10
share/src/systemdcommunicator.cpp

@@ -45,41 +45,43 @@ void SystemdCommunicator::setServiceName(const QString &name)
     if (name != m_serviceName)
     {
         m_serviceName = name;
-        emit serviceNameChanged();
 
         if (serviceExists())
         {
-            QList<QVariant> arguments;
+            QVariantList arguments;
             arguments << QVariant(m_serviceName + ".service");
             QDBusMessage dbusreply = m_managerInterface->callWithArgumentList(QDBus::AutoDetect, "LoadUnit", arguments);
             if (dbusreply.type() == QDBusMessage::ErrorMessage)
             {
                 m_error = dbusreply.errorMessage();
                 emit errorChanged();
-                m_servicePath.clear();
+                m_serviceObjectPath.clear();
             }
             else
             {
-                m_servicePath = qdbus_cast<QDBusObjectPath>(dbusreply.arguments().first()).path();
+                m_serviceObjectPath = qdbus_cast<QDBusObjectPath>(dbusreply.arguments().first()).path();
 
                 if (m_serviceInterface)
                     m_serviceInterface->deleteLater();
 
                 m_serviceInterface = new QDBusInterface("org.freedesktop.systemd1",
-                                                        m_servicePath,
+                                                        m_serviceObjectPath,
                                                         "org.freedesktop.systemd1.Unit",
                                                         QDBusConnection::systemBus(),
                                                         this);
             }
         }
     }
+
+    emit serviceNameChanged();
+    emit serviceEnabledChanged();
 }
 
 bool SystemdCommunicator::serviceExists()
 {
     QDBusMessage dbusreply;
 
-    if (m_managerInterface->isValid())
+    if (m_managerInterface && m_managerInterface->isValid())
         dbusreply = m_managerInterface->call(QDBus::AutoDetect, "ListUnitFiles");
 
     if (dbusreply.type() == QDBusMessage::ErrorMessage)
@@ -117,12 +119,52 @@ bool SystemdCommunicator::serviceActive()
     return false;
 }
 
-void SystemdCommunicator::dbusAction(const QString &method, const QList<QVariant> &arguments)
+bool SystemdCommunicator::serviceEnabled()
+{
+    if (m_serviceInterface && m_serviceInterface->isValid())
+    {
+        if (m_serviceInterface->property("UnitFileState").toString() == "enabled")
+        {
+            return true;
+        }
+    }
+    return false;
+}
+
+void SystemdCommunicator::setServiceEnabled(bool enabled)
+{
+    if (enabled != serviceEnabled() && serviceExists())
+    {
+        QVariantList arguments;
+        QStringList files = QStringList() << m_serviceName + ".service";
+
+        if (enabled)
+        {
+            arguments << files << false << true;
+            dbusAction("EnableUnitFiles", arguments);
+            dbusAction("Reload");
+        }
+        else
+        {
+            arguments << files << false;
+            dbusAction("DisableUnitFiles", arguments);
+            dbusAction("Reload");
+        }
+        emit serviceEnabledChanged();
+    }
+}
+
+void SystemdCommunicator::dbusAction(const QString &method, const QVariantList &arguments)
 {
     QDBusMessage dbusreply;
 
     if (m_managerInterface && m_managerInterface->isValid())
-        dbusreply = m_managerInterface->callWithArgumentList(QDBus::AutoDetect, method, arguments);
+    {
+        if (arguments.isEmpty())
+            dbusreply = m_managerInterface->call(QDBus::AutoDetect, method);
+        else
+            dbusreply = m_managerInterface->callWithArgumentList(QDBus::AutoDetect, method, arguments);
+    }
 
     if (dbusreply.type() == QDBusMessage::ErrorMessage)
     {
@@ -145,7 +187,7 @@ void SystemdCommunicator::dbusAction(const QString &method, const QList<QVariant
             }
             else
             {
-                m_error = "Success";
+                m_error = method + " succeeded";
                 emit errorChanged();
             }
             return;
@@ -156,7 +198,7 @@ void SystemdCommunicator::dbusAction(const QString &method, const QList<QVariant
     }
     else
     {
-        m_error = "Success";
+        m_error = method + " succeeded";
         emit errorChanged();
     }
 }

+ 7 - 3
share/src/systemdcommunicator.h

@@ -31,6 +31,7 @@ class FANCONTROL_GUI_EXPORT SystemdCommunicator : public QObject
     Q_PROPERTY(QString serviceName READ serviceName WRITE setServiceName NOTIFY serviceNameChanged)
     Q_PROPERTY(QString error READ error NOTIFY errorChanged)
     Q_PROPERTY(bool serviceExists READ serviceExists NOTIFY serviceNameChanged)
+    Q_PROPERTY(bool serviceEnabled READ serviceEnabled WRITE setServiceEnabled NOTIFY serviceEnabledChanged)
 
 public:
 
@@ -39,22 +40,25 @@ public:
     QString serviceName() const { return m_serviceName; }
     void setServiceName(const QString &name);
     bool serviceExists();
+    bool serviceEnabled();
+    void setServiceEnabled(bool enabled);
     Q_INVOKABLE bool serviceActive();
     void setServiceActive(bool active);
     QString error() const { return m_error; }
-    Q_INVOKABLE void dbusAction(const QString &method, const QList<QVariant> &arguments);
+    Q_INVOKABLE void dbusAction(const QString &method, const QVariantList &arguments = QVariantList());
 
 
 signals:
 
     void serviceNameChanged();
+    void serviceEnabledChanged();
     void errorChanged();
 
 
 protected:
 
     QString m_serviceName;
-    QString m_servicePath;
+    QString m_serviceObjectPath;
     QString m_error;
     QDBusInterface *m_managerInterface;
     QDBusInterface *m_serviceInterface;
@@ -65,8 +69,8 @@ typedef struct
     QString path;
     QString state;
 } SystemdUnitFile;
-
 Q_DECLARE_METATYPE(SystemdUnitFile)
+
 typedef QList<SystemdUnitFile> SystemdUnitFileList;
 Q_DECLARE_METATYPE(SystemdUnitFileList)