comparison frontends/src/quick_frontend/quick_chat.py @ 1290:faa1129559b8 frontends_multi_profiles

core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit): /!\ not finished, everything is still instable ! - bridge: DBus bridge has been modified to allow blocking call to be called in the same way as asynchronous calls - bridge: calls with a callback and no errback are now possible, default errback log the error - constants: removed hack to manage presence without OrderedDict, as an OrderedDict like class has been implemented in Libervia - core: getLastResource has been removed and replaced by getMainResource (there is a global better management of resources) - various style improvments: use of constants when possible, fixed variable overlaps, import of module instead of direct class import - frontends: printInfo and printMessage methods in (Quick)Chat are more generic (use of extra instead of timestamp) - frontends: bridge creation and option parsing (command line arguments) are now specified by the frontend in QuickApp __init__ - frontends: ProfileManager manage a more complete plug sequence (some stuff formerly manage in contact_list have moved to ProfileManager) - quick_frontend (quick_widgets): QuickWidgetsManager is now iterable (all widgets are then returned), or can return an iterator on a specific class (return all widgets of this class) with getWidgets - frontends: tools.jid can now be used in Pyjamas, with some care - frontends (XMLUI): profile is now managed - core (memory): big improvment on entities cache management (and specially resource management) - core (params/exceptions): added PermissionError - various fixes and improvments, check diff for more details
author Goffi <goffi@goffi.org>
date Sat, 24 Jan 2015 01:00:29 +0100
parents e3a9ea76de35
children ebf72fe68d1c
comparison
equal deleted inserted replaced
1289:653f2e2eea31 1290:faa1129559b8
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.log import getLogger 21 from sat.core.log import getLogger
22 log = getLogger(__name__) 22 log = getLogger(__name__)
23 from sat_frontends.tools.jid import JID 23 from sat_frontends.tools import jid
24 from sat_frontends.quick_frontend import quick_widgets 24 from sat_frontends.quick_frontend import quick_widgets
25 from sat_frontends.quick_frontend.constants import Const as C 25 from sat_frontends.quick_frontend.constants import Const as C
26
27
28 try:
29 # FIXME: to be removed when an acceptable solution is here
30 unicode('') # XXX: unicode doesn't exist in pyjamas
31 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options
32 unicode = lambda x: str(x)
26 33
27 34
28 class QuickChat(quick_widgets.QuickWidget): 35 class QuickChat(quick_widgets.QuickWidget):
29 36
30 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, profiles=None): 37 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, profiles=None):
151 """ 158 """
152 log.debug(_("now we print the history (%d messages)") % size) 159 log.debug(_("now we print the history (%d messages)") % size)
153 160
154 def onHistory(history): 161 def onHistory(history):
155 for line in history: 162 for line in history:
156 timestamp, from_jid, to_jid, message, _type, extra = line 163 timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
157 if ((self.type == C.CHAT_GROUP and _type != C.MESS_TYPE_GROUPCHAT) or 164 if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
158 (self.type == C.CHAT_ONE2ONE and _type == C.MESS_TYPE_GROUPCHAT)): 165 (self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)):
159 continue 166 continue
160 self.printMessage(JID(from_jid), message, profile, timestamp) 167 self.printMessage(jid.JID(from_jid), message, {'timestamp':timestamp}, profile)
161 self.afterHistoryPrint() 168 self.afterHistoryPrint()
162 169
163 def onHistoryError(err): 170 def onHistoryError(err):
164 log.error(_("Can't get history")) 171 log.error(_("Can't get history"))
165 172
166 target = self.target.bare 173 target = self.target.bare
167 174
168 return self.host.bridge.getHistory(self.host.profiles[profile].whoami.bare, target, size, search=search, profile=profile, callback=onHistory, errback=onHistoryError) 175 self.host.bridge.getHistory(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, search, profile, callback=onHistory, errback=onHistoryError)
169 176
170 def _get_nick(self, entity): 177 def _get_nick(self, entity):
171 """Return nick of this entity when possible""" 178 """Return nick of this entity when possible"""
172 if self.type == C.CHAT_GROUP: 179 if self.type == C.CHAT_GROUP:
173 return entity.resource 180 return entity.resource
191 if self.type == C.CHAT_GROUP and target.resource and type_ != C.MESS_TYPE_GROUPCHAT: 198 if self.type == C.CHAT_GROUP and target.resource and type_ != C.MESS_TYPE_GROUPCHAT:
192 # we have a private message, we forward it to a private conversation widget 199 # we have a private message, we forward it to a private conversation widget
193 chat_widget = self.getOrCreatePrivateWidget(target) 200 chat_widget = self.getOrCreatePrivateWidget(target)
194 chat_widget.newMessage(from_jid, target, msg, type_, extra, profile) 201 chat_widget.newMessage(from_jid, target, msg, type_, extra, profile)
195 else: 202 else:
196 timestamp = extra.get('archive')
197 if type_ == C.MESS_TYPE_INFO: 203 if type_ == C.MESS_TYPE_INFO:
198 self.printInfo(msg, timestamp=float(timestamp) if timestamp else None) 204 self.printInfo(msg, extra=extra)
199 else: 205 else:
200 self.printMessage(from_jid, msg, profile, float(timestamp) if timestamp else None) 206 self.printMessage(from_jid, msg, extra, profile)
201 207
202 def printMessage(self, from_jid, msg, profile, timestamp=None): 208 def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
203 """Print message in chat window. Must be implemented by child class""" 209 """Print message in chat window. Must be implemented by child class"""
204 jid = JID(from_jid) 210 nick = self._get_nick(from_jid)
205 nick = self._get_nick(jid) 211 mymess = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare) #mymess = True if message comes from local user
206 mymess = (jid.resource == self.nick) if self.type == C.CHAT_GROUP else (jid.bare == self.host.profiles[profile].whoami.bare) #mymess = True if message comes from local user
207 if msg.startswith('/me '): 212 if msg.startswith('/me '):
208 self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', timestamp=timestamp) 213 self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
209 return 214 return
210 return jid, nick, mymess 215 return nick, mymess
211 216
212 def printInfo(self, msg, type_='normal', timestamp=None): 217 def printInfo(self, msg, type_='normal', extra=None):
213 """Print general info 218 """Print general info
214 @param msg: message to print 219 @param msg: message to print
215 @type_: one of: 220 @type_: one of:
216 normal: general info like "toto has joined the room" 221 normal: general info like "toto has joined the room"
217 me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist" 222 me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
218 @param timestamp (float): number of seconds since epoch 223 @param extra (dict): message data
219 """ 224 """
220 raise NotImplementedError 225 raise NotImplementedError
221 226
222 def startGame(self, game_type, referee, players): 227 def startGame(self, game_type, referee, players):
223 """Configure the chat window to start a game""" 228 """Configure the chat window to start a game"""