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)
|
2018-01-04 14:23:17 +01:00
|
|
|
fd = None
|
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
|
|
|
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
|
2018-01-04 14:23:17 +01:00
|
|
|
if fd is not None:
|
2013-03-11 12:55:11 +01:00
|
|
|
os.close(fd)
|
2018-01-04 14:23:17 +01:00
|
|
|
fd = None
|
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 "
|
2018-06-27 00:52:14 +02:00
|
|
|
"a file descriptor for sleep inhibitor")
|
2017-12-09 15:04:15 +01:00
|
|
|
|
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
|
|
|
|
|
2018-01-04 14:23:17 +01:00
|
|
|
if fd is not None:
|
2018-06-27 00:52:14 +02:00
|
|
|
# something is wrong, we haven't closed the previous file descriptor
|
2013-03-11 12:55:11 +01:00
|
|
|
# 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')
|
|
|
|
|
2018-01-04 14:23:17 +01:00
|
|
|
ret = connection.call_with_unix_fd_list_sync(
|
2017-12-09 15:04:15 +01:00
|
|
|
'org.freedesktop.login1',
|
|
|
|
'/org/freedesktop/login1',
|
|
|
|
'org.freedesktop.login1.Manager',
|
|
|
|
'Inhibit',
|
|
|
|
GLib.Variant('(ssss)', ('sleep', 'org.gajim.Gajim',
|
2018-01-27 00:20:53 +01:00
|
|
|
_('Disconnect from the network'), 'delay')),
|
2018-01-04 14:23:17 +01:00
|
|
|
GLib.VariantType.new('(h)'),
|
|
|
|
Gio.DBusCallFlags.NONE, -1, None, None)
|
2017-12-09 15:04:15 +01:00
|
|
|
|
2018-01-19 00:20:47 +01:00
|
|
|
fd = ret.out_fd_list.get(0)
|
2017-12-09 15:04:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def appeared(connection, name, name_owner, *user_data):
|
|
|
|
'''Set up a listener for suspend signals'''
|
|
|
|
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)
|