Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logind_listener.py « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ca35ac0550a6035029bc531f9008f190a87b454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
## src/logind_listener.py
##
## Copyright (C) 2014 Kamil Paral <kamil.paral AT gmail.com>
##
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## Gajim is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##

'''
Watch for system suspend using systemd-logind.
Documentation: http://www.freedesktop.org/wiki/Software/systemd/inhibit
'''

import os
import logging

from common import dbus_support
from common import gajim

log = logging.getLogger('gajim.logind_listener')
supported = False
fd = -1  # file descriptor of the inhibitor; negative number means we don't
         # hold any (yet)


def on_suspend(active):
    '''Signal handler for suspend event'''

    global fd

    if not active:
        # we just resumed, we should take another inhibitor
        get_inhibitor()
        return

    # we're going for suspend, let's disconnect
    log.debug('System suspend detected, disconnecting from network...')
    for name, conn in gajim.connections.items():
        if gajim.account_is_connected(name):
            conn.old_show = gajim.SHOW_LIST[conn.connected]
            st = conn.status
            conn.change_status('offline', _('Machine going to sleep'))
            conn.status = st
            conn.time_to_reconnect = 5

    # close the file descriptor and let the computer suspend
    if fd >= 0:
        os.close(fd)
        fd = -1;
    else:
        # something is wrong, the system is suspending but we don't have
        # a lock file
        log.warning("System suspend detected, but we don't seem to be holding "
            "a file descriptor for sleep inihibitor")

def get_inhibitor():
    '''Ask for a suspend delay inhibitor'''

    from common.dbus_support import system_bus, dbus
    bus = system_bus.bus()
    global fd

    if fd >= 0:
        # someting is wrong, we haven't closed the previous file descriptor
        # and we ask for yet another one
        log.warning('We are about to ask for a sleep inhibitor, but we seem '
            'to be holding one already')

    login_object = bus.get_object('org.freedesktop.login1',
        '/org/freedesktop/login1')
    login_manager = dbus.Interface(login_object,
        'org.freedesktop.login1.Manager')

    ret = login_manager.Inhibit('sleep', 'Gajim', 'Disconnect from the network',
        'delay')
    fd = ret.take()

def set_listener():
    '''Set up a listener for suspend signals

    @return bool whether it succeeded
    '''
    from common.dbus_support import system_bus
    bus = system_bus.bus()

    if not 'org.freedesktop.login1' in bus.list_names():
        # logind is not present
        log.debug("logind is not on D-Bus, not activating logind listener")
        return False

    bus.add_signal_receiver(on_suspend, signal_name='PrepareForSleep',
        bus_name='org.freedesktop.login1',
        path='/org/freedesktop/login1',
        dbus_interface='org.freedesktop.login1.Manager')
    return True

if dbus_support.supported:
    try:
        if set_listener():
            get_inhibitor()
            supported = True
    except Exception as ex:
        log.error("A problem occured while activating logind listener")