From 52fa57795ac04fb273c3686f05adb3d766f3f454 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <philipp@hoerist.com>
Date: Thu, 22 Feb 2018 00:37:54 +0100
Subject: [PATCH] Better emoticon theme fallback strategy

Fallback must be happening in init_emoticon() instead of PreferencesWindow
---
 gajim/common/helpers.py | 20 ++++++++++++++++++--
 gajim/config.py         |  5 +----
 gajim/gui_interface.py  | 18 ++++++++----------
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py
index 4d8ed27af6..97840abf6d 100644
--- a/gajim/common/helpers.py
+++ b/gajim/common/helpers.py
@@ -1596,15 +1596,31 @@ def version_condition(current_version, required_version):
 def get_available_emoticon_themes():
     emoticons_themes = []
     emoticons_data_path = os.path.join(app.DATA_DIR, 'emoticons')
+    font_theme_path = os.path.join(
+        app.DATA_DIR, 'emoticons', 'font-emoticons', 'emoticons_theme.py')
 
     folders = os.listdir(emoticons_data_path)
     if os.path.isdir(app.MY_EMOTS_PATH):
         folders += os.listdir(app.MY_EMOTS_PATH)
 
+    file = 'emoticons_theme.py'
+    if os.name == 'nt' and not os.path.exists(font_theme_path):
+        # When starting Gajim from source .py files are available
+        # We test this with font-emoticons and fallback to .pyc files otherwise
+        file = 'emoticons_theme.pyc'
+
     for theme in folders:
-        theme_path = os.path.join(
-            emoticons_data_path, theme, 'emoticons_theme.py')
+        theme_path = os.path.join(emoticons_data_path, theme, file)
         if os.path.exists(theme_path):
             emoticons_themes.append(theme)
     emoticons_themes.sort()
     return emoticons_themes
+
+def get_emoticon_theme_path(theme):
+    emoticons_data_path = os.path.join(app.DATA_DIR, 'emoticons', theme)
+    if os.path.exists(emoticons_data_path):
+        return emoticons_data_path
+
+    emoticons_user_path = os.path.join(app.MY_EMOTS_PATH, theme)
+    if os.path.exists(emoticons_user_path):
+        return emoticons_user_path
diff --git a/gajim/config.py b/gajim/config.py
index 8a718df982..cbcad280d0 100644
--- a/gajim/config.py
+++ b/gajim/config.py
@@ -151,13 +151,10 @@ class PreferencesWindow:
 
         config_theme = app.config.get('emoticons_theme')
         if config_theme not in emoticon_themes:
-            # Fallback theme
-            config_theme = 'font-emoticons'
-            app.config.set('emoticons_theme', 'font-emoticons')
+            config_theme = _('Disabled')
         emoticons_combobox.set_id_column(0)
         emoticons_combobox.set_active_id(config_theme)
 
-
         # Set default for single window type
         choices = c_config.opt_one_window_types
         type_ = app.config.get('one_message_window')
diff --git a/gajim/gui_interface.py b/gajim/gui_interface.py
index 641c010e9a..9b0af16650 100644
--- a/gajim/gui_interface.py
+++ b/gajim/gui_interface.py
@@ -1996,18 +1996,16 @@ class Interface:
         if 'preferences' in app.interface.instances:
             transient_for = app.interface.instances['preferences'].window
 
-        path = os.path.join(app.DATA_DIR, 'emoticons', emot_theme)
-        if not os.path.exists(path):
-            # It's maybe a user theme
-            path = os.path.join(app.MY_EMOTS_PATH, emot_theme)
-            if not os.path.exists(path):
-                # theme doesn't exist, disable emoticons
-                dialogs.WarningDialog(_('Emoticons disabled'),
-                    _('Your configured emoticons theme has not been found, so '
-                    'emoticons have been disabled.'),
-                    transient_for=transient_for)
+        themes = helpers.get_available_emoticon_themes()
+        if emot_theme not in themes:
+            if 'font-emoticons' in themes:
+                emot_theme = 'font-emoticons'
+                app.config.set('emoticons_theme', 'font-emoticons')
+            else:
                 app.config.set('emoticons_theme', '')
                 return
+
+        path = helpers.get_emoticon_theme_path(emot_theme)
         if not emoticons.load(path, ascii_emoticons):
             dialogs.WarningDialog(
                     _('Emoticons disabled'),
-- 
GitLab