9 def create_contact_list_channel(connection, h):
10 if h.get_name() == 'subscribe':
11 channel_class = TheOneRingSubscribeListChannel
12 elif h.get_name() == 'publish':
13 channel_class = TheOneRingPublishListChannel
14 elif h.get_name() == 'hide':
15 logging.warn("Unsuported type %s" % h.get_name())
16 elif h.get_name() == 'allow':
17 logging.warn("Unsuported type %s" % h.get_name())
18 elif h.get_name() == 'deny':
19 logging.warn("Unsuported type %s" % h.get_name())
21 raise TypeError("Unknown list type : " + h.get_name())
22 return channel_class(connection, h)
25 class TheOneRingListChannel(
26 telepathy.server.ChannelTypeContactList,
27 telepathy.server.ChannelInterfaceGroup,
29 "Abstract Contact List channels"
31 def __init__(self, connection, h):
32 telepathy.server.ChannelTypeContactList.__init__(self, connection, h)
33 telepathy.server.ChannelInterfaceGroup.__init__(self)
35 self._conn_ref = weakref.ref(connection)
37 def GetLocalPendingMembersWithInfo(self):
41 class TheOneRingSubscribeListChannel(TheOneRingListChannel):
43 Subscribe List channel.
45 This channel contains the list of contact to whom the current used is
46 'subscribed', basically this list contains the contact for whom you are
47 supposed to receive presence notification.
50 def __init__(self, connection, h):
51 TheOneRingListChannel.__init__(self, connection, h)
52 self.GroupFlagsChanged(
53 telepathy.CHANNEL_GROUP_FLAG_CAN_ADD |
54 telepathy.CHANNEL_GROUP_FLAG_CAN_REMOVE,
58 def AddMembers(self, contacts, message):
59 addressBook = self._conn.gvoice_client
61 h = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, h)
66 account = contact.account
67 groups = list(h.pending_groups)
68 h.pending_groups = set()
69 addressBook.add_messenger_contact(account,
70 invite_message=message.encode('utf-8'),
73 def RemoveMembers(self, contacts, message):
74 addressBook = self._conn.gvoice_client
76 h = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, h)
78 addressBook.delete_contact(contact)
81 class TheOneRingPublishListChannel(TheOneRingListChannel):
83 def __init__(self, connection, h):
84 TheOneRingListChannel.__init__(self, connection, h)
85 self.GroupFlagsChanged(0, 0)
87 def AddMembers(self, contacts, message):
88 addressBook = self._conn.gvoice_client
89 for contactHandleId in contacts:
90 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
92 contact = contactHandle.contact
93 addressBook.accept_contact_invitation(contact, False)
95 def RemoveMembers(self, contacts, message):
96 addressBook = self._conn.gvoice_client
97 for contactHandleId in contacts:
98 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
100 contact = contactHandle.contact
102 def GetLocalPendingMembersWithInfo(self):
103 addressBook = self._conn.gvoice_client
105 for contact in addressBook.contacts:
106 h = handle.create_handle(self._conn_ref(), 'contact',
107 contact.account, contact.network_id)
109 telepathy.CHANNEL_GROUP_CHANGE_REASON_INVITED,
110 contact.attributes.get('invite_message', '')))
114 class TheOneRingGroupChannel(TheOneRingListChannel):
116 def __init__(self, connection, h):
117 self.__pending_add = []
118 self.__pending_remove = []
119 TheOneRingListChannel.__init__(self, connection, h)
120 self.GroupFlagsChanged(
121 telepathy.CHANNEL_GROUP_FLAG_CAN_ADD | telepathy.CHANNEL_GROUP_FLAG_CAN_REMOVE,
125 def AddMembers(self, contacts, message):
126 addressBook = self._conn.gvoice_client
127 if self._handle.group is None:
128 for contactHandleId in contacts:
129 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
130 logging.info("Adding contact %r to pending group %r" % (contactHandle, self._handle))
131 if contactHandleId in self.__pending_remove:
132 self.__pending_remove.remove(contactHandleId)
134 self.__pending_add.append(contactHandleId)
137 for contactHandleId in contacts:
138 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
139 logging.info("Adding contact %r to group %r" % (contactHandle, self._handle))
140 contact = contactHandle.contact
141 group = self._handle.group
142 if contact is not None:
143 addressBook.add_contact_to_group(group, contact)
145 contactHandle.pending_groups.add(group)
147 def RemoveMembers(self, contacts, message):
148 addressBook = self._conn.gvoice_client
149 if self._handle.group is None:
150 for contactHandleId in contacts:
151 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
152 logging.info("Adding contact %r to pending group %r" % (contactHandle, self._handle))
153 if contactHandleId in self.__pending_add:
154 self.__pending_add.remove(contactHandleId)
156 self.__pending_remove.append(contactHandleId)
159 for contactHandleId in contacts:
160 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
161 logging.info("Removing contact %r from pending group %r" % (contactHandle, self._handle))
162 contact = contactHandle.contact
163 group = self._handle.group
164 if contact is not None:
165 addressBook.delete_contact_from_group(group, contact)
167 contactHandle.pending_groups.discard(group)
170 logging.debug("Deleting group %s" % self._handle.name)
171 addressBook = self._conn.gvoice_client
172 group = self._handle.group
173 addressBook.delete_group(group)