File indexing completed on 2024-12-22 05:15:23

0001 /*
0002     SPDX-FileCopyrightText: 2015 Eike Hein <hein@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "systementry.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusInterface>
0011 #include <QDBusPendingCall>
0012 
0013 #include <KAuthorized>
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 #include <sessionmanagement.h>
0018 
0019 int SystemEntry::s_instanceCount = 0;
0020 SessionManagement *SystemEntry::s_sessionManagement = nullptr;
0021 
0022 SystemEntry::SystemEntry(AbstractModel *owner, Action action)
0023     : AbstractEntry(owner)
0024     , m_initialized(false)
0025     , m_action(action)
0026     , m_valid(false)
0027 {
0028     refresh();
0029     ++s_instanceCount;
0030     m_initialized = true;
0031 }
0032 
0033 SystemEntry::SystemEntry(AbstractModel *owner, const QString &id)
0034     : AbstractEntry(owner)
0035     , m_initialized(false)
0036     , m_action(NoAction)
0037     , m_valid(false)
0038 {
0039     if (id == QLatin1String("lock-screen")) {
0040         m_action = LockSession;
0041     } else if (id == QLatin1String("logout")) {
0042         m_action = LogoutSession;
0043     } else if (id == QLatin1String("save-session")) {
0044         m_action = SaveSession;
0045     } else if (id == QLatin1String("switch-user")) {
0046         m_action = SwitchUser;
0047     } else if (id == QLatin1String("suspend")) {
0048         m_action = Suspend;
0049     } else if (id == QLatin1String("hibernate")) {
0050         m_action = Hibernate;
0051     } else if (id == QLatin1String("reboot")) {
0052         m_action = Reboot;
0053     } else if (id == QLatin1String("shutdown")) {
0054         m_action = Shutdown;
0055     }
0056 
0057     refresh();
0058     ++s_instanceCount;
0059     m_initialized = true;
0060 }
0061 
0062 SystemEntry::~SystemEntry()
0063 {
0064     --s_instanceCount;
0065 
0066     if (!s_instanceCount) {
0067         delete s_sessionManagement;
0068         s_sessionManagement = nullptr;
0069     }
0070 }
0071 
0072 SystemEntry::Action SystemEntry::action() const
0073 {
0074     return m_action;
0075 }
0076 
0077 void SystemEntry::refresh()
0078 {
0079     if (!s_sessionManagement) {
0080         s_sessionManagement = new SessionManagement();
0081         QObject::connect(s_sessionManagement, &SessionManagement::stateChanged, this, &SystemEntry::sessionManagementStateChanged);
0082     }
0083 
0084     bool valid = false;
0085 
0086     switch (m_action) {
0087     case LockSession: {
0088         valid = s_sessionManagement->canLock();
0089         QObject::connect(s_sessionManagement, &SessionManagement::canLockChanged, this, &SystemEntry::refresh);
0090         break;
0091     }
0092     case LogoutSession: {
0093         valid = s_sessionManagement->canLogout();
0094         QObject::connect(s_sessionManagement, &SessionManagement::canLogoutChanged, this, &SystemEntry::refresh);
0095         break;
0096     }
0097     case SaveSession: {
0098         valid = s_sessionManagement->canSaveSession();
0099         QObject::connect(s_sessionManagement, &SessionManagement::canSaveSessionChanged, this, &SystemEntry::refresh);
0100         break;
0101     }
0102     case SwitchUser: {
0103         valid = s_sessionManagement->canSwitchUser();
0104         QObject::connect(s_sessionManagement, &SessionManagement::canSwitchUserChanged, this, &SystemEntry::refresh);
0105         break;
0106     }
0107     case Suspend: {
0108         valid = s_sessionManagement->canSuspend();
0109         QObject::connect(s_sessionManagement, &SessionManagement::canSuspendChanged, this, &SystemEntry::refresh);
0110         break;
0111     }
0112     case Hibernate: {
0113         valid = s_sessionManagement->canHibernate();
0114         QObject::connect(s_sessionManagement, &SessionManagement::canHibernateChanged, this, &SystemEntry::refresh);
0115         break;
0116     }
0117     case Reboot: {
0118         valid = s_sessionManagement->canReboot();
0119         QObject::connect(s_sessionManagement, &SessionManagement::canRebootChanged, this, &SystemEntry::refresh);
0120         break;
0121     }
0122     case Shutdown: {
0123         valid = s_sessionManagement->canShutdown();
0124         QObject::connect(s_sessionManagement, &SessionManagement::canShutdownChanged, this, &SystemEntry::refresh);
0125         break;
0126     }
0127     default:
0128         break;
0129     }
0130 
0131     if (m_valid != valid) {
0132         m_valid = valid;
0133 
0134         if (m_initialized) {
0135             Q_EMIT isValidChanged();
0136         }
0137     }
0138 }
0139 
0140 bool SystemEntry::isValid() const
0141 {
0142     return m_valid;
0143 }
0144 
0145 QString SystemEntry::icon() const
0146 {
0147     const QString &name = iconName();
0148 
0149     if (!name.isEmpty()) {
0150         return name;
0151     }
0152 
0153     return QStringLiteral("unknown");
0154 }
0155 
0156 QString SystemEntry::iconName() const
0157 {
0158     switch (m_action) {
0159     case LockSession:
0160         return QStringLiteral("system-lock-screen");
0161         break;
0162     case LogoutSession:
0163         return QStringLiteral("system-log-out");
0164         break;
0165     case SaveSession:
0166         return QStringLiteral("system-save-session");
0167         break;
0168     case SwitchUser:
0169         return QStringLiteral("system-switch-user");
0170         break;
0171     case Suspend:
0172         return QStringLiteral("system-suspend");
0173         break;
0174     case Hibernate:
0175         return QStringLiteral("system-suspend-hibernate");
0176         break;
0177     case Reboot:
0178         return QStringLiteral("system-reboot");
0179         break;
0180     case Shutdown:
0181         return QStringLiteral("system-shutdown");
0182         break;
0183     default:
0184         break;
0185     }
0186 
0187     return QString();
0188 }
0189 
0190 QString SystemEntry::name() const
0191 {
0192     switch (m_action) {
0193     case LockSession:
0194         return i18n("Lock");
0195         break;
0196     case LogoutSession:
0197         return i18n("Log Out");
0198         break;
0199     case SaveSession:
0200         return i18n("Save Session");
0201         break;
0202     case SwitchUser:
0203         return i18n("Switch User");
0204         break;
0205     case Suspend:
0206         return i18nc("Suspend to RAM", "Sleep");
0207         break;
0208     case Hibernate:
0209         return i18n("Hibernate");
0210         break;
0211     case Reboot:
0212         return i18n("Restart");
0213         break;
0214     case Shutdown:
0215         return i18n("Shut Down");
0216         break;
0217     default:
0218         break;
0219     }
0220 
0221     return QString();
0222 }
0223 
0224 QString SystemEntry::group() const
0225 {
0226     switch (m_action) {
0227     case LockSession:
0228     case LogoutSession:
0229     case SaveSession:
0230     case SwitchUser:
0231         return i18n("Session");
0232     case Suspend:
0233     case Hibernate:
0234     case Reboot:
0235     case Shutdown:
0236         return i18n("System");
0237     default:
0238         break;
0239     }
0240 
0241     return QString();
0242 }
0243 
0244 QString SystemEntry::description() const
0245 {
0246     switch (m_action) {
0247     case LockSession:
0248         return i18n("Lock screen");
0249         break;
0250     case LogoutSession:
0251         return i18n("End session");
0252         break;
0253     case SaveSession:
0254         return i18n("Save Session");
0255         break;
0256     case SwitchUser:
0257         return i18n("Start a parallel session as a different user");
0258         break;
0259     case Suspend:
0260         return i18n("Suspend to RAM");
0261         break;
0262     case Hibernate:
0263         return i18n("Suspend to disk");
0264         break;
0265     case Reboot:
0266         return i18n("Restart computer");
0267         break;
0268     case Shutdown:
0269         return i18n("Turn off computer");
0270         break;
0271     default:
0272         break;
0273     }
0274 
0275     return QString();
0276 }
0277 
0278 QString SystemEntry::id() const
0279 {
0280     switch (m_action) {
0281     case LockSession:
0282         return QStringLiteral("lock-screen");
0283         break;
0284     case LogoutSession:
0285         return QStringLiteral("logout");
0286         break;
0287     case SaveSession:
0288         return QStringLiteral("save-session");
0289         break;
0290     case SwitchUser:
0291         return QStringLiteral("switch-user");
0292         break;
0293     case Suspend:
0294         return QStringLiteral("suspend");
0295         break;
0296     case Hibernate:
0297         return QStringLiteral("hibernate");
0298         break;
0299     case Reboot:
0300         return QStringLiteral("reboot");
0301         break;
0302     case Shutdown:
0303         return QStringLiteral("shutdown");
0304         break;
0305 
0306     default:
0307         break;
0308     }
0309 
0310     return QString();
0311 }
0312 
0313 bool SystemEntry::run(const QString &actionId, const QVariant &argument)
0314 {
0315     Q_UNUSED(actionId)
0316     Q_UNUSED(argument)
0317 
0318     if (!m_valid) {
0319         return false;
0320     }
0321 
0322     switch (m_action) {
0323     case LockSession:
0324         s_sessionManagement->lock();
0325         break;
0326     case LogoutSession:
0327         s_sessionManagement->requestLogout();
0328         break;
0329     case SaveSession:
0330         s_sessionManagement->saveSession();
0331         break;
0332     case SwitchUser:
0333         s_sessionManagement->switchUser();
0334         break;
0335     case Suspend:
0336         s_sessionManagement->suspend();
0337         break;
0338     case Hibernate:
0339         s_sessionManagement->hibernate();
0340         break;
0341     case Reboot:
0342         s_sessionManagement->requestReboot();
0343         break;
0344     case Shutdown:
0345         s_sessionManagement->requestShutdown();
0346         break;
0347     default:
0348         break;
0349     }
0350 
0351     return true;
0352 }