# -*- coding: UTF-8 -*- # Copyright (C) 2009 Andrés Ambrois # # This program 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; either version 2 of the License, or # (at your option) any later version. # # This program 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 this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import gobject import telepathy from sugar.presence import presenceservice class BaseChat(gobject.GObject): ''' Base class for Chat models ''' channels = {} def __init__(self, activity_ps, keys=[]): ''' activity_ps -- sugar.presence.activity instance where we get the telepathy channels from. keys -- If we need to act on received messages, give us a list of the public keys of these buddies. ''' gobject.GObject.__init__(self) self.keys = dict.fromkeys(keys) self._ps = presenceservice.get_instance() self.channel = None try: self._find_channel(activity_ps) except KeyError: activity_ps.connect('new-channel', lambda obj, path: self._find_channel(activity_ps)) def _find_channel(self, activity_ps): id = activity_ps.props.id try: self.channel = BaseChat.channels[id] except KeyError: # Get the activity text chan from the presence service bus_name, conn_path, channel_paths = activity_ps.get_channels() for channel_path in channel_paths: channel = telepathy.client.Channel(bus_name, channel_path) htype, handle = channel.GetHandle() if htype == telepathy.HANDLE_TYPE_ROOM: if channel.GetChannelType() == telepathy.CHANNEL_TYPE_TEXT: BaseChat.channels[id] = channel self.channel = channel self._received_sh = self.channel[ telepathy.CHANNEL_TYPE_TEXT].connect_to_signal( 'Received', self.message_received_cb) for id, timestamp, sender, type, flags, text in \ self.channel[telepathy.CHANNEL_TYPE_TEXT ].ListPendingMessages(False): self.message_received_cb(id, timestamp, sender, type, flags, text) if self.channel == None: raise KeyError def message_received_cb(self, id, timestamp, sender, type, flags, text): if self.keys: self._find_handles() def _find_handles(self): group = self.channel[telepathy.CHANNEL_INTERFACE_GROUP] conn_name, conn_path = self._ps.get_preferred_connection() if group.GetGroupFlags() & \ telepathy.CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES: owners = self.channel.GetAll(telepathy.CHANNEL_INTERFACE_GROUP, dbus_interface='org.freedesktop.DBus.Properties').get( 'HandleOwners') for handle, owner in owners.items(): buddy = self._ps.get_buddy_by_telepathy_handle( conn_name, conn_path, owner) if buddy is None: continue if buddy.props.key in self.keys.keys(): # keys, keys, keys!! self.keys[buddy.props.key] = handle else: buddy = self._ps.get_buddy_by_telepathy_handle( conn_name, conn_path, sender) if buddy.props.key in self.keys.keys(): self.keys[buddy.props.key] = sender def disconnect_signals(self, object=None): self._received_sh.remove() class ChatCatcher(BaseChat): __gsignals__ = { 'chat-received': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([gobject.TYPE_STRING])), } def __init__(self, activity_ps, key): BaseChat.__init__(self, activity_ps, [key]) def message_received_cb(self, id, timestamp, sender, type, flags, text): BaseChat.message_received_cb(self, id, timestamp, sender, type, flags, text) if sender in self.keys.values(): self.emit('chat-received', text) self.channel[ telepathy.CHANNEL_TYPE_TEXT].AcknowledgePendingMessages([id]) class ChatPitcher(BaseChat): def __init__(self, activity_ps): BaseChat.__init__(self, activity_ps) def send(self, text): if self.channel is not None: self.channel[telepathy.CHANNEL_TYPE_TEXT].Send( telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, text)