comparison browser_side/contact.py @ 226:744426c2b699

browser_side, misc: better PEP8 compliance
author souliane <souliane@mailoo.org>
date Thu, 26 Sep 2013 14:32:24 +0200
parents 0f5c2f799913
children 266e9678eec0
comparison
equal deleted inserted replaced
225:9b93a21dc5e2 226:744426c2b699
17 17
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 import pyjd # this is dummy in pyjs 22 import pyjd # this is dummy in pyjs
23 from pyjamas.ui.SimplePanel import SimplePanel 23 from pyjamas.ui.SimplePanel import SimplePanel
24 from pyjamas.ui.ScrollPanel import ScrollPanel 24 from pyjamas.ui.ScrollPanel import ScrollPanel
25 from pyjamas.ui.VerticalPanel import VerticalPanel 25 from pyjamas.ui.VerticalPanel import VerticalPanel
26 from pyjamas.ui.ClickListener import ClickHandler 26 from pyjamas.ui.ClickListener import ClickHandler
27 from pyjamas.ui.Label import Label 27 from pyjamas.ui.Label import Label
52 def __init__(self, host, jid, name=None): 52 def __init__(self, host, jid, name=None):
53 HTML.__init__(self) 53 HTML.__init__(self)
54 self.host = host 54 self.host = host
55 self.name = name or jid 55 self.name = name or jid
56 self.waiting = False 56 self.waiting = False
57 self.jid=jid 57 self.jid = jid
58 self._fill() 58 self._fill()
59 self.setStyleName('contact') 59 self.setStyleName('contact')
60 DragLabel.__init__(self, jid, "CONTACT") 60 DragLabel.__init__(self, jid, "CONTACT")
61 ClickHandler.__init__(self) 61 ClickHandler.__init__(self)
62 self.addClickListener(self) 62 self.addClickListener(self)
92 92
93 def remove(self, group): 93 def remove(self, group):
94 for wid in self: 94 for wid in self:
95 if isinstance(wid, GroupLabel) and wid.group == group: 95 if isinstance(wid, GroupLabel) and wid.group == group:
96 VerticalPanel.remove(self, wid) 96 VerticalPanel.remove(self, wid)
97 97
98 class ContactList(VerticalPanel): 98 class ContactList(VerticalPanel):
99 99
100 def __init__(self, host): 100 def __init__(self, host):
101 VerticalPanel.__init__(self) 101 VerticalPanel.__init__(self)
102 self.host = host 102 self.host = host
149 else: 149 else:
150 _item.addStyleName('contactConnected') 150 _item.addStyleName('contactConnected')
151 elif type == 'messageWaiting': 151 elif type == 'messageWaiting':
152 _item.setMessageWaiting(state) 152 _item.setMessageWaiting(state)
153 153
154
154 class ContactTitleLabel(DragLabel, Label, ClickHandler): 155 class ContactTitleLabel(DragLabel, Label, ClickHandler):
155 def __init__(self, host, text): 156 def __init__(self, host, text):
156 Label.__init__(self, text) #, Element=DOM.createElement('div') 157 Label.__init__(self, text) #, Element=DOM.createElement('div')
157 self.host = host 158 self.host = host
158 self.setStyleName('contactTitle') 159 self.setStyleName('contactTitle')
164 new_wid = MicroblogPanel.createMeta(self.host, None) 165 new_wid = MicroblogPanel.createMeta(self.host, None)
165 self.host.addWidget(new_wid) 166 self.host.addWidget(new_wid)
166 167
167 class ContactPanel(SimplePanel): 168 class ContactPanel(SimplePanel):
168 """Manage the contacts and groups""" 169 """Manage the contacts and groups"""
169 170
170 def __init__(self, host): 171 def __init__(self, host):
171 SimplePanel.__init__(self) 172 SimplePanel.__init__(self)
172 173
173 self.scroll_panel = ScrollPanel() 174 self.scroll_panel = ScrollPanel()
174 175
175 self.host = host 176 self.host = host
176 self.groups={} 177 self.groups = {}
177 self.connected = {} #jid connected as key and their status 178 self.connected = {} # jid connected as key and their status
178 179
179 self.vPanel = VerticalPanel() 180 self.vPanel = VerticalPanel()
180 _title = ContactTitleLabel(host, 'Contacts') 181 _title = ContactTitleLabel(host, 'Contacts')
181 DOM.setStyleAttribute(_title.getElement(), "cursor", "pointer") 182 DOM.setStyleAttribute(_title.getElement(), "cursor", "pointer")
182 183
183 self._contact_list = ContactList(host) 184 self._contact_list = ContactList(host)
184 self._contact_list.setStyleName('contactList') 185 self._contact_list.setStyleName('contactList')
185 self._groupList = GroupList(self) 186 self._groupList = GroupList(self)
186 self._groupList.setStyleName('groupList') 187 self._groupList.setStyleName('groupList')
187 188
188 self.vPanel.add(_title) 189 self.vPanel.add(_title)
189 self.vPanel.add(self._groupList) 190 self.vPanel.add(self._groupList)
190 self.vPanel.add(self._contact_list) 191 self.vPanel.add(self._contact_list)
191 self.scroll_panel.add(self.vPanel) 192 self.scroll_panel.add(self.vPanel)
192 self.add(self.scroll_panel) 193 self.add(self.scroll_panel)
198 _elts = doc().getElementsByClassName('gwt-TabBar') 199 _elts = doc().getElementsByClassName('gwt-TabBar')
199 if not _elts.length: 200 if not _elts.length:
200 print ("ERROR: no TabBar found, it should exist !") 201 print ("ERROR: no TabBar found, it should exist !")
201 tab_bar_h = height 202 tab_bar_h = height
202 else: 203 else:
203 tab_bar_h = DOM.getAbsoluteTop( _elts.item(0)) or height #getAbsoluteTop can be 0 if tabBar is hidden 204 tab_bar_h = DOM.getAbsoluteTop(_elts.item(0)) or height # getAbsoluteTop can be 0 if tabBar is hidden
204 205
205 ideal_height = tab_bar_h - DOM.getAbsoluteTop(contact_panel_elt) - 5 206 ideal_height = tab_bar_h - DOM.getAbsoluteTop(contact_panel_elt) - 5
206 self.scroll_panel.setHeight("%s%s" % (ideal_height, "px")); 207 self.scroll_panel.setHeight("%s%s" % (ideal_height, "px"));
207 208
208 209
209 def updateContact(self, jid, attributes, groups): 210 def updateContact(self, jid, attributes, groups):
210 """Add a contact to the panel if it doesn't exist, update it else 211 """Add a contact to the panel if it doesn't exist, update it else
211 @param jid: jid 212 @param jid: jid
212 @attributes: cf SàT Bridge API's newContact 213 @attributes: cf SàT Bridge API's newContact
214 _current_groups = self.getContactGroups(jid) 215 _current_groups = self.getContactGroups(jid)
215 _new_groups = set(groups) 216 _new_groups = set(groups)
216 _key = "@%s: " 217 _key = "@%s: "
217 218
218 for group in _current_groups.difference(_new_groups): 219 for group in _current_groups.difference(_new_groups):
219 #We remove the contact from the groups where he isn't anymore 220 # We remove the contact from the groups where he isn't anymore
220 self.groups[group].remove(jid) 221 self.groups[group].remove(jid)
221 if not self.groups[group]: 222 if not self.groups[group]:
222 #The group is now empty, we must remove it 223 # The group is now empty, we must remove it
223 del self.groups[group] 224 del self.groups[group]
224 self._groupList.remove(group) 225 self._groupList.remove(group)
225 self.host.uni_box.removeKey(_key % group) 226 self.host.uni_box.removeKey(_key % group)
226 227
227 for group in _new_groups.difference(_current_groups): 228 for group in _new_groups.difference(_current_groups):
228 #We add the contact to the groups he joined 229 # We add the contact to the groups he joined
229 if not self.groups.has_key(group): 230 if not self.groups.has_key(group):
230 self.groups[group] = set() 231 self.groups[group] = set()
231 self._groupList.add(group) 232 self._groupList.add(group)
232 self.host.uni_box.addKey(_key % group) 233 self.host.uni_box.addKey(_key % group)
233 self.groups[group].add(jid) 234 self.groups[group].add(jid)
234 235
235 #We add the contact to contact list, it will check if contact already exists 236 # We add the contact to contact list, it will check if contact already exists
236 self._contact_list.add(jid) 237 self._contact_list.add(jid)
237 238
238 def removeContact(self, jid): 239 def removeContact(self, jid):
239 """Remove contacts from groups where he is and contact list""" 240 """Remove contacts from groups where he is and contact list"""
240 self.updateContact(jid, {}, []) #we remove contact from every group 241 self.updateContact(jid, {}, []) # we remove contact from every group
241 self._contact_list.remove(jid) 242 self._contact_list.remove(jid)
242 243
243 def setConnected(self, jid, resource, availability, priority, statuses): 244 def setConnected(self, jid, resource, availability, priority, statuses):
244 """Set connection status""" 245 """Set connection status"""
245 if availability=='unavailable': 246 if availability == 'unavailable':
246 if self.connected.has_key(jid): 247 if self.connected.has_key(jid):
247 if self.connected[jid].has_key(resource): 248 if self.connected[jid].has_key(resource):
248 del self.connected[jid][resource] 249 del self.connected[jid][resource]
249 if not self.connected[jid]: 250 if not self.connected[jid]:
250 del self.connected[jid] 251 del self.connected[jid]
251 else: 252 else:
252 if not self.connected.has_key(jid): 253 if not self.connected.has_key(jid):
253 self.connected[jid] = {} 254 self.connected[jid] = {}
254 self.connected[jid][resource] = (availability, priority, statuses) 255 self.connected[jid][resource] = (availability, priority, statuses)
255 self._contact_list.setState(jid, "availability", availability) 256 self._contact_list.setState(jid, "availability", availability)
256 257
258 """Show an visual indicator that contact has send a message 259 """Show an visual indicator that contact has send a message
259 @param jid: jid of the contact 260 @param jid: jid of the contact
260 @param waiting: True if message are waiting""" 261 @param waiting: True if message are waiting"""
261 self._contact_list.setState(jid, "messageWaiting", waiting) 262 self._contact_list.setState(jid, "messageWaiting", waiting)
262 263
263
264
265 def getConnected(self): 264 def getConnected(self):
266 """return a list of all jid (bare jid) connected""" 265 """return a list of all jid (bare jid) connected"""
267 return self.connected.keys() 266 return self.connected.keys()
268 267
269 def getContactGroups(self, contact_jid): 268 def getContactGroups(self, contact_jid):
270 """Get groups where contact is 269 """Get groups where contact is
271 @param group: string of single group, or list of string 270 @param group: string of single group, or list of string
272 @param contact_jid: jid to test 271 @param contact_jid: jid to test
273 """ 272 """
274 result=set() 273 result = set()
275 for group in self.groups: 274 for group in self.groups:
276 if self.isContactInGroup(group, contact_jid): 275 if self.isContactInGroup(group, contact_jid):
277 result.add(group) 276 result.add(group)
278 return result 277 return result
279 278
299 def getGroups(self): 298 def getGroups(self):
300 return self.groups.keys() 299 return self.groups.keys()
301 300
302 def onMouseMove(self, sender, x, y): 301 def onMouseMove(self, sender, x, y):
303 pass 302 pass
304 303
305 def onMouseDown(self, sender, x, y): 304 def onMouseDown(self, sender, x, y):
306 pass 305 pass
307 306
308 def onMouseUp(self, sender, x, y): 307 def onMouseUp(self, sender, x, y):
309 pass 308 pass
311 def onMouseEnter(self, sender): 310 def onMouseEnter(self, sender):
312 if isinstance(sender, GroupLabel): 311 if isinstance(sender, GroupLabel):
313 for contact in self._contact_list: 312 for contact in self._contact_list:
314 if contact.jid in self.groups[sender.group]: 313 if contact.jid in self.groups[sender.group]:
315 contact.addStyleName("selected") 314 contact.addStyleName("selected")
316 315
317 def onMouseLeave(self, sender): 316 def onMouseLeave(self, sender):
318 if isinstance(sender, GroupLabel): 317 if isinstance(sender, GroupLabel):
319 for contact in self._contact_list: 318 for contact in self._contact_list:
320 if contact.jid in self.groups[sender.group]: 319 if contact.jid in self.groups[sender.group]:
321 contact.removeStyleName("selected") 320 contact.removeStyleName("selected")