Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/presence/chat.py
blob: 86950226ca0106291f4b37c0f7164438f4443bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- 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)