File indexing completed on 2024-04-21 05:26:43

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2016 Kai Uwe Broulik <kde@privat.broulik.de>
0004     SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
0005 */
0006 
0007 #pragma once
0008 
0009 #include <chrono>
0010 
0011 #include <QDBusServiceWatcher>
0012 #include <QTimer>
0013 
0014 using namespace std::chrono_literals;
0015 
0016 class DBusServiceWatcher : public QObject
0017 {
0018     Q_OBJECT
0019 public:
0020     using QObject::QObject;
0021 
0022     virtual void start();
0023     QList<QString> serviceNames() const;
0024 
0025 Q_SIGNALS:
0026     void serviceUnregistered();
0027     void serviceRegistered();
0028 
0029 private:
0030     // org.kde.StatusNotifierWatcher (kded5): SNI won't be registered
0031     // org.freedesktop.Notifications (plasmashell): SNI won't be visualized
0032     const QList<QString> m_serviceNames{QStringLiteral("org.kde.StatusNotifierWatcher"), QStringLiteral("org.freedesktop.Notifications")};
0033     QDBusServiceWatcher *m_watcher = nullptr;
0034 };
0035 
0036 class IdleWatcher : public QObject
0037 {
0038     Q_OBJECT
0039 public:
0040     using QObject::QObject;
0041 
0042     virtual void start();
0043 
0044 Q_SIGNALS:
0045     void idle();
0046     void notIdle();
0047 };
0048 
0049 // Somewhat horrible container to track automatic activation and close conditions.
0050 // This tracks the availability of dbus interfaces that are required for visualization
0051 // of the an SNI, if they are missing then drkonqi may auto activate the window.
0052 // This also tracks system idleness as we want to prevent auto closing of the SNI
0053 // when the user isn't at the system.
0054 // If neither is applicable the SNI gets auto closed after a while.
0055 class ActivationCloseTimer : public QObject
0056 {
0057     Q_OBJECT
0058 public:
0059     using QObject::QObject;
0060 
0061     void start(DBusServiceWatcher *dbusWatcher, IdleWatcher *idleWatcher);
0062 
0063     void setActivationTimeout(std::chrono::milliseconds timeout);
0064     void setCloseTimeout(std::chrono::milliseconds timeout);
0065 
0066 Q_SIGNALS:
0067     void autoClose();
0068     void autoActivate();
0069 
0070 private Q_SLOTS:
0071     void watchDBus(DBusServiceWatcher *watcher);
0072     void watchIdle(IdleWatcher *idleWatcher);
0073 
0074     void refresh();
0075 
0076 private:
0077     int m_pendingActivations = 0;
0078     QTimer m_closeTimer;
0079     bool m_idle = false;
0080     std::chrono::milliseconds m_activationTimeout = 10s;
0081     std::chrono::milliseconds m_closeTimeout = 1min;
0082 };