Selaa lähdekoodia

added profiles

Malte Veerman 6 vuotta sitten
vanhempi
commit
2bff5e489a

+ 54 - 1
fancontrol-gui/package/contents/ui/PwmFansTab.qml

@@ -30,6 +30,7 @@ Item {
     property QtObject systemdCom: Fancontrol.Base.hasSystemdCommunicator() ? Fancontrol.Base.systemdCom : null
     property QtObject pwmFanModel: Fancontrol.Base.pwmFanModel
     property QtObject tempModel: Fancontrol.Base.tempModel
+    property QtObject profileModel: Fancontrol.Base.profileModel
     property var pwmFans: pwmFanModel.fans
 
     id: root
@@ -37,11 +38,46 @@ Item {
     anchors.margins: Kirigami.Units.smallSpacing
 
     RowLayout {
-        id: fanRow
+        id: profileRow
 
         anchors.top: parent.top
         width: parent.width
         height: childrenRect.height
+
+        Label {
+            text: i18n("Profile:")
+            Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
+            renderType: Text.NativeRendering
+        }
+        ComboBox {
+            id: profileComboBox
+
+            property string saveText: editText.length > 0 ? editText : currentText
+
+            editable: true
+            model: profileModel
+            textRole: "display"
+            Layout.fillWidth: true
+            Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
+
+            onActivated: Fancontrol.Base.applyProfile(index)
+        }
+        Button {
+            Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
+            action: saveProfileAction
+        }
+        Button {
+            Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
+            action: deleteProfileAction
+        }
+    }
+
+    RowLayout {
+        id: fanRow
+
+        anchors.top: profileRow.bottom
+        width: parent.width
+        height: childrenRect.height
         visible: pwmFans.length > 0
 
         Label {
@@ -51,6 +87,7 @@ Item {
         }
         ComboBox {
             id: fanComboBox
+
             model: pwmFanModel
             textRole: "display"
             Layout.fillWidth: true
@@ -78,6 +115,7 @@ Item {
 
     ColumnLayout {
         id: noFansInfo
+
         anchors.centerIn: parent
         spacing: Kirigami.Units.smallSpacing * 2
         visible: pwmFans.length === 0
@@ -95,8 +133,23 @@ Item {
         }
     }
 
+    Action {
+        id: saveProfileAction
+
+        text: i18n("Save profile")
+        iconName: "document-save"
+        onTriggered: Fancontrol.Base.saveProfile(profileComboBox.saveText)
+    }
+    Action {
+        id: deleteProfileAction
+
+        text: i18n("Delete profile")
+        iconName: "edit-delete"
+        onTriggered: Fancontrol.Base.deleteProfile(profileComboBox.currentIndex)
+    }
     Action {
         id: detectFansAction
+
         text: loader.sensorsDetected ? i18n("Detect fans again") : i18n("Detect fans")
         iconName: "dialog-password"
         onTriggered: loader.detectSensors()

+ 2 - 0
import/src/config.cpp

@@ -48,6 +48,8 @@ Config::Config(QObject *parent) : KCoreConfigSkeleton(KSharedConfig::openConfig(
     addItemDouble(QStringLiteral("MaxTemp"), m_maxTemp, 90.0);
     addItemString(QStringLiteral("ServiceName"), m_serviceName, QStringLiteral(STANDARD_SERVICE_NAME));
     addItemPath(QStringLiteral("ConfigUrl"), m_configUrl, QStringLiteral("file://") + STANDARD_CONFIG_FILE);
+    addItemStringList(QStringLiteral("Profiles"), m_profiles, QStringList());
+    addItemStringList(QStringLiteral("ProfileNames"), m_profileNames, QStringList());
 
     load();
 }

+ 2 - 0
import/src/config.h

@@ -51,6 +51,8 @@ private:
     double m_maxTemp;
     QString m_serviceName;
     QString m_configUrl;
+    QStringList m_profiles;
+    QStringList m_profileNames;
 };
 
 }

+ 114 - 2
import/src/guibase.cpp

@@ -46,7 +46,8 @@ GUIBase::GUIBase(QObject *parent) : QObject(parent),
     m_configValid(false),
     m_configChanged(false),
     m_pwmFanModel(new PwmFanModel(this)),
-    m_tempModel(new TempModel(this))
+    m_tempModel(new TempModel(this)),
+    m_profileModel(new QStringListModel(this))
 {
     connect(this, &GUIBase::unitChanged, m_tempModel, &TempModel::setUnit);
     connect(m_loader, &Loader::needsSaveChanged, this, &GUIBase::needsApplyChanged);
@@ -76,6 +77,8 @@ void GUIBase::load()
     m_config->load();
     m_configValid = m_loader->load(configUrl());
 
+    m_profileModel->setStringList(m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList());
+
 #ifndef NO_SYSTEMD
     m_com->setServiceName(serviceName());
     m_com->reset();
@@ -89,21 +92,25 @@ void GUIBase::load()
 
 qreal GUIBase::maxTemp() const
 {
+    m_config->setCurrentGroup(QStringLiteral("preferences"));
     return m_config->findItem(QStringLiteral("MaxTemp"))->property().toReal();
 }
 
 qreal GUIBase::minTemp() const
 {
+    m_config->setCurrentGroup(QStringLiteral("preferences"));
     return m_config->findItem(QStringLiteral("MinTemp"))->property().toReal();
 }
 
 QString GUIBase::serviceName() const
 {
+    m_config->setCurrentGroup(QStringLiteral("preferences"));
     return m_config->findItem(QStringLiteral("ServiceName"))->property().toString();
 }
 
 QUrl GUIBase::configUrl() const
 {
+    m_config->setCurrentGroup(QStringLiteral("preferences"));
     return QUrl(m_config->findItem(QStringLiteral("ConfigUrl"))->property().toString());
 }
 
@@ -111,6 +118,7 @@ void GUIBase::setMaxTemp(qreal temp)
 {
     if (temp != maxTemp())
     {
+        m_config->setCurrentGroup(QStringLiteral("preferences"));
         m_config->findItem(QStringLiteral("MaxTemp"))->setProperty(temp);
         emit maxTempChanged();
 
@@ -123,6 +131,7 @@ void GUIBase::setMinTemp(qreal temp)
 {
     if (temp != minTemp())
     {
+        m_config->setCurrentGroup(QStringLiteral("preferences"));
         m_config->findItem(QStringLiteral("MinTemp"))->setProperty(temp);
         emit minTempChanged();
 
@@ -133,8 +142,9 @@ void GUIBase::setMinTemp(qreal temp)
 
 void GUIBase::setServiceName(const QString& name)
 {
-    if(name != serviceName())
+    if (name != serviceName())
     {
+        m_config->setCurrentGroup(QStringLiteral("preferences"));
         m_config->findItem(QStringLiteral("ServiceName"))->setProperty(name);
 
 #ifndef NO_SYSTEMD
@@ -154,6 +164,7 @@ void GUIBase::setConfigUrl(const QUrl &url)
     {
         m_configValid = m_loader->load(url);
 
+        m_config->setCurrentGroup(QStringLiteral("preferences"));
         m_config->findItem(QStringLiteral("ConfigUrl"))->setProperty(url.toString());
         emit configUrlChanged();
 
@@ -243,5 +254,106 @@ void GUIBase::handleInfo(const QString &info)
         qInfo() << info;
 }
 
+void GUIBase::applyProfile(const QString& profile)
+{
+    qDebug() << "apply profile:" << profile;
+
+    if (!m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList().contains(profile))
+    {
+        handleError(i18n("Unable to apply unknown profile: %1", profile));
+        return;
+    }
+
+    int index = m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList().indexOf(profile);
+
+    applyProfile(index);
+}
+
+void GUIBase::applyProfile(int index)
+{
+    qDebug() << "apply profile:" << index;
+
+    auto profileNames = m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList();
+
+    if (index < 0 || index >= profileNames.size())
+        return;
+
+    auto newConfig = m_config->findItem(QStringLiteral("Profiles"))->property().toStringList().value(index);
+
+    if (newConfig.isEmpty())
+    {
+        handleError(i18n("Unable to read data for profile: %1", index));
+        profileNames.removeAt(index);
+        m_config->findItem(QStringLiteral("ProfileNames"))->setProperty(profileNames);
+        return;
+    }
+
+    if (m_loader->config() == newConfig)
+        return;
+
+    m_loader->load(newConfig);
+}
+
+void GUIBase::saveProfile(const QString& profile, bool updateModel)
+{
+    qDebug() << "save profile:" << profile;
+
+    auto profileNames = m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList();
+    int index = profileNames.indexOf(profile);
+
+    qDebug() << "index:" << index;
+
+    if (index < 0)
+    {
+        index = profileNames.size();
+
+        auto profileNames = m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList();
+        m_config->findItem(QStringLiteral("ProfileNames"))->setProperty(profileNames << profile);
+
+        if (updateModel)
+            m_profileModel->insertRow(index);
+    }
+
+    auto profiles = m_config->findItem(QStringLiteral("Profiles"))->property().toStringList();
+    profiles.insert(index, m_loader->config());
+    m_config->findItem(QStringLiteral("Profiles"))->setProperty(profiles);
+
+    if (updateModel)
+        m_profileModel->setData(m_profileModel->index(index, 0), profile);
+
+    m_configChanged = true;
+    emit needsApplyChanged();
+}
+
+void GUIBase::deleteProfile(const QString& profile, bool updateModel)
+{
+    qDebug() << "delete profile:" << profile;
+
+    int index = m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList().indexOf(profile);
+
+    deleteProfile(index, updateModel);
+}
+
+void GUIBase::deleteProfile(int index, bool updateModel)
+{
+    qDebug() << "delete profile:" << index;
+
+    auto profileNames = m_config->findItem(QStringLiteral("ProfileNames"))->property().toStringList();
+
+    if (index < 0 || index >= profileNames.size())
+        return;
+
+    profileNames.removeAt(index);
+    m_config->findItem(QStringLiteral("ProfileNames"))->setProperty(profileNames);
+    auto profiles = m_config->findItem(QStringLiteral("Profiles"))->property().toStringList();
+    profileNames.removeAt(index);
+    m_config->findItem(QStringLiteral("Profiles"))->setProperty(profiles);
+
+    if (updateModel)
+        m_profileModel->removeRow(index);
+
+    m_configChanged = true;
+    emit needsApplyChanged();
+}
 
 }

+ 9 - 1
import/src/guibase.h

@@ -24,6 +24,7 @@
 #define GUIBASE_H
 
 #include <QtCore/QObject>
+#include <QtCore/QStringListModel>
 #include <QtCore/QUrl>
 
 #include "loader.h"
@@ -50,6 +51,7 @@ class GUIBase : public QObject
 
     Q_PROPERTY(PwmFanModel *pwmFanModel READ pwmFanModel CONSTANT)
     Q_PROPERTY(TempModel *tempModel READ tempModel CONSTANT)
+    Q_PROPERTY(QStringListModel *profileModel READ profileModel CONSTANT)
     Q_PROPERTY(Loader* loader READ loader CONSTANT)
     Q_PROPERTY(qreal minTemp READ minTemp WRITE setMinTemp NOTIFY minTempChanged)
     Q_PROPERTY(qreal maxTemp READ maxTemp WRITE setMaxTemp NOTIFY maxTempChanged)
@@ -85,11 +87,16 @@ public:
     bool needsApply() const;
     PwmFanModel *pwmFanModel() const { return m_pwmFanModel; }
     TempModel *tempModel() const { return m_tempModel; }
+    QStringListModel *profileModel() const { return m_profileModel; }
 
     Q_INVOKABLE bool hasSystemdCommunicator() const;
     Q_INVOKABLE void apply();
     Q_INVOKABLE void reset();
-
+    Q_INVOKABLE void applyProfile(const QString &profile);
+    Q_INVOKABLE void applyProfile(int);
+    Q_INVOKABLE void saveProfile(const QString &profile, bool updateModel = true);
+    Q_INVOKABLE void deleteProfile(const QString &profile, bool updateModel = true);
+    Q_INVOKABLE void deleteProfile(int, bool updateModel = true);
 
 public slots:
 
@@ -123,6 +130,7 @@ private:
     bool m_configChanged;
     PwmFanModel *m_pwmFanModel;
     TempModel *m_tempModel;
+    QStringListModel *m_profileModel;
 };
 
 }

+ 15 - 0
import/src/loader.cpp

@@ -482,6 +482,21 @@ bool Loader::load(const QUrl &url)
     return parseConfig(m_configFileContent);
 }
 
+void Loader::load(const QString& config)
+{
+    if (m_config == config)
+        return;
+
+    if (config.isEmpty())
+    {
+        emit error(i18n("Cannot load empty config."));
+        return;
+    }
+
+    m_config = config;
+    parseConfig(config);
+}
+
 bool Loader::save(const QUrl &url)
 {
     QString fileName;

+ 2 - 0
import/src/loader.h

@@ -64,6 +64,8 @@ public:
     Q_INVOKABLE void testFans();
     Q_INVOKABLE void abortTestingFans();
     Q_INVOKABLE void detectSensors();
+
+    void load(const QString &config);
     QUrl configUrl() const { return m_configUrl; }
     QString configPath() const { return m_configUrl.path(); }
     QString config() const { return m_config; }