~ubuntu-branches/ubuntu/maverick/coherence/maverick

« back to all changes in this revision

Viewing changes to coherence/extern/telepathy/tubeconn.py

  • Committer: Bazaar Package Importer
  • Author(s): Charlie Smotherman
  • Date: 2010-01-02 10:57:15 UTC
  • mfrom: (1.1.7 upstream) (3.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100102105715-sghzl2nw4lr5b1ob
Tags: 0.6.6.2-1
*  New  upstream release, summary of changes:
    - adding all necessary files to MANIFEST.in, to compensate for the
      gone 'auto-include-all-files-under-version-control' setuptools
      feature.
    - rearranging genre and genres attribute in DIDLLite - thx Caleb  
    - fix face_path typo, fixes #275.   

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU Lesser General Public License as published
 
5
# by the Free Software Foundation; either version 2.1 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Lesser General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Lesser General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 
 
17
# Modified by Philippe Normand.
 
18
 
 
19
from dbus.connection import Connection
 
20
from dbus import PROPERTIES_IFACE
 
21
 
 
22
from telepathy.interfaces import CHANNEL_TYPE_DBUS_TUBE
 
23
 
 
24
from coherence import log
 
25
 
 
26
class TubeConnection(Connection, log.Loggable):
 
27
    logCategory = "tube_connection"
 
28
 
 
29
    def __new__(cls, conn, tube, address, group_iface=None, mainloop=None):
 
30
        self = super(TubeConnection, cls).__new__(cls, address,
 
31
                                                  mainloop=mainloop)
 
32
 
 
33
        self._tube = tube
 
34
        self.participants = {}
 
35
        self.bus_name_to_handle = {}
 
36
        self._mapping_watches = []
 
37
 
 
38
        if group_iface is None:
 
39
            method = conn.GetSelfHandle
 
40
        else:
 
41
            method = group_iface.GetSelfHandle
 
42
 
 
43
        method(reply_handler=self._on_get_self_handle_reply,
 
44
               error_handler=self._on_get_self_handle_error)
 
45
 
 
46
        return self
 
47
 
 
48
    def _on_get_self_handle_reply(self, handle):
 
49
        self.self_handle = handle
 
50
        tube_channel = self._tube[CHANNEL_TYPE_DBUS_TUBE]
 
51
        match = tube_channel.connect_to_signal('DBusNamesChanged',
 
52
                                               self._on_dbus_names_changed)
 
53
        self._tube[PROPERTIES_IFACE].Get(CHANNEL_TYPE_DBUS_TUBE, 'DBusNames',
 
54
                                         reply_handler=self._on_get_dbus_names_reply,
 
55
                                         error_handler=self._on_get_dbus_names_error)
 
56
        self._dbus_names_changed_match = match
 
57
 
 
58
    def _on_get_self_handle_error(self, e):
 
59
        self.warning('GetSelfHandle failed: %s', e)
 
60
 
 
61
    def close(self):
 
62
        self._dbus_names_changed_match.remove()
 
63
        self._on_dbus_names_changed({}, self.participants.keys())
 
64
        super(TubeConnection, self).close()
 
65
 
 
66
    def _on_get_dbus_names_reply(self, names):
 
67
        self._on_dbus_names_changed(names, ())
 
68
 
 
69
    def _on_get_dbus_names_error(self, e):
 
70
        self.warning('Get DBusNames property failed: %s', e)
 
71
 
 
72
    def _on_dbus_names_changed(self, added, removed):
 
73
        for handle, bus_name in added.items():
 
74
            if handle == self.self_handle:
 
75
                # I've just joined - set my unique name
 
76
                self.set_unique_name(bus_name)
 
77
            self.participants[handle] = bus_name
 
78
            self.bus_name_to_handle[bus_name] = handle
 
79
 
 
80
        # call the callback while the removed people are still in
 
81
        # participants, so their bus names are available
 
82
        for callback in self._mapping_watches:
 
83
            callback(added, removed)
 
84
 
 
85
        for handle in removed:
 
86
            bus_name = self.participants.pop(handle, None)
 
87
            self.bus_name_to_handle.pop(bus_name, None)
 
88
 
 
89
    def watch_participants(self, callback):
 
90
        self._mapping_watches.append(callback)
 
91
        if self.participants:
 
92
            # GetDBusNames already returned: fake a participant add event
 
93
            # immediately
 
94
            added = list(self.participants.iteritems())
 
95
            callback(added, [])