[plasma-mobile/kclock] src: Add ability to run a command at timer's timeout

Boris Petrov null at kde.org
Fri Jul 23 23:01:58 BST 2021


Git commit e823410c779e841e98ecbe1ca1a7904519726c28 by Boris Petrov.
Committed on 20/07/2021 at 12:59.
Pushed by devinlin into branch 'master'.

Add ability to run a command at timer's timeout

This commits adds the possibility to run a command when
a timer ends.

FEATURE: Add ability to run a command at timer's timeout
GUI:

M  +25   -0    src/kclock/qml/TimerListPage.qml
M  +26   -0    src/kclock/timer.cpp
M  +16   -0    src/kclock/timer.h
M  +28   -0    src/kclockd/timer.cpp
M  +16   -0    src/kclockd/timer.h

https://invent.kde.org/plasma-mobile/kclock/commit/e823410c779e841e98ecbe1ca1a7904519726c28

diff --git a/src/kclock/qml/TimerListPage.qml b/src/kclock/qml/TimerListPage.qml
index 905c447..4d76cf9 100644
--- a/src/kclock/qml/TimerListPage.qml
+++ b/src/kclock/qml/TimerListPage.qml
@@ -181,6 +181,31 @@ Kirigami.ScrollablePage {
                             }
                         }
                     }
+                    
+                    RowLayout {
+                        anchors.left: parent.left
+                        anchors.right: parent.right
+                        ToolButton {
+                            icon.name: "dialog-scripts"
+                            display: AbstractButton.IconOnly
+                            text: i18n("Command")
+                            checked: timerDelegate && timerDelegate.isCommandTimeout
+                            onClicked: timerDelegate.toggleIsCommandTimeout()
+                            
+                            ToolTip.visible: hovered && text.length > 0
+                            ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
+                            ToolTip.text: text
+                        }
+                        TextField {
+                            Layout.fillWidth: true
+                            enabled: timerDelegate && timerDelegate.isCommandTimeout
+                            focus: true
+                            font.family: "Monospace"
+                            onEditingFinished: timerDelegate.saveCommandTimeout(text)
+                            text: timerDelegate.commandTimeout
+                            placeholderText: "Command to execute at timeout…"
+                        }
+                    }
                 }
             }
         }
diff --git a/src/kclock/timer.cpp b/src/kclock/timer.cpp
index 479f28f..3ee5d5f 100644
--- a/src/kclock/timer.cpp
+++ b/src/kclock/timer.cpp
@@ -24,9 +24,13 @@ Timer::Timer(QString uuid, bool justCreated)
         m_length = m_interface->length();
         m_running = m_interface->running();
         m_elapsed = m_interface->elapsed();
+        m_isCommandTimeout = m_interface->isCommandTimeout();
+        m_commandTimeout = m_interface->commandTimeout();
         connect(m_interface, &OrgKdeKclockTimerInterface::lengthChanged, this, &Timer::updateLength);
         connect(m_interface, &OrgKdeKclockTimerInterface::labelChanged, this, &Timer::updateLabel);
         connect(m_interface, &OrgKdeKclockTimerInterface::runningChanged, this, &Timer::updateRunning);
+        connect(m_interface, &OrgKdeKclockTimerInterface::isCommandTimeoutChanged, this, &Timer::updateIsCommandTimeout);
+        connect(m_interface, &OrgKdeKclockTimerInterface::commandTimeoutChanged, this, &Timer::updateCommandTimeout);
 
         updateRunning(); // start animation
     }
@@ -37,6 +41,16 @@ void Timer::toggleRunning()
     m_interface->toggleRunning();
 }
 
+void Timer::toggleIsCommandTimeout()
+{
+    m_interface->toggleIsCommandTimeout();
+}
+
+void Timer::saveCommandTimeout(QString command)
+{
+    m_interface->saveCommandTimeout(command);
+}
+
 void Timer::reset()
 {
     m_interface->reset();
@@ -75,6 +89,18 @@ void Timer::updateRunning()
     Q_EMIT elapsedChanged();
 }
 
+void Timer::updateIsCommandTimeout()
+{
+    m_isCommandTimeout = m_interface->isCommandTimeout();
+    Q_EMIT propertyChanged();
+}
+
+void Timer::updateCommandTimeout()
+{
+    m_commandTimeout = m_interface->commandTimeout();
+    Q_EMIT propertyChanged();
+}
+
 void Timer::animation(bool start)
 {
     if (!m_timer) {
diff --git a/src/kclock/timer.h b/src/kclock/timer.h
index a14e5f7..fe408a2 100644
--- a/src/kclock/timer.h
+++ b/src/kclock/timer.h
@@ -24,6 +24,8 @@ class Timer : public QObject
     Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY propertyChanged)
     Q_PROPERTY(bool running READ running NOTIFY propertyChanged)
     Q_PROPERTY(bool justCreated MEMBER m_justCreated NOTIFY propertyChanged)
+    Q_PROPERTY(bool isCommandTimeout READ isCommandTimeout NOTIFY propertyChanged)
+    Q_PROPERTY(QString commandTimeout READ commandTimeout NOTIFY propertyChanged)
 
 public:
     Timer();
@@ -33,6 +35,8 @@ public:
         return m_uuid;
     };
     Q_INVOKABLE void toggleRunning();
+    Q_INVOKABLE void toggleIsCommandTimeout();
+    Q_INVOKABLE void saveCommandTimeout(QString);
     Q_INVOKABLE void reset();
     Q_INVOKABLE void addMinute();
 
@@ -70,6 +74,14 @@ public:
     {
         return m_running;
     }
+    const bool &isCommandTimeout() const
+    {
+        return m_isCommandTimeout;
+    }
+    const QString &commandTimeout() const
+    {
+        return m_commandTimeout;
+    }
 
 signals:
     void propertyChanged();
@@ -78,6 +90,8 @@ private slots:
     void updateLength();
     void updateLabel();
     void updateRunning();
+    void updateIsCommandTimeout();
+    void updateCommandTimeout();
 
 private:
     void animation(bool start);
@@ -85,6 +99,8 @@ private:
     int m_length, m_elapsed; // seconds
     QString m_label;
     bool m_running;
+    bool m_isCommandTimeout;
+    QString m_commandTimeout;
     bool m_justCreated;
 
     QUuid m_uuid;
diff --git a/src/kclockd/timer.cpp b/src/kclockd/timer.cpp
index 5b31f7b..31c0db2 100644
--- a/src/kclockd/timer.cpp
+++ b/src/kclockd/timer.cpp
@@ -6,6 +6,8 @@
 
 #include "timer.h"
 
+#include <QProcess>
+
 #include "utilities.h"
 
 #include <KLocalizedString>
@@ -38,6 +40,8 @@ Timer::Timer(const QJsonObject &obj)
     m_length = obj[QStringLiteral("length")].toInt();
     m_label = obj[QStringLiteral("label")].toString();
     m_uuid = QUuid(obj[QStringLiteral("uuid")].toString());
+    m_isCommandTimeout = obj[QStringLiteral("isCommandTimeout")].toBool();
+    m_commandTimeout = obj[QStringLiteral("commandTimeout")].toString();
 
     connect(&Utilities::instance(), &Utilities::wakeup, this, &Timer::timeUp);
     QDBusConnection::sessionBus().registerObject(QStringLiteral("/Timers/") + this->m_uuid.toString(QUuid::Id128),
@@ -61,6 +65,8 @@ QJsonObject Timer::serialize()
     obj[QStringLiteral("length")] = m_length;
     obj[QStringLiteral("label")] = m_label;
     obj[QStringLiteral("uuid")] = m_uuid.toString();
+    obj[QStringLiteral("isCommandTimeout")] = m_isCommandTimeout;
+    obj[QStringLiteral("commandTimeout")] = m_commandTimeout;
     return obj;
 }
 
@@ -69,6 +75,24 @@ void Timer::toggleRunning()
     setRunning(!m_running);
 }
 
+void Timer::toggleIsCommandTimeout()
+{
+    m_isCommandTimeout = !m_isCommandTimeout;
+
+    Q_EMIT isCommandTimeoutChanged();
+
+    TimerModel::instance()->save();
+}
+
+void Timer::saveCommandTimeout(QString command)
+{
+    m_commandTimeout = command;
+
+    Q_EMIT commandTimeoutChanged();
+
+    TimerModel::instance()->save();
+}
+
 void Timer::reset()
 {
     setRunning(false);
@@ -82,6 +106,10 @@ void Timer::timeUp(int cookie)
     if (cookie == m_cookie) {
         this->sendNotification();
         this->m_cookie = -1;
+        if (m_isCommandTimeout) {
+            QProcess *process = new QProcess;
+            process->start(m_commandTimeout);
+        }
         this->setRunning(false);
         this->m_hasElapsed = m_length;
     }
diff --git a/src/kclockd/timer.h b/src/kclockd/timer.h
index 71fc4cf..66445a7 100644
--- a/src/kclockd/timer.h
+++ b/src/kclockd/timer.h
@@ -20,6 +20,8 @@ class Timer : public QObject
     Q_PROPERTY(int length READ length WRITE setLength NOTIFY lengthChanged)
     Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
     Q_PROPERTY(bool running READ running NOTIFY runningChanged)
+    Q_PROPERTY(bool isCommandTimeout READ isCommandTimeout NOTIFY isCommandTimeoutChanged)
+    Q_PROPERTY(QString commandTimeout READ commandTimeout NOTIFY commandTimeoutChanged)
 
 public:
     explicit Timer(int length = 0, QString label = QStringLiteral(), bool running = false);
@@ -29,6 +31,8 @@ public:
     QJsonObject serialize();
 
     Q_SCRIPTABLE void toggleRunning();
+    Q_SCRIPTABLE void toggleIsCommandTimeout();
+    Q_SCRIPTABLE void saveCommandTimeout(QString);
     Q_SCRIPTABLE void reset();
     Q_SCRIPTABLE int elapsed() const
     {
@@ -69,11 +73,21 @@ public:
     {
         return m_running;
     }
+    const bool &isCommandTimeout() const
+    {
+        return m_isCommandTimeout;
+    }
+    const QString &commandTimeout() const
+    {
+        return m_commandTimeout;
+    }
 
 Q_SIGNALS:
     Q_SCRIPTABLE void lengthChanged();
     Q_SCRIPTABLE void labelChanged();
     Q_SCRIPTABLE void runningChanged();
+    Q_SCRIPTABLE void isCommandTimeoutChanged();
+    Q_SCRIPTABLE void commandTimeoutChanged();
 private Q_SLOTS:
     void timeUp(int cookie);
     void reschedule();
@@ -88,6 +102,8 @@ private:
     int m_cookie = -1;
     QString m_label;
     bool m_running = false;
+    bool m_isCommandTimeout = false;
+    QString m_commandTimeout = "";
 };
 
 #endif // KCLOCKD_TIMER_H



More information about the kde-doc-english mailing list