diff src/cagou/plugins/plugin_wid_chat.py @ 122:dcd6fbb3f010

chat: handle new OTR state signal and change encryption icon consequently
author Goffi <goffi@goffi.org>
date Wed, 01 Feb 2017 21:47:16 +0100
parents c498178d8122
children
line wrap: on
line diff
--- a/src/cagou/plugins/plugin_wid_chat.py	Tue Jan 31 23:11:49 2017 +0100
+++ b/src/cagou/plugins/plugin_wid_chat.py	Wed Feb 01 21:47:16 2017 +0100
@@ -46,6 +46,14 @@
     "icon_medium": u"{media}/icons/muchoslava/png/chat_new_44.png"
 }
 
+# following const are here temporary, they should move to quick frontend
+OTR_STATE_UNTRUSTED = 'untrusted'
+OTR_STATE_TRUSTED = 'trusted'
+OTR_STATE_TRUST = (OTR_STATE_UNTRUSTED, OTR_STATE_TRUSTED)
+OTR_STATE_UNENCRYPTED = 'unencrypted'
+OTR_STATE_ENCRYPTED = 'encrypted'
+OTR_STATE_ENCRYPTION = (OTR_STATE_UNENCRYPTED, OTR_STATE_ENCRYPTED)
+
 
 class MessAvatar(Image):
     pass
@@ -128,11 +136,25 @@
         """
         @param chat(Chat): Chat instance
         """
+        self.chat = chat
         # for now we do a simple ContextMenu as we have only OTR
         self.otr_menu = OtrMenu(chat)
         super(EncryptionButton, self).__init__(**kwargs)
         self.bind(on_release=self.otr_menu.open)
 
+    def getIconSource(self):
+        """get path of icon"""
+        # TODO: use a more generic method to get icon name
+        if self.chat.otr_state_encryption == OTR_STATE_UNENCRYPTED:
+            icon_name = 'cadenas_ouvert'
+        else:
+            if self.chat.otr_state_trust == OTR_STATE_TRUSTED:
+                icon_name = 'cadenas_ferme'
+            else:
+                icon_name = 'cadenas_ferme_pas_authenthifie'
+
+        return G.host.app.expand("{media}/icons/muchoslava/png/" + icon_name + "_30.png")
+
 
 class OtrMenu(DropDown):
 
@@ -183,9 +205,12 @@
 
     def __init__(self, host, target, type_=C.CHAT_ONE2ONE, nick=None, occupants=None, subject=None, profiles=None):
         quick_chat.QuickChat.__init__(self, host, target, type_, nick, occupants, subject, profiles=profiles)
+        self.otr_state_encryption = OTR_STATE_UNENCRYPTED
+        self.otr_state_trust = OTR_STATE_UNTRUSTED
         cagou_widget.CagouWidget.__init__(self)
         if type_ == C.CHAT_ONE2ONE:
-            self.headerInputAddExtra(EncryptionButton(self))
+            self.encryption_btn = EncryptionButton(self)
+            self.headerInputAddExtra(self.encryption_btn)
         self.header_input.hint_text = u"{}".format(target)
         self.host.addListener('progressError', self.onProgressError, profiles)
         self.host.addListener('progressFinished', self.onProgressFinished, profiles)
@@ -411,6 +436,17 @@
         self.host.removeListener('progressError', self.onProgressError)
         return super(Chat, self).onDelete()
 
+    def onOTRState(self, state, dest_jid, profile):
+        assert profile in self.profiles
+        if state in OTR_STATE_ENCRYPTION:
+            self.otr_state_encryption = state
+        elif state in OTR_STATE_TRUST:
+            self.otr_state_trust = state
+        else:
+            log.error(_(u"Unknown OTR state received: {}".format(state)))
+            return
+        self.encryption_btn.source = self.encryption_btn.getIconSource()
+
     def onDelete(self, force=False):
         if force==True:
             return self._onDelete()