2
0
Эх сурвалжийг харах

little improvements everywhere. kcm slowly starts working :)

Malte Veerman 10 жил өмнө
parent
commit
cb7192f12b

+ 7 - 1
fancontrol-gui/src/gui.cpp

@@ -23,7 +23,13 @@
 
 #include <QtQml>
 
-GUI::GUI(QObject *parent) : QObject(parent)
+GUI::GUI(QObject *parent) : QObject(parent),
+    
+#ifndef NO_SYSTEMD
+    m_com(new SystemdCommunicator(this)),
+#endif
+
+    m_loader(new Loader(this))
 {
     m_loader->load(QUrl::fromLocalFile("/etc/fancontrol"));
     

+ 3 - 4
fancontrol-gui/src/gui.h

@@ -55,13 +55,12 @@ public:
 
     
 protected:
-    
-    Loader *const m_loader = new Loader(this);
-    
+        
 #ifndef NO_SYSTEMD
-    SystemdCommunicator *const m_com = new SystemdCommunicator(this);
+    SystemdCommunicator *const m_com;
 #endif
     
+    Loader *const m_loader;    
 };
 
 #endif // GUI_H

+ 33 - 11
kcm/src/fancontrolkcm.cpp

@@ -33,10 +33,15 @@
 
 K_PLUGIN_FACTORY_WITH_JSON(FancontrolKCMFactory, "kcm_fancontrol.json", registerPlugin<FancontrolKCM>();)
 
-FancontrolKCM::FancontrolKCM(QObject *parent, const QVariantList& args) : ConfigModule(parent, args)
-{
-    KLocalizedString::setApplicationDomain("fancontrol-gui");
+FancontrolKCM::FancontrolKCM(QObject *parent, const QVariantList& args)
+    : ConfigModule(parent, args),
 
+#ifndef NO_SYSTEMD
+    m_communicator(new SystemdCommunicator(this)),
+#endif
+
+    m_loader(new Loader(this))
+{
     KAboutData *about = new KAboutData("kcm_fancontrol",
                                        i18n("Fancontrol-KCM"),
                                        "0.1",
@@ -49,10 +54,11 @@ FancontrolKCM::FancontrolKCM(QObject *parent, const QVariantList& args) : Config
     about->addAuthor(i18n("Malte Veerman"), i18n("Main Developer"), "maldela@halloarsch.de"); 
     setAboutData(about);
     
-    setButtons(FancontrolKCM::Apply);
+    setButtons(Apply | Default);
     setAuthActionName("fancontrol.gui.helper.action");
     
-    connect(m_loader, SIGNAL(configFileChanged()), this, SLOT(setNeedsSave()));
+    connect(m_loader, &Loader::configFileChanged, [this] () { setNeedsSave(true);
+							      qDebug() << "Changes made..."; });
     
     qmlRegisterType<Loader>();
     qmlRegisterType<Hwmon>();
@@ -63,23 +69,39 @@ FancontrolKCM::FancontrolKCM(QObject *parent, const QVariantList& args) : Config
 #ifndef NO_SYSTEMD
     qmlRegisterType<SystemdCommunicator>();
 #endif
+    
+}
+
+FancontrolKCM::~FancontrolKCM() 
+{
 }
 
 void FancontrolKCM::save()
 {
     qDebug() << "saving...";
-    setNeedsSave(false);
-    m_loader->save();
-    
+    setNeedsSave(!m_loader->save()
+
 #ifndef NO_SYSTEMD
-    m_communicator->restartService();
+		 || !m_communicator->restartService()
 #endif
+
+		);
 }
 
 void FancontrolKCM::load()
 {
-    setNeedsSave(false);
-    m_loader->load(QUrl::fromLocalFile("/etc/fancontrol"));
+    setNeedsSave(!m_loader->load(QUrl::fromLocalFile("/etc/fancontrol")));
+    qDebug() << "Loaded config file";
+}
+
+void FancontrolKCM::defaults() 
+{
+    
+#ifndef NO_SYSTEMD
+    m_communicator->setServiceEnabled(false);
+    m_communicator->setServiceActive(false);    
+#endif
+    
 }
 
 

+ 7 - 8
kcm/src/fancontrolkcm.h

@@ -49,6 +49,7 @@ class FancontrolKCM : public ConfigModule
 public:
     
     explicit FancontrolKCM(QObject *parent, const QVariantList &args = QVariantList());
+    ~FancontrolKCM() Q_DECL_OVERRIDE;
     
     Loader *loader() const { return m_loader; }
         
@@ -63,18 +64,16 @@ public slots:
     
     void load() Q_DECL_OVERRIDE;
     void save() Q_DECL_OVERRIDE;
-//     void defaults() { load(); }
-    
-    void setNeedsSave(bool needs = true) { ConfigModule::setNeedsSave(needs); qDebug() << "Needs save: " << needs; }
-    
+    void defaults() Q_DECL_OVERRIDE;
+        
     
 protected:
-    
-    Loader *const m_loader = new Loader(this);
-    
+        
 #ifndef NO_SYSTEMD
-    SystemdCommunicator *const m_communicator = new SystemdCommunicator(this);
+    SystemdCommunicator *const m_communicator;
 #endif
+    
+    Loader *const m_loader;
 };
 
 #endif // FANCONTROLKCM_H

+ 276 - 198
lib/src/loader.cpp

@@ -28,12 +28,11 @@
 
 #define HWMON_PATH "/sys/class/hwmon"
 
-Loader::Loader(QObject *parent) : QObject(parent)
+Loader::Loader(QObject *parent) : QObject(parent),
+    m_interval(10),
+    m_configUrl(QUrl::fromLocalFile("/etc/fancontrol")),
+    m_error("Success")
 {
-    m_configUrl = QUrl::fromLocalFile("/etc/fancontrol");
-    m_interval = 10;
-    m_error = "Success";
-    
     parseHwmons();
     
     m_timer.setSingleShot(false);
@@ -74,7 +73,7 @@ void Loader::parseHwmons()
     emit allPwmFansChanged();
 }
 
-void Loader::load(const QUrl &url)
+bool Loader::load(const QUrl &url)
 {
     QString fileName;
     if (url.isEmpty())
@@ -82,20 +81,24 @@ void Loader::load(const QUrl &url)
         qDebug() << "Given empty url. Fallback to " << m_configUrl;
         fileName = m_configUrl.toLocalFile();
     }
-    else
+    else if (url.isLocalFile())
         fileName = url.toLocalFile();
+    else
+    {
+	setError("Url is not a local file");
+	return false;
+    }
     
-    QFile file(fileName);
     QTextStream stream;
-    QString string;
-    QStringList lines;
+    QFile file(fileName);
 
     if (file.open(QFile::ReadOnly | QFile::Text))
     {
         stream.setDevice(&file);
-        m_error = "Success";
-        emit errorChanged();
+	m_configFile = stream.readAll();
+	emit configFileChanged();
     }
+	
     else if (file.exists())
     {
         KAuth::Action action("fancontrol.gui.helper.action");
@@ -107,25 +110,24 @@ void Loader::load(const QUrl &url)
         KAuth::ExecuteJob *reply = action.execute();
         if (!reply->exec())
         {
-            m_error = reply->errorString();
-            emit errorChanged();
-            return;
+            setError(reply->errorString());
+            return false;
         }
         else
         {
-            m_error = "Success";
-            emit errorChanged();
-            string = reply->data()["content"].toString();
-            stream.setString(&string);
+            m_configFile = reply->data()["content"].toString();
+	    emit configFileChanged();
         }
     }
-    m_configFile = stream.readAll();
-    emit configFileChanged();
+    else
+    {
+	setError("File does not exist"); 
+	return false;
+    }
+    
     m_configUrl = url;
     emit configUrlChanged();
 
-    stream.seek(0);
-
     foreach (Hwmon *hwmon, m_hwmons)
     {
         foreach (QObject *pwmFan, hwmon->pwmFans())
@@ -133,7 +135,9 @@ void Loader::load(const QUrl &url)
             qobject_cast<PwmFan *>(pwmFan)->reset();
         }
     }
-    
+
+    stream.setString(&m_configFile);
+    QStringList lines;
     do
     {
         QString line(stream.readLine());
@@ -148,150 +152,197 @@ void Loader::load(const QUrl &url)
     foreach (QString line, lines)
     {
         if (line.startsWith("INTERVAL="))
-        {
-            setInterval(line.remove("INTERVAL=").toInt());
-        }
+	{
+	    line.remove("INTERVAL=");
+	    bool success;
+	    int interval = line.toInt(&success);
+	    if (success)
+		setInterval(interval, false);
+	    
+	    else
+	    {
+		setError("Unable to parse interval line");
+		return false;
+	    }
+	}
         else if (line.startsWith("FCTEMPS="))
         {
+	    line.remove("FCTEMPS=");
             QStringList fctemps = line.split(' ');
-            if (!fctemps.isEmpty())
-                fctemps.first().remove("FCTEMPS=");
             foreach (QString fctemp, fctemps)
             {
-                QString pwm = fctemp.split('=').at(0);
-                QString temp = fctemp.split('=').at(1);
-                int pwmSensorIndex = getSensorNumber(pwm);
-                int tempSensorIndex = getSensorNumber(temp);
-                Hwmon *pwmHwmon = m_hwmons.value(getHwmonNumber(pwm), Q_NULLPTR);
-
-                if (pwmHwmon)
-                {
-                    Hwmon *tempHwmon = m_hwmons.value(getHwmonNumber(temp), Q_NULLPTR);
-                    PwmFan *pwmPointer = pwmHwmon->pwmFan(pwmSensorIndex);
-                    if (tempHwmon)
-                    {
-                        Temp *tempPointer = tempHwmon->temp(tempSensorIndex);
-
-                        if (pwmPointer)
-                        {
-                            pwmPointer->setTemp(tempPointer);
-                            pwmPointer->setMinPwm(0);
-                        }
-                    }
-                    else if (pwmPointer)
-                        pwmPointer->setTemp(Q_NULLPTR);
-                }
+		QStringList nameValuePair = fctemp.split('=');
+		if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    QString temp = nameValuePair.at(1);
+		    int pwmSensorIndex = getSensorNumber(pwm);
+		    int tempSensorIndex = getSensorNumber(temp);
+		    Hwmon *pwmHwmon = m_hwmons.value(getHwmonNumber(pwm), Q_NULLPTR);
+
+		    if (pwmHwmon)
+		    {
+			Hwmon *tempHwmon = m_hwmons.value(getHwmonNumber(temp), Q_NULLPTR);
+			PwmFan *pwmPointer = pwmHwmon->pwmFan(pwmSensorIndex);
+			if (tempHwmon)
+			{
+			    Temp *tempPointer = tempHwmon->temp(tempSensorIndex);
+
+			    if (pwmPointer)
+			    {
+				pwmPointer->setTemp(tempPointer);
+				pwmPointer->setMinPwm(0);
+			    }
+			}
+			else if (pwmPointer)
+			    pwmPointer->setTemp(Q_NULLPTR);
+		    }
+		}
             }
         }
         else if (line.startsWith("MINTEMP="))
         {
+	    line.remove("MINTEMP=");
             QStringList mintemps = line.split(' ');
-            if (!mintemps.isEmpty())
-                mintemps.first().remove("MINTEMP=");
             foreach (QString mintemp, mintemps)
             {
-                QString pwm = mintemp.split('=').at(0);
-                int value = mintemp.split('=').at(1).toInt();
-                int pwmHwmon = getHwmonNumber(pwm);
-                int pwmSensor = getSensorNumber(pwm);
-                PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
-                if (pwmPointer)
-                    pwmPointer->setMinTemp(value);
+		QStringList nameValuePair = mintemp.split('=');
+                if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    int value = nameValuePair.at(1).toInt();
+		    int pwmHwmon = getHwmonNumber(pwm);
+		    int pwmSensor = getSensorNumber(pwm);
+		    PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
+		    if (pwmPointer)
+			pwmPointer->setMinTemp(value);
+		}
             }
         }
         else if (line.startsWith("MAXTEMP="))
         {
+	    line.remove("MAXTEMP=");
             QStringList maxtemps = line.split(' ');
-            if (!maxtemps.isEmpty())
-                maxtemps.first().remove("MAXTEMP=");
             foreach (QString maxtemp, maxtemps)
             {
-                QString pwm = maxtemp.split('=').at(0);
-                int value = maxtemp.split('=').at(1).toInt();
-                int pwmHwmon = getHwmonNumber(pwm);
-                int pwmSensor = getSensorNumber(pwm);
-                PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
-                if (pwmPointer)
-                    pwmPointer->setMaxTemp(value);
+		QStringList nameValuePair = maxtemp.split('=');
+                if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    int value = nameValuePair.at(1).toInt();
+		    int pwmHwmon = getHwmonNumber(pwm);
+		    int pwmSensor = getSensorNumber(pwm);
+		    PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
+		    if (pwmPointer)
+			pwmPointer->setMaxTemp(value);
+		}
             }
         }
         else if (line.startsWith("MINSTART="))
         {
+	    line.remove("MINSTART=");
             QStringList minstarts = line.split(' ');
-            if (!minstarts.isEmpty())
-                minstarts.first().remove("MINSTART=");
             foreach (QString minstart, minstarts)
             {
-                QString pwm = minstart.split('=').at(0);
-                int value = minstart.split('=').at(1).toInt();
-                int pwmHwmon = getHwmonNumber(pwm);
-                int pwmSensor = getSensorNumber(pwm);
-                PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
-                if (pwmPointer)
-                    pwmPointer->setMinStart(value);
+		QStringList nameValuePair = minstart.split('=');
+                if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    int value = nameValuePair.at(1).toInt();
+		    int pwmHwmon = getHwmonNumber(pwm);
+		    int pwmSensor = getSensorNumber(pwm);
+		    PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
+		    if (pwmPointer)
+			pwmPointer->setMinStart(value);
+		}
             }
         }
         else if (line.startsWith("MINSTOP="))
         {
+	    line.remove("MINSTOP=");
             QStringList minstops = line.split(' ');
-            if (!minstops.isEmpty())
-                minstops.first().remove("MINSTOP=");
             foreach (QString minstop, minstops)
             {
-                QString pwm = minstop.split('=').at(0);
-                int value = minstop.split('=').at(1).toInt();
-                int pwmHwmon = getHwmonNumber(pwm);
-                int pwmSensor = getSensorNumber(pwm);
-                PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
-                if (pwmPointer)
-                    pwmPointer->setMinStop(value);
+		QStringList nameValuePair = minstop.split('=');
+                if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    int value = nameValuePair.at(1).toInt();
+		    int pwmHwmon = getHwmonNumber(pwm);
+		    int pwmSensor = getSensorNumber(pwm);
+		    PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
+		    if (pwmPointer)
+			pwmPointer->setMinStop(value);
+		}
             }
         }
         else if (line.startsWith("MINPWM="))
         {
+	    line.remove("MINPWM=");
             QStringList minpwms = line.split(' ');
-            if (!minpwms.isEmpty())
-                minpwms.first().remove("MINPWM=");
             foreach (QString minpwm, minpwms)
             {
-                QString pwm = minpwm.split('=').at(0);
-                int value = minpwm.split('=').at(1).toInt();
-                int pwmHwmon = getHwmonNumber(pwm);
-                int pwmSensor = getSensorNumber(pwm);
-                PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
-                if (pwmPointer)
-                    pwmPointer->setMinPwm(value);
+		QStringList nameValuePair = minpwm.split('=');
+                if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    int value = nameValuePair.at(1).toInt();
+		    int pwmHwmon = getHwmonNumber(pwm);
+		    int pwmSensor = getSensorNumber(pwm);
+		    PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
+		    if (pwmPointer)
+			pwmPointer->setMinPwm(value);
+		}
             }
         }
         else if (line.startsWith("MAXPWM="))
         {
+	    line.remove("MAXPWM=");
             QStringList maxpwms = line.split(' ');
-            if (!maxpwms.isEmpty())
-                maxpwms.first().remove("MAXPWM=");
             foreach (QString maxpwm, maxpwms)
             {
-                QString pwm = maxpwm.split('=').at(0);
-                int value = maxpwm.split('=').at(1).toInt();
-                int pwmHwmon = getHwmonNumber(pwm);
-                int pwmSensor = getSensorNumber(pwm);
-                PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
-                if (pwmPointer)
-                    pwmPointer->setMaxPwm(value);
+		QStringList nameValuePair = maxpwm.split('=');
+                if (nameValuePair.size() == 2)
+		{
+		    QString pwm = nameValuePair.at(0);
+		    int value = nameValuePair.at(1).toInt();
+		    int pwmHwmon = getHwmonNumber(pwm);
+		    int pwmSensor = getSensorNumber(pwm);
+		    PwmFan *pwmPointer = m_hwmons.value(pwmHwmon, Q_NULLPTR)->pwmFan(pwmSensor);
+		    if (pwmPointer)
+			pwmPointer->setMaxPwm(value);
+		}
             }
         }
     }
+    
+    success();
+    return true;
 }
 
-void Loader::save(const QUrl &url)
-{  
-    QString fileName = url.isEmpty() ? m_configUrl.toLocalFile() : url.toLocalFile();
+bool Loader::save(const QUrl &url)
+{
+    QString fileName;
+    if (url.isEmpty())
+    {
+	qDebug() << "Given empty url. Fallback to " << m_configUrl;
+	fileName = m_configUrl.toLocalFile();
+    }
+    else if (url.isLocalFile())
+	fileName = url.toLocalFile();
+    
+    else
+    {
+	setError("Url is not a local file");
+	return false;
+    }
+    
     QFile file(fileName);
     
     if (file.open(QFile::WriteOnly | QFile::Text))
     {
         QTextStream stream(&file);
         stream << m_configFile;
-        qDebug() << m_configFile;
     }
     else
     {
@@ -308,10 +359,13 @@ void Loader::save(const QUrl &url)
 
         if (!reply->exec())
         {
-            m_error = reply->errorString();
-            emit errorChanged();
+            setError(reply->errorString());
+            return false;
         }
     }
+    
+    success();
+    return true;
 }
 
 void Loader::createConfigFile()
@@ -334,102 +388,126 @@ void Loader::createConfigFile()
         }
     }
     
-    m_configFile = "# This file was created by Fancontrol-GUI \n";
-
-    m_configFile += "INTERVAL=" + QString::number(m_interval) + "\n";
-
-    m_configFile += "DEVPATH=";
-    foreach (Hwmon *hwmon, usedHwmons)
-    {
-        QString sanitizedPath = hwmon->path();
-        sanitizedPath.remove(QRegExp("^/sys/"));
-        sanitizedPath.remove(QRegExp("/hwmon/hwmon\\d\\s*$"));
-        m_configFile += "hwmon" + QString::number(hwmon->index()) + "=" + sanitizedPath + " ";
-    }
-    m_configFile += "\n";
+    QString configFile = "# This file was created by Fancontrol-GUI \n";
 
-    m_configFile += "DEVNAME=";
-    foreach (Hwmon *hwmon, usedHwmons)
-    {
-        m_configFile += "hwmon" + QString::number(hwmon->index()) + "=" + hwmon->name().split('.').first() + " ";
-    }
-    m_configFile += "\n";
+    if (m_interval != 0)
+	configFile += "INTERVAL=" + QString::number(m_interval) + "\n";
 
-    m_configFile += "FCTEMPS=";
-    foreach (PwmFan *pwmFan, usedFans)
+    if (!usedHwmons.isEmpty())
     {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += "hwmon" + QString::number(pwmFan->temp()->parent()->index()) + "/";
-        m_configFile += "temp" + QString::number(pwmFan->temp()->index()) + "_input ";
+	configFile += "DEVPATH=";
+	foreach (Hwmon *hwmon, usedHwmons)
+	{
+	    QString sanitizedPath = hwmon->path();
+	    sanitizedPath.remove(QRegExp("^/sys/"));
+	    sanitizedPath.remove(QRegExp("/hwmon/hwmon\\d\\s*$"));
+	    configFile += "hwmon" + QString::number(hwmon->index()) + "=" + sanitizedPath + " ";
+	}
+	configFile += "\n";
+
+	configFile += "DEVNAME=";
+	foreach (Hwmon *hwmon, usedHwmons)
+	{
+	    configFile += "hwmon" + QString::number(hwmon->index()) + "=" + hwmon->name().split('.').first() + " ";
+	}
+	configFile += "\n";
+
+	if (!usedFans.isEmpty())
+	{
+	    configFile += "FCTEMPS=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += "hwmon" + QString::number(pwmFan->temp()->parent()->index()) + "/";
+		configFile += "temp" + QString::number(pwmFan->temp()->index()) + "_input ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "FCFANS=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "fan" + QString::number(pwmFan->index()) + "_input ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "MINTEMP=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += QString::number(pwmFan->minTemp()) + " ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "MAXTEMP=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += QString::number(pwmFan->maxTemp()) + " ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "MINSTART=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += QString::number(pwmFan->minStart()) + " ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "MINSTOP=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += QString::number(pwmFan->minStop()) + " ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "MINPWM=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += QString::number(pwmFan->minPwm()) + " ";
+	    }
+	    configFile += "\n";
+
+	    configFile += "MAXPWM=";
+	    foreach (PwmFan *pwmFan, usedFans)
+	    {
+		configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
+		configFile += "pwm" + QString::number(pwmFan->index()) + "=";
+		configFile += QString::number(pwmFan->maxPwm()) + " ";
+	    }
+	    configFile += "\n";
+	}
     }
-    m_configFile += "\n";
 
-    m_configFile += "FCFANS=";
-    foreach (PwmFan *pwmFan, usedFans)
+    if (configFile != m_configFile)
     {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "fan" + QString::number(pwmFan->index()) + "_input ";
+	m_configFile = configFile;
+	emit configFileChanged();
     }
-    m_configFile += "\n";
-
-    m_configFile += "MINTEMP=";
-    foreach (PwmFan *pwmFan, usedFans)
-    {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += QString::number(pwmFan->minTemp()) + " ";
-    }
-    m_configFile += "\n";
-
-    m_configFile += "MAXTEMP=";
-    foreach (PwmFan *pwmFan, usedFans)
-    {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += QString::number(pwmFan->maxTemp()) + " ";
-    }
-    m_configFile += "\n";
-
-    m_configFile += "MINSTART=";
-    foreach (PwmFan *pwmFan, usedFans)
-    {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += QString::number(pwmFan->minStart()) + " ";
-    }
-    m_configFile += "\n";
-
-    m_configFile += "MINSTOP=";
-    foreach (PwmFan *pwmFan, usedFans)
-    {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += QString::number(pwmFan->minStop()) + " ";
-    }
-    m_configFile += "\n";
-
-    m_configFile += "MINPWM=";
-    foreach (PwmFan *pwmFan, usedFans)
-    {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += QString::number(pwmFan->minPwm()) + " ";
-    }
-    m_configFile += "\n";
+}
 
-    m_configFile += "MAXPWM=";
-    foreach (PwmFan *pwmFan, usedFans)
+void Loader::setInterval(int interval, bool writeNewConfig)
+{
+    if (interval != m_interval)
     {
-        m_configFile += "hwmon" + QString::number(pwmFan->parent()->index()) + "/";
-        m_configFile += "pwm" + QString::number(pwmFan->index()) + "=";
-        m_configFile += QString::number(pwmFan->maxPwm()) + " ";
+	m_interval = interval;
+	emit intervalChanged();
+	qDebug() << "Changed interval to" << interval;
+	
+	if (writeNewConfig)
+	    createConfigFile();
     }
-    m_configFile += "\n";
-
-    emit configFileChanged();
 }
 
 void Loader::testFans()

+ 10 - 8
lib/src/loader.h

@@ -42,18 +42,18 @@ class FANCONTROL_GUI_LIB_EXPORT Loader : public QObject
 
 public:
 
-    explicit Loader(QObject *parent = 0);
+    explicit Loader(QObject *parent = Q_NULLPTR);
     
     Q_INVOKABLE void parseHwmons();
-    Q_INVOKABLE void load(const QUrl & = QUrl());
-    Q_INVOKABLE void save(const QUrl & = QUrl());
+    Q_INVOKABLE bool load(const QUrl & = QUrl());
+    Q_INVOKABLE bool save(const QUrl & = QUrl());
     Q_INVOKABLE void testFans();
     QUrl configUrl() const { return m_configUrl; }
     QString configFile() const { return m_configFile; }
     QList<QObject *> hwmons() const;
     QList<QObject *> allPwmFans() const;
-    int interval() { return m_interval; }
-    void setInterval(int interval) { if (interval != m_interval) { m_interval = interval; emit intervalChanged(m_interval*1000); createConfigFile(); } }
+    int interval() const { return m_interval; }
+    void setInterval(int interval, bool writeNewConfig = true);
     Hwmon * hwmon(int i) { return m_hwmons.value(i, Q_NULLPTR); }
     QString error() const { return m_error; }
     
@@ -73,8 +73,10 @@ protected slots:
 
 
 protected:
-
-    bool m_parsed;
+    
+    void setError(const QString &error) { if (error != m_error) { m_error = error; emit errorChanged(); } }
+    void success() { setError("Success"); }
+    
     int m_interval;
     QList<Hwmon *> m_hwmons;
     QUrl m_configUrl;
@@ -88,7 +90,7 @@ signals:
     void configUrlChanged();
     void configFileChanged();
     void hwmonsChanged();
-    void intervalChanged(int);
+    void intervalChanged();
     void errorChanged();
     void sensorsUpdateNeeded();
     void allPwmFansChanged();

+ 35 - 53
lib/src/systemdcommunicator.cpp

@@ -30,6 +30,7 @@ using namespace KAuth;
 SystemdCommunicator::SystemdCommunicator(QObject *parent) : QObject(parent)
 {
     setServiceName("fancontrol");
+    m_error = "Success";
 }
 
 void SystemdCommunicator::setServiceName(const QString &name)
@@ -133,9 +134,8 @@ bool SystemdCommunicator::serviceEnabled()
     if (serviceExists())
     {
         if (m_serviceInterface->property("UnitFileState").toString() == "enabled")
-        {
             return true;
-        }
+
     }
     return false;
 }
@@ -144,49 +144,33 @@ void SystemdCommunicator::setServiceEnabled(bool enabled)
 {
     if (serviceExists() && enabled != serviceEnabled())
     {
-        QVariantList arguments;
+	QString action = enabled ? "EnableUnitFiles" : "DisableUnitFiles";
         QStringList files = QStringList() << m_serviceName + ".service";
+        QVariantList arguments = QVariantList() << files << false;
+	if (enabled)
+	    arguments << true;
 
-        if (enabled)
-        {
-            arguments << files << false << true;
-            dbusAction("EnableUnitFiles", arguments);
-            dbusAction("Reload");
-        }
-        else
-        {
-            arguments << files << false;
-            dbusAction("DisableUnitFiles", arguments);
-            dbusAction("Reload");
+	if (dbusAction(action, arguments))
+	{
+	    dbusAction("Reload");
+            emit serviceEnabledChanged();
         }
-        emit serviceEnabledChanged();
     }
 }
 
 void SystemdCommunicator::setServiceActive(bool active)
 {
-    if (serviceExists())
+    if (serviceExists() && active != serviceActive())
     {
-        if (active && !serviceActive())
-        {
-            QVariantList args;
-            args << m_serviceName + ".service" << "replace";
-            dbusAction("ReloadOrRestartUnit", args);
-            emit serviceActiveChanged();
-        }
-        else if (!active && serviceActive()) 
-        {
-            QVariantList args;
-            args << m_serviceName + ".service" << "replace";
-            dbusAction("StopUnit", args);
-            emit serviceActiveChanged();
-        }
-        else 
-            qDebug() << "ServiceActive already " << active;
+	QVariantList args = QVariantList() << m_serviceName + ".service" << "replace";
+	QString action = active ? "ReloadOrRestartUnit" : "StopUnit";
+	
+	if (dbusAction(action, args))
+		emit serviceActiveChanged();
     }
 }
 
-void SystemdCommunicator::dbusAction(const QString &method, const QVariantList &arguments)
+bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &arguments)
 {
     QDBusMessage dbusreply;
 
@@ -200,7 +184,6 @@ void SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
 
     if (dbusreply.type() == QDBusMessage::ErrorMessage)
     {
-#ifndef NO_KF5_AUTH
         if (dbusreply.errorMessage() == "Interactive authentication required.")
         {
             Action action("fancontrol.gui.helper.action");
@@ -214,36 +197,35 @@ void SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
             ExecuteJob *reply = action.execute();
 
             if (!reply->exec())
-            {
-                m_error = reply->errorString();
-                emit errorChanged();
-            }
+	    {
+                setError(reply->errorString());
+		return false;
+	    }
             else
-            {
-                m_error = method + " succeeded";
-                emit errorChanged();
-            }
-            return;
+	    {
+                success();
+		return true;
+	    }
         }
-#endif
-        m_error = dbusreply.errorMessage();
-        emit errorChanged();
-    }
-    else
-    {
-        m_error = method + " succeeded";
-        emit errorChanged();
+        setError(dbusreply.errorMessage());
+        return false;
     }
+    
+    success();
+    return true;
 }
 
-void SystemdCommunicator::restartService()
+bool SystemdCommunicator::restartService()
 {
     if (serviceExists())
     {
         QVariantList args;
         args << m_serviceName + ".service" << "replace";
-        dbusAction("ReloadOrRestartUnit", args);
+        return dbusAction("ReloadOrRestartUnit", args);
     }
+    
+    setError("Service doesn't exist");
+    return false;
 }
 
 void SystemdCommunicator::updateServiceProperties(QString, QVariantMap propchanged, QStringList)

+ 6 - 3
lib/src/systemdcommunicator.h

@@ -47,8 +47,8 @@ public:
     void setServiceEnabled(bool enabled);
     void setServiceActive(bool active);
     QString error() const { return m_error; }
-    Q_INVOKABLE void dbusAction(const QString &method, const QVariantList &arguments = QVariantList());
-    Q_INVOKABLE void restartService();
+    Q_INVOKABLE bool dbusAction(const QString &method, const QVariantList &arguments = QVariantList());
+    Q_INVOKABLE bool restartService();
 
 
 signals:
@@ -65,10 +65,13 @@ protected slots:
     
     
 protected:
+    
+    void setError(const QString &error) { if (error != m_error) { m_error = error; emit errorChanged(); } }
+    void success() { setError("Success"); }
 
     QString m_serviceName;
     QString m_serviceObjectPath;
-    QString m_error = "Success";
+    QString m_error;
     QDBusInterface *m_managerInterface = new QDBusInterface("org.freedesktop.systemd1",
                                                             "/org/freedesktop/systemd1",
                                                             "org.freedesktop.systemd1.Manager",

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

@@ -37,15 +37,6 @@ Item {
     anchors.fill: parent
     anchors.topMargin: 5
 
-    onIntervalChanged: {
-        if (loader !== null) {
-            var fans = loader.allPwmFans;
-            for (var i=0; i<fans.length; i++) {
-                fans[i].interval = interval;
-            }
-        }
-    }
-
     Column {
         id: column
         anchors.fill: parent
@@ -68,7 +59,7 @@ Item {
                 Layout.fillWidth: true
                 inputMethodHints: Qt.ImhDigitsOnly
                 text: interval
-                onTextChanged: if (text != "") loader.interval = parseInt(text)
+                onTextChanged: if (text && text != "0") loader.interval = parseInt(text)
             }
         }
         RowLayout {