comparison frontends/src/quick_frontend/quick_chat.py @ 2067:7834743705f0

quich frontend, primivius (chat): better avatar handling: - new getAvatar method in QuickApp which request vCard if avatar if needed and return default avatar is no avatar is found - frontends based on quick frontend can now specify if they handle avatar or not using AVATARS_HANDLER - when avatar is updated, occupant widget(s) and all message widget(s) of the concerned entity are updated
author Goffi <goffi@goffi.org>
date Fri, 09 Sep 2016 23:54:33 +0200
parents 09c18fcd8225
children 4f3ebf786fbc
comparison
equal deleted inserted replaced
2066:09c18fcd8225 2067:7834743705f0
115 time_format = u"%c" if timestamp < self.parent.day_change else u"%H:%M" 115 time_format = u"%c" if timestamp < self.parent.day_change else u"%H:%M"
116 return time.strftime(time_format, timestamp).decode(getlocale()[1]) 116 return time.strftime(time_format, timestamp).decode(getlocale()[1])
117 117
118 @property 118 @property
119 def avatar(self): 119 def avatar(self):
120 contact_list = self.host.contact_lists[self.profile] 120 return self.host.getAvatar(self.from_jid, self.profile)
121 return contact_list.getCache(self.from_jid, "avatar") or self.host.getDefaultAvatar(self.from_jid)
122 121
123 def getNick(self, entity): 122 def getNick(self, entity):
124 """Return nick of an entity when possible""" 123 """Return nick of an entity when possible"""
125 contact_list = self.host.contact_lists[self.profile] 124 contact_list = self.host.contact_lists[self.profile]
126 if self.type == C.MESS_TYPE_INFO and self.info_type in ROOM_USER_MOVED: 125 if self.type == C.MESS_TYPE_INFO and self.info_type in ROOM_USER_MOVED:
165 nick = self.nick 164 nick = self.nick
166 for lang, mess in self.message.iteritems(): 165 for lang, mess in self.message.iteritems():
167 self.message[lang] = u"* " + nick + mess[3:] 166 self.message[lang] = u"* " + nick + mess[3:]
168 167
169 168
170
171 class Occupant(object): 169 class Occupant(object):
172 """Occupant metadata""" 170 """Occupant metadata"""
173 171
174 def __init__(self, parent, data, profile): 172 def __init__(self, parent, data, profile):
175 self.parent = parent 173 self.parent = parent
213 @state.setter 211 @state.setter
214 def state(self, new_state): 212 def state(self, new_state):
215 self._state = new_state 213 self._state = new_state
216 for w in self.widgets: 214 for w in self.widgets:
217 w.updated(["state"]) 215 w.updated(["state"])
216
217 def update(self, key=None):
218 for w in self.widgets:
219 w.update(key)
218 220
219 221
220 class QuickChat(quick_widgets.QuickWidget): 222 class QuickChat(quick_widgets.QuickWidget):
221 visible_states = ['chat_state'] # FIXME: to be removed, used only in quick_games 223 visible_states = ['chat_state'] # FIXME: to be removed, used only in quick_games
222 224
247 self.messages = OrderedDict() # key: uid, value: Message instance 249 self.messages = OrderedDict() # key: uid, value: Message instance
248 self.games = {} # key=game name (unicode), value=instance of quick_games.RoomGame 250 self.games = {} # key=game name (unicode), value=instance of quick_games.RoomGame
249 self.subject = subject 251 self.subject = subject
250 lt = time.localtime() 252 lt = time.localtime()
251 self.day_change = (lt.tm_year, lt.tm_mon, lt.tm_mday, 0, 0, 0, lt.tm_wday, lt.tm_yday, lt.tm_isdst) # struct_time of day changing time 253 self.day_change = (lt.tm_year, lt.tm_mon, lt.tm_mday, 0, 0, 0, lt.tm_wday, lt.tm_yday, lt.tm_isdst) # struct_time of day changing time
254 if self.host.AVATARS_HANDLER:
255 self.host.addListener('avatar', self.onAvatar, profiles)
252 256
253 def postInit(self): 257 def postInit(self):
254 """Method to be called by frontend after widget is initialised 258 """Method to be called by frontend after widget is initialised
255 259
256 handle the display of history and subject 260 handle the display of history and subject
257 """ 261 """
258 self.historyPrint(profile=self.profile) 262 self.historyPrint(profile=self.profile)
259 if self.subject is not None: 263 if self.subject is not None:
260 self.setSubject(self.subject) 264 self.setSubject(self.subject)
265
266 def onDelete(self):
267 if self.host.AVATARS_HANDLER:
268 self.host.removeListener('avatar', self.onAvatar)
261 269
262 @property 270 @property
263 def contact_list(self): 271 def contact_list(self):
264 return self.host.contact_lists[self.profile] 272 return self.host.contact_lists[self.profile]
265 273
537 def update(self, entity=None): 545 def update(self, entity=None):
538 """Update one or all entities. 546 """Update one or all entities.
539 547
540 @param entity (jid.JID): entity to update 548 @param entity (jid.JID): entity to update
541 """ 549 """
550 # FIXME: to remove ?
542 raise NotImplementedError 551 raise NotImplementedError
543 552
544 ## events ## 553 ## events ##
545 554
546 def onChatState(self, from_jid, state, profile): 555 def onChatState(self, from_jid, state, profile):
559 except KeyError: 568 except KeyError:
560 pass 569 pass
561 else: 570 else:
562 mess_data.status = status 571 mess_data.status = status
563 572
573 def onAvatar(self, entity, filename, profile):
574 if self.type == C.CHAT_GROUP:
575 if entity.bare == self.target and entity.resource in self.occupants:
576 self.occupants[entity.resource].update('avatar')
577 for m in self.messages.values():
578 if m.nick == entity.resource:
579 for w in m.widgets:
580 w.update('avatar', filename)
581 else:
582 if entity.bare == self.target.bare:
583 log.info("entity avatar updated")
584 # TODO: call a specific method
585
564 586
565 quick_widgets.register(QuickChat) 587 quick_widgets.register(QuickChat)