~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/lock_logout/lockout.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com>           *
 
3
 *   Copyright (C) 2009 by Frederik Gladhorn <gladhorn@kde.org>            *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
19
 ***************************************************************************/
 
20
 
 
21
#include "lockout.h"
 
22
 
 
23
// Plasma
 
24
#include <Plasma/IconWidget>
 
25
#include <Plasma/ToolTipManager>
 
26
 
 
27
// Qt
 
28
#include <QtGui/QWidget> // QWIDGETSIZE_MAX
 
29
#include <QtDBus/QDBusInterface>
 
30
#include <QtDBus/QDBusReply>
 
31
#include <QtDBus/QDBusConnectionInterface>
 
32
#include <QGraphicsLinearLayout>
 
33
 
 
34
// KDE
 
35
#include <KDebug>
 
36
#include <KIcon>
 
37
#include <KJob>
 
38
#include <KAuthorized>
 
39
#ifndef Q_OS_WIN
 
40
#include <KConfigDialog>
 
41
#include <KSharedConfig>
 
42
#include <kworkspace/kworkspace.h>
 
43
#include <krunner_interface.h>
 
44
#include <screensaver_interface.h>
 
45
#endif
 
46
 
 
47
// Windows
 
48
#ifdef Q_OS_WIN
 
49
#include <windows.h>
 
50
#endif // Q_OS_WIN
 
51
 
 
52
static const int MINBUTTONSIZE = 16;
 
53
static const int MARGINSIZE = 1;
 
54
 
 
55
LockOut::LockOut(QObject *parent, const QVariantList &args)
 
56
    : Plasma::Applet(parent, args), 
 
57
      m_iconLock(0), 
 
58
      m_iconSwitchUser(0), 
 
59
      m_iconLogout(0), 
 
60
      m_iconSleep(0), 
 
61
      m_iconHibernate(0),
 
62
      m_changed(false)
 
63
{
 
64
#ifndef Q_OS_WIN
 
65
    setHasConfigurationInterface(true);
 
66
#endif
 
67
    setAspectRatioMode(Plasma::IgnoreAspectRatio);
 
68
}
 
69
 
 
70
void LockOut::init()
 
71
{
 
72
    m_layout = new QGraphicsLinearLayout(this);
 
73
    m_layout->setContentsMargins(0,0,0,0);
 
74
    m_layout->setSpacing(0);
 
75
 
 
76
    configChanged();
 
77
}
 
78
 
 
79
void LockOut::configChanged()
 
80
{
 
81
    #ifndef Q_OS_WIN
 
82
        KConfigGroup cg = config();
 
83
        m_showLockButton = cg.readEntry("showLockButton", true);
 
84
        m_showSwitchUserButton = cg.readEntry("showSwitchUserButton", false);
 
85
        m_showLogoutButton = cg.readEntry("showLogoutButton", true);
 
86
        m_showSleepButton = cg.readEntry("showSleepButton", false);
 
87
        m_showHibernateButton = cg.readEntry("showHibernateButton", false);
 
88
    #endif
 
89
    countButtons();
 
90
 
 
91
    if (m_visibleButtons == 0 ) {
 
92
        m_showLockButton = true;
 
93
        m_showSwitchUserButton = false;
 
94
        m_showLogoutButton = true;
 
95
        m_showSleepButton =  false;
 
96
        m_showHibernateButton =  false;
 
97
        countButtons();
 
98
    }
 
99
    showButtons();
 
100
}
 
101
void LockOut::buttonChanged()
 
102
{
 
103
#ifndef Q_WS_WIN
 
104
    if (m_showLockButton != ui.checkBox_lock->isChecked()) {
 
105
       m_showLockButton = !m_showLockButton;
 
106
       m_changed = true;
 
107
    }
 
108
 
 
109
    if (m_showSwitchUserButton != ui.checkBox_switchUser->isChecked()) {
 
110
        m_showSwitchUserButton = !m_showSwitchUserButton;
 
111
        m_changed = true;
 
112
    }
 
113
 
 
114
    if (m_showLogoutButton != ui.checkBox_logout->isChecked()) {
 
115
        m_showLogoutButton = !m_showLogoutButton;
 
116
        m_changed = true;
 
117
    }
 
118
 
 
119
    if (m_showSleepButton != ui.checkBox_sleep->isChecked()) {
 
120
        m_showSleepButton = !m_showSleepButton;
 
121
        m_changed = true;
 
122
    }
 
123
 
 
124
    if (m_showHibernateButton != ui.checkBox_hibernate->isChecked()) {
 
125
        m_showHibernateButton = !m_showHibernateButton;
 
126
        m_changed = true;
 
127
    }
 
128
 
 
129
    setCheckable();
 
130
 
 
131
    emit configUiChanged();
 
132
#endif
 
133
}
 
134
void LockOut::setCheckable()
 
135
{
 
136
#ifndef Q_WS_WIN
 
137
    countButtons();
 
138
    if (m_visibleButtons == 1) {
 
139
        if (ui.checkBox_lock->isChecked()) {
 
140
            ui.checkBox_lock->setEnabled(false);
 
141
        }
 
142
        if (ui.checkBox_switchUser->isChecked()) {
 
143
            ui.checkBox_switchUser->setEnabled(false);
 
144
        }
 
145
        if (ui.checkBox_logout->isChecked()) {
 
146
            ui.checkBox_logout->setEnabled(false);
 
147
        }
 
148
        if (ui.checkBox_sleep->isChecked()) {
 
149
            ui.checkBox_sleep->setEnabled(false);
 
150
        }
 
151
        if(ui.checkBox_hibernate->isChecked()) {
 
152
            ui.checkBox_hibernate->setEnabled(false);
 
153
        }
 
154
    } else {
 
155
        ui.checkBox_lock->setEnabled(true);
 
156
        ui.checkBox_switchUser->setEnabled(true);
 
157
        ui.checkBox_logout->setEnabled(true);
 
158
        ui.checkBox_sleep->setEnabled(true);
 
159
        ui.checkBox_hibernate->setEnabled(true);
 
160
    }
 
161
#endif
 
162
}
 
163
 
 
164
void LockOut::countButtons()
 
165
{
 
166
    m_visibleButtons = 0;
 
167
 
 
168
    if (m_showLockButton) {
 
169
        m_visibleButtons++;
 
170
    }
 
171
 
 
172
    if (m_showSwitchUserButton) {
 
173
        m_visibleButtons++;
 
174
    }
 
175
 
 
176
    if (m_showLogoutButton) {
 
177
        m_visibleButtons++;
 
178
    }
 
179
 
 
180
    if (m_showSleepButton) {
 
181
        m_visibleButtons++;
 
182
    }
 
183
 
 
184
    if (m_showHibernateButton) {
 
185
        m_visibleButtons++;
 
186
    }
 
187
}
 
188
 
 
189
LockOut::~LockOut()
 
190
{
 
191
}
 
192
 
 
193
void LockOut::checkLayout()
 
194
{
 
195
    qreal left,top, right, bottom;
 
196
    getContentsMargins(&left,&top, &right, &bottom);
 
197
    int width = geometry().width() - left - right;
 
198
    int height = geometry().height() - top - bottom;
 
199
 
 
200
    Qt::Orientation direction = Qt::Vertical;
 
201
    int minWidth = 0;
 
202
    int minHeight = 0;
 
203
 
 
204
    switch (formFactor()) {
 
205
        case Plasma::Vertical:
 
206
            if (width >= (MINBUTTONSIZE + MARGINSIZE) * m_visibleButtons) {
 
207
                direction = Qt::Horizontal;
 
208
                height = qMax(width / m_visibleButtons, MINBUTTONSIZE);
 
209
                minHeight = MINBUTTONSIZE;
 
210
            } else {
 
211
                minHeight = MINBUTTONSIZE * m_visibleButtons + top + bottom;
 
212
            }
 
213
            break;
 
214
 
 
215
        case Plasma::Horizontal:
 
216
            if (height < (MINBUTTONSIZE + MARGINSIZE) * m_visibleButtons) {
 
217
                direction = Qt::Horizontal;
 
218
                minWidth = MINBUTTONSIZE * m_visibleButtons + left + right;
 
219
            } else {
 
220
                width = qMax(height / m_visibleButtons, MINBUTTONSIZE);
 
221
                minWidth = MINBUTTONSIZE;
 
222
            }
 
223
            break;
 
224
 
 
225
        default:
 
226
            if (width > height) {
 
227
                direction = Qt::Horizontal;
 
228
                minWidth = MINBUTTONSIZE * m_visibleButtons + left + right;
 
229
                minHeight = MINBUTTONSIZE + top + bottom;
 
230
            } else {
 
231
                minWidth = MINBUTTONSIZE + left + right;
 
232
                minHeight = MINBUTTONSIZE * m_visibleButtons + top + bottom;
 
233
            }
 
234
            break;
 
235
    }
 
236
 
 
237
    m_layout->setOrientation(direction);
 
238
    setMinimumSize(minWidth, minHeight);
 
239
    if (direction == Qt::Horizontal) {
 
240
        setPreferredSize(height * m_visibleButtons + left + right, height + top + bottom);
 
241
    } else {
 
242
        setPreferredSize(width + left + right, width * m_visibleButtons + top + bottom);
 
243
    }
 
244
}
 
245
 
 
246
void LockOut::constraintsEvent(Plasma::Constraints constraints)
 
247
{
 
248
    if (constraints & Plasma::FormFactorConstraint ||
 
249
        constraints & Plasma::SizeConstraint) {
 
250
        checkLayout();
 
251
    }
 
252
}
 
253
 
 
254
void LockOut::clickLock()
 
255
{
 
256
    kDebug()<<"LockOut:: lock clicked ";
 
257
 
 
258
#ifndef Q_OS_WIN
 
259
    QString interface("org.freedesktop.ScreenSaver");
 
260
    org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
 
261
                                              QDBusConnection::sessionBus());
 
262
    if (screensaver.isValid()) {
 
263
        screensaver.Lock();
 
264
    }
 
265
#else
 
266
    LockWorkStation();
 
267
#endif // !Q_OS_WIN
 
268
}
 
269
 
 
270
void LockOut::clickLogout()
 
271
{
 
272
    if (!KAuthorized::authorizeKAction("logout")) {
 
273
        return;
 
274
    }
 
275
 
 
276
    kDebug()<<"LockOut:: logout clicked ";
 
277
#ifndef Q_OS_WIN
 
278
    KWorkSpace::requestShutDown( KWorkSpace::ShutdownConfirmDefault,
 
279
                                 KWorkSpace::ShutdownTypeDefault,
 
280
                                 KWorkSpace::ShutdownModeDefault);
 
281
#endif
 
282
}
 
283
 
 
284
void LockOut::clickSwitchUser()
 
285
{
 
286
#ifndef Q_OS_WIN
 
287
    // Taken from kickoff/core/itemhandlers.cpp
 
288
    QString interface("org.kde.krunner");
 
289
    org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
 
290
    krunner.switchUser();
 
291
#endif
 
292
}
 
293
 
 
294
#include <KMessageBox>
 
295
 
 
296
void LockOut::clickSleep()
 
297
{
 
298
    if (KMessageBox::questionYesNo(0,
 
299
                                   i18n("Do you want to suspend to RAM (sleep)?"),
 
300
                                   i18n("Suspend"))
 
301
            != KMessageBox::Yes) {
 
302
        return;
 
303
    }
 
304
    // Check if KDE Power Management System is running, and use its methods to suspend if available
 
305
    if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
 
306
        kDebug() << "Using KDE Power Management System to suspend";
 
307
        QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
 
308
                                                           "/org/kde/Solid/PowerManagement",
 
309
                                                           "org.kde.Solid.PowerManagement",
 
310
                                                           "suspendToRam");
 
311
        QDBusConnection::sessionBus().asyncCall(call);
 
312
    } else {
 
313
        kDebug() << "KDE Power Management System not available, suspend failed";
 
314
    }
 
315
}
 
316
 
 
317
void LockOut::clickHibernate()
 
318
{
 
319
    if (KMessageBox::questionYesNo(0,
 
320
                                   i18n("Do you want to suspend to disk (hibernate)?"),
 
321
                                   i18n("Hibernate"))
 
322
            != KMessageBox::Yes) {
 
323
        return;
 
324
    }
 
325
    // Check if KDE Power Management System is running, and use its methods to hibernate if available
 
326
    if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
 
327
        kDebug() << "Using KDE Power Management System to hibernate";
 
328
        QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
 
329
                                                           "/org/kde/Solid/PowerManagement",
 
330
                                                           "org.kde.Solid.PowerManagement",
 
331
                                                           "suspendToDisk");
 
332
        QDBusConnection::sessionBus().asyncCall(call);
 
333
    } else {
 
334
        kDebug() << "KDE Power Management System not available, hibernate failed";
 
335
    }
 
336
}
 
337
 
 
338
void LockOut::configAccepted()
 
339
{
 
340
#ifndef Q_OS_WIN
 
341
    KConfigGroup cg = config();
 
342
    if (m_changed) {
 
343
        int oldButtonCount = m_visibleButtons;
 
344
        countButtons();
 
345
        if(m_visibleButtons == 0) {
 
346
            configChanged(); // if no button was selected, reject configuration and reload previous
 
347
            return;
 
348
        }
 
349
        
 
350
        cg.writeEntry("showHibernateButton", m_showHibernateButton);
 
351
        cg.writeEntry("showSleepButton", m_showSleepButton);
 
352
        cg.writeEntry("showLogoutButton", m_showLogoutButton);
 
353
        cg.writeEntry("showSwitchUserButton", m_showSwitchUserButton);
 
354
        cg.writeEntry("showLockButton", m_showLockButton);
 
355
        
 
356
        showButtons();
 
357
        if (formFactor() != Plasma::Horizontal && formFactor() != Plasma::Vertical) {
 
358
            resize(size().width(), (size().height() / oldButtonCount) * m_visibleButtons);
 
359
        }
 
360
        emit configNeedsSaving();
 
361
    }
 
362
#endif
 
363
}
 
364
 
 
365
void LockOut::createConfigurationInterface(KConfigDialog *parent)
 
366
{
 
367
#ifndef Q_OS_WIN
 
368
    QWidget *widget = new QWidget(parent);
 
369
    ui.setupUi(widget);
 
370
    parent->addPage(widget, i18n("Please select one or more items on the list below"), Applet::icon());
 
371
    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
 
372
    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
 
373
    
 
374
    countButtons();
 
375
    if (m_visibleButtons == 1) {
 
376
        ui.checkBox_lock->setEnabled(!m_showLockButton);
 
377
        ui.checkBox_switchUser->setEnabled(!m_showSwitchUserButton);
 
378
        ui.checkBox_logout->setEnabled(!m_showLogoutButton);
 
379
        ui.checkBox_sleep->setEnabled(!m_showSleepButton);
 
380
        ui.checkBox_hibernate->setEnabled(!m_showHibernateButton);
 
381
    } 
 
382
 
 
383
    ui.checkBox_lock->setChecked(m_showLockButton);
 
384
    connect(ui.checkBox_lock, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
 
385
    ui.checkBox_switchUser->setChecked(m_showSwitchUserButton);
 
386
    connect(ui.checkBox_switchUser, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
 
387
    ui.checkBox_logout->setChecked(m_showLogoutButton);
 
388
    connect(ui.checkBox_logout, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
 
389
    ui.checkBox_sleep->setChecked(m_showSleepButton);
 
390
    connect(ui.checkBox_sleep, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
 
391
    ui.checkBox_hibernate->setChecked(m_showHibernateButton);
 
392
    connect(ui.checkBox_hibernate, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
 
393
 
 
394
    connect(this, SIGNAL(configUiChanged()), parent, SLOT(settingsModified()));
 
395
    connect(ui.checkBox_logout, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
 
396
    connect(ui.checkBox_lock, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
 
397
    connect(ui.checkBox_switchUser, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
 
398
    connect(ui.checkBox_hibernate, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
 
399
    connect(ui.checkBox_sleep, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
 
400
#endif
 
401
}
 
402
 
 
403
void LockOut::showButtons()
 
404
{
 
405
    while(m_layout->count() > 0) {
 
406
        m_layout->removeAt(0);
 
407
    }
 
408
    
 
409
    // can this be done more beautiful?
 
410
    delete m_iconLock;
 
411
    m_iconLock = 0;
 
412
    delete m_iconSwitchUser;
 
413
    m_iconSwitchUser = 0;
 
414
    delete m_iconLogout;
 
415
    m_iconLogout = 0;
 
416
    delete m_iconSleep;
 
417
    m_iconSleep = 0;
 
418
    delete m_iconHibernate;
 
419
    m_iconHibernate = 0;
 
420
    
 
421
#ifdef Q_OS_WIN
 
422
    //Tooltip strings maybe should be different (eg. "Leave..."->"Logout")?
 
423
    m_iconLock = new Plasma::IconWidget(KIcon("system-lock-screen"), "", this);
 
424
    connect(m_iconLock, SIGNAL(clicked()), this, SLOT(clickLock()));
 
425
    Plasma::ToolTipContent lockToolTip(i18n("Lock"),i18n("Lock the screen"),m_iconLock->icon());
 
426
    Plasma::ToolTipManager::self()->setContent(m_iconLock, lockToolTip);
 
427
    m_layout->addItem(m_iconLock);
 
428
#else
 
429
 
 
430
    if (m_showLockButton) {
 
431
        //Tooltip strings maybe should be different (eg. "Leave..."->"Logout")?
 
432
        m_iconLock = new Plasma::IconWidget(KIcon("system-lock-screen"), "", this);
 
433
        connect(m_iconLock, SIGNAL(clicked()), this, SLOT(clickLock()));
 
434
        Plasma::ToolTipContent lockToolTip(i18n("Lock"),i18n("Lock the screen"),m_iconLock->icon());
 
435
        Plasma::ToolTipManager::self()->setContent(m_iconLock, lockToolTip);
 
436
        m_layout->addItem(m_iconLock);
 
437
    }
 
438
 
 
439
    if (m_showSwitchUserButton) {
 
440
        m_iconSwitchUser = new Plasma::IconWidget(KIcon("system-switch-user"), "", this);
 
441
        connect(m_iconSwitchUser, SIGNAL(clicked()), this, SLOT(clickSwitchUser()));
 
442
        Plasma::ToolTipContent switchUserToolTip(i18n("Switch user"),i18n("Start a parallel session as a different user"),m_iconSwitchUser->icon());
 
443
        Plasma::ToolTipManager::self()->setContent(m_iconSwitchUser, switchUserToolTip);
 
444
        m_layout->addItem(m_iconSwitchUser);
 
445
    }
 
446
 
 
447
    if (m_showLogoutButton) {
 
448
        m_iconLogout = new Plasma::IconWidget(KIcon("system-shutdown"), "", this);
 
449
        connect(m_iconLogout, SIGNAL(clicked()), this, SLOT(clickLogout()));
 
450
        Plasma::ToolTipContent logoutToolTip(i18n("Leave..."),i18n("Logout, turn off or restart the computer"),m_iconLogout->icon());
 
451
        Plasma::ToolTipManager::self()->setContent(m_iconLogout, logoutToolTip);
 
452
        m_layout->addItem(m_iconLogout);
 
453
    }
 
454
 
 
455
    if (m_showSleepButton) {
 
456
        m_iconSleep = new Plasma::IconWidget(KIcon("system-suspend"), "", this);
 
457
        connect(m_iconSleep, SIGNAL(clicked()), this, SLOT(clickSleep()));
 
458
        Plasma::ToolTipContent sleepToolTip(i18n("Suspend"),i18n("Sleep (suspend to RAM)"),m_iconSleep->icon());
 
459
        Plasma::ToolTipManager::self()->setContent(m_iconSleep, sleepToolTip);  
 
460
        m_layout->addItem(m_iconSleep);
 
461
    }
 
462
 
 
463
    if (m_showHibernateButton) {
 
464
        m_iconHibernate = new Plasma::IconWidget(KIcon("system-suspend-hibernate"), "", this);
 
465
        connect(m_iconHibernate, SIGNAL(clicked()), this, SLOT(clickHibernate()));
 
466
        Plasma::ToolTipContent hibernateToolTip(i18n("Hibernate"),i18n("Hibernate (suspend to disk)"),m_iconHibernate->icon());
 
467
        Plasma::ToolTipManager::self()->setContent(m_iconHibernate, hibernateToolTip);
 
468
        m_layout->addItem(m_iconHibernate);
 
469
    }
 
470
 
 
471
    setConfigurationRequired(!m_showLockButton && !m_showSwitchUserButton && !m_showLogoutButton && !m_showSleepButton && !m_showHibernateButton);
 
472
    checkLayout();
 
473
#endif // !Q_OS_WIN
 
474
}
 
475
 
 
476
#include "lockout.moc"