add more spacing
[personal-kdebase.git] / workspace / plasma / containments / screensaver / desktop.cpp
blobbbb58f35f73b5fd1117c6023060aad8146491112
1 /*
2 * Copyright 2007 by Aaron Seigo <aseigo@kde.org>
3 * Copyright 2008 by Chani Armitage <chanika@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License version 2,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "desktop.h"
22 #include <QAction>
23 #include <QApplication>
24 #include <QDBusConnection>
25 #include <QDBusInterface>
26 #include <QDBusMessage>
28 #include <KDebug>
29 #include <KIcon>
31 #include "plasma/corona.h"
32 #include "plasma/theme.h"
34 using namespace Plasma;
36 SaverDesktop::SaverDesktop(QObject *parent, const QVariantList &args)
37 : Containment(parent, args),
38 m_lockDesktopAction(0),
39 m_appletBrowserAction(0)
43 SaverDesktop::~SaverDesktop()
47 void SaverDesktop::init()
49 Containment::init();
51 bool unlocked = immutability() == Mutable;
52 //re-wire the lock action so we can check for a password
53 QAction *lock = action("lock widgets");
54 if (lock) {
55 lock->disconnect(this);
56 connect(lock, SIGNAL(triggered(bool)), this, SLOT(toggleLock()));
57 lock->setText(unlocked ? i18n("Lock") : i18n("Unlock"));
60 //remove the desktop actions
61 //FIXME do we really need to removeToolBoxAction?
62 QAction *unwanted = action("zoom in");
63 removeToolBoxAction(unwanted);
64 delete unwanted;
65 unwanted = action("zoom out");
66 removeToolBoxAction(unwanted);
67 delete unwanted;
68 unwanted = action("add sibling containment");
69 removeToolBoxAction(unwanted);
70 delete unwanted;
72 lock = new QAction(unlocked ? i18n("Quit") : i18n("Unlock and Quit"), this);
73 lock->setIcon(KIcon("system-lock-screen"));
74 //TODO kbd shortcut
75 lock->setShortcutContext(Qt::WidgetShortcut);
76 lock->setShortcut(QKeySequence("esc"));
77 connect(lock, SIGNAL(triggered(bool)), this, SLOT(unlockDesktop()));
78 addAction("unlock desktop", lock);
79 addToolBoxAction(lock);
81 QAction *a = action("configure");
82 if (a) {
83 a->setText(i18n("Settings"));
84 addToolBoxAction(a);
87 //rearrange the toolboxtools
88 a = action("add widgets");
89 if (a) {
90 removeToolBoxAction(a);
91 addToolBoxAction(a);
95 void SaverDesktop::constraintsEvent(Plasma::Constraints constraints)
97 if (constraints & Plasma::ImmutableConstraint) {
98 bool unlocked = immutability() == Mutable;
99 QAction *a = action("lock widgets");
100 if (a) {
101 a->setText(unlocked ? i18n("Lock") : i18n("Unlock"));
103 a = action("unlock desktop");
104 if (a) {
105 a->setText(unlocked ? i18n("Quit") : i18n("Unlock and Quit"));
110 QList<QAction*> SaverDesktop::contextualActions()
112 if (!m_appletBrowserAction) {
113 m_appletBrowserAction = action("add widgets");
114 m_lockDesktopAction = action("lock widgets");
116 QAction *config = action("configure");
117 QAction *quit = action("unlock desktop");
119 QList<QAction*> actions;
120 actions.append(m_appletBrowserAction);
121 if (config) {
122 actions.append(config);
124 actions.append(m_lockDesktopAction);
125 actions.append(quit);
127 return actions;
130 void SaverDesktop::toggleLock()
132 //requre a password to unlock
133 if (!corona()) {
134 return; //I'm lazy, I know this'll never happen
136 QDBusInterface lockprocess("org.kde.screenlocker", "/LockProcess",
137 "org.kde.screenlocker.LockProcess", QDBusConnection::sessionBus(), this);
138 if (corona()->immutability() == Mutable) {
139 corona()->setImmutability(UserImmutable);
140 lockprocess.call(QDBus::NoBlock, "startLock");
141 kDebug() << "blaaaaaaaaaaaaaaaaa!!!!";
142 emit locked();
143 } else if (corona()->immutability() == UserImmutable) {
144 QList<QVariant> args;
145 args << i18n("Unlock Plasma Widgets");
146 bool sent = lockprocess.callWithCallback("checkPass", args, this, SLOT(unlock(QDBusMessage)), SLOT(dbusError(QDBusError)));
147 kDebug() << sent;
151 void SaverDesktop::unlock(QDBusMessage reply)
153 //assuming everything went as expected
154 if (reply.arguments().isEmpty()) {
155 kDebug() << "quit succeeded, I guess";
156 return;
158 bool success = reply.arguments().first().toBool();
159 kDebug() << success;
160 if (success) {
161 corona()->setImmutability(Mutable);
162 emit unlocked(); //FIXME bad code
166 void SaverDesktop::dbusError(QDBusError error)
168 //Q_UNUSED(error)
169 kDebug() << error.errorString(error.type());
170 kDebug() << "bailing out";
171 //ok, now i care. if it was the quit call and it failed, we shouldn't leave the user stuck in
172 //plasma-overlay forever.
173 qApp->quit();
176 void SaverDesktop::unlockDesktop()
178 QDBusInterface lockprocess("org.kde.screenlocker", "/LockProcess",
179 "org.kde.screenlocker.LockProcess", QDBusConnection::sessionBus(), this);
180 bool sent = (lockprocess.isValid() &&
181 lockprocess.callWithCallback("quit", QList<QVariant>(), this, SLOT(unlock(QDBusMessage)), SLOT(dbusError(QDBusError))));
182 //the unlock slot above is a dummy that should never be called.
183 //somehow I need a valid reply slot or the error slot is never ever used.
184 if (!sent) {
185 //ah crud.
186 kDebug() << "bailing out!";
187 qApp->quit();
191 K_EXPORT_PLASMA_APPLET(saverdesktop, SaverDesktop)
193 #include "desktop.moc"