comparison browser_side/contact.py @ 55:d5266c41ca24

Roster list update, contact deletion + some refactoring
author Goffi <goffi@goffi.org>
date Sun, 29 May 2011 02:13:53 +0200
parents f25c4077f6b9
children 12e889a683ce
comparison
equal deleted inserted replaced
54:f25c4077f6b9 55:d5266c41ca24
88 def add(self, group): 88 def add(self, group):
89 _item = GroupLabel(group) 89 _item = GroupLabel(group)
90 _item.addMouseListener(self._parent) 90 _item.addMouseListener(self._parent)
91 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer") 91 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
92 VerticalPanel.add(self, _item) 92 VerticalPanel.add(self, _item)
93
94 def remove(self, group):
95 for wid in self:
96 if isinstance(wid, GroupLabel) and wid.group == group:
97 VerticalPanel.remove(self, wid)
93 98
94 class ContactList(VerticalPanel): 99 class ContactList(VerticalPanel):
95 100
96 def __init__(self): 101 def __init__(self):
97 VerticalPanel.__init__(self) 102 VerticalPanel.__init__(self)
98 self.contacts=[] 103 self.contacts = set()
99 104
100 def __iter__(self):
101 return self.contacts.__iter__()
102
103 def add(self, jid, name=None): 105 def add(self, jid, name=None):
106 if jid in self.contacts:
107 return
108 self.contacts.add(jid)
104 _item = ContactLabel(jid, name) 109 _item = ContactLabel(jid, name)
105 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer") 110 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
106 VerticalPanel.add(self, _item) 111 VerticalPanel.add(self, _item)
107 self.contacts.append(_item) 112
113 def remove(self, jid):
114 wid = self.getContactLabel(jid)
115 if not wid:
116 return
117 VerticalPanel.remove(self, wid)
118 self.contacts.remove(jid)
119
120 def isContactPresent(self, contact_jid):
121 """Return True if a contact is present in the panel"""
122 return contact_jid in self.contacts
123
124 def getContacts(self):
125 return self.contacts
126
127 def getContactLabel(self, contact_jid):
128 """get contactList widget of a contact
129 @return: ContactLabel item if present, else None"""
130 for wid in self:
131 if isinstance(wid, ContactLabel) and wid.jid == contact_jid:
132 return wid
133 return None
108 134
109 def setState(self, jid, state): 135 def setState(self, jid, state):
110 """Change the appearance of the contact, according to the state 136 """Change the appearance of the contact, according to the state
111 @param jid: jid which need to change state 137 @param jid: jid which need to change state
112 @param state: 'unavailable' if not connected, else presence like RFC6121 #4.7.2.1""" 138 @param state: 'unavailable' if not connected, else presence like RFC6121 #4.7.2.1"""
113 _item = None 139 _item = self.getContactLabel(jid)
114 for contact in self.contacts:
115 if contact.jid == jid:
116 _item = contact
117 break
118 if _item: 140 if _item:
119 if state == 'unavailable': 141 if state == 'unavailable':
120 _item.removeStyleName('contactConnected') 142 _item.removeStyleName('contactConnected')
121 else: 143 else:
122 _item.addStyleName('contactConnected') 144 _item.addStyleName('contactConnected')
149 self.vPanel.add(self._groupList) 171 self.vPanel.add(self._groupList)
150 self.vPanel.add(self._contact_list) 172 self.vPanel.add(self._contact_list)
151 self.add(self.vPanel) 173 self.add(self.vPanel)
152 self.setStyleName('contactBox') 174 self.setStyleName('contactBox')
153 175
154 def addContact(self, jid, attributes, groups): 176 def updateContact(self, jid, attributes, groups):
155 """Add a contact to the panel 177 """Add a contact to the panel if it doesn't exist, update it else
156 @param jid: jid 178 @param jid: jid
157 @attributes: cf SàT Bridge API's newContact 179 @attributes: cf SàT Bridge API's newContact
158 @param groups: list of groups""" 180 @param groups: list of groups"""
159 for group in groups: 181 _current_groups = self.getContactGroups(jid)
182 _new_groups = set(groups)
183 _key = "@%s: "
184
185 for group in _current_groups.difference(_new_groups):
186 #We remove the contact from the groups where he isn't anymore
187 self.groups[group].remove(jid)
188 if not self.groups[group]:
189 #The group is now empty, we must remove it
190 del self.groups[group]
191 self._groupList.remove(group)
192 self.host.uni_box.removeKey(_key % group)
193
194 for group in _new_groups.difference(_current_groups):
195 #We add the contact to the groups he joined
160 if not self.groups.has_key(group): 196 if not self.groups.has_key(group):
161 self.groups[group] = set() 197 self.groups[group] = set()
162 self._groupList.add(group) 198 self._groupList.add(group)
163 self.host.uni_box.addKey("@%s: " % group) 199 self.host.uni_box.addKey(_key % group)
164 self.groups[group].add(jid) 200 self.groups[group].add(jid)
201
202 #We add the contact to contact list, it will check if contact already exists
165 self._contact_list.add(jid) 203 self._contact_list.add(jid)
204
205 def removeContact(self, jid):
206 """Remove contacts from groups where he is and contact list"""
207 self.updateContact(jid, {}, []) #we remove contact from every group
208 self._contact_list.remove(jid)
166 209
167 def setConnected(self, jid, resource, availability, priority, statuses): 210 def setConnected(self, jid, resource, availability, priority, statuses):
168 """Set connection status""" 211 """Set connection status"""
169 if availability=='unavailable': 212 if availability=='unavailable':
170 if self.connected.has_key(jid): 213 if self.connected.has_key(jid):
180 223
181 def getConnected(self): 224 def getConnected(self):
182 """return a list of all jid (bare jid) connected""" 225 """return a list of all jid (bare jid) connected"""
183 return self.connected.keys() 226 return self.connected.keys()
184 227
228 def getContactGroups(self, contact_jid):
229 """Get groups where contact is
230 @param group: string of single group, or list of string
231 @param contact_jid: jid to test
232 """
233 result=set()
234 for group in self.groups:
235 if self.isContactInGroup(group, contact_jid):
236 result.add(group)
237 return result
238
185 def isContactInGroup(self, group, contact_jid): 239 def isContactInGroup(self, group, contact_jid):
186 """Test if the contact_jid is in the group 240 """Test if the contact_jid is in the group
187 @param group: string of single group, or list of string 241 @param group: string of single group, or list of string
188 @param contact_jid: jid to test 242 @param contact_jid: jid to test
189 @return: True if contact_jid is in on of the groups""" 243 @return: True if contact_jid is in on of the groups"""
190 if self.groups.has_key(group) and contact_jid in self.groups[group]: 244 if group in self.groups and contact_jid in self.groups[group]:
191 return True 245 return True
192 return False 246 return False
193 247
194 def isContactInRoster(self, contact_jid): 248 def isContactInRoster(self, contact_jid):
195 """Test if the contact is in our roster list""" 249 """Test if the contact is in our roster list"""
196 for _contact_label in self._contact_list: 250 for _contact_label in self._contact_list:
197 if contact_jid == _contact_label.jid: 251 if contact_jid == _contact_label.jid:
198 return True 252 return True
199 return False 253 return False
200 254
255 def getContacts(self):
256 return self._contact_list.getContacts()
257
201 def getGroups(self): 258 def getGroups(self):
202 return self.groups.keys() 259 return self.groups.keys()
203 260
204 def onMouseMove(self, sender, x, y): 261 def onMouseMove(self, sender, x, y):
205 pass 262 pass