diff --git a/gajim/common/dbus/__init__.py b/gajim/common/dbus/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/gajim/common/location_listener.py b/gajim/common/dbus/location.py similarity index 98% rename from gajim/common/location_listener.py rename to gajim/common/dbus/location.py index fa7175d03..1a1da8ce4 100644 --- a/gajim/common/location_listener.py +++ b/gajim/common/dbus/location.py @@ -24,7 +24,7 @@ gi.require_version('Geoclue', '2.0') from gi.repository import Geoclue from gi.repository import GLib -log = logging.getLogger('gajim.c.location_listener') +log = logging.getLogger('gajim.c.dbus.location') class LocationListener: diff --git a/gajim/music_track_listener.py b/gajim/common/dbus/music_track.py similarity index 99% rename from gajim/music_track_listener.py rename to gajim/common/dbus/music_track.py index eabe10db5..43155c8e0 100644 --- a/gajim/music_track_listener.py +++ b/gajim/common/dbus/music_track.py @@ -24,7 +24,7 @@ import logging from gi.repository import GObject from gi.repository import Gio, GLib -log = logging.getLogger('gajim.music_track_listener') +log = logging.getLogger('gajim.c.dbus.music_track') MPRIS_PLAYER_PREFIX = 'org.mpris.MediaPlayer2.' diff --git a/gajim/common/dbus/screensaver.py b/gajim/common/dbus/screensaver.py new file mode 100644 index 000000000..86ad545e4 --- /dev/null +++ b/gajim/common/dbus/screensaver.py @@ -0,0 +1,103 @@ +# Copyright (C) 2018 André Apitzsch +# +# 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 . + +import logging + +from gi.repository import Gio + +from gajim.common import app + +log = logging.getLogger('gajim.c.dbus.screensaver') + + +class ScreensaverListener: + _instance = None + + @classmethod + def get(cls): + if cls._instance is None: + cls._instance = cls() + return cls._instance + + def __init__(self): + Gio.bus_watch_name( + Gio.BusType.SESSION, + 'org.gnome.ScreenSaver', + Gio.BusNameWatcherFlags.NONE, + self._appeared, + None) + + @staticmethod + def _signal_received(_connection, _sender_name, _object_path, + interface_name, _signal_name, parameters, *_user_data): + '''Signal handler for screensaver active change''' + + log.info('Signal received: %s - %s', interface_name, parameters) + + roster = app.interface.roster + + if not parameters[0]: + for account in app.connections: + if app.account_is_connected(account) and \ + app.sleeper_state[account] == 'autoaway-forced': + # We came back online after screensaver + # autoaway + roster.send_status(account, 'online', + app.status_before_autoaway[account]) + app.status_before_autoaway[account] = '' + app.sleeper_state[account] = 'online' + return + if not app.config.get('autoaway'): + # Don't go auto away if user disabled the option + return + for account in app.connections: + if account not in app.sleeper_state or not app.sleeper_state[account]: + continue + if app.sleeper_state[account] == 'online': + if not app.account_is_connected(account): + continue + # we save our online status + app.status_before_autoaway[account] = \ + app.connections[account].status + # we go away (no auto status) [we pass True to auto param] + auto_message = app.config.get('autoaway_message') + if not auto_message: + auto_message = app.connections[account].status + else: + auto_message = auto_message.replace('$S', '%(status)s') + auto_message = auto_message.replace('$T', '%(time)s') + auto_message = auto_message % { + 'status': app.status_before_autoaway[account], + 'time': app.config.get('autoxatime')} + roster.send_status(account, 'away', auto_message, auto=True) + app.sleeper_state[account] = 'autoaway-forced' + + def _appeared(self, connection, name, _name_owner, *_user_data): + '''Set up a listener for screensaver signals''' + log.info('%s appeared', name) + connection.signal_subscribe( + 'org.gnome.ScreenSaver', + 'org.gnome.ScreenSaver', + 'ActiveChanged', + '/org/gnome/ScreenSaver', + None, + Gio.DBusSignalFlags.NONE, + self._signal_received, + None) + + +def enable(): + ScreensaverListener.get() diff --git a/gajim/gui_interface.py b/gajim/gui_interface.py index 200f5cd29..f5cb1a8a3 100644 --- a/gajim/gui_interface.py +++ b/gajim/gui_interface.py @@ -56,11 +56,7 @@ except Exception: from gajim.common import app from gajim.common import events - -from gajim.music_track_listener import MusicTrackListener - -if app.is_installed('GEOCLUE'): - from gajim.common import location_listener +from gajim.common import dbus from gajim import gtkgui_helpers from gajim import gui_menu_builder @@ -1118,7 +1114,7 @@ class Interface: # enable location listener if (pep_supported and app.is_installed('GEOCLUE') and app.config.get_per('accounts', account, 'publish_location')): - location_listener.enable() + dbus.location.enable() @staticmethod def show_httpupload_progress(file): @@ -2083,14 +2079,14 @@ class Interface: def enable_music_listener(self): - listener = MusicTrackListener.get() + listener = dbus.music_track.MusicTrackListener.get() if not self.music_track_changed_signal: self.music_track_changed_signal = listener.connect( 'music-track-changed', self.music_track_changed) listener.start() def disable_music_listener(self): - listener = MusicTrackListener.get() + listener = dbus.music_track.MusicTrackListener.get() listener.disconnect(self.music_track_changed_signal) self.music_track_changed_signal = None listener.stop() @@ -2726,7 +2722,7 @@ class Interface: # Handle screensaver if sys.platform == 'linux': from gajim import logind_listener # pylint: disable=unused-variable - from gajim import screensaver_listener # pylint: disable=unused-variable + dbus.screensaver.enable() self.show_vcard_when_connect = [] diff --git a/gajim/roster_window.py b/gajim/roster_window.py index 688eae2c3..ffbf561bc 100644 --- a/gajim/roster_window.py +++ b/gajim/roster_window.py @@ -62,7 +62,7 @@ from gajim.common import i18n from gajim.common.i18n import _ from gajim.common.const import PEPEventType, AvatarSize, StyleAttr if app.is_installed('GEOCLUE'): - from gajim.common import location_listener + from gajim.common import dbus from gajim.common import ged from gajim.message_window import MessageWindowMgr @@ -3659,7 +3659,7 @@ class RosterWindow: active = widget.get_active() app.config.set_per('accounts', account, 'publish_location', active) if active: - location_listener.enable() + dbus.location.enable() else: app.connections[account].get_module('UserLocation').send(None) diff --git a/gajim/screensaver_listener.py b/gajim/screensaver_listener.py deleted file mode 100644 index 7701f390b..000000000 --- a/gajim/screensaver_listener.py +++ /dev/null @@ -1,90 +0,0 @@ -# Copyright (C) 2018 André Apitzsch -# -# 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 . - -import logging - -from gi.repository import Gio - -from gajim.common import app - -log = logging.getLogger('gajim.screensaver_listener') - - -def signal_received(connection, sender_name, object_path, - interface_name, signal_name, parameters, *user_data): - '''Signal handler for screensaver active change''' - - log.info('Signal received: %s - %s', interface_name, parameters) - - roster = app.interface.roster - - if not parameters[0]: - for account in app.connections: - if app.account_is_connected(account) and \ - app.sleeper_state[account] == 'autoaway-forced': - # We came back online after screensaver - # autoaway - roster.send_status(account, 'online', - app.status_before_autoaway[account]) - app.status_before_autoaway[account] = '' - app.sleeper_state[account] = 'online' - return - if not app.config.get('autoaway'): - # Don't go auto away if user disabled the option - return - for account in app.connections: - if account not in app.sleeper_state or not app.sleeper_state[account]: - continue - if app.sleeper_state[account] == 'online': - if not app.account_is_connected(account): - continue - # we save our online status - app.status_before_autoaway[account] = \ - app.connections[account].status - # we go away (no auto status) [we pass True to auto param] - auto_message = app.config.get('autoaway_message') - if not auto_message: - auto_message = app.connections[account].status - else: - auto_message = auto_message.replace('$S', '%(status)s') - auto_message = auto_message.replace('$T', '%(time)s') - auto_message = auto_message % { - 'status': app.status_before_autoaway[account], - 'time': app.config.get('autoxatime')} - roster.send_status(account, 'away', auto_message, auto=True) - app.sleeper_state[account] = 'autoaway-forced' - - -def appeared(connection, name, name_owner, *user_data): - '''Set up a listener for screensaver signals''' - log.info('%s appeared', name) - connection.signal_subscribe( - 'org.gnome.ScreenSaver', - 'org.gnome.ScreenSaver', - 'ActiveChanged', - '/org/gnome/ScreenSaver', - None, - Gio.DBusSignalFlags.NONE, - signal_received, - None) - - -Gio.bus_watch_name( - Gio.BusType.SESSION, - 'org.gnome.ScreenSaver', - Gio.BusNameWatcherFlags.NONE, - appeared, - None)