File indexing completed on 2024-04-28 16:54:32

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <KConfigWatcher>
0010 #include <QObject>
0011 
0012 #include "kworkspace_export.h"
0013 #include "sessionmanagement.h"
0014 
0015 class OrgFreedesktopLogin1ManagerInterface;
0016 class OrgFreedesktopUPowerInterface;
0017 class OrgFreedesktopConsoleKitManagerInterface;
0018 
0019 /**
0020  * Performs direct system actions that could kill the session
0021  *
0022  * Semi-internal. Symbols exported, but not the header
0023  * To be used only by the daemon that performs logout (currently ksmserver)
0024  *
0025  * All other users should go via the public SessionManagement that prompts and logs out properly.
0026  */
0027 class KWORKSPACE_EXPORT SessionBackend : public QObject
0028 {
0029     Q_OBJECT
0030 public:
0031     static SessionBackend *self();
0032     virtual SessionManagement::State state() const = 0;
0033 
0034     virtual void shutdown() = 0;
0035     virtual void reboot() = 0;
0036     virtual void suspend() = 0;
0037     virtual void hybridSuspend() = 0;
0038     virtual void hibernate() = 0;
0039 
0040     virtual bool canShutdown() const = 0;
0041     virtual bool canReboot() const = 0;
0042     virtual bool canSuspend() const = 0;
0043     virtual bool canHybridSuspend() const = 0;
0044     virtual bool canHibernate() const = 0;
0045 
0046     virtual bool canSwitchUser() const;
0047 
0048     bool confirmLogout() const;
0049 
0050 Q_SIGNALS:
0051     void stateChanged();
0052     void canShutdownChanged();
0053     void canRebootChanged();
0054     void canSuspendChanged();
0055     void canHybridSuspendChanged();
0056     void canHibernateChanged();
0057 
0058     void aboutToSuspend();
0059     void resumingFromSuspend();
0060 
0061 protected:
0062     SessionBackend();
0063     ~SessionBackend() override = default;
0064 
0065 private:
0066     KConfigWatcher::Ptr m_kserverConfig;
0067 };
0068 
0069 /*
0070  * This class wraps both Logind and CK2
0071  * Abstraction for that is handled in OrgFreedesktopLogin1ManagerInterface
0072  */
0073 class LogindSessionBackend : public SessionBackend
0074 {
0075     Q_OBJECT
0076 public:
0077     static bool exists();
0078     LogindSessionBackend();
0079 
0080     SessionManagement::State state() const override;
0081     void shutdown() override;
0082     void reboot() override;
0083     void suspend() override;
0084     void hybridSuspend() override;
0085     void hibernate() override;
0086     bool canShutdown() const override;
0087     bool canReboot() const override;
0088     bool canSuspend() const override;
0089     bool canHybridSuspend() const override;
0090     bool canHibernate() const override;
0091 
0092 private:
0093     OrgFreedesktopLogin1ManagerInterface *m_login1;
0094     SessionManagement::State m_state = SessionManagement::State::Loading;
0095     bool m_canShutdown = false;
0096     bool m_canReboot = false;
0097     bool m_canSuspend = false;
0098     bool m_canHybridSuspend = false;
0099     bool m_canHibernate = false;
0100     uint m_pendingJobs = 0;
0101 };
0102 
0103 /* Maybe misleadingly named, consolekit doesn't support suspend directly so it's
0104  * suplemented with upower where available
0105  */
0106 class ConsoleKitSessionBackend : public SessionBackend
0107 {
0108     Q_OBJECT
0109 public:
0110     static bool exists();
0111     ConsoleKitSessionBackend();
0112 
0113     SessionManagement::State state() const override;
0114     void shutdown() override;
0115     void reboot() override;
0116     void suspend() override;
0117     void hybridSuspend() override
0118     {
0119     }
0120     void hibernate() override;
0121     bool canShutdown() const override;
0122     bool canReboot() const override;
0123     bool canSuspend() const override;
0124     bool canHybridSuspend() const override
0125     {
0126         return false;
0127     }
0128     bool canHibernate() const override;
0129 
0130 private:
0131     OrgFreedesktopUPowerInterface *m_upower;
0132     OrgFreedesktopConsoleKitManagerInterface *m_ck;
0133 
0134     SessionManagement::State m_state = SessionManagement::State::Loading;
0135     bool m_canShutdown = false;
0136     bool m_canReboot = false;
0137     bool m_canSuspend = false;
0138     bool m_canHibernate = false;
0139     uint m_pendingJobs = 0;
0140 };
0141 
0142 class DummySessionBackend : public SessionBackend
0143 {
0144     Q_OBJECT
0145 public:
0146     DummySessionBackend();
0147 
0148     SessionManagement::State state() const override
0149     {
0150         return SessionManagement::State::Error;
0151     }
0152     void shutdown() override
0153     {
0154     }
0155     void reboot() override
0156     {
0157     }
0158     void suspend() override
0159     {
0160     }
0161     void hybridSuspend() override
0162     {
0163     }
0164     void hibernate() override
0165     {
0166     }
0167     bool canShutdown() const override
0168     {
0169         return false;
0170     }
0171     bool canReboot() const override
0172     {
0173         return false;
0174     }
0175     bool canSuspend() const override
0176     {
0177         return false;
0178     }
0179     bool canHybridSuspend() const override
0180     {
0181         return false;
0182     }
0183     bool canHibernate() const override
0184     {
0185         return false;
0186     }
0187 };
0188 
0189 class TestSessionBackend : public SessionBackend
0190 {
0191     Q_OBJECT
0192 public:
0193     TestSessionBackend();
0194 
0195     SessionManagement::State state() const override
0196     {
0197         return SessionManagement::State::Ready;
0198     }
0199     void shutdown() override;
0200     void reboot() override;
0201     void suspend() override;
0202     void hybridSuspend() override;
0203     void hibernate() override;
0204 
0205     bool canShutdown() const override
0206     {
0207         return true;
0208     }
0209     bool canReboot() const override
0210     {
0211         return true;
0212     }
0213     bool canSuspend() const override
0214     {
0215         return true;
0216     }
0217     bool canHybridSuspend() const override
0218     {
0219         return true;
0220     }
0221     bool canHibernate() const override
0222     {
0223         return true;
0224     }
0225 };