From 30442be2a68b1aad29f3bcde9f7a4b9ff4d14052 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <forenjunkie@chello.at>
Date: Sat, 15 Sep 2018 10:24:28 +0200
Subject: [PATCH] ConvTextview: Rework display of encryption

---
 gajim/chat_control_base.py     |  4 ++-
 gajim/conversation_textview.py | 58 +++++++++++++++++++++++++++++++---
 gajim/gtk/history.py           |  4 +++
 3 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/gajim/chat_control_base.py b/gajim/chat_control_base.py
index 683f865c68..d5128f25d3 100644
--- a/gajim/chat_control_base.py
+++ b/gajim/chat_control_base.py
@@ -338,6 +338,7 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
             self._schedule_activity_timers()
 
         self.encryption = self.get_encryption_state()
+        self.conv_textview.encryption_enabled = self.encryption is not None
 
         # PluginSystem: adding GUI extension point for ChatControlBase
         # instance object (also subclasses, eg. ChatControl or GroupchatControl)
@@ -429,8 +430,9 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
     def set_encryption_state(self, encryption):
         config_key = '%s-%s' % (self.account, self.contact.jid)
         self.encryption = encryption
+        self.conv_textview.encryption_enabled = encryption is not None
         app.config.set_per('encryption', config_key,
-                             'encryption', self.encryption or '')
+                           'encryption', self.encryption or '')
 
     def get_encryption_state(self):
         config_key = '%s-%s' % (self.account, self.contact.jid)
diff --git a/gajim/conversation_textview.py b/gajim/conversation_textview.py
index 8923defca7..92856a81b7 100644
--- a/gajim/conversation_textview.py
+++ b/gajim/conversation_textview.py
@@ -186,6 +186,7 @@ class ConversationTextview(GObject.GObject):
         self.tv.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
         self.tv.set_left_margin(2)
         self.tv.set_right_margin(2)
+        self._buffer = self.tv.get_buffer()
         self.handlers = {}
         self.image_cache = {}
         self.xep0184_marks = {}
@@ -204,6 +205,7 @@ class ConversationTextview(GObject.GObject):
         self.account = account
         self.cursor_changed = False
         self.last_time_printout = 0
+        self.encryption_enabled = False
 
         style = self.tv.get_style_context()
         style.add_class('gajim-conversation-font')
@@ -261,6 +263,9 @@ class ConversationTextview(GObject.GObject):
         self.tagMarked.set_property('foreground', color)
         self.tagMarked.set_property('weight', Pango.Weight.BOLD)
 
+        textview_icon = buffer_.create_tag('textview-icon')
+        textview_icon.set_property('rise', Pango.units_from_double(-4.45))
+
         tag = buffer_.create_tag('time_sometimes')
         tag.set_property('foreground', 'darkgrey')
         #Pango.SCALE_SMALL
@@ -1147,14 +1152,13 @@ class ConversationTextview(GObject.GObject):
         if kind == 'status':
             direction_mark = i18n.direction_mark
 
+        # print the encryption icon
+        self.print_encryption_status(iter_, additional_data)
+
         # print the time stamp
         self.print_time(text, kind, tim, simple, direction_mark,
             other_tags_for_time, iter_)
 
-        icon = load_icon('channel-secure-croped-symbolic', self.tv, pixbuf=True)
-        if encrypted:
-            buffer_.insert_pixbuf(iter_, icon)
-
         # If there's a displaymarking, print it here.
         if displaymarking:
             self.print_displaymarking(displaymarking, iter_=iter_)
@@ -1275,6 +1279,52 @@ class ConversationTextview(GObject.GObject):
         if text.startswith('/me ') or text.startswith('/me\n'):
             return kind
 
+    def print_encryption_status(self, iter_, additional_data):
+        details = self._get_encryption_details(additional_data)
+        if details is None:
+            # Message was not encrypted
+            if not self.encryption_enabled:
+                return
+            icon = 'security-low-symbolic'
+            color = 'error-color'
+            tooltip = _('Not encrypted')
+        else:
+            icon = 'security-high-symbolic'
+            color = 'success-color'
+            name, fingerprint = details
+            if fingerprint is None:
+                tooltip = name
+            else:
+                tooltip = '%s %s' % (name, fingerprint)
+
+        temp_mark = self._buffer.create_mark(None, iter_, True)
+        self._buffer.insert(iter_, ' ')
+        anchor = self._buffer.create_child_anchor(iter_)
+        anchor.plaintext = ''
+        self._buffer.insert(iter_, ' ')
+
+        # Apply mark to vertically center the icon
+        start = self._buffer.get_iter_at_mark(temp_mark)
+        self._buffer.apply_tag_by_name('textview-icon', start, iter_)
+
+        image = Gtk.Image.new_from_icon_name(icon, Gtk.IconSize.MENU)
+        image.show()
+        image.set_tooltip_text(tooltip)
+        image.get_style_context().add_class(color)
+        self.tv.add_child_at_anchor(image, anchor)
+
+    @staticmethod
+    def _get_encryption_details(additional_data):
+        encrypted = additional_data.get('encrypted')
+        if encrypted is None:
+            return
+
+        name = encrypted.get('name')
+        if name is None:
+            return
+        fingerprint = encrypted.get('fingerprint')
+        return name, fingerprint
+
     def print_time(self, text, kind, tim, simple, direction_mark, other_tags_for_time, iter_):
         local_tim = time.localtime(tim)
         buffer_ = self.tv.get_buffer()
diff --git a/gajim/gtk/history.py b/gajim/gtk/history.py
index 350fac89ae..1752655847 100644
--- a/gajim/gtk/history.py
+++ b/gajim/gtk/history.py
@@ -534,6 +534,10 @@ class HistoryWindow:
                 buf.insert_with_tags_by_name(
                     end_iter, tim + '\n', 'time_sometimes')
 
+        # print the encryption icon
+        self.history_textview.print_encryption_status(
+            end_iter, additional_data)
+
         tag_name = ''
         tag_msg = ''
 
-- 
GitLab