quick_frontends, primitivus: move the chat states logic to quick_frontend

This commit is contained in:
souliane 2015-03-20 16:28:19 +01:00
commit 14f4b4b8ec
3 changed files with 70 additions and 35 deletions

View file

@ -42,8 +42,7 @@ class Const(constants.Const):
XMLUI_STATUS_CANCELLED = constants.Const.XMLUI_DATA_CANCELLED
# MUC
ALL_OCCUPANTS = 1
MUC_USER_STATES = {
USER_CHAT_STATES = {
"active": u'✔',
"inactive": u'☄',
"gone": u'✈',

View file

@ -574,16 +574,22 @@ class QuickApp(object):
log.debug("new subject for room [%(room_jid)s]: %(subject)s" % {'room_jid': room_jid, "subject": subject})
def chatStateReceivedHandler(self, from_jid_s, state, profile):
"""Called when a new chat state is received.
"""Called when a new chat state (XEP-0085) is received.
@param from_jid_s: JID of the contact who sent his state, or '@ALL@'
@param state: new state (string)
@profile: current profile
@param from_jid_s (unicode): JID of a contact or C.ENTITY_ALL
@param state (unicode): new state
@param profile (unicode): current profile
"""
from_jid = jid.JID(from_jid_s) if from_jid_s != C.ENTITY_ALL else C.ENTITY_ALL
for widget in self.widgets.getWidgets(quick_chat.QuickChat):
if from_jid == C.ENTITY_ALL or from_jid.bare == widget.target.bare:
widget.updateChatState(from_jid, state)
if profile != widget.profile:
continue
to_display = C.USER_CHAT_STATES[state] if (state and widget.type == C.CHAT_GROUP) else state
if widget.type == C.CHAT_GROUP and from_jid_s == C.ENTITY_ALL:
for occupant in [jid.newResource(self.target, nick) for nick in widget.occupants]:
widget.updateEntityState(occupant, 'chat_state', to_display)
elif from_jid.bare == widget.target.bare: # roster contact or MUC occupant
widget.updateEntityState(from_jid, 'chat_state', to_display)
def personalEventHandler(self, sender, event_type, data):
"""Called when a PEP event is received.

View file

@ -23,7 +23,7 @@ log = getLogger(__name__)
from sat_frontends.tools import jid
from sat_frontends.quick_frontend import quick_widgets
from sat_frontends.quick_frontend.constants import Const as C
from collections import OrderedDict
try:
# FIXME: to be removed when an acceptable solution is here
@ -34,6 +34,8 @@ except (TypeError, AttributeError): # Error raised is not the same depending on
class QuickChat(quick_widgets.QuickWidget):
visible_states = ['chat_state']
def __init__(self, host, target, type_=C.CHAT_ONE2ONE, profiles=None):
"""
@param type_: can be C.CHAT_ONE2ONE for single conversation or C.CHAT_GROUP for chat à la IRC
@ -207,12 +209,25 @@ class QuickChat(quick_widgets.QuickWidget):
"""
raise NotImplementedError
def updateChatState(self, from_jid, state):
"""Set the chat state (XEP-0085) of the contact.
def updateEntityState(self, entity, type_, value):
"""Update a state value for the given entity.
@param state: the new chat state
@param entity (jid.JID): entity to update
@param type_ (unicode): type of state (e.g. 'chat_state')
@param value (unicode): new value
"""
raise NotImplementedError
contact_list = self.host.contact_lists[self.profile]
contact_list.setCache(entity, type_, value)
clist = self.host.contact_lists[self.profile]
states = OrderedDict()
for key in self.visible_states:
value = clist.getCache(entity, key)
if value:
states[key] = value
if self.type == C.CHAT_GROUP:
self.setOccupantStates(entity, states)
else:
self.setContactStates(entity, states)
def addGamePanel(self, widget):
"""Insert a game panel to this Chat dialog.