1
# ubuntuone.platform.linux.session
3
# Author: Alejandro J. Cura <alecu@canonical.com>
5
# Copyright 2011 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
18
"""Inhibit session logout when busy thru the Gnome Session DBus service."""
22
from twisted.internet import defer
24
SESSION_MANAGER_BUSNAME = "org.gnome.SessionManager"
25
SESSION_MANAGER_IFACE = "org.gnome.SessionManager"
26
SESSION_MANAGER_PATH = "/org/gnome/SessionManager"
28
INHIBIT_LOGGING_OUT = 1
29
INHIBIT_USER_SWITCHING = 2
30
INHIBIT_SUSPENDING_COMPUTER = 4
31
INHIBIT_SESSION_IDLE = 8
32
INHIBIT_LOGOUT_SUSPEND = INHIBIT_LOGGING_OUT | INHIBIT_SUSPENDING_COMPUTER
37
class Inhibitor(object):
38
"""An object representing an inhibition, that can be cancelled."""
42
"""Initialize this instance."""
44
bus = dbus.SessionBus()
45
obj = bus.get_object(bus_name=SESSION_MANAGER_BUSNAME,
46
object_path=SESSION_MANAGER_PATH,
47
follow_name_owner_changes=True)
48
self.proxy = dbus.Interface(object=obj,
49
dbus_interface=SESSION_MANAGER_IFACE)
51
def inhibit(self, flags, reason):
52
"""Inhibit some events with a given reason."""
55
def inhibit_handler(cookie):
56
"""Got the cookie for this inhibition."""
60
self.proxy.Inhibit(APP_ID, TOPLEVEL_XID, reason, flags,
61
reply_handler=inhibit_handler,
62
error_handler=d.errback)
66
"""Cancel the inhibition for the current cookie."""
68
self.proxy.Uninhibit(self.cookie,
69
reply_handler=lambda: d.callback(self),
70
error_handler=d.errback)
73
def inhibit_logout_suspend(reason):
74
"""Inhibit the suspend and logout. The result can be cancelled."""
75
return Inhibitor().inhibit(INHIBIT_LOGOUT_SUSPEND, reason)