File indexing completed on 2024-04-21 05:27:34

0001 /*
0002 SPDX-FileCopyrightText: 2019 Kevin Ottens <kevin.ottens@enioka.com>
0003 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
0004 
0005 SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kscreensaversettings.h"
0009 
0010 #include <KActionCollection>
0011 #include <KGlobalAccel>
0012 #include <KLocalizedString>
0013 #include <KPackage/Package>
0014 #include <KPackage/PackageLoader>
0015 
0016 #include <QCollator>
0017 
0018 QList<QKeySequence> KScreenSaverSettings::defaultShortcuts()
0019 {
0020     return {Qt::META | Qt::Key_L, Qt::Key_ScreenSaver};
0021 }
0022 
0023 QString KScreenSaverSettings::defaultWallpaperPlugin()
0024 {
0025     return QStringLiteral("org.kde.image");
0026 }
0027 
0028 class KScreenSaverSettingsStore : public QObject
0029 {
0030     Q_OBJECT
0031     Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
0032 public:
0033     KScreenSaverSettingsStore(KScreenSaverSettings *parent)
0034         : QObject(parent)
0035         , m_actionCollection(new KActionCollection(this, QStringLiteral("ksmserver")))
0036         , m_lockAction(nullptr)
0037     {
0038         m_actionCollection->setConfigGlobal(true);
0039         m_actionCollection->setComponentDisplayName(i18n("Session Management"));
0040         m_lockAction = m_actionCollection->addAction(QStringLiteral("Lock Session"));
0041         m_lockAction->setProperty("isConfigurationAction", true);
0042         m_lockAction->setText(i18n("Lock Session"));
0043         KGlobalAccel::self()->setShortcut(m_lockAction, parent->defaultShortcuts());
0044     }
0045 
0046     QKeySequence shortcut() const
0047     {
0048         const QList<QKeySequence> shortcuts = KGlobalAccel::self()->shortcut(m_lockAction);
0049         if (shortcuts.count() > 0) {
0050             return shortcuts.first();
0051         }
0052         return QKeySequence();
0053     }
0054     void setShortcut(const QKeySequence &sequence) const
0055     {
0056         auto shortcuts = KGlobalAccel::self()->shortcut(m_lockAction);
0057         if (shortcuts.isEmpty()) {
0058             shortcuts << QKeySequence();
0059         }
0060         shortcuts[0] = sequence;
0061         KGlobalAccel::self()->setShortcut(m_lockAction, shortcuts, KGlobalAccel::NoAutoloading);
0062     }
0063 
0064 private:
0065     KActionCollection *m_actionCollection;
0066     QAction *m_lockAction;
0067 };
0068 
0069 KScreenSaverSettings &KScreenSaverSettings::getInstance()
0070 {
0071     static KScreenSaverSettings instance;
0072     return instance;
0073 }
0074 
0075 KScreenSaverSettings::KScreenSaverSettings(QObject *parent)
0076     : KScreenSaverSettingsBase()
0077     , m_store(new KScreenSaverSettingsStore(this))
0078 {
0079     setParent(parent);
0080 
0081     const auto wallpaperPackages = KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"));
0082     for (auto &package : wallpaperPackages) {
0083         m_availableWallpaperPlugins.append({package.name(), package.pluginId()});
0084     }
0085     QCollator collator;
0086     collator.setCaseSensitivity(Qt::CaseInsensitive);
0087     std::sort(m_availableWallpaperPlugins.begin(), m_availableWallpaperPlugins.end(), [](const WallpaperInfo &w1, const WallpaperInfo &w2) {
0088         return w1.name < w2.name;
0089     });
0090 
0091     auto shortcutItem = new KPropertySkeletonItem(m_store, "shortcut", defaultShortcuts().first());
0092     addItem(shortcutItem, QStringLiteral("shortcut"));
0093     shortcutItem->setNotifyFunction([this] {
0094         Q_EMIT shortcutChanged();
0095     });
0096 }
0097 
0098 KScreenSaverSettings::~KScreenSaverSettings()
0099 {
0100 }
0101 
0102 QList<WallpaperInfo> KScreenSaverSettings::availableWallpaperPlugins() const
0103 {
0104     return m_availableWallpaperPlugins;
0105 }
0106 
0107 QKeySequence KScreenSaverSettings::shortcut() const
0108 {
0109     return findItem(QStringLiteral("shortcut"))->property().value<QKeySequence>();
0110 }
0111 
0112 void KScreenSaverSettings::setShortcut(const QKeySequence &sequence)
0113 {
0114     findItem(QStringLiteral("shortcut"))->setProperty(sequence);
0115 }
0116 
0117 #include "kscreensaversettings.moc"
0118 
0119 #include "moc_kscreensaversettings.cpp"