quick_frontend, primitivus (contact_list): improved and simplified handling of "special" entities:

- special_extras has been removed
    - specials handle all entities (bare + full) in a single set
This commit is contained in:
Goffi 2016-07-24 17:47:09 +02:00
commit 57daef07a7
4 changed files with 31 additions and 61 deletions

View file

@ -553,7 +553,7 @@ class Chat(PrimitivusWidget, quick_chat.QuickChat):
super(Chat, self)._onHistoryPrinted() super(Chat, self)._onHistoryPrinted()
def onPrivateCreated(self, widget): def onPrivateCreated(self, widget):
self.host.contact_lists[widget.profile].specialResourceVisible(widget.target) self.host.contact_lists[widget.profile].setSpecial(widget.target, C.CONTACT_SPECIAL_GROUP)
def onSelected(self): def onSelected(self):
self.focus_marker_set = False self.focus_marker_set = False

View file

@ -122,17 +122,6 @@ class ContactList(PrimitivusWidget, QuickContactList):
log.debug(u"Not element found for {} in setFocus".format(text)) log.debug(u"Not element found for {} in setFocus".format(text))
def specialResourceVisible(self, entity):
"""Assure a resource of a special entity is visible and clickable
Mainly used to display private conversation in MUC rooms
@param entity: full jid of the resource to show
"""
assert isinstance(entity, jid.JID)
if entity not in self._special_extras:
self._special_extras.add(entity)
self.update()
# events # events
def _groupClicked(self, group_wid): def _groupClicked(self, group_wid):
@ -163,7 +152,7 @@ class ContactList(PrimitivusWidget, QuickContactList):
# Methods to build the widget # Methods to build the widget
def _buildEntityWidget(self, entity, keys=None, use_bare_jid=False, with_notifs=True, with_show_attr=True, markup_prepend=None, markup_append = None): def _buildEntityWidget(self, entity, keys=None, use_bare_jid=False, with_notifs=True, with_show_attr=True, markup_prepend=None, markup_append=None, special=False):
"""Build one contact markup data """Build one contact markup data
@param entity (jid.JID): entity to build @param entity (jid.JID): entity to build
@ -177,6 +166,7 @@ class ContactList(PrimitivusWidget, QuickContactList):
@param with_show_attr (bool): if True, show color corresponding to presence status @param with_show_attr (bool): if True, show color corresponding to presence status
@param markup_prepend (list): markup to prepend to the generated one before building the widget @param markup_prepend (list): markup to prepend to the generated one before building the widget
@param markup_append (list): markup to append to the generated one before building the widget @param markup_append (list): markup to append to the generated one before building the widget
@param special (bool): True if entity is a special one
@return (list): markup data are expected by Urwid text widgets @return (list): markup data are expected by Urwid text widgets
""" """
markup = [] markup = []
@ -269,25 +259,18 @@ class ContactList(PrimitivusWidget, QuickContactList):
def _buildSpecials(self, content): def _buildSpecials(self, content):
"""Build the special entities""" """Build the special entities"""
specials = list(self.contact_list._specials) specials = sorted(self.contact_list.getSpecials())
specials.sort() current = None
extra_shown = set()
for entity in specials: for entity in specials:
# the special widgets if current is not None and current.bare == entity.bare:
widget = self._buildEntityWidget(entity, ('cache_nick', 'cache_name', 'node'), with_show_attr=False) # nested entity (e.g. MUC private conversations)
content.append(widget) widget = self._buildEntityWidget(entity, ('resource',), markup_prepend=' ', special=True)
else:
# resources which must be displayed (e.g. MUC private conversations) # the special widgets
extras = [extra for extra in self.contact_list._special_extras if extra.bare == entity.bare] if entity.resource:
extras.sort() widget = self._buildEntityWidget(entity, ('resource',), special=True)
for extra in extras: else:
widget = self._buildEntityWidget(extra, ('resource',), markup_prepend = ' ') widget = self._buildEntityWidget(entity, ('cache_nick', 'cache_name', 'node'), with_show_attr=False, special=True)
content.append(widget)
extra_shown.add(extra)
# entities which must be visible but not resource of current special entities
for extra in self.contact_list._special_extras.difference(extra_shown):
widget = self._buildEntityWidget(extra, ('resource',))
content.append(widget) content.append(widget)
def _buildList(self): def _buildList(self):

View file

@ -106,7 +106,7 @@ class Message(object):
except KeyError: except KeyError:
log.error(u"extra data is missing user nick for uid {}".format(self.uid)) log.error(u"extra data is missing user nick for uid {}".format(self.uid))
return "" return ""
if self.parent.type == C.CHAT_GROUP or entity in contact_list.getSpecialExtras(C.CONTACT_SPECIAL_GROUP): if self.parent.type == C.CHAT_GROUP or entity in contact_list.getSpecials(C.CONTACT_SPECIAL_GROUP):
return entity.resource or "" return entity.resource or ""
if entity.bare in contact_list: if entity.bare in contact_list:
return contact_list.getCache(entity, 'nick') or contact_list.getCache(entity, 'name') or entity.node or entity return contact_list.getCache(entity, 'nick') or contact_list.getCache(entity, 'name') or entity.node or entity

View file

@ -56,10 +56,9 @@ class ProfileContactList(object):
# for different profiles (e.g. directed presence) # for different profiles (e.g. directed presence)
self._cache = {} self._cache = {}
# special entities (groupchat, gateways, etc), bare jids # special entities (groupchat, gateways, etc)
# may be bare or full jid
self._specials = set() self._specials = set()
# extras are specials with full jids (e.g.: private MUC conversation)
self._special_extras = set()
# group data contain jids in groups and misc frontend data # group data contain jids in groups and misc frontend data
# None key is used for jids with not group # None key is used for jids with not group
@ -178,20 +177,6 @@ class ProfileContactList(object):
""" """
return self._cache[entity] return self._cache[entity]
def getSpecialExtras(self, special_type=None):
"""Return special extras with given type
If special_type is None, return all special extras.
@param special_type(unicode, None): one of special type (e.g. C.CONTACT_SPECIAL_GROUP)
None to return all special extras.
@return (set[jid.JID])
"""
if special_type is None:
return self._special_extras
specials = self.getSpecials(special_type)
return {extra for extra in self._special_extras if extra.bare in specials}
def _gotContacts(self, contacts): def _gotContacts(self, contacts):
"""Called during filling, add contacts and notice parent that contacts are filled""" """Called during filling, add contacts and notice parent that contacts are filled"""
for contact in contacts: for contact in contacts:
@ -289,22 +274,25 @@ class ProfileContactList(object):
"""Set special flag on an entity """Set special flag on an entity
@param entity(jid.JID): jid of the special entity @param entity(jid.JID): jid of the special entity
if the jid is full, will be added to special extras
@param special_type: one of special type (e.g. C.CONTACT_SPECIAL_GROUP) or None to remove special flag @param special_type: one of special type (e.g. C.CONTACT_SPECIAL_GROUP) or None to remove special flag
""" """
assert special_type in C.CONTACT_SPECIAL_ALLOWED + (None,) assert special_type in C.CONTACT_SPECIAL_ALLOWED + (None,)
self.setCache(entity, C.CONTACT_SPECIAL, special_type) self.setCache(entity, C.CONTACT_SPECIAL, special_type)
def getSpecials(self, special_type=None): def getSpecials(self, special_type=None, bare=False):
"""Return all the bare JIDs of the special roster entities of with given type. """Return all the bare JIDs of the special roster entities of with given type.
If special_type is None, return all specials. @param special_type(unicode, None): if not None, filter by special type (e.g. C.CONTACT_SPECIAL_GROUP)
@param special_type: one of special type (e.g. C.CONTACT_SPECIAL_GROUP) or None to return all specials. @param bare(bool): return only bare jids if True
@return: set(jid.JID) @return (iter[jid.JID]): found special entities
""" """
if special_type is None: for entity in self._specials:
return self._specials if bare and entity.resource:
return set([entity for entity in self._specials if self.getCache(entity, C.CONTACT_SPECIAL) == special_type]) continue
if special_type is not None and self.getCache(entity, C.CONTACT_SPECIAL) != special_type:
continue
yield entity
def disconnect(self): def disconnect(self):
# for now we just clear contacts on disconnect # for now we just clear contacts on disconnect
@ -320,7 +308,6 @@ class ProfileContactList(object):
self._cache.clear() self._cache.clear()
self._groups.clear() self._groups.clear()
self._specials.clear() self._specials.clear()
self._special_extras.clear()
self._roster.clear() self._roster.clear()
self.update() self.update()
@ -369,9 +356,9 @@ class ProfileContactList(object):
if C.CONTACT_SPECIAL in attributes: if C.CONTACT_SPECIAL in attributes:
if attributes[C.CONTACT_SPECIAL] is None: if attributes[C.CONTACT_SPECIAL] is None:
del attributes[C.CONTACT_SPECIAL] del attributes[C.CONTACT_SPECIAL]
self._specials.remove(entity_bare) self._specials.remove(entity)
else: else:
self._specials.add(entity_bare) self._specials.add(entity)
cache[C.CONTACT_MAIN_RESOURCE] = None cache[C.CONTACT_MAIN_RESOURCE] = None
# now the attributes we keep in cache # now the attributes we keep in cache
@ -441,7 +428,7 @@ class ProfileContactList(object):
self._groups[group]['jids'].remove(entity_bare) self._groups[group]['jids'].remove(entity_bare)
if not self._groups[group]['jids']: if not self._groups[group]['jids']:
self._groups.pop(group) # FIXME: we use pop because of pyjamas: http://wiki.goffi.org/wiki/Issues_with_Pyjamas/en self._groups.pop(group) # FIXME: we use pop because of pyjamas: http://wiki.goffi.org/wiki/Issues_with_Pyjamas/en
for iterable in (self._selected, self._specials, self._special_extras): for iterable in (self._selected, self._specials):
to_remove = set() to_remove = set()
for set_entity in iterable: for set_entity in iterable:
if set_entity.bare == entity.bare: if set_entity.bare == entity.bare: