~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

« back to all changes in this revision

Viewing changes to ubuntuone/platform/linux/session.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-23 18:34:09 UTC
  • mfrom: (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110223183409-535o7yo165wbjmca
Tags: 1.5.5-0ubuntu1
* New upstream release.
  - Subscribing to a RO share will not download content (LP: #712528)
  - Can't synchronize "~/Ubuntu One Music" (LP: #714976)
  - Syncdaemon needs to show progress in Unity launcher (LP: #702116)
  - Notifications say "your cloud" (LP: #715887)
  - No longer requires python-libproxy
  - Recommend unity and indicator libs by default

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ubuntuone.platform.linux.session
 
2
#
 
3
# Author: Alejandro J. Cura <alecu@canonical.com>
 
4
#
 
5
# Copyright 2011 Canonical Ltd.
 
6
#
 
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.
 
10
#
 
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.
 
15
#
 
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."""
 
19
 
 
20
import dbus
 
21
 
 
22
from twisted.internet import defer
 
23
 
 
24
SESSION_MANAGER_BUSNAME = "org.gnome.SessionManager"
 
25
SESSION_MANAGER_IFACE = "org.gnome.SessionManager"
 
26
SESSION_MANAGER_PATH = "/org/gnome/SessionManager"
 
27
 
 
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
 
33
 
 
34
APP_ID = "Ubuntu One"
 
35
TOPLEVEL_XID = 0
 
36
 
 
37
class Inhibitor(object):
 
38
    """An object representing an inhibition, that can be cancelled."""
 
39
 
 
40
 
 
41
    def __init__(self):
 
42
        """Initialize this instance."""
 
43
        self.cookie = None
 
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)
 
50
 
 
51
    def inhibit(self, flags, reason):
 
52
        """Inhibit some events with a given reason."""
 
53
        d = defer.Deferred()
 
54
 
 
55
        def inhibit_handler(cookie):
 
56
            """Got the cookie for this inhibition."""
 
57
            self.cookie = cookie
 
58
            d.callback(self)
 
59
 
 
60
        self.proxy.Inhibit(APP_ID, TOPLEVEL_XID, reason, flags,
 
61
                           reply_handler=inhibit_handler,
 
62
                           error_handler=d.errback)
 
63
        return d
 
64
 
 
65
    def cancel(self):
 
66
        """Cancel the inhibition for the current cookie."""
 
67
        d = defer.Deferred()
 
68
        self.proxy.Uninhibit(self.cookie,
 
69
                             reply_handler=lambda: d.callback(self),
 
70
                             error_handler=d.errback)
 
71
        return d
 
72
 
 
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)