diff libervia.py @ 21:77c2e48efa29

browser side: a warning message now show who will receive the message entered in UniBox, with a color depending on how many people will be able to see it
author Goffi <goffi@goffi.org>
date Sun, 17 Apr 2011 00:21:44 +0200
parents 8f4b1a8914c3
children 586f69e85559
line wrap: on
line diff
--- a/libervia.py	Sat Apr 16 18:06:02 2011 +0200
+++ b/libervia.py	Sun Apr 17 00:21:44 2011 +0200
@@ -23,6 +23,9 @@
 from pyjamas.ui.SimplePanel import SimplePanel
 from pyjamas.ui.RootPanel import RootPanel
 from pyjamas.ui.AutoComplete import AutoCompleteTextBox
+from pyjamas.ui.PopupPanel import PopupPanel
+from pyjamas.ui.HTML import HTML
+from pyjamas.Timer import Timer
 from pyjamas import Window
 from pyjamas.JSONService import JSONProxy
 from pyjamas.ui.KeyboardListener import KEY_ENTER
@@ -79,14 +82,91 @@
 
     def __init__(self, host):
         AutoCompleteTextBox.__init__(self)
+        self._popup = None
+        self._timer = Timer(notify=self._timeCb)
         self.host = host
 
     def addKey(self, key):
         self.getCompletionItems().completions.append(key)
 
+    def showWarning(self, target_data):
+        type, target = target_data
+        if type == "PUBLIC":
+            msg = "This message will be PUBLIC and everybody will be able to see it, even people you don't know"
+            style = "targetPublic"
+        elif type == "GROUP":
+            msg = "This message will be published for all the people of the group <span class='warningTarget'>%s</span>" % (target or '')
+            style = "targetGroup"
+        elif type == "STATUS":
+            msg = "This will be your new status message"
+            style = "targetStatus"
+        elif type == "ONE2ONE":
+            msg = "This message will be sent to your contact <span class='warningTarget'>%s</span>" % target
+            style = "targetOne2One"
+        else:
+            print "WARNING: undetermined target for this message"
+            return
+        contents = HTML(msg)
+
+        self._popup = PopupPanel(autoHide=False, modal=False)
+        self._popup.target_data = target_data
+        self._popup.add(contents)
+        self._popup.setStyleName("warningPopup")
+        if style:
+            self._popup.addStyleName(style)
+
+        left = 0
+        top  = 0 #max(0, self.getAbsoluteTop() - contents.getOffsetHeight() - 2)   
+        self._popup.setPopupPosition(left, top)
+        self._popup.setPopupPosition(left, top)
+        self._popup.show()
+
+    def _timeCb(self, timer):
+        if self._popup:
+            self._popup.hide()
+        del self._popup
+        self._popup = None
+
+    def _getTarget(self, txt):
+        """Say who will receive the messsage
+        Return a tuple (target_type, target info)"""
+        type = None
+        target = None
+        if txt.startswith('@@: '):
+            type = "PUBLIC"
+        elif txt.startswith('@'):
+            type = "GROUP"
+            _end = txt.find(': ')
+            if _end == -1:
+                type = "STATUS"
+            else:
+                target = txt[1:_end] #only one target group is managed for the moment
+                if not target in self.host.contactPanel.getGroups():
+                    target = None
+        elif self.host.selected == None:
+            type = "STATUS"
+        elif isinstance(self.host.selected, ChatPanel):
+            type = "ONE2ONE"
+            target = str(self.host.selected.target)
+        else:
+            print self.host.selected
+            type = "UNKNOWN"
+        return (type, target)
+
     def onKeyPress(self, sender, keycode, modifiers):
+        _txt = self.getText()
+        if not self._popup:
+            self.showWarning(self._getTarget(_txt))
+        else:
+            _target = self._getTarget(_txt)
+            if _target != self._popup.target_data:
+                self._popup.hide()
+                del self._popup
+                self.showWarning(_target)
+
+        self._timer.schedule(1500)
+
         if keycode == KEY_ENTER and not self.visible:
-            _txt = self.getText()
             if _txt:
                 if _txt.startswith('@'):
                     self.host.bridge.call('sendMblog', None, self.getText())