diff frontends/src/primitivus/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 8ea8fa13c351
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Sat Jan 24 00:15:01 2015 +0100
+++ b/frontends/src/primitivus/chat.py	Sat Jan 24 01:00:29 2015 +0100
@@ -30,7 +30,7 @@
 from sat_frontends.primitivus.keys import action_key_map as a_key
 from sat_frontends.primitivus.widget import PrimitivusWidget
 import time
-from sat_frontends.tools.jid  import JID
+from sat_frontends.tools import jid
 
 
 class ChatText(urwid.FlowWidget):
@@ -83,7 +83,7 @@
 
 class Chat(PrimitivusWidget, QuickChat):
 
-    def __init__(self, host, target, type_='one2one', profiles=None):
+    def __init__(self, host, target, type_=C.CHAT_ONE2ONE, profiles=None):
         QuickChat.__init__(self, host, target, type_, profiles=profiles)
         self.content = urwid.SimpleListWalker([])
         self.text_list = urwid.ListBox(self.content)
@@ -93,10 +93,10 @@
         self.pile = urwid.Pile([self.chat_colums])
         PrimitivusWidget.__init__(self, self.pile, self.target)
 
-        # we must adapt the behavious with the type
-        if type_ == 'one2one':
+        # we must adapt the behaviour with the type
+        if type_ == C.CHAT_ONE2ONE:
             self.historyPrint(profile=self.profile)
-        elif type_ == 'group':
+        elif type_ == C.CHAT_GROUP:
             if len(self.chat_colums.contents) == 1:
                 present_widget = self._buildPresentList()
                 self.present_panel = sat_widgets.VerticalSeparator(present_widget)
@@ -110,7 +110,7 @@
 
     def keypress(self, size, key):
         if key == a_key['OCCUPANTS_HIDE']: #user wants to (un)hide the presents panel
-            if self.type == 'group':
+            if self.type == C.CHAT_GROUP:
                 widgets = [widget for (widget, options) in self.chat_colums.contents]
                 if self.present_panel in widgets:
                     self._removePresentPanel()
@@ -140,11 +140,11 @@
     def getMenu(self):
         """Return Menu bar"""
         menu = sat_widgets.Menu(self.host.loop)
-        if self.type == 'group':
+        if self.type == C.CHAT_GROUP:
             self.host.addMenus(menu, C.MENU_ROOM, {'room_jid': self.target.bare})
             game = _("Game")
             menu.addMenu(game, "Tarot", self.onTarotRequest)
-        elif self.type == 'one2one':
+        elif self.type == C.CHAT_ONE2ONE:
             self.host.addMenus(menu, C.MENU_SINGLE, {'jid': self.target})
             menu.addMenu(_("Action"), _("Send file"), self.onSendFileRequest)
         return menu
@@ -170,13 +170,13 @@
             self.title_dynamic = '({})'.format(state)
 
     def _presentClicked(self, list_wid, clicked_wid):
-        assert self.type == 'group'
+        assert self.type == C.CHAT_GROUP
         nick = clicked_wid.getValue().value
         if nick == self.getUserNick():
             #We ignore clicks on our own nick
             return
         contact_list = self.host.contact_lists[self.profile]
-        full_jid = JID("%s/%s" % (self.target.bare, nick))
+        full_jid = jid.JID("%s/%s" % (self.target.bare, nick))
 
         #we have a click on a nick, we need to create the widget if it doesn't exists
         self.getOrCreatePrivateWidget(full_jid)
@@ -258,10 +258,16 @@
     def onPrivateCreated(self, widget):
         self.host.contact_lists[widget.profile].specialResourceVisible(widget.target)
 
-    def printMessage(self, from_jid, msg, profile, timestamp=None):
-        assert isinstance(from_jid, JID)
+    def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
+        assert isinstance(from_jid, jid.JID)
+        if extra is None:
+            extra = {}
         try:
-            jid, nick, mymess = QuickChat.printMessage(self, from_jid, msg, profile, timestamp)
+            timestamp = float(extra['timestamp'])
+        except KeyError:
+            timestamp=None
+        try:
+            nick, mymess = QuickChat.printMessage(self, from_jid, msg, extra, profile)
         except TypeError:
             # None is returned, the message is managed
             return
@@ -291,7 +297,7 @@
             # all messages and not only with the messages coming from the history.
             self._notify(from_jid, msg)
 
-    def printInfo(self, msg, type_='normal', timestamp=None):
+    def printInfo(self, msg, type_='normal', extra=None):
         """Print general info
         @param msg: message to print
         @type_: one of:
@@ -299,6 +305,12 @@
             me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
         @param timestamp (float): number of seconds since epoch
         """
+        if extra is None:
+            extra = {}
+        try:
+            timestamp = float(extra['timestamp'])
+        except KeyError:
+            timestamp=None
         _widget = ChatText(self, timestamp, None, False, msg, is_info=True)
         self.content.append(_widget)
         self._notify(msg=msg)
@@ -316,7 +328,7 @@
             self.text_list.focus_position = len(self.content) - 1
         self.host.redraw()
         if not self.host.x_notify.hasFocus():
-            if self.type == "one2one":
+            if self.type == C.CHAT_ONE2ONE:
                 self.host.x_notify.sendNotification(_("Primitivus: %s is talking to you") % from_jid)
             elif self.getUserNick().lower() in msg.lower():
                 self.host.x_notify.sendNotification(_("Primitivus: %(user)s mentioned you in room '%(room)s'") % {'user': from_jid, 'room': self.target})
@@ -356,9 +368,9 @@
             self.host.showDialog(_(u"File has a unicode error in its name, it's not yet managed by SàT"), title=_("Can't send file"), type_="error")
             return
         #FIXME: check last_resource: what if self.target.resource exists ?
-        last_resource = self.host.bridge.getLastResource(unicode(self.target.bare), self.profile)
+        last_resource = self.host.bridge.getMainResource(unicode(self.target.bare), self.profile)
         if last_resource:
-            full_jid = JID("%s/%s" % (self.target.bare, last_resource))
+            full_jid = jid.JID("%s/%s" % (self.target.bare, last_resource))
         else:
             full_jid = self.target
         progress_id = self.host.bridge.sendFile(full_jid, filepath, {}, self.profile)