Przeglądaj źródła

seperated construction of action

Malte Veerman 9 lat temu
rodzic
commit
ba67d8ef15

+ 48 - 0
lib/src/fancontrolaction.h

@@ -0,0 +1,48 @@
+/*
+ * <one line to give the library's name and an idea of what it does.>
+ * Copyright 2015  Malte Veerman maldela@halloarsch.de
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License or (at your option) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+#ifndef FANCONTROLACTION_H
+#define FANCONTROLACTION_H
+
+
+#include <QtCore/QDebug>
+#include <KAuth/KAuthAction>
+
+
+namespace Fancontrol
+{
+
+inline KAuth::Action newFancontrolAction()
+{
+    KAuth::Action action("fancontrol.gui.helper.action");
+    action.setHelperId("fancontrol.gui.helper");
+    
+    if (!action.isValid())
+        qDebug() << "Action is invalid!";
+    
+    return action;
+}
+
+}
+
+#endif // FANCONTROLACTION_H

+ 4 - 7
lib/src/loader.cpp

@@ -21,6 +21,7 @@
 #include "loader.h"
 
 #include "hwmon.h"
+#include "fancontrolaction.h"
 
 #include <QtCore/QFile>
 #include <QtCore/QDir>
@@ -245,8 +246,7 @@ bool Loader::load(const QUrl &url)
     }
     else if (file.exists())
     {
-        KAuth::Action action("fancontrol.gui.helper.action");
-        action.setHelperId("fancontrol.gui.helper");
+        KAuth::Action action = newFancontrolAction();
         QVariantMap map;
         map["action"] = "read";
         map["filename"] = fileName;
@@ -413,9 +413,7 @@ bool Loader::save(const QUrl &url)
     }
     else
     {
-        KAuth::Action action("fancontrol.gui.helper.action");
-        action.setHelperId("fancontrol.gui.helper");
-
+        KAuth::Action action = newFancontrolAction();
         QVariantMap map;
         map["action"] = "write";
         map["filename"] = fileName;
@@ -594,8 +592,7 @@ void Loader::abortTestingFans()
 
 void Loader::detectSensors()
 {
-    KAuth::Action action("fancontrol.gui.helper.action");
-    action.setHelperId("fancontrol.gui.helper");
+    KAuth::Action action = newFancontrolAction();
     QVariantMap map;
     map["action"] = "detectSensors";
 

+ 6 - 29
lib/src/pwmfan.cpp

@@ -24,6 +24,7 @@
 #include "pwmfan.h"
 
 #include "hwmon.h"
+#include "fancontrolaction.h"
 
 #include <QtCore/QTextStream>
 #include <QtCore/QTimer>
@@ -172,14 +173,7 @@ bool PwmFan::setPwm(int pwm, bool write)
                 *m_pwmStream << pwm;
             else
             {
-                KAuth::Action action("fancontrol.gui.helper.action");
-                action.setHelperId("fancontrol.gui.helper");
-                if (!action.isValid())
-                {
-                    emit errorChanged(i18n("Action is invalid"));
-                    return false;
-                }
-                
+                KAuth::Action action = newFancontrolAction();
                 QVariantMap map;
                 map["action"] = "write";
                 map["filename"] = qobject_cast<QFile *>(m_pwmStream->device())->fileName();
@@ -226,13 +220,7 @@ bool PwmFan::setPwmMode(int pwmMode, bool write)
 
             else
             {
-                KAuth::Action action("fancontrol.gui.helper.action");
-                action.setHelperId("fancontrol.gui.helper");
-                if (!action.isValid())
-                {
-                    emit errorChanged(i18n("Action is invalid"));
-                    return false;
-                }
+                KAuth::Action action = newFancontrolAction();
                 
                 QVariantMap map;
                 map["action"] = "write";
@@ -268,13 +256,7 @@ void PwmFan::handleSetPwmModeResult(KJob *job)
 
 void PwmFan::test()
 {
-    KAuth::Action action("fancontrol.gui.helper.action");
-    action.setHelperId("fancontrol.gui.helper");
-    if (!action.isValid())
-    {
-        emit errorChanged("Test action is invalid");
-        return;
-    }
+    KAuth::Action action = newFancontrolAction();
     KAuth::ExecuteJob *job = action.execute();
     connect(job, SIGNAL(result(KJob*)), this, SLOT(handleTestAuthReply(KJob*)));
     job->start();
@@ -324,13 +306,8 @@ void PwmFan::abortTest()
 
 void PwmFan::continueTest()
 {
-    KAuth::Action action("fancontrol.gui.helper.action");
-    action.setHelperId("fancontrol.gui.helper");
-    if (!action.isValid())
-    {
-        emit errorChanged("Test action is invalid");
-        return;
-    }
+    KAuth::Action action = newFancontrolAction();
+    
     if (action.status() != KAuth::Action::AuthorizedStatus)
     {
         m_testStatus = Error;

+ 3 - 2
lib/src/systemdcommunicator.cpp

@@ -20,6 +20,8 @@
 
 #include "systemdcommunicator.h"
 
+#include "fancontrolaction.h"
+
 #include <QtCore/QDebug>
 #include <QtCore/QVariant>
 #include <QtCore/QTimer>
@@ -253,8 +255,7 @@ bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
     {      
         if (dbusreply.errorMessage() == "Interactive authentication required.")
         {
-            KAuth::Action action("fancontrol.gui.helper.action");
-            action.setHelperId("fancontrol.gui.helper");
+            KAuth::Action action = newFancontrolAction();
             QVariantMap map;
             map["action"] = "dbusaction";
             map["method"] = method;