on fdb4474 help sign in
unity8/plugins/SessionManager/SessionManager.cpp
105 lines 4.3 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Michael Terry <michael.terry@canonical.com>
17 */
19#include "SessionManager.h"
20#include <QtCore/QEvent>
21#include <QtDBus/QDBusPendingReply>
23SessionManager::SessionManager(QObject* parent)
24 : QObject(parent),
25 l1_manager(NULL),
26 l1_session(NULL),
27 ldm_session(NULL),
28 is_active(true) // assume we're active w/o logind
30 l1_manager = new QDBusInterface("org.freedesktop.login1",
31 "/org/freedesktop/login1",
32 "org.freedesktop.login1.Manager",
33 QDBusConnection::SM_BUSNAME(), this);
34 if (l1_manager->isValid()) {
35 QDBusPendingCall pcall = l1_manager->asyncCall("GetSession", QString(qgetenv("XDG_SESSION_ID")));
36 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
37 QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
38 this, SLOT(getSessionSlot(QDBusPendingCallWatcher*)));
39 }
41 ldm_session = new QDBusInterface("org.freedesktop.DisplayManager",
42 QString(qgetenv("XDG_SESSION_PATH")),
43 "org.freedesktop.DisplayManager.Session",
44 QDBusConnection::SM_BUSNAME(), this);
47bool SessionManager::active() const
49 // We cache this value for performance reasons, as QDBusInterface does not
50 // cache for us.
51 return is_active;
54void SessionManager::lock()
56 // We use ldm_session to handle locking rather than l1_session, because
57 // l1_session doesn't have permissions to lock from the user session, but
58 // ldm_session does. This is what the rest of Ubuntu uses to lock.
59 if (ldm_session != NULL && ldm_session->isValid())
60 ldm_session->asyncCall("Lock");
63void SessionManager::getSessionSlot(QDBusPendingCallWatcher *watcher)
65 QDBusPendingReply<QDBusObjectPath> reply = *watcher;
66 if (!reply.isError()) {
67 QDBusObjectPath path = reply.argumentAt<0>();
68 l1_session = new QDBusInterface("org.freedesktop.login1",
69 path.path(),
70 "org.freedesktop.login1.Session",
71 l1_manager->connection(), this);
73 l1_manager->connection().connect("org.freedesktop.login1",
74 path.path(),
75 "org.freedesktop.DBus.Properties",
76 "PropertiesChanged",
77 this,
78 SLOT(propertiesChanged(QString, QVariantMap, QStringList)));
80 propertiesChanged("", QVariantMap(), QStringList() << "Active");
81 }
82 watcher->deleteLater();
85void SessionManager::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid)
87 Q_UNUSED(interface)
89 QVariant value = changed.value("Active");
91 // If Active isn't in changed, check if we were at least notified about it
92 // changing on the server. Also check if IdleHint was modified, because
93 // sometimes logind does not emit a changed signal for Active becoming
94 // false, but will emit for IdleHint when that does happen.
95 if (!value.isValid() && (invalid.contains("Active") || invalid.contains("IdleHint") || changed.contains("IdleHint")) && l1_session->isValid()) {
96 value = l1_session->property("Active");
97 if (!value.isValid())
98 value = QVariant(false);
99 }
101 if (value.isValid() && value.toBool() != is_active) {
102 is_active = value.toBool();
103 Q_EMIT activeChanged();
104 }