1 /********************************************************************
2 This file is part of the KDE project.
3
4 Copyright 2019 Kevin Ottens <kevin.ottens@enioka.com>
5 Copyright (C) 2020 David Redondo <kde@david-redondo.de>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20
21 #include "kscreensaversettings.h"
22
23 #include <KActionCollection>
24 #include <KGlobalAccel>
25 #include <KLocalizedString>
26 #include <KPackage/Package>
27 #include <KPackage/PackageLoader>
28
29 #include <QCollator>
30
defaultShortcuts()31 QList<QKeySequence> KScreenSaverSettings::defaultShortcuts()
32 {
33 return {Qt::META | Qt::Key_L, Qt::ALT | Qt::CTRL | Qt::Key_L, Qt::Key_ScreenSaver};
34 }
35
defaultWallpaperPlugin()36 QString KScreenSaverSettings::defaultWallpaperPlugin()
37 {
38 return QStringLiteral("org.kde.image");
39 }
40
41 class KScreenSaverSettingsStore : public QObject
42 {
43 Q_OBJECT
44 Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
45 public:
KScreenSaverSettingsStore(KScreenSaverSettings * parent)46 KScreenSaverSettingsStore(KScreenSaverSettings *parent)
47 : QObject(parent)
48 , m_actionCollection(new KActionCollection(this, QStringLiteral("ksmserver")))
49 , m_lockAction(nullptr)
50 {
51 m_actionCollection->setConfigGlobal(true);
52 m_actionCollection->setComponentDisplayName(i18n("Session Management"));
53 m_lockAction = m_actionCollection->addAction(QStringLiteral("Lock Session"));
54 m_lockAction->setProperty("isConfigurationAction", true);
55 m_lockAction->setText(i18n("Lock Session"));
56 KGlobalAccel::self()->setShortcut(m_lockAction, parent->defaultShortcuts());
57 }
58
shortcut() const59 QKeySequence shortcut() const
60 {
61 const QList<QKeySequence> shortcuts = KGlobalAccel::self()->shortcut(m_lockAction);
62 if (shortcuts.count() > 0) {
63 return shortcuts.first();
64 }
65 return QKeySequence();
66 }
setShortcut(const QKeySequence & sequence) const67 void setShortcut(const QKeySequence &sequence) const
68 {
69 auto shortcuts = KGlobalAccel::self()->shortcut(m_lockAction);
70 if (shortcuts.isEmpty()) {
71 shortcuts << QKeySequence();
72 }
73 shortcuts[0] = sequence;
74 KGlobalAccel::self()->setShortcut(m_lockAction, shortcuts, KGlobalAccel::NoAutoloading);
75 }
76
77 private:
78 KActionCollection *m_actionCollection;
79 QAction *m_lockAction;
80 };
81
getInstance()82 KScreenSaverSettings &KScreenSaverSettings::getInstance()
83 {
84 static KScreenSaverSettings instance;
85 return instance;
86 }
87
KScreenSaverSettings(QObject * parent)88 KScreenSaverSettings::KScreenSaverSettings(QObject *parent)
89 : KScreenSaverSettingsBase()
90 , m_store(new KScreenSaverSettingsStore(this))
91 {
92 setParent(parent);
93
94 const auto wallpaperPackages = KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"));
95 for (auto &package : wallpaperPackages) {
96 m_availableWallpaperPlugins.append({package.name(), package.pluginId()});
97 }
98 QCollator collator;
99 collator.setCaseSensitivity(Qt::CaseInsensitive);
100 std::sort(m_availableWallpaperPlugins.begin(), m_availableWallpaperPlugins.end(), [](const WallpaperInfo &w1, const WallpaperInfo &w2) {
101 return w1.name < w2.name;
102 });
103
104 auto shortcutItem = new KPropertySkeletonItem(m_store, "shortcut", defaultShortcuts().first());
105 addItem(shortcutItem, QStringLiteral("shortcut"));
106 shortcutItem->setNotifyFunction([this] {
107 Q_EMIT shortcutChanged();
108 });
109 }
110
~KScreenSaverSettings()111 KScreenSaverSettings::~KScreenSaverSettings()
112 {
113 }
114
availableWallpaperPlugins() const115 QVector<WallpaperInfo> KScreenSaverSettings::availableWallpaperPlugins() const
116 {
117 return m_availableWallpaperPlugins;
118 }
119
shortcut() const120 QKeySequence KScreenSaverSettings::shortcut() const
121 {
122 return findItem(QStringLiteral("shortcut"))->property().value<QKeySequence>();
123 }
124
setShortcut(const QKeySequence & sequence)125 void KScreenSaverSettings::setShortcut(const QKeySequence &sequence)
126 {
127 findItem(QStringLiteral("shortcut"))->setProperty(sequence);
128 }
129
130 #include "kscreensaversettings.moc"
131