File indexing completed on 2024-09-22 05:13:06

0001 /*
0002  * Copyright 2020 Han Young <hanyoung@protonmail.com>
0003  * Copyright 2021 Boris Petrov <boris.v.petrov@protonmail.com>
0004  * Copyright 2022 Devin Lin <devin@kde.org>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "timermodel.h"
0012 #include "utilities.h"
0013 
0014 #include <QDBusConnection>
0015 #include <QDateTime>
0016 #include <QJsonObject>
0017 #include <QObject>
0018 #include <QProcess>
0019 #include <QUuid>
0020 
0021 #include <KLocalizedString>
0022 #include <KNotification>
0023 
0024 class Timer : public QObject
0025 {
0026     Q_OBJECT
0027     Q_CLASSINFO("D-Bus Interface", "org.kde.kclock.Timer")
0028     Q_PROPERTY(int length READ length WRITE setLength NOTIFY lengthChanged)
0029     Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
0030     Q_PROPERTY(QString commandTimeout READ commandTimeout WRITE setCommandTimeout NOTIFY commandTimeoutChanged)
0031     Q_PROPERTY(bool running READ running NOTIFY runningChanged)
0032     Q_PROPERTY(bool looping READ looping NOTIFY loopingChanged)
0033     Q_PROPERTY(bool ringing READ ringing NOTIFY ringingChanged)
0034 
0035 public:
0036     explicit Timer(int length = 0,
0037                    const QString &label = QString{},
0038                    const QString &commandTimeout = QString{},
0039                    bool running = false,
0040                    QObject *parent = nullptr);
0041     explicit Timer(const QJsonObject &obj, QObject *parent);
0042 
0043     ~Timer();
0044 
0045     void init();
0046 
0047     // serialize this timer to json
0048     QJsonObject serialize();
0049 
0050     Q_SCRIPTABLE void toggleRunning();
0051     Q_SCRIPTABLE void toggleLooping();
0052     Q_SCRIPTABLE void reset();
0053     Q_SCRIPTABLE int elapsed() const;
0054 
0055     Q_SCRIPTABLE QString uuid() const;
0056 
0057     int length() const;
0058     void setLength(int length);
0059 
0060     QString label() const;
0061     void setLabel(const QString &label);
0062 
0063     QString commandTimeout() const;
0064     void setCommandTimeout(const QString &commandTimeout);
0065 
0066     bool looping() const;
0067     bool running() const;
0068     bool ringing() const;
0069     Q_SCRIPTABLE void dismiss();
0070 
0071 Q_SIGNALS:
0072     Q_SCRIPTABLE void lengthChanged();
0073     Q_SCRIPTABLE void labelChanged();
0074     Q_SCRIPTABLE void commandTimeoutChanged();
0075     Q_SCRIPTABLE void runningChanged();
0076     Q_SCRIPTABLE void loopingChanged();
0077     Q_SCRIPTABLE void ringingChanged();
0078 
0079 private Q_SLOTS:
0080     void timeUp(int cookie);
0081     void reschedule();
0082 
0083 private:
0084     void setRunning(bool running);
0085     void ring();
0086 
0087     // -- properties persisted to storage: --
0088 
0089     // the uuid of the timer
0090     QUuid m_uuid;
0091 
0092     // the total length of the timer, in seconds
0093     int m_length;
0094 
0095     // the name of the timer, can be blank
0096     QString m_label;
0097 
0098     // the command to run when the timer finishes, can be blank
0099     QString m_commandTimeout;
0100 
0101     // -- properties that are not persisted: --
0102 
0103     // the unix timestamp (seconds) at which the timer was started
0104     int m_startTime = 0;
0105 
0106     // the time the timer elapsed till the most recent pause/stop, only updated when timer is stopped or finished
0107     int m_hasElapsed = 0;
0108 
0109     // the PowerDevil cookie used for system wakeup when the timer is supposed to go off
0110     int m_cookie = -1;
0111 
0112     // whether the timer is running
0113     bool m_running = false;
0114 
0115     // whether the timer is looping
0116     bool m_looping = false;
0117 
0118     // whether the timer is ringing
0119     bool m_ringing = false;
0120 
0121     KNotification *m_notification =
0122         new KNotification{QStringLiteral("timerFinished"), KNotification::NotificationFlag::LoopSound | KNotification::NotificationFlag::Persistent, this};
0123 };