9578053 Jan 22 2022 distfiles.gentoo.org/distfiles/gajim-1.3.3-2.tar.gz
This commit is contained in:
parent
a5b3822651
commit
4c1b226bff
1045 changed files with 753037 additions and 18 deletions
0
gajim/common/dbus/__init__.py
Normal file
0
gajim/common/dbus/__init__.py
Normal file
112
gajim/common/dbus/location.py
Normal file
112
gajim/common/dbus/location.py
Normal file
|
@ -0,0 +1,112 @@
|
|||
# Copyright (C) 2009-2014 Yann Leboulanger <asterix AT lagaule.org>
|
||||
#
|
||||
# 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/>.
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from gi.repository import GLib
|
||||
from nbxmpp.structs import LocationData
|
||||
|
||||
from gajim.common import app
|
||||
|
||||
if app.is_installed('GEOCLUE'):
|
||||
from gi.repository import Geoclue # pylint: disable=ungrouped-imports,no-name-in-module
|
||||
|
||||
log = logging.getLogger('gajim.c.dbus.location')
|
||||
|
||||
|
||||
class LocationListener:
|
||||
_instance = None
|
||||
|
||||
@classmethod
|
||||
def get(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
self._data = {}
|
||||
self.location_info = {}
|
||||
self.simple = None
|
||||
|
||||
# Note: do not remove third parameter `param`
|
||||
# because notify signal expects three parameters
|
||||
def _on_location_update(self, simple, _param=None):
|
||||
location = simple.get_location()
|
||||
timestamp = location.get_property("timestamp")[0]
|
||||
lat = location.get_property("latitude")
|
||||
lon = location.get_property("longitude")
|
||||
alt = location.get_property("altitude")
|
||||
# in XEP-0080 it's horizontal accuracy
|
||||
acc = location.get_property("accuracy")
|
||||
|
||||
# update data with info we just received
|
||||
self._data = {'lat': lat, 'lon': lon, 'alt': alt, 'accuracy': acc}
|
||||
self._data['timestamp'] = self._timestamp_to_utc(timestamp)
|
||||
self._send_location()
|
||||
|
||||
def _on_simple_ready(self, _obj, result):
|
||||
try:
|
||||
self.simple = Geoclue.Simple.new_finish(result)
|
||||
except GLib.Error as error:
|
||||
log.warning("Could not enable geolocation: %s", error.message)
|
||||
else:
|
||||
self.simple.connect('notify::location', self._on_location_update)
|
||||
self._on_location_update(self.simple)
|
||||
|
||||
def get_data(self):
|
||||
Geoclue.Simple.new("org.gajim.Gajim",
|
||||
Geoclue.AccuracyLevel.EXACT,
|
||||
None,
|
||||
self._on_simple_ready)
|
||||
|
||||
def start(self):
|
||||
self.location_info = {}
|
||||
self.get_data()
|
||||
|
||||
def _send_location(self):
|
||||
accounts = app.connections.keys()
|
||||
for acct in accounts:
|
||||
if not app.account_is_available(acct):
|
||||
continue
|
||||
if not app.settings.get_account_setting(acct, 'publish_location'):
|
||||
continue
|
||||
if self.location_info == self._data:
|
||||
continue
|
||||
if 'timestamp' in self.location_info and 'timestamp' in self._data:
|
||||
last_data = self.location_info.copy()
|
||||
del last_data['timestamp']
|
||||
new_data = self._data.copy()
|
||||
del new_data['timestamp']
|
||||
if last_data == new_data:
|
||||
continue
|
||||
app.connections[acct].get_module('UserLocation').set_location(
|
||||
LocationData(**self._data))
|
||||
self.location_info = self._data.copy()
|
||||
|
||||
@staticmethod
|
||||
def _timestamp_to_utc(timestamp):
|
||||
time = datetime.utcfromtimestamp(timestamp)
|
||||
return time.strftime('%Y-%m-%dT%H:%MZ')
|
||||
|
||||
|
||||
def enable():
|
||||
if not app.is_installed('GEOCLUE'):
|
||||
log.warning('GeoClue not installed')
|
||||
return
|
||||
|
||||
listener = LocationListener.get()
|
||||
listener.start()
|
142
gajim/common/dbus/logind.py
Normal file
142
gajim/common/dbus/logind.py
Normal file
|
@ -0,0 +1,142 @@
|
|||
# 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 sleep using systemd-logind.
|
||||
Documentation: http://www.freedesktop.org/wiki/Software/systemd/inhibit
|
||||
'''
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
||||
from gi.repository import Gio
|
||||
from gi.repository import GLib
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.i18n import _
|
||||
|
||||
log = logging.getLogger('gajim.c.dbus.logind')
|
||||
|
||||
|
||||
class LogindListener:
|
||||
_instance = None
|
||||
|
||||
@classmethod
|
||||
def get(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
# file descriptor object of the inhibitor
|
||||
self._inhibit_fd = None
|
||||
|
||||
Gio.bus_watch_name(
|
||||
Gio.BusType.SYSTEM,
|
||||
'org.freedesktop.login1',
|
||||
Gio.BusNameWatcherFlags.NONE,
|
||||
self._on_appear_logind,
|
||||
self._on_vanish_logind)
|
||||
|
||||
def _on_prepare_for_sleep(self, connection, _sender_name, _object_path,
|
||||
interface_name, signal_name, parameters,
|
||||
*_user_data):
|
||||
'''Signal handler for PrepareForSleep event'''
|
||||
log.debug('Received signal %s.%s%s',
|
||||
interface_name, signal_name, parameters)
|
||||
|
||||
before = parameters[0] # Signal is either before or after sleep occurs
|
||||
if before:
|
||||
warn = self._inhibit_fd is None
|
||||
log.log(
|
||||
logging.WARNING if warn else logging.INFO,
|
||||
'Preparing for sleep by disconnecting from network%s',
|
||||
', without holding a sleep inhibitor' if warn else '')
|
||||
|
||||
for name, conn in app.connections.items():
|
||||
if app.account_is_connected(name):
|
||||
st = conn.status_message
|
||||
conn.change_status('offline',
|
||||
_('Machine is going to sleep'))
|
||||
# TODO: Make this nicer
|
||||
conn._status_message = st # pylint: disable=protected-access
|
||||
conn.time_to_reconnect = 5
|
||||
|
||||
self._disinhibit_sleep()
|
||||
else:
|
||||
try:
|
||||
self._inhibit_sleep(connection)
|
||||
except GLib.Error as error:
|
||||
log.warning('Inhibit failed: %s', error)
|
||||
|
||||
for conn in app.connections.values():
|
||||
if conn.state.is_disconnected and conn.time_to_reconnect:
|
||||
conn.reconnect()
|
||||
|
||||
def _inhibit_sleep(self, connection):
|
||||
'''Obtain a sleep delay inhibitor from logind'''
|
||||
if self._inhibit_fd is not None:
|
||||
# Something is wrong, we have an inhibitor fd, and we are asking for
|
||||
# yet another one.
|
||||
log.warning('Trying to obtain a sleep inhibitor '
|
||||
'while already holding one.')
|
||||
|
||||
ret, ret_fdlist = connection.call_with_unix_fd_list_sync(
|
||||
'org.freedesktop.login1',
|
||||
'/org/freedesktop/login1',
|
||||
'org.freedesktop.login1.Manager',
|
||||
'Inhibit',
|
||||
GLib.Variant('(ssss)', (
|
||||
'sleep', 'org.gajim.Gajim', _('Disconnect from the network'),
|
||||
'delay' # Inhibitor will delay but not block sleep
|
||||
)),
|
||||
GLib.VariantType.new('(h)'),
|
||||
Gio.DBusCallFlags.NONE, -1, None, None)
|
||||
|
||||
log.info('Inhibit sleep')
|
||||
self._inhibit_fd = ret_fdlist.get(ret.unpack()[0])
|
||||
|
||||
def _disinhibit_sleep(self):
|
||||
'''Relinquish our sleep delay inhibitor'''
|
||||
if self._inhibit_fd is not None:
|
||||
os.close(self._inhibit_fd)
|
||||
self._inhibit_fd = None
|
||||
log.info('Disinhibit sleep')
|
||||
|
||||
def _on_appear_logind(self, connection, name, name_owner, *_user_data):
|
||||
'''Use signal and locks provided by org.freedesktop.login1'''
|
||||
log.info('Name %s appeared, owned by %s', name, name_owner)
|
||||
|
||||
connection.signal_subscribe(
|
||||
'org.freedesktop.login1',
|
||||
'org.freedesktop.login1.Manager',
|
||||
'PrepareForSleep',
|
||||
'/org/freedesktop/login1',
|
||||
None,
|
||||
Gio.DBusSignalFlags.NONE,
|
||||
self._on_prepare_for_sleep,
|
||||
None)
|
||||
self._inhibit_sleep(connection)
|
||||
|
||||
def _on_vanish_logind(self, _connection, name, *_user_data):
|
||||
'''Release remaining resources related to org.freedesktop.login1'''
|
||||
log.info('Name %s vanished', name)
|
||||
self._disinhibit_sleep()
|
||||
|
||||
|
||||
def enable():
|
||||
return
|
||||
# LogindListener.get()
|
208
gajim/common/dbus/music_track.py
Normal file
208
gajim/common/dbus/music_track.py
Normal file
|
@ -0,0 +1,208 @@
|
|||
# Copyright (C) 2006 Gustavo Carneiro <gjcarneiro AT gmail.com>
|
||||
# Nikos Kouremenos <kourem AT gmail.com>
|
||||
# Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
||||
# Copyright (C) 2008 Jean-Marie Traissard <jim AT lapin.org>
|
||||
# Jonathan Schleifer <js-gajim AT webkeks.org>
|
||||
# Stephan Erb <steve-e AT h3c.de>
|
||||
#
|
||||
# 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/>.
|
||||
|
||||
import logging
|
||||
|
||||
from gi.repository import Gio
|
||||
from gi.repository import GLib
|
||||
from nbxmpp.structs import TuneData
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.nec import NetworkEvent
|
||||
|
||||
log = logging.getLogger('gajim.c.dbus.music_track')
|
||||
|
||||
MPRIS_PLAYER_PREFIX = 'org.mpris.MediaPlayer2.'
|
||||
|
||||
|
||||
class MusicTrackListener:
|
||||
|
||||
_instance = None
|
||||
|
||||
@classmethod
|
||||
def get(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
self.players = {}
|
||||
self.connection = None
|
||||
self._current_tune = None
|
||||
|
||||
def _emit(self, info):
|
||||
self._current_tune = info
|
||||
app.nec.push_incoming_event(
|
||||
NetworkEvent('music-track-changed', info=info))
|
||||
|
||||
@property
|
||||
def current_tune(self):
|
||||
return self._current_tune
|
||||
|
||||
def start(self):
|
||||
proxy = Gio.DBusProxy.new_for_bus_sync(
|
||||
Gio.BusType.SESSION,
|
||||
Gio.DBusProxyFlags.NONE,
|
||||
None,
|
||||
'org.freedesktop.DBus',
|
||||
'/org/freedesktop/DBus',
|
||||
'org.freedesktop.DBus',
|
||||
None)
|
||||
|
||||
self.connection = proxy.get_connection()
|
||||
self.connection.signal_subscribe(
|
||||
'org.freedesktop.DBus',
|
||||
'org.freedesktop.DBus',
|
||||
'NameOwnerChanged',
|
||||
'/org/freedesktop/DBus',
|
||||
None,
|
||||
Gio.DBusSignalFlags.NONE,
|
||||
self._signal_name_owner_changed)
|
||||
|
||||
try:
|
||||
result = proxy.call_sync(
|
||||
'ListNames',
|
||||
None,
|
||||
Gio.DBusCallFlags.NONE,
|
||||
-1,
|
||||
None)
|
||||
except GLib.Error as error:
|
||||
log.debug("Could not list names: %s", error.message)
|
||||
return
|
||||
|
||||
for name in result[0]:
|
||||
if name.startswith(MPRIS_PLAYER_PREFIX):
|
||||
self._add_player(name)
|
||||
|
||||
for name in list(self.players):
|
||||
self._get_playing_track(name)
|
||||
|
||||
def stop(self):
|
||||
for name in list(self.players):
|
||||
if name.startswith(MPRIS_PLAYER_PREFIX):
|
||||
self._remove_player(name)
|
||||
|
||||
def _signal_name_owner_changed(self,
|
||||
_connection,
|
||||
_sender_name,
|
||||
_object_path,
|
||||
_interface_name,
|
||||
_signal_name,
|
||||
parameters,
|
||||
*_user_data):
|
||||
name, old_owner, new_owner = parameters
|
||||
if name.startswith(MPRIS_PLAYER_PREFIX):
|
||||
if new_owner and not old_owner:
|
||||
self._add_player(name)
|
||||
else:
|
||||
self._remove_player(name)
|
||||
|
||||
def _add_player(self, name):
|
||||
'''Set up a listener for music player signals'''
|
||||
log.info('%s appeared', name)
|
||||
|
||||
if name in self.players:
|
||||
return
|
||||
|
||||
self.players[name] = self.connection.signal_subscribe(
|
||||
name,
|
||||
'org.freedesktop.DBus.Properties',
|
||||
'PropertiesChanged',
|
||||
'/org/mpris/MediaPlayer2',
|
||||
None,
|
||||
Gio.DBusSignalFlags.NONE,
|
||||
self._signal_received,
|
||||
name)
|
||||
|
||||
def _remove_player(self, name):
|
||||
log.info('%s vanished', name)
|
||||
if name in self.players:
|
||||
self.connection.signal_unsubscribe(
|
||||
self.players[name])
|
||||
self.players.pop(name)
|
||||
|
||||
self._emit(None)
|
||||
|
||||
def _signal_received(self,
|
||||
_connection,
|
||||
_sender_name,
|
||||
_object_path,
|
||||
interface_name,
|
||||
_signal_name,
|
||||
parameters,
|
||||
*user_data):
|
||||
'''Signal handler for PropertiesChanged event'''
|
||||
|
||||
log.info('Signal received: %s - %s', interface_name, parameters)
|
||||
self._get_playing_track(user_data[0])
|
||||
|
||||
@staticmethod
|
||||
def _get_music_info(properties):
|
||||
meta = properties.get('Metadata')
|
||||
if meta is None or not meta:
|
||||
return None
|
||||
|
||||
status = properties.get('PlaybackStatus')
|
||||
if status is None or status == 'Paused':
|
||||
return None
|
||||
|
||||
title = meta.get('xesam:title')
|
||||
album = meta.get('xesam:album')
|
||||
# xesam:artist is always a list of strings if not None
|
||||
artist = meta.get('xesam:artist')
|
||||
if artist is not None:
|
||||
artist = ', '.join(artist)
|
||||
return TuneData(artist=artist, title=title, source=album)
|
||||
|
||||
def _get_playing_track(self, name):
|
||||
'''Return a TuneData for the currently playing
|
||||
song, or None if no song is playing'''
|
||||
proxy = Gio.DBusProxy.new_for_bus_sync(
|
||||
Gio.BusType.SESSION,
|
||||
Gio.DBusProxyFlags.NONE,
|
||||
None,
|
||||
name,
|
||||
'/org/mpris/MediaPlayer2',
|
||||
'org.freedesktop.DBus.Properties',
|
||||
None)
|
||||
|
||||
def proxy_call_finished(proxy, res):
|
||||
try:
|
||||
result = proxy.call_finish(res)
|
||||
except GLib.Error as error:
|
||||
log.debug("Could not enable music listener: %s", error.message)
|
||||
return
|
||||
|
||||
info = self._get_music_info(result[0])
|
||||
if info is not None:
|
||||
self._emit(info)
|
||||
|
||||
proxy.call("GetAll",
|
||||
GLib.Variant('(s)', ('org.mpris.MediaPlayer2.Player',)),
|
||||
Gio.DBusCallFlags.NONE,
|
||||
-1,
|
||||
None,
|
||||
proxy_call_finished)
|
||||
|
||||
|
||||
def enable():
|
||||
listener = MusicTrackListener.get()
|
||||
listener.start()
|
Loading…
Add table
Add a link
Reference in a new issue