summaryrefslogtreecommitdiff
path: root/butterfly/capabilities.py
blob: 4155fe34d6c02a010e4f90b6c9f3f6d940386535 (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
126
127
128
129
130
131
# telepathy-butterfly - an MSN connection manager for Telepathy
#
# Copyright (C) 2009 Collabora Ltd.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import logging
import dbus

import telepathy
import papyon
import papyon.event

from butterfly.util.decorator import async
from butterfly.handle import ButterflyHandleFactory

__all__ = ['ButterflyCapabilities']

logger = logging.getLogger('Butterfly.Capabilities')

class ButterflyCapabilities(
        telepathy.server.ConnectionInterfaceCapabilities,
        papyon.event.ContactEventInterface):

    def __init__(self):
        telepathy.server.ConnectionInterfaceCapabilities.__init__(self)
        papyon.event.ContactEventInterface.__init__(self, self.msn_client)
        dbus_interface = telepathy.CONNECTION_INTERFACE_CAPABILITIES

    def AdvertiseCapabilities(self, add, remove):
        #for caps, specs in add:
            #if caps == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
                #if specs & telepathy.CHANNEL_MEDIA_CAPABILITY_VIDEO:
                    #self._self_handle.profile.client_id.has_webcam = True
                    #self._self_handle.profile.client_id.supports_rtc_video = True
        #for caps in remove:
            #if caps == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
                #self._self_handle.profile.client_id.has_webcam = False

        return telepathy.server.ConnectionInterfaceCapabilities.\
            AdvertiseCapabilities(self, add, remove)

    # papyon.event.ContactEventInterface
    def on_contact_client_capabilities_changed(self, contact):
        self._update_capabilities(contact)

    # papyon.event.AddressBookEventInterface
    def on_addressbook_contact_added(self, contact):
        """When we add a contact in our contact list, add the
        capabilities to create text channel to the contact"""
        if contact.is_member(Membership.FORWARD):
            handle = ButterflyHandleFactory(self._conn_ref(), 'contact',
                    contact.account, contact.network_id)
            self.add_text_capabilities([handle])

    def add_text_capabilities(self, contacts_handles):
        """Add the create capability for text channel to these contacts."""
        ret = []
        for handle in contacts_handles:
            ctype = telepathy.CHANNEL_TYPE_TEXT
            if handle in self._caps:
                old_gen, old_spec = self._caps[handle][ctype]
            else:
                old_gen = 0
                old_spec = 0
            new_gen = old_gen
            new_gen |= telepathy.CONNECTION_CAPABILITY_FLAG_CREATE

            diff = (int(handle), ctype, old_gen, new_gen, old_spec, old_spec)
            ret.append(diff)

        self.CapabilitiesChanged(ret)

    def _update_capabilities(self, contact):
        handle = ButterflyHandleFactory(self, 'contact',
                contact.account, contact.network_id)
        ctype = telepathy.CHANNEL_TYPE_STREAMED_MEDIA

        new_gen, new_spec = self._get_capabilities(contact)
        if handle in self._caps:
            old_gen, old_spec = self._caps[handle][ctype]
        else:
            old_gen = 0
            old_spec = 0

        if old_gen == new_gen and old_spec == new_spec:
            return

        diff = (int(handle), ctype, old_gen, new_gen, old_spec, new_spec)
        self.CapabilitiesChanged([diff])

    def _get_capabilities(self, contact):
        gen_caps = 0
        spec_caps = 0

        caps = contact.client_capabilities
        #if caps.supports_sip_invite:
            #gen_caps |= telepathy.CONNECTION_CAPABILITY_FLAG_CREATE
            #gen_caps |= telepathy.CONNECTION_CAPABILITY_FLAG_INVITE
            #spec_caps |= telepathy.CHANNEL_MEDIA_CAPABILITY_AUDIO
            #spec_caps |= telepathy.CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN

            #if caps.has_webcam:
                #spec_caps |= telepathy.CHANNEL_MEDIA_CAPABILITY_VIDEO

        return gen_caps, spec_caps

    @async
    def _populate_capabilities(self):
        """ Add the capability to create text channels to all contacts in our
        contacts list."""
        handles = set()
        for contact in self.msn_client.address_book.contacts:
            if contact.is_member(papyon.Membership.FORWARD):
                handle = ButterflyHandleFactory(self, 'contact',
                        contact.account, contact.network_id)
                handles.add(handle)
        self.add_text_capabilities(handles)