File indexing completed on 2024-06-23 05:45:43

0001 /*
0002     SPDX-FileCopyrightText: 2020 HanY <hanyoung@protonmail.com>
0003     SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "kclock_1x2.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QDBusConnection>
0012 #include <QDBusInterface>
0013 #include <QDBusReply>
0014 
0015 KClock_1x2::KClock_1x2(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0016     : Plasma::Applet(parent, metaData, args)
0017 {
0018     m_timer = new QTimer(this);
0019     connect(m_timer, &QTimer::timeout, this, &KClock_1x2::initialTimeUpdate);
0020     m_timer->setSingleShot(true);
0021 
0022     // initial interval is milliseconds to next minute
0023     m_timer->start((60 - (QTime::currentTime().msecsSinceStartOfDay() / 1000) % 60) * 1000); // seconds to next minute
0024 
0025     bool daemonSuccess = QDBusConnection::sessionBus().connect(QStringLiteral("org.kde.kclockd"),
0026                                                                QStringLiteral("/Alarms"),
0027                                                                QStringLiteral("org.kde.kclock.AlarmModel"),
0028                                                                QStringLiteral("nextAlarm"),
0029                                                                this,
0030                                                                SLOT(updateAlarm(qulonglong)));
0031     if (!daemonSuccess) {
0032         m_string = QStringLiteral("connection to kclockd failed");
0033     }
0034 
0035     QDBusInterface *interface = new QDBusInterface(QStringLiteral("org.kde.kclockd"),
0036                                                    QStringLiteral("/Alarms"),
0037                                                    QStringLiteral("org.kde.kclock.AlarmModel"),
0038                                                    QDBusConnection::sessionBus(),
0039                                                    this);
0040     QDBusReply<quint64> reply = interface->call(QStringLiteral("getNextAlarm"));
0041 
0042     if (reply.isValid()) {
0043         auto alarmTime = reply.value();
0044         if (alarmTime > 0) {
0045             auto dateTime = QDateTime::fromSecsSinceEpoch(alarmTime).toLocalTime();
0046             m_string = QStringLiteral("%1, %2").arg(m_locale.standaloneDayName(dateTime.date().dayOfWeek(), QLocale::ShortFormat),
0047                                                     m_locale.toString(dateTime.time(), QStringLiteral("hh:mm")));
0048             m_hasAlarm = true;
0049         } else {
0050             m_hasAlarm = false;
0051         }
0052     }
0053 
0054     Q_EMIT propertyChanged();
0055 }
0056 
0057 QString KClock_1x2::alarmTime()
0058 {
0059     return m_string;
0060 };
0061 
0062 bool KClock_1x2::hasAlarm()
0063 {
0064     return m_hasAlarm;
0065 }
0066 
0067 void KClock_1x2::updateAlarm(qulonglong time)
0068 {
0069     auto dateTime = QDateTime::fromSecsSinceEpoch(time).toLocalTime();
0070     if (time > 0) {
0071         m_string = QStringLiteral("%1, %2").arg(m_locale.standaloneDayName(dateTime.date().dayOfWeek(), QLocale::ShortFormat),
0072                                                 m_locale.toString(dateTime.time(), QStringLiteral("hh:mm")));
0073         m_hasAlarm = true;
0074     } else {
0075         m_hasAlarm = false;
0076     }
0077     Q_EMIT propertyChanged();
0078 }
0079 
0080 void KClock_1x2::openKClock()
0081 {
0082     m_process = new QProcess(this);
0083     m_process->start(QStringLiteral("kclock"), QStringList());
0084 }
0085 
0086 void KClock_1x2::initialTimeUpdate()
0087 {
0088     Q_EMIT timeChanged();
0089     disconnect(m_timer, &QTimer::timeout, this, &KClock_1x2::initialTimeUpdate); // disconnect
0090     m_timer->setSingleShot(false);
0091     connect(m_timer, &QTimer::timeout, this, &KClock_1x2::timeChanged);
0092     m_timer->start(60000); // update every minute
0093 }
0094 
0095 QString KClock_1x2::date() const
0096 {
0097     return m_locale.toString(QDate::currentDate(), QStringLiteral("ddd, MMM d"));
0098 }
0099 
0100 QDateTime KClock_1x2::datetime() const
0101 {
0102     return QDateTime::currentDateTime();
0103 }
0104 
0105 KClock_1x2::~KClock_1x2()
0106 {
0107 }
0108 
0109 K_PLUGIN_CLASS(KClock_1x2)
0110 
0111 #include "kclock_1x2.moc"