From 0852a8ce7139936e9b5ddbfc588f632df821196a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Apitzsch?= <git@apitzsch.eu>
Date: Fri, 5 Feb 2021 22:03:46 +0100
Subject: [PATCH] Remove conversion of ASCII emojis

Display emojis as intended by the sender, i.e. don't replace it.

Closes #10416.
---
 gajim/common/config.py         |  1 -
 gajim/common/setting_values.py |  1 -
 gajim/gtk/emoji_data.py        | 36 +---------------------------------
 gajim/gtk/emoji_data.pyi       |  5 +----
 gajim/gtk/preferences.py       | 30 +++++++++++-----------------
 gajim/gui_interface.py         |  3 ---
 6 files changed, 13 insertions(+), 63 deletions(-)

diff --git a/gajim/common/config.py b/gajim/common/config.py
index 88f28fb801..101956e04c 100644
--- a/gajim/common/config.py
+++ b/gajim/common/config.py
@@ -93,7 +93,6 @@ class Config:
         'speller_language': [opt_str, '', _('Language used for spell checking.')],
         'print_time': [opt_str, 'always', _('\'always\' - print time for every message.\n\'sometimes\' - print time every print_ichat_every_foo_minutes minute.\n\'never\' - never print time.')],
         'emoticons_theme': [opt_str, 'noto-emoticons', '', True],
-        'ascii_emoticons': [opt_bool, True, _('When enabled, ASCII emojis will be converted to graphical emojis.'), True],
         'ascii_formatting': [opt_bool, True,
                 _('Treat * / _ pairs as possible formatting characters.'), True],
         'show_ascii_formatting_chars': [opt_bool, True, _('If enabled, do not '
diff --git a/gajim/common/setting_values.py b/gajim/common/setting_values.py
index e28dc1ec5c..0ab6cf363d 100644
--- a/gajim/common/setting_values.py
+++ b/gajim/common/setting_values.py
@@ -41,7 +41,6 @@ class _ACCOUNT_DEFAULT:
     'speller_language': '',
     'print_time': 'always',
     'emoticons_theme': 'noto-emoticons',
-    'ascii_emoticons': True,
     'ascii_formatting': True,
     'show_ascii_formatting_chars': True,
     'sounds_on': True,
diff --git a/gajim/gtk/emoji_data.py b/gajim/gtk/emoji_data.py
index 31dc643ac9..5f1b62d00a 100644
--- a/gajim/gtk/emoji_data.py
+++ b/gajim/gtk/emoji_data.py
@@ -144,45 +144,11 @@ def _replace_emojis(self):
                     start(), end(), pixbuf.copy(), codepoint)
 
 
-class EmojiAsciiData(dict):
-    def get_regex(self):
-        # When an emoticon is bordered by an alphanumeric character it
-        # is NOT expanded.  e.g., foo:) NO, foo :) YES, (brb) NO,
-        # (:)) YES, etc. We still allow multiple emoticons
-        # side-by-side like :P:P:P
-
-        # TODO: Refactor this
-        keys = sorted(self.keys(), key=len, reverse=True)
-        pre_pattern = ''
-        post_pattern = ''
-        emoticon_length = 0
-        emoticons_pattern = ''
-        for emoticon in keys:
-            # escape regexp metachars
-            emoticon_escaped = re.escape(emoticon)
-            emoticons_pattern += emoticon_escaped + '|'
-            if emoticon_length != len(emoticon):
-                # Build up expressions to match emoticons next to others
-                pre_pattern = pre_pattern[:-1] + ')|(?<='
-                post_pattern = post_pattern[:-1] + ')|(?='
-                emoticon_length = len(emoticon)
-            pre_pattern += emoticon_escaped + '|'
-            post_pattern += emoticon_escaped + '|'
-        # We match from our list of emoticons, but they must either have
-        # whitespace, or another emoticon next to it to match successfully
-        # [\w.] alphanumeric and dot (for not matching 8) in (2.8))
-        emoticons_pattern = r'(?:(?<![\w.]' + \
-            pre_pattern[:-1] + '))' + '(?:' + \
-            emoticons_pattern[:-1] + ')' + r'(?:(?![\w]' + \
-            post_pattern[:-1] + '))'
-        return emoticons_pattern
-
-
 emoji_pixbufs = EmojiPixbufs()
 
 # pylint: disable=line-too-long
 
-emoji_ascii_data = EmojiAsciiData([
+emoji_ascii_data = dict([
     ("':-D", '\U0001F605'),
     (':)', '\U0001F642'),
     (':-O', '\U0001F62e'),
diff --git a/gajim/gtk/emoji_data.pyi b/gajim/gtk/emoji_data.pyi
index 3607a61a0a..de25e94584 100644
--- a/gajim/gtk/emoji_data.pyi
+++ b/gajim/gtk/emoji_data.pyi
@@ -29,11 +29,8 @@ class EmojiData(OrderedDict):
 class EmojiPixbufs(dict):
     ...
 
-class EmojiAsciiData(dict):
-    ...
-
 emoji_pixbufs = EmojiPixbufs()
 
-emoji_ascii_data = EmojiAsciiData()
+emoji_ascii_data: dict[str, str]
 
 emoji_data = EmojiData()
diff --git a/gajim/gtk/preferences.py b/gajim/gtk/preferences.py
index 6e5a5f9ebd..f2ba543c1c 100644
--- a/gajim/gtk/preferences.py
+++ b/gajim/gtk/preferences.py
@@ -103,6 +103,8 @@ def __init__(self):
         self._ui.connect_signals(self)
 
         self.show_all()
+        if sys.platform not in ('win32', 'darwin'):
+            self._ui.emoji.hide()
 
     def get_ui(self):
         return self._ui
@@ -727,30 +729,24 @@ def _on_dark_theme(value, *args):
 
 class Emoji(PreferenceBox):
     def __init__(self, *args):
+        if sys.platform not in ('win32', 'darwin'):
+            PreferenceBox.__init__(self, [])
+            return
 
         emoji_themes_items = []
         for theme in helpers.get_available_emoticon_themes():
             emoji_themes_items.append(theme)
 
         settings = [
-            Setting(SettingKind.SWITCH,
-                    _('Convert ASCII Emojis'),
+            Setting(SettingKind.POPOVER,
+                    _('Emoji Theme'),
                     SettingType.CONFIG,
-                    'ascii_emoticons',
-                    desc=_('Typing short codes like :-) will display emojis'),
-                    callback=self._on_ascii_emoticons),
+                    'emoticons_theme',
+                    desc=_('Choose from various emoji styles'),
+                    props={'entries': emoji_themes_items},
+                    callback=self._on_emoticons_theme)
         ]
 
-        if sys.platform in ('win32', 'darwin'):
-            settings.append(
-                Setting(SettingKind.POPOVER,
-                        _('Emoji Theme'),
-                        SettingType.CONFIG,
-                        'emoticons_theme',
-                        desc=_('Choose from various emoji styles'),
-                        props={'entries': emoji_themes_items},
-                        callback=self._on_emoticons_theme))
-
         PreferenceBox.__init__(self, settings)
 
     def _on_emoticons_theme(self, *args):
@@ -763,10 +759,6 @@ def _toggle_emoticons():
         for ctrl in controls:
             ctrl.toggle_emoticons()
 
-    @staticmethod
-    def _on_ascii_emoticons(*args):
-        app.interface.make_regexps()
-
 
 class StatusIcon(PreferenceBox):
     def __init__(self, *args):
diff --git a/gajim/gui_interface.py b/gajim/gui_interface.py
index dfd03606f9..da0bbaefa3 100644
--- a/gajim/gui_interface.py
+++ b/gajim/gui_interface.py
@@ -102,7 +102,6 @@
 from gajim.gui.dialogs import PassphraseDialog
 from gajim.gui.filechoosers import FileChooserDialog
 from gajim.gui.emoji_data import emoji_data
-from gajim.gui.emoji_data import emoji_ascii_data
 from gajim.gui.filetransfer import FileTransfersWindow
 from gajim.gui.filetransfer_progress import FileTransferProgress
 from gajim.gui.roster_item_exchange import RosterItemExchangeWindow
@@ -1392,8 +1391,6 @@ def make_regexps(self):
         # because emoticons match later (in the string) they need to be after
         # basic matches that may occur earlier
         emoticons = emoji_data.get_regex()
-        if app.settings.get('ascii_emoticons'):
-            emoticons += '|%s' % emoji_ascii_data.get_regex()
 
         self.emot_and_basic = '%s|%s' % (basic_pattern, emoticons)
 
-- 
GitLab