comparison frontends/src/primitivus/chat.py @ 1388:a025242bebe7

quick_frontend, primitivus: remove QuickChat.updateEntityState and QuickChat.setContactStates, use more general QuickChat.update (also called when joining the room to initialise the occupants list)
author souliane <souliane@mailoo.org>
date Tue, 24 Mar 2015 17:31:08 +0100
parents 1f3513cfb246
children eb907923dce9
comparison
equal deleted inserted replaced
1387:3511ff4b40a0 1388:a025242bebe7
98 elif type_ == C.CHAT_GROUP: 98 elif type_ == C.CHAT_GROUP:
99 if len(self.chat_colums.contents) == 1: 99 if len(self.chat_colums.contents) == 1:
100 self.occupants_list = sat_widgets.GenericList([], option_type=sat_widgets.ClickableText, on_click=self._occupantsClicked) 100 self.occupants_list = sat_widgets.GenericList([], option_type=sat_widgets.ClickableText, on_click=self._occupantsClicked)
101 self.occupants_panel = sat_widgets.VerticalSeparator(self.occupants_list) 101 self.occupants_panel = sat_widgets.VerticalSeparator(self.occupants_list)
102 self._appendOccupantsPanel() 102 self._appendOccupantsPanel()
103 self.host.addListener('presence', self.presenceListener, [profiles])
103 104
104 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) # struct_time of day changing time 105 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) # struct_time of day changing time
105 self.show_timestamp = True 106 self.show_timestamp = True
106 self.show_short_nick = False 107 self.show_short_nick = False
107 self.show_title = 1 # 0: clip title; 1: full title; 2: no title 108 self.show_title = 1 # 0: clip title; 1: full title; 2: no title
146 elif self.type == C.CHAT_ONE2ONE: 147 elif self.type == C.CHAT_ONE2ONE:
147 self.host.addMenus(menu, C.MENU_SINGLE, {'jid': self.target}) 148 self.host.addMenus(menu, C.MENU_SINGLE, {'jid': self.target})
148 menu.addMenu(_("Action"), _("Send file"), self.onSendFileRequest) 149 menu.addMenu(_("Action"), _("Send file"), self.onSendFileRequest)
149 return menu 150 return menu
150 151
151 def setContactStates(self, contact_jid, states): 152 def presenceListener(self, entity, show, priority, statuses, profile):
152 """Set a contact (one2one or MUC occupant) states. 153 """Update entity's presence status
153 154
154 @param contact_jid (jid.JID): contact 155 @param entity (jid.JID): entity updated
155 @param states (dict{unicode: unicode}): new states 156 @param show: availability
156 """ 157 @param priority: resource's priority
157 if self.type == C.CHAT_GROUP: 158 @param statuses: dict of statuses
158 options = self.occupants_list.getAllValues() 159 @param profile: %(doc_profile)s
159 for index in xrange(0, len(options)): 160 """
160 nick = options[index].value 161 assert self.type == C.CHAT_GROUP
161 if nick == contact_jid.resource: 162 if entity.bare != self.target:
162 options[index] = (nick, "%s %s" % (u''.join(states.values()), nick)) 163 return
163 self.occupants_list.changeValues(options) 164 self.update(entity)
164 break 165
165 else: 166 def update(self, entity=None):
167 """Update one or all entities.
168
169 @param entity (jid.JID): entity to update
170 """
171 contact_list = self.host.contact_lists[self.profile]
172
173 if self.type == C.CHAT_ONE2ONE: # only update the chat title
174 states = self.getEntityStates(self.target)
166 self.title_dynamic = ' '.join([u'({})'.format(state) for state in states.values()]) 175 self.title_dynamic = ' '.join([u'({})'.format(state) for state in states.values()])
167 self.host.redraw() 176 self.host.redraw()
177 return
178
179 nicks = list(self.occupants)
180 if entity is None: # rebuild all the occupants list
181 values = []
182 nicks.sort()
183 for nick in nicks:
184 values.append(self._buildOccupantMarkup(jid.newResource(self.target, nick)))
185 self.occupants_list.changeValues(values)
186 else: # add, remove or update only one occupant
187 nick = entity.resource
188 show = contact_list.getCache(entity, C.PRESENCE_SHOW)
189 if show == C.PRESENCE_UNAVAILABLE:
190 self.occupants_list.deleteValue(nick)
191 else:
192 values = self.occupants_list.getAllValues()
193 if not values: # room has just been created
194 self.occupants_list.changeValues([self._buildOccupantMarkup(entity)])
195 else: # add or update the occupant, keep the list sorted
196 index = 0
197 for entry in values:
198 order = cmp(entry.value if hasattr(entry, 'value') else entry, nick)
199 if order < 0:
200 index += 1
201 continue
202 if order > 0: # insert the occupant the occupant
203 values.insert(index, self._buildOccupantMarkup(entity))
204 else: # update the occupant
205 values[index] = self._buildOccupantMarkup(entity)
206 self.occupants_list.changeValues(values)
207 break
208 self.host.redraw()
209
210 def _buildOccupantMarkup(self, entity):
211 """Return the option attributes for a MUC occupant.
212
213 @param nick (unicode): occupant nickname
214 """
215 contact_list = self.host.contact_lists[self.profile]
216 show = contact_list.getCache(entity, C.PRESENCE_SHOW)
217 states = self.getEntityStates(entity)
218 nick = entity.resource
219 # TODO: use entity_attr and return (nick, markup), but ListOption is unicode and not urwid.Text
220 show_icon, entity_attr = C.PRESENCE.get(show, (u'', u'default'))
221 text = "%s%s %s" % (u''.join(states.values()), show_icon, nick)
222 return (nick, text)
168 223
169 def _occupantsClicked(self, list_wid, clicked_wid): 224 def _occupantsClicked(self, list_wid, clicked_wid):
170 assert self.type == C.CHAT_GROUP 225 assert self.type == C.CHAT_GROUP
171 nick = clicked_wid.getValue().value 226 nick = clicked_wid.getValue().value
172 if nick == self.nick: 227 if nick == self.nick:
214 QuickChat.setSubject(self, subject) 269 QuickChat.setSubject(self, subject)
215 self.subject = subject 270 self.subject = subject
216 self.subj_wid = urwid.Text(unicode(subject.replace('\n', '|') if wrap == 'clip' else subject), 271 self.subj_wid = urwid.Text(unicode(subject.replace('\n', '|') if wrap == 'clip' else subject),
217 align='left' if wrap == 'clip' else 'center', wrap=wrap) 272 align='left' if wrap == 'clip' else 'center', wrap=wrap)
218 self.chat_widget.header = urwid.AttrMap(self.subj_wid, 'title') 273 self.chat_widget.header = urwid.AttrMap(self.subj_wid, 'title')
219 self.host.redraw()
220
221 def addUser(self, param_nick):
222 """Add user if it is not in the group list"""
223 nick = unicode(param_nick) # FIXME: should be done in DBus bridge
224 QuickChat.addUser(self, nick)
225 occupants = self.occupants_list.getAllValues()
226 if nick not in [occupants.value for occupants in occupants]:
227 occupants.append(nick)
228 occupants.sort(cmp=lambda a, b: cmp(a.value if hasattr(a, 'value') else a, b.value if hasattr(b, 'value') else b))
229 self.occupants_list.changeValues(occupants)
230 self.host.redraw()
231
232 def removeUser(self, param_nick):
233 """Remove a user from the group list"""
234 nick = unicode(param_nick) # FIXME: should be done in DBus bridge
235 QuickChat.removeUser(self, nick)
236 self.occupants_list.deleteValue(nick)
237 self.host.redraw() 274 self.host.redraw()
238 275
239 def clearHistory(self): 276 def clearHistory(self):
240 """Clear the content of this chat.""" 277 """Clear the content of this chat."""
241 del self.content[:] 278 del self.content[:]
354 full_jid = self.target 391 full_jid = self.target
355 progress_id = self.host.bridge.sendFile(full_jid, filepath, {}, self.profile) 392 progress_id = self.host.bridge.sendFile(full_jid, filepath, {}, self.profile)
356 self.host.addProgress(progress_id, filepath) 393 self.host.addProgress(progress_id, filepath)
357 self.host.showDialog(_(u"You file request has been sent, we are waiting for your contact answer"), title=_("File request sent")) 394 self.host.showDialog(_(u"You file request has been sent, we are waiting for your contact answer"), title=_("File request sent"))
358 395
396 def onDelete(self):
397 QuickChat.onDelete(self)
398 if self.type == C.CHAT_GROUP:
399 self.host.removeListener('presence', self.presenceListener)
400
359 401
360 quick_widgets.register(QuickChat, Chat) 402 quick_widgets.register(QuickChat, Chat)
361 quick_widgets.register(quick_games.Tarot, game_tarot.TarotGame) 403 quick_widgets.register(quick_games.Tarot, game_tarot.TarotGame)