Missed some locations
[theonering] / src / channel / contact_list.py
1 import weakref
2 import logging
3
4 import telepathy
5
6 import handle
7
8
9 _moduleLogger = logging.getLogger("channel.contact_list")
10
11
12 def create_contact_list_channel(connection, h):
13         if h.get_name() == 'subscribe':
14                 channel_class = SubscribeListChannel
15         elif h.get_name() == 'publish':
16                 channel_class = PublishListChannel
17         elif h.get_name() == 'hide':
18                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
19         elif h.get_name() == 'allow':
20                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
21         elif h.get_name() == 'deny':
22                 _moduleLogger.warn("Unsuported type %s" % h.get_name())
23         else:
24                 raise TypeError("Unknown list type : " + h.get_name())
25         return channel_class(connection, h)
26
27
28 class AbstractListChannel(
29                 telepathy.server.ChannelTypeContactList,
30                 telepathy.server.ChannelInterfaceGroup,
31         ):
32         "Abstract Contact List channels"
33
34         def __init__(self, connection, h):
35                 telepathy.server.ChannelTypeContactList.__init__(self, connection, h)
36                 telepathy.server.ChannelInterfaceGroup.__init__(self)
37
38                 self._conn_ref = weakref.ref(connection)
39
40         def GetLocalPendingMembersWithInfo(self):
41                 return []
42
43
44 class SubscribeListChannel(AbstractListChannel):
45         """
46         Subscribe List channel.
47
48         This channel contains the list of contact to whom the current used is
49         'subscribed', basically this list contains the contact for whom you are
50         supposed to receive presence notification.
51         """
52
53         def __init__(self, connection, h):
54                 AbstractListChannel.__init__(self, connection, h)
55                 self.GroupFlagsChanged(
56                         telepathy.CHANNEL_GROUP_FLAG_CAN_ADD |
57                         telepathy.CHANNEL_GROUP_FLAG_CAN_REMOVE,
58                         0,
59                 )
60
61         def AddMembers(self, contacts, message):
62                 addressBook = self._conn.gvoice_client
63                 for h in contacts:
64                         h = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, h)
65                         contact = h.contact
66                         if contact is None:
67                                 account = h.account
68                         else:
69                                 account = contact.account
70                         groups = list(h.pending_groups)
71                         h.pending_groups = set()
72                         addressBook.add_messenger_contact(account,
73                                         invite_message=message.encode('utf-8'),
74                                         groups=groups)
75
76         def RemoveMembers(self, contacts, message):
77                 addressBook = self._conn.gvoice_client
78                 for h in contacts:
79                         h = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, h)
80                         contact = h.contact
81                         addressBook.delete_contact(contact)
82
83
84 class PublishListChannel(AbstractListChannel):
85
86         def __init__(self, connection, h):
87                 AbstractListChannel.__init__(self, connection, h)
88                 self.GroupFlagsChanged(0, 0)
89
90         def AddMembers(self, contacts, message):
91                 addressBook = self._conn.gvoice_client
92                 for contactHandleId in contacts:
93                         contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
94                                                 contactHandleId)
95                         contact = contactHandle.contact
96                         addressBook.accept_contact_invitation(contact, False)
97
98         def RemoveMembers(self, contacts, message):
99                 addressBook = self._conn.gvoice_client
100                 for contactHandleId in contacts:
101                         contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
102                                                 contactHandleId)
103                         contact = contactHandle.contact
104
105         def GetLocalPendingMembersWithInfo(self):
106                 addressBook = self._conn.gvoice_client
107                 result = []
108                 for contact in addressBook.contacts:
109                         h = handle.create_handle(self._conn_ref(), 'contact',
110                                         contact.account, contact.network_id)
111                         result.append((h, h,
112                                         telepathy.CHANNEL_GROUP_CHANGE_REASON_INVITED,
113                                         contact.attributes.get('invite_message', '')))
114                 return result
115
116
117 class GroupChannel(AbstractListChannel):
118
119         def __init__(self, connection, h):
120                 self.__pending_add = []
121                 self.__pending_remove = []
122                 AbstractListChannel.__init__(self, connection, h)
123                 self.GroupFlagsChanged(
124                         telepathy.CHANNEL_GROUP_FLAG_CAN_ADD | telepathy.CHANNEL_GROUP_FLAG_CAN_REMOVE,
125                         0,
126                 )
127
128         def AddMembers(self, contacts, message):
129                 addressBook = self._conn.gvoice_client
130                 if self._handle.group is None:
131                         for contactHandleId in contacts:
132                                 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
133                                 _moduleLogger.info("Adding contact %r to pending group %r" % (contactHandle, self._handle))
134                                 if contactHandleId in self.__pending_remove:
135                                         self.__pending_remove.remove(contactHandleId)
136                                 else:
137                                         self.__pending_add.append(contactHandleId)
138                         return
139                 else:
140                         for contactHandleId in contacts:
141                                 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
142                                 _moduleLogger.info("Adding contact %r to group %r" % (contactHandle, self._handle))
143                                 contact = contactHandle.contact
144                                 group = self._handle.group
145                                 if contact is not None:
146                                         addressBook.add_contact_to_group(group, contact)
147                                 else:
148                                         contactHandle.pending_groups.add(group)
149
150         def RemoveMembers(self, contacts, message):
151                 addressBook = self._conn.gvoice_client
152                 if self._handle.group is None:
153                         for contactHandleId in contacts:
154                                 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
155                                 _moduleLogger.info("Adding contact %r to pending group %r" % (contactHandle, self._handle))
156                                 if contactHandleId in self.__pending_add:
157                                         self.__pending_add.remove(contactHandleId)
158                                 else:
159                                         self.__pending_remove.append(contactHandleId)
160                         return
161                 else:
162                         for contactHandleId in contacts:
163                                 contactHandle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT, contactHandleId)
164                                 _moduleLogger.info("Removing contact %r from pending group %r" % (contactHandle, self._handle))
165                                 contact = contactHandle.contact
166                                 group = self._handle.group
167                                 if contact is not None:
168                                         addressBook.delete_contact_from_group(group, contact)
169                                 else:
170                                         contactHandle.pending_groups.discard(group)
171
172         def Close(self):
173                 _moduleLogger.debug("Deleting group %s" % self._handle.name)
174                 addressBook = self._conn.gvoice_client
175                 group = self._handle.group
176                 addressBook.delete_group(group)