Mercurial > libervia-web
comparison libervia.py @ 230:266e9678eec0
browser_side: added the flag REUSE_EXISTING_LIBERVIA_WIDGETS
- do not create a new chat panel for contacts/groups if a similar one is found in the same tab
- this goes with a modification of the methods to create new panels, especially arguments handling
- improvement of the accepted groups list for MicroblogPanel (remove duplicates and keep sorted)
Details for the new flag:
# Set to true to not create a new LiberviaWidget when a similar one
# already exist (i.e. a chat panel with the same target). Instead
# the existing widget will be eventually removed from its parent
# and added to new WidgetsPanel, or replaced to the expected
# position if the previous and the new parent are the same.
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 08 Oct 2013 13:57:35 +0200 |
parents | e632f77c4219 |
children | b304cdf13a3b |
comparison
equal
deleted
inserted
replaced
229:e632f77c4219 | 230:266e9678eec0 |
---|---|
32 from browser_side.panels import MicroblogItem | 32 from browser_side.panels import MicroblogItem |
33 from browser_side import panels, dialog | 33 from browser_side import panels, dialog |
34 from browser_side.jid import JID | 34 from browser_side.jid import JID |
35 from browser_side.tools import html_sanitize | 35 from browser_side.tools import html_sanitize |
36 | 36 |
37 MAX_MBLOG_CACHE = 500 #Max microblog entries kept in memories | 37 |
38 MAX_MBLOG_CACHE = 500 # Max microblog entries kept in memories | |
39 | |
40 # Set to true to not create a new LiberviaWidget when a similar one | |
41 # already exist (i.e. a chat panel with the same target). Instead | |
42 # the existing widget will be eventually removed from its parent | |
43 # and added to new WidgetsPanel, or replaced to the expected | |
44 # position if the previous and the new parent are the same. | |
45 REUSE_EXISTING_LIBERVIA_WIDGETS = True | |
46 | |
38 | 47 |
39 class LiberviaJsonProxy(JSONProxy): | 48 class LiberviaJsonProxy(JSONProxy): |
40 def __init__(self, *args, **kwargs): | 49 def __init__(self, *args, **kwargs): |
41 JSONProxy.__init__(self, *args, **kwargs) | 50 JSONProxy.__init__(self, *args, **kwargs) |
42 self.handler = self | 51 self.handler = self |
235 self.bridge.call('getEntityData', (dataReceived, avatarError), jid_str, ['avatar']) | 244 self.bridge.call('getEntityData', (dataReceived, avatarError), jid_str, ['avatar']) |
236 self.avatars_cache[jid_str] = "/media/misc/empty_avatar" | 245 self.avatars_cache[jid_str] = "/media/misc/empty_avatar" |
237 return self.avatars_cache[jid_str] | 246 return self.avatars_cache[jid_str] |
238 | 247 |
239 def registerWidget(self, wid): | 248 def registerWidget(self, wid): |
240 print "Registering", wid | 249 print "Registering", wid.getDebugName() |
241 self.libervia_widgets.add(wid) | 250 self.libervia_widgets.add(wid) |
242 | 251 |
243 def unregisterWidget(self, wid): | 252 def unregisterWidget(self, wid): |
244 try: | 253 try: |
245 self.libervia_widgets.remove(wid) | 254 self.libervia_widgets.remove(wid) |
246 except KeyError: | 255 except KeyError: |
247 print ('WARNING: trying to remove a non registered Widget:', wid) | 256 print ('WARNING: trying to remove a non registered Widget:', wid.getDebugName()) |
248 | 257 |
249 def setUniBox(self, unibox): | 258 def setUniBox(self, unibox): |
250 """register the unibox widget""" | 259 """register the unibox widget""" |
251 self.uni_box = unibox | 260 self.uni_box = unibox |
252 self.uni_box.addKey("@@: ") | 261 self.uni_box.addKey("@@: ") |
443 for lib_wid in self.libervia_widgets: | 452 for lib_wid in self.libervia_widgets: |
444 if isinstance(lib_wid, panels.MicroblogPanel): | 453 if isinstance(lib_wid, panels.MicroblogPanel): |
445 if lib_wid.isJidAccepted(entity): | 454 if lib_wid.isJidAccepted(entity): |
446 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10) | 455 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10) |
447 | 456 |
457 def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True): | |
458 """Get the corresponding panel if it exists. | |
459 @param class_: class of the panel (ChatPanel, MicroblogPanel...) | |
460 @param entity: polymorphic parameter, see class_.matchEntity. | |
461 @param ignoreOtherTabs: if True, the widgets that are not | |
462 contained by the currently selected tab will be ignored | |
463 @return: the existing widget that has been found or None.""" | |
464 selected_tab = self.tab_panel.getCurrentPanel() | |
465 for lib_wid in self.libervia_widgets: | |
466 parent = lib_wid.getWidgetsPanel(verbose=False) | |
467 if parent is None or (ignoreOtherTabs and parent != selected_tab): | |
468 # do not return a widget that is not in the currently selected tab | |
469 continue | |
470 if isinstance(lib_wid, class_): | |
471 try: | |
472 if lib_wid.matchEntity(entity): | |
473 print "existing widget found: %s" % lib_wid.getDebugName() | |
474 return lib_wid | |
475 except AttributeError as e: | |
476 e.stack_list() | |
477 return None | |
478 return None | |
479 | |
480 def getOrCreateLiberviaWidget(self, class_, entity, add=True, refresh=True, add=True): | |
481 """Get the matching LiberviaWidget if it exists, or create a new one. | |
482 @param class_: class of the panel (ChatPanel, MicroblogPanel...) | |
483 @param entity: polymorphic parameter, see class_.matchEntity. | |
484 @param refresh: if True, refresh the display of a widget that is found | |
485 (ignored if no widget is found and a new one is created) | |
486 @param add: if True, add a widget that is created to the selected tab | |
487 (ignored if a widget is found and no new one is created) | |
488 @return: the newly created wigdet if REUSE_EXISTING_LIBERVIA_WIDGETS | |
489 is set to False or if the widget has not been found, the existing | |
490 widget that has been found otherwise.""" | |
491 lib_wid = None | |
492 if REUSE_EXISTING_LIBERVIA_WIDGETS: | |
493 lib_wid = self.getLiberviaWidget(class_, entity) | |
494 if lib_wid is None: | |
495 lib_wid = class_.createPanel(self, entity) | |
496 elif refresh: | |
497 # remove the widget from its previous panel | |
498 panel = lib_wid.getWidgetsPanel(verbose=False) | |
499 if panel is not None: | |
500 panel.removeWidget(lib_wid) | |
501 if add or refresh: | |
502 self.addWidget(lib_wid) | |
503 if refresh: | |
504 # must be done after the widget is added, | |
505 # for example to scroll to the bottom | |
506 self.setSelected(lib_wid) | |
507 lib_wid.refresh() | |
508 return lib_wid | |
448 | 509 |
449 def _newMessageCb(self, from_jid, msg, msg_type, to_jid): | 510 def _newMessageCb(self, from_jid, msg, msg_type, to_jid): |
450 _from = JID(from_jid) | 511 _from = JID(from_jid) |
451 _to = JID(to_jid) | 512 _to = JID(to_jid) |
452 showed = False | 513 other = _to if _from.bare == self.whoami.bare else _from |
453 for lib_wid in self.libervia_widgets: | 514 lib_wid = self.getLiberviaWidget(panels.ChatPanel, other, ignoreOtherTabs=False) |
454 if isinstance(lib_wid,panels.ChatPanel) and (lib_wid.target.bare == _from.bare or lib_wid.target.bare == _to.bare): | 515 if lib_wid is not None: |
455 lib_wid.printMessage(_from, msg) | 516 lib_wid.printMessage(_from, msg) |
456 showed = True | 517 else: |
457 if not showed: | 518 # The message has not been showed, we must indicate it |
458 #The message has not been showed, we must indicate it | |
459 other = _to if _from.bare == self.whoami.bare else _from | |
460 self.contact_panel.setContactMessageWaiting(other.bare, True) | 519 self.contact_panel.setContactMessageWaiting(other.bare, True) |
461 | 520 |
462 def _presenceUpdateCb(self, entity, show, priority, statuses): | 521 def _presenceUpdateCb(self, entity, show, priority, statuses): |
463 _entity = JID(entity) | 522 _entity = JID(entity) |
464 #XXX: QnD way to get our status | 523 #XXX: QnD way to get our status |