2013-03-11 12:55:11 +01:00
|
|
|
## src/logind_listener.py
|
|
|
|
##
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2014 Kamil Paral <kamil.paral AT gmail.com>
|
2013-03-11 12:55:11 +01:00
|
|
|
##
|
|
|
|
## 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
|
|
|
|
|
2017-12-09 15:04:15 +01:00
|
|
|
from gi.repository import Gio, GLib
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2013-03-11 12:55:11 +01:00
|
|
|
|
|
|
|
log = logging.getLogger('gajim.logind_listener')
|
2017-12-09 15:04:15 +01:00
|
|
|
|
|
|
|
# file descriptor of the inhibitor; negative number means we don't
|
|
|
|
# hold any (yet)
|
|
|
|
fd = -1
|
2013-03-11 12:55:11 +01:00
|
|
|
|
|
|
|
|
2017-12-09 15:04:15 +01:00
|
|
|
def signal_received(connection, sender_name, object_path,
|
|
|
|
interface_name, signal_name, parameters, *user_data):
|
2013-03-11 12:55:11 +01:00
|
|
|
'''Signal handler for suspend event'''
|
|
|
|
|
|
|
|
global fd
|
|
|
|
|
2017-12-09 15:04:15 +01:00
|
|
|
connected = None
|
|
|
|
log.info('Signal received: %s - %s', interface_name, parameters)
|
|
|
|
|
|
|
|
# signal is sent right before (with the parameter True) and after
|
|
|
|
# (with the parameter False) the system goes down for suspend/hibernate
|
|
|
|
if not parameters[0]:
|
2013-03-11 12:55:11 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
# we're going for suspend, let's disconnect
|
2017-02-04 23:29:45 +01:00
|
|
|
log.debug('System suspend detected, disconnecting from network…')
|
2017-08-13 13:18:56 +02:00
|
|
|
for name, conn in app.connections.items():
|
|
|
|
if app.account_is_connected(name):
|
|
|
|
conn.old_show = app.SHOW_LIST[conn.connected]
|
2013-03-11 12:55:11 +01:00
|
|
|
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)
|
2017-02-07 21:56:47 +01:00
|
|
|
fd = -1
|
2013-03-11 12:55:11 +01:00
|
|
|
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 "
|
2017-12-09 15:04:15 +01:00
|
|
|
"a file descriptor for sleep inihibitor")
|
|
|
|
|
2013-03-11 12:55:11 +01:00
|
|
|
|
2017-12-09 15:04:15 +01:00
|
|
|
def get_inhibitor(connection):
|
2013-03-11 12:55:11 +01:00
|
|
|
'''Ask for a suspend delay inhibitor'''
|
|
|
|
|
|
|
|
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 '
|
2017-12-09 15:04:15 +01:00
|
|
|
'to be holding one already')
|
|
|
|
|
|
|
|
ret = connection.call_sync(
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
'/org/freedesktop/login1',
|
|
|
|
'org.freedesktop.login1.Manager',
|
|
|
|
'Inhibit',
|
|
|
|
GLib.Variant('(ssss)', ('sleep', 'org.gajim.Gajim',
|
|
|
|
'Disconnect from the network', 'delay')),
|
|
|
|
None,
|
|
|
|
Gio.DBusCallFlags.NONE, -1, None)
|
|
|
|
|
|
|
|
fd = ret[0]
|
|
|
|
|
|
|
|
|
|
|
|
def appeared(connection, name, name_owner, *user_data):
|
|
|
|
'''Set up a listener for suspend signals'''
|
|
|
|
global supported
|
|
|
|
supported = True
|
|
|
|
log.info('%s appeared', name)
|
|
|
|
if name == 'org.freedesktop.login1':
|
|
|
|
connection.signal_subscribe(
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
'org.freedesktop.login1.Manager',
|
|
|
|
'PrepareForSleep',
|
|
|
|
'/org/freedesktop/login1',
|
|
|
|
None,
|
|
|
|
Gio.DBusSignalFlags.NONE,
|
|
|
|
signal_received,
|
|
|
|
None)
|
|
|
|
get_inhibitor(connection)
|
|
|
|
|
|
|
|
|
|
|
|
Gio.bus_watch_name(
|
|
|
|
Gio.BusType.SYSTEM,
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
Gio.BusNameWatcherFlags.NONE,
|
|
|
|
appeared,
|
|
|
|
None)
|