[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 89af15c17d2bef22ea1d11e8b5b50ed3124063c2 by Boris Petrov.
Committed on 23/07/2021 at 18:01.
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/89af15c17d2bef22ea1d11e8b5b50ed3124063c2

diff --git a/src/kclock/qml/TimerListPage.qml b/src/kclock/qml/TimerListPage.qml
index d319b72..77f2bed 100644
--- a/src/kclock/qml/TimerListPage.qml
+++ b/src/kclock/qml/TimerListPage.qml
@@ -191,6 +191,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 de80bee..a21cb2b 100644
--- a/src/kclock/timer.cpp
+++ b/src/kclock/timer.cpp
@@ -25,10 +25,14 @@ Timer::Timer(QString uuid, bool justCreated)
         m_running = m_interface->running();
         m_elapsed = m_interface->elapsed();
         m_looping = m_interface->looping();
+        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::loopingChanged, this, &Timer::updateLooping);
+        connect(m_interface, &OrgKdeKclockTimerInterface::isCommandTimeoutChanged, this, &Timer::updateIsCommandTimeout);
+        connect(m_interface, &OrgKdeKclockTimerInterface::commandTimeoutChanged, this, &Timer::updateCommandTimeout);
 
         updateRunning(); // start animation
     }
@@ -44,6 +48,16 @@ void Timer::toggleLooping()
     m_interface->toggleLooping();
 }
 
+void Timer::toggleIsCommandTimeout()
+{
+    m_interface->toggleIsCommandTimeout();
+}
+
+void Timer::saveCommandTimeout(QString command)
+{
+    m_interface->saveCommandTimeout(command);
+}
+
 void Timer::reset()
 {
     m_interface->reset();
@@ -88,6 +102,18 @@ void Timer::updateLooping()
     Q_EMIT propertyChanged();
 }
 
+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 fe83246..df9064e 100644
--- a/src/kclock/timer.h
+++ b/src/kclock/timer.h
@@ -25,6 +25,8 @@ class Timer : public QObject
     Q_PROPERTY(bool running READ running NOTIFY propertyChanged)
     Q_PROPERTY(bool looping READ looping 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();
@@ -35,6 +37,8 @@ public:
     };
     Q_INVOKABLE void toggleRunning();
     Q_INVOKABLE void toggleLooping();
+    Q_INVOKABLE void toggleIsCommandTimeout();
+    Q_INVOKABLE void saveCommandTimeout(QString);
     Q_INVOKABLE void reset();
     Q_INVOKABLE void addMinute();
 
@@ -76,6 +80,14 @@ public:
     {
         return m_looping;
     }
+    const bool &isCommandTimeout() const
+    {
+        return m_isCommandTimeout;
+    }
+    const QString &commandTimeout() const
+    {
+        return m_commandTimeout;
+    }
 
 signals:
     void propertyChanged();
@@ -85,6 +97,8 @@ private slots:
     void updateLabel();
     void updateRunning();
     void updateLooping();
+    void updateIsCommandTimeout();
+    void updateCommandTimeout();
 
 private:
     void animation(bool start);
@@ -93,6 +107,8 @@ private:
     QString m_label;
     bool m_running;
     bool m_looping;
+    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 64e54f6..5dab380 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>
@@ -39,6 +41,8 @@ Timer::Timer(const QJsonObject &obj)
     m_label = obj[QStringLiteral("label")].toString();
     m_uuid = QUuid(obj[QStringLiteral("uuid")].toString());
     m_looping = obj[QStringLiteral("looping")].toBool();
+    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),
@@ -63,6 +67,8 @@ QJsonObject Timer::serialize()
     obj[QStringLiteral("label")] = m_label;
     obj[QStringLiteral("uuid")] = m_uuid.toString();
     obj[QStringLiteral("looping")] = m_looping;
+    obj[QStringLiteral("isCommandTimeout")] = m_isCommandTimeout;
+    obj[QStringLiteral("commandTimeout")] = m_commandTimeout;
     return obj;
 }
 
@@ -80,6 +86,24 @@ void Timer::toggleLooping()
     TimerModel::instance()->save();
 }
 
+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);
@@ -93,6 +117,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);
+        }
         if (m_looping) {
             this->reset();
             this->setRunning(true);
diff --git a/src/kclockd/timer.h b/src/kclockd/timer.h
index 23eed5e..3f759c9 100644
--- a/src/kclockd/timer.h
+++ b/src/kclockd/timer.h
@@ -21,6 +21,8 @@ class Timer : public QObject
     Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
     Q_PROPERTY(bool running READ running NOTIFY runningChanged)
     Q_PROPERTY(bool looping READ looping NOTIFY loopingChanged)
+    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);
@@ -31,6 +33,8 @@ public:
 
     Q_SCRIPTABLE void toggleRunning();
     Q_SCRIPTABLE void toggleLooping();
+    Q_SCRIPTABLE void toggleIsCommandTimeout();
+    Q_SCRIPTABLE void saveCommandTimeout(QString);
     Q_SCRIPTABLE void reset();
     Q_SCRIPTABLE int elapsed() const
     {
@@ -75,12 +79,22 @@ public:
     {
         return m_looping;
     }
+    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 loopingChanged();
+    Q_SCRIPTABLE void isCommandTimeoutChanged();
+    Q_SCRIPTABLE void commandTimeoutChanged();
 private Q_SLOTS:
     void timeUp(int cookie);
     void reschedule();
@@ -96,6 +110,8 @@ private:
     QString m_label;
     bool m_running = false;
     bool m_looping = false;
+    bool m_isCommandTimeout = false;
+    QString m_commandTimeout = "";
 };
 
 #endif // KCLOCKD_TIMER_H



More information about the kde-doc-english mailing list