Port to TelepathyGLib

Co-authored-by: tonadev <tonadevv@gmail.com> [initial port]
Co-authored-by: Aniket21mathur <aniketmathur320@gmail.com> [further work]
Co-authored-by: James Cameron <quozl@laptop.org> [minimising changes]
This commit is contained in:
Aniket Mathur 2019-07-06 09:41:18 +05:30 committed by James Cameron
parent ac7482081e
commit 88ea8e1f4f
6 changed files with 149 additions and 67 deletions

View File

@ -172,22 +172,17 @@ import json
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
gi.require_version('TelepathyGLib', '0.12')
gi.require_version('SugarExt', '1.0') gi.require_version('SugarExt', '1.0')
from gi.repository import GLib from gi.repository import GLib
from gi.repository import GObject from gi.repository import GObject
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import TelepathyGLib
import dbus import dbus
import dbus.service import dbus.service
from dbus import PROPERTIES_IFACE from dbus import PROPERTIES_IFACE
from telepathy.server import DBusProperties
from telepathy.interfaces import CHANNEL, \
CHANNEL_TYPE_TEXT, \
CLIENT, \
CLIENT_HANDLER
from telepathy.constants import CONNECTION_HANDLE_TYPE_CONTACT
from telepathy.constants import CONNECTION_HANDLE_TYPE_ROOM
from sugar3 import util from sugar3 import util
from sugar3 import power from sugar3 import power
@ -222,6 +217,14 @@ N_BUS_NAME = 'org.freedesktop.Notifications'
N_OBJ_PATH = '/org/freedesktop/Notifications' N_OBJ_PATH = '/org/freedesktop/Notifications'
N_IFACE_NAME = 'org.freedesktop.Notifications' N_IFACE_NAME = 'org.freedesktop.Notifications'
CHANNEL = TelepathyGLib.IFACE_CHANNEL
CHANNEL_TYPE_TEXT = TelepathyGLib.IFACE_CHANNEL_TYPE_TEXT
CLIENT = TelepathyGLib.IFACE_CLIENT
CLIENT_HANDLER = TelepathyGLib.IFACE_CLIENT_HANDLER
CONNECTION_HANDLE_TYPE_CONTACT = TelepathyGLib.HandleType.CONTACT
CONNECTION_HANDLE_TYPE_ROOM = TelepathyGLib.HandleType.ROOM
CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties' CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'
PREVIEW_SIZE = style.zoom(300), style.zoom(225) PREVIEW_SIZE = style.zoom(300), style.zoom(225)
@ -1418,7 +1421,7 @@ class Activity(Window, Gtk.Container):
Gdk.flush() Gdk.flush()
class _ClientHandler(dbus.service.Object, DBusProperties): class _ClientHandler(dbus.service.Object):
def __init__(self, bundle_id, got_channel_cb): def __init__(self, bundle_id, got_channel_cb):
self._interfaces = set([CLIENT, CLIENT_HANDLER, PROPERTIES_IFACE]) self._interfaces = set([CLIENT, CLIENT_HANDLER, PROPERTIES_IFACE])
self._got_channel_cb = got_channel_cb self._got_channel_cb = got_channel_cb
@ -1429,12 +1432,13 @@ class _ClientHandler(dbus.service.Object, DBusProperties):
path = '/' + name.replace('.', '/') path = '/' + name.replace('.', '/')
dbus.service.Object.__init__(self, bus_name, path) dbus.service.Object.__init__(self, bus_name, path)
DBusProperties.__init__(self)
self._implement_property_get(CLIENT, { self._prop_getters = {}
self._prop_setters = {}
self._prop_getters.setdefault(CLIENT, {}).update({
'Interfaces': lambda: list(self._interfaces), 'Interfaces': lambda: list(self._interfaces),
}) })
self._implement_property_get(CLIENT_HANDLER, { self._prop_getters.setdefault(CLIENT_HANDLER, {}).update({
'HandlerChannelFilter': self.__get_filters_cb, 'HandlerChannelFilter': self.__get_filters_cb,
}) })
@ -1465,6 +1469,34 @@ class _ClientHandler(dbus.service.Object, DBusProperties):
except Exception as e: except Exception as e:
logging.exception(e) logging.exception(e)
@dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE,
in_signature='ss', out_signature='v')
def Get(self, interface_name, property_name):
if interface_name in self._prop_getters \
and property_name in self._prop_getters[interface_name]:
return self._prop_getters[interface_name][property_name]()
else:
logging.debug('InvalidArgument')
@dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE,
in_signature='ssv', out_signature='')
def Set(self, interface_name, property_name, value):
if interface_name in self._prop_setters \
and property_name in self._prop_setters[interface_name]:
self._prop_setters[interface_name][property_name](value)
else:
logging.debug('PermissionDenied')
@dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE,
in_signature='s', out_signature='a{sv}')
def GetAll(self, interface_name):
if interface_name in self._prop_getters:
r = {}
for k, v in self._prop_getters[interface_name].items():
r[k] = v()
return r
else:
logging.debug('InvalidArgument')
_session = None _session = None

View File

@ -25,27 +25,32 @@ import six
import logging import logging
from functools import partial from functools import partial
import gi
gi.require_version('TelepathyGLib', '0.12')
import dbus import dbus
from dbus import PROPERTIES_IFACE from dbus import PROPERTIES_IFACE
from gi.repository import GObject from gi.repository import GObject
from telepathy.client import Channel from gi.repository import TelepathyGLib
from telepathy.interfaces import CHANNEL, \
CHANNEL_INTERFACE_GROUP, \
CHANNEL_TYPE_TUBES, \
CHANNEL_TYPE_TEXT, \
CONNECTION, \
PROPERTIES_INTERFACE
from telepathy.constants import CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES, \
HANDLE_TYPE_ROOM, \
HANDLE_TYPE_CONTACT, \
PROPERTY_FLAG_WRITE
from sugar3.presence.buddy import Buddy from sugar3.presence.buddy import Buddy
CHANNEL = TelepathyGLib.IFACE_CHANNEL
CHANNEL_INTERFACE_GROUP = TelepathyGLib.IFACE_CHANNEL_INTERFACE_GROUP
CONN_INTERFACE_ROOM_CONFIG = \
TelepathyGLib.IFACE_CHANNEL_INTERFACE_ROOM_CONFIG
CHANNEL_TYPE_TUBES = TelepathyGLib.IFACE_CHANNEL_TYPE_TUBES
CHANNEL_TYPE_TEXT = TelepathyGLib.IFACE_CHANNEL_TYPE_TEXT
CONNECTION = TelepathyGLib.IFACE_CONNECTION
PROPERTIES_INTERFACE = TelepathyGLib.IFACE_PROPERTIES_INTERFACE
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES = \
TelepathyGLib.ChannelGroupFlags.CHANNEL_SPECIFIC_HANDLES
HANDLE_TYPE_CONTACT = TelepathyGLib.HandleType.CONTACT
HANDLE_TYPE_ROOM = TelepathyGLib.HandleType.ROOM
PROPERTY_FLAG_WRITE = TelepathyGLib.PropertyFlags.WRITE
CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties' CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'
CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo' CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo'
CONN_INTERFACE_ROOM_CONFIG = \
'org.freedesktop.Telepathy.Channel.Interface.RoomConfig1'
_logger = logging.getLogger('sugar3.presence.activity') _logger = logging.getLogger('sugar3.presence.activity')
@ -274,7 +279,7 @@ class Activity(GObject.GObject):
if not self._joined: if not self._joined:
raise RuntimeError('Cannot invite a buddy to an activity that is' raise RuntimeError('Cannot invite a buddy to an activity that is'
'not shared.') 'not shared.')
self.telepathy_text_chan.AddMembers( self.telepathy_text_chan[CHANNEL].AddMembers(
[buddy.contact_handle], message, [buddy.contact_handle], message,
dbus_interface=CHANNEL_INTERFACE_GROUP, dbus_interface=CHANNEL_INTERFACE_GROUP,
reply_handler=partial( reply_handler=partial(
@ -489,7 +494,7 @@ class Activity(GObject.GObject):
def leave(self): def leave(self):
"""Leave this shared activity""" """Leave this shared activity"""
_logger.debug('%r: leaving' % self) _logger.debug('%r: leaving' % self)
self.telepathy_text_chan.Close() self.telepathy_text_chan[CHANNEL].Close()
class _BaseCommand(GObject.GObject): class _BaseCommand(GObject.GObject):
@ -586,6 +591,7 @@ class _JoinCommand(_BaseCommand):
def __get_self_handle_cb(self, handle): def __get_self_handle_cb(self, handle):
self._global_self_handle = handle self._global_self_handle = handle
self._text_ready = False
self._connection.RequestChannel( self._connection.RequestChannel(
CHANNEL_TYPE_TEXT, CHANNEL_TYPE_TEXT,
HANDLE_TYPE_ROOM, HANDLE_TYPE_ROOM,
@ -594,6 +600,7 @@ class _JoinCommand(_BaseCommand):
error_handler=self.__error_handler_cb, error_handler=self.__error_handler_cb,
dbus_interface=CONNECTION) dbus_interface=CONNECTION)
self._tubes_ready = False
self._connection.RequestChannel( self._connection.RequestChannel(
CHANNEL_TYPE_TUBES, CHANNEL_TYPE_TUBES,
HANDLE_TYPE_ROOM, HANDLE_TYPE_ROOM,
@ -604,18 +611,52 @@ class _JoinCommand(_BaseCommand):
dbus_interface=CONNECTION) dbus_interface=CONNECTION)
def __create_text_channel_cb(self, channel_path): def __create_text_channel_cb(self, channel_path):
Channel(self._connection.requested_bus_name, channel_path, self.text_channel = {}
ready_handler=self.__text_channel_ready_cb) self.text_proxy = dbus.Bus().get_object(
self._connection.requested_bus_name, channel_path)
self.text_channel[PROPERTIES_IFACE] = dbus.Interface(
self.text_proxy, PROPERTIES_IFACE)
self.text_channel[CHANNEL_TYPE_TEXT] = \
dbus.Interface(self.text_proxy, CHANNEL_TYPE_TEXT)
self.text_channel[CHANNEL] = dbus.Interface(self.text_proxy, CHANNEL)
self.text_channel[CHANNEL].GetInterfaces(
reply_handler=self.__text_get_interfaces_reply_cb,
error_handler=self.__error_handler_cb)
def __text_get_interfaces_reply_cb(self, interfaces):
for interface in interfaces:
self.text_channel[interface] = dbus.Interface(
self.text_proxy, interface)
_logger.debug('%r: Text channel is ready' % (self))
self._text_ready = True
self._ready()
def __create_tubes_channel_cb(self, channel_path): def __create_tubes_channel_cb(self, channel_path):
Channel(self._connection.requested_bus_name, channel_path, self.tubes_channel = {}
ready_handler=self.__tubes_channel_ready_cb) self.tubes_proxy = dbus.Bus().get_object(
self._connection.requested_bus_name, channel_path)
self.tubes_channel[PROPERTIES_IFACE] = dbus.Interface(
self.tubes_proxy, PROPERTIES_IFACE)
self.tubes_channel[CHANNEL_TYPE_TUBES] = \
dbus.Interface(self.tubes_proxy, CHANNEL_TYPE_TUBES)
self.tubes_channel[CHANNEL] = dbus.Interface(self.tubes_proxy, CHANNEL)
self.tubes_channel[CHANNEL].GetInterfaces(
reply_handler=self.__tubes_get_interfaces_reply_cb,
error_handler=self.__error_handler_cb)
def __tubes_get_interfaces_reply_cb(self, interfaces):
for interface in interfaces:
self.tubes_channel[interface] = dbus.Interface(
self.tubes_proxy, interface)
_logger.debug('%r: Tubes channel is ready' % (self))
self._tubes_ready = True
self._ready()
def __tubes_error_handler_cb(self, error): def __tubes_error_handler_cb(self, error):
if (error.get_dbus_name() == if (error.get_dbus_name() ==
'org.freedesktop.Telepathy.Error.NotImplemented'): 'org.freedesktop.Telepathy.Error.NotImplemented'):
self._tubes_supported = False self._tubes_supported = False
self._tubes_ready() self._ready()
else: else:
self._finished = True self._finished = True
self.emit('finished', error) self.emit('finished', error)
@ -624,22 +665,12 @@ class _JoinCommand(_BaseCommand):
self._finished = True self._finished = True
self.emit('finished', error) self.emit('finished', error)
def __tubes_channel_ready_cb(self, channel): def _ready(self):
_logger.debug('%r: Tubes channel %r is ready' % (self, channel)) if not self._text_ready or \
self.tubes_channel = channel (self._tubes_supported and not self._tubes_ready):
self._tubes_ready()
def __text_channel_ready_cb(self, channel):
_logger.debug('%r: Text channel %r is ready' % (self, channel))
self.text_channel = channel
self._tubes_ready()
def _tubes_ready(self):
if self.text_channel is None or \
(self._tubes_supported and self.tubes_channel is None):
return return
_logger.debug('%r: finished setting up tubes' % self) _logger.debug('%r: finished setting up channel' % self)
self._add_self_to_channel() self._add_self_to_channel()

View File

@ -22,18 +22,26 @@ STABLE.
""" """
import logging import logging
import six import six
import gi
gi.require_version('TelepathyGLib', '0.12')
from gi.repository import GObject from gi.repository import GObject
import dbus import dbus
from telepathy.interfaces import CONNECTION, \
CONNECTION_INTERFACE_ALIASING, \
CONNECTION_INTERFACE_CONTACTS
from telepathy.constants import HANDLE_TYPE_CONTACT
from sugar3.presence.connectionmanager import get_connection_manager from sugar3.presence.connectionmanager import get_connection_manager
from sugar3.profile import get_color, get_nick_name from sugar3.profile import get_color, get_nick_name
ACCOUNT_MANAGER_SERVICE = 'org.freedesktop.Telepathy.AccountManager' from gi.repository import TelepathyGLib
CONNECTION = TelepathyGLib.IFACE_CONNECTION
CONNECTION_INTERFACE_ALIASING = \
TelepathyGLib.IFACE_CONNECTION_INTERFACE_ALIASING
CONNECTION_INTERFACE_CONTACTS = \
TelepathyGLib.IFACE_CONNECTION_INTERFACE_CONTACTS
HANDLE_TYPE_CONTACT = TelepathyGLib.HandleType.CONTACT
CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo' CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo'
_logger = logging.getLogger('sugar3.presence.buddy') _logger = logging.getLogger('sugar3.presence.buddy')

View File

@ -23,12 +23,20 @@ from functools import partial
import dbus import dbus
from dbus import PROPERTIES_IFACE from dbus import PROPERTIES_IFACE
from telepathy.interfaces import ACCOUNT, \
ACCOUNT_MANAGER
from telepathy.constants import CONNECTION_STATUS_CONNECTED
ACCOUNT_MANAGER_SERVICE = 'org.freedesktop.Telepathy.AccountManager' from gi.repository import TelepathyGLib
ACCOUNT_MANAGER_PATH = '/org/freedesktop/Telepathy/AccountManager'
ACCOUNT_MANAGER_SERVICE = TelepathyGLib.ACCOUNT_MANAGER_BUS_NAME
ACCOUNT_MANAGER_PATH = TelepathyGLib.ACCOUNT_MANAGER_OBJECT_PATH
ACCOUNT_MANAGER = TelepathyGLib.IFACE_ACCOUNT_MANAGER
ACCOUNT = TelepathyGLib.IFACE_ACCOUNT
HANDLE_TYPE_CONTACT = TelepathyGLib.HandleType.CONTACT
CONNECTION = TelepathyGLib.IFACE_CONNECTION
CONNECTION_STATUS_CONNECTED = TelepathyGLib.ConnectionStatus.CONNECTED
class Connection(object): class Connection(object):

View File

@ -21,8 +21,6 @@ STABLE.
""" """
import logging import logging
from gi.repository import GObject
import dbus import dbus
import dbus.exceptions import dbus.exceptions
from dbus import PROPERTIES_IFACE from dbus import PROPERTIES_IFACE
@ -31,16 +29,20 @@ from sugar3.presence.buddy import Buddy, Owner
from sugar3.presence.activity import Activity from sugar3.presence.activity import Activity
from sugar3.presence.connectionmanager import get_connection_manager from sugar3.presence.connectionmanager import get_connection_manager
from telepathy.interfaces import ACCOUNT, \ from gi.repository import GObject
ACCOUNT_MANAGER, \ from gi.repository import TelepathyGLib
CONNECTION
from telepathy.constants import HANDLE_TYPE_CONTACT
_logger = logging.getLogger('sugar3.presence.presenceservice') _logger = logging.getLogger('sugar3.presence.presenceservice')
ACCOUNT_MANAGER_SERVICE = 'org.freedesktop.Telepathy.AccountManager' ACCOUNT_MANAGER_SERVICE = TelepathyGLib.ACCOUNT_MANAGER_BUS_NAME
ACCOUNT_MANAGER_PATH = '/org/freedesktop/Telepathy/AccountManager' ACCOUNT_MANAGER_PATH = TelepathyGLib.ACCOUNT_MANAGER_OBJECT_PATH
ACCOUNT_MANAGER = TelepathyGLib.IFACE_ACCOUNT_MANAGER
ACCOUNT = TelepathyGLib.IFACE_ACCOUNT
HANDLE_TYPE_CONTACT = TelepathyGLib.HandleType.CONTACT
CONNECTION = TelepathyGLib.IFACE_CONNECTION
CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties' CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'

View File

@ -19,11 +19,12 @@
STABLE. STABLE.
""" """
from telepathy.constants import (
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES)
from sugar3.presence.tubeconn import TubeConnection from sugar3.presence.tubeconn import TubeConnection
from sugar3.presence import presenceservice from sugar3.presence import presenceservice
from gi.repository import TelepathyGLib
CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES = \
TelepathyGLib.ChannelGroupFlags.CHANNEL_SPECIFIC_HANDLES
class SugarTubeConnection(TubeConnection): class SugarTubeConnection(TubeConnection):