From 81e2cd2f92cc21d1ebaddbcdc26b7223e48bc4aa Mon Sep 17 00:00:00 2001
From: A_D <aunderscored@gmail.com>
Date: Tue, 3 Nov 2020 17:52:21 +0200
Subject: [PATCH] Added warning popup on start

Popup shows on start when there are killswitches that match the current
version.
---
 EDMarketConnector.py | 52 ++++++++++++++++++++++++++++++++++++++++----
 killswitch.py        | 22 +++++++++++++++----
 2 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/EDMarketConnector.py b/EDMarketConnector.py
index d7e3f8f4..55915717 100755
--- a/EDMarketConnector.py
+++ b/EDMarketConnector.py
@@ -1435,6 +1435,50 @@ Locale LC_TIME: {locale.getlocale(locale.LC_TIME)}'''
                  )
 
 
+def setup_killswitches():
+    """Download and setup the main killswitch list."""
+    logger.debug('fetching killswitches...')
+    killswitch.setup_main_list()
+
+
+def show_killswitch_poppup(root=None):
+    """Show a warning popup if there are any killswitches that match the current version."""
+    if len(kills := killswitch.kills_for_version()) == 0:
+        return
+
+    text = (
+        "Some EDMC Features have been disabled for known issues.\n"
+        "Please update EDMC as soon as possible to resolve any issues.\n"
+    )
+
+    tl = tk.Toplevel(root)
+    tl.wm_attributes('-topmost', True)
+    tl.geometry(f'+{root.winfo_rootx()}+{root.winfo_rooty()}')
+
+    tl.columnconfigure(1, weight=1)
+    tl.title("EDMC Features have been disabled")
+
+    frame = tk.Frame(tl)
+    frame.grid()
+    t = tk.Label(frame, text=text)
+    t.grid(columnspan=2)
+    idx = 1
+
+    for version in kills:
+        tk.Label(frame, text=f'Version: {version.version}').grid(row=idx, sticky=tk.W)
+        idx += 1
+        for id, reason in version.kills.items():
+            tk.Label(frame, text=id).grid(column=0, row=idx, sticky=tk.W, padx=(10, 0))
+            tk.Label(frame, text=reason).grid(column=1, row=idx, sticky=tk.E, padx=(0, 10))
+            idx += 1
+        idx += 1
+
+    ok_button = tk.Button(frame, text="ok", command=tl.destroy)
+    ok_button.grid(columnspan=2, sticky=tk.EW)
+
+    theme.apply(tl)
+
+
 # Run the app
 if __name__ == "__main__":
     # Command-line arguments
@@ -1531,7 +1575,8 @@ sys.path: {sys.path}'''
 
             except Exception:
                 logger.exception(
-                    f"Exception other than locale.Error on setting LC_ALL=('{locale_startup[0]}', 'UTF_8')")
+                    f"Exception other than locale.Error on setting LC_ALL=('{locale_startup[0]}', 'UTF_8')"
+                )
 
             else:
                 log_locale('After switching to UTF-8 encoding (same language)')
@@ -1566,9 +1611,7 @@ sys.path: {sys.path}'''
 
     Translations.install(config.get_str('language'))  # Can generate errors so wait til log set up
 
-    logger.debug('fetching killswitches...')
-    killswitch.setup_main_list()
-
+    setup_killswitches()
     root = tk.Tk(className=appname.lower())
 
     # UI Scaling
@@ -1622,6 +1665,7 @@ sys.path: {sys.path}'''
     root.wm_attributes('-alpha', ui_transparency / 100)
 
     root.after(0, messagebox_not_py3)
+    root.after(1, show_killswitch_poppup, root)
     root.mainloop()
 
     logger.info('Exiting')
diff --git a/killswitch.py b/killswitch.py
index b2336111..44c92177 100644
--- a/killswitch.py
+++ b/killswitch.py
@@ -51,14 +51,23 @@ class KillSwitchSet:
 
         return DisabledResult(False, "")
 
-    def is_disabled(self, id: str, *, version=_current_version) -> bool:
+    def is_disabled(self, id: str, *, version: semantic_version.Version = _current_version) -> bool:
         """Return whether or not a given feature ID is disabled for the given version."""
         return self.get_disabled(id, version=version).disabled
 
-    def get_reason(self, id: str, version=_current_version) -> str:
+    def get_reason(self, id: str, version: semantic_version.Version = _current_version) -> str:
         """Return a reason for why the given id is disabled for the given version, if any."""
         return self.get_disabled(id, version=version).reason
 
+    def kills_for_version(self, version: semantic_version.Version = _current_version) -> List[KillSwitch]:
+        """
+        Get all killswitch entries that apply to the given version.
+
+        :param version: the version to check against, defaults to the current EDMC version
+        :return: the matching kill switches
+        """
+        return [k for k in self.kill_switches if version in k.version]
+
     def __str__(self) -> str:
         """Return a string representation of KillSwitchSet."""
         return f'KillSwitchSet: {str(self.kill_switches)}'
@@ -172,16 +181,21 @@ def get_disabled(id: str, *, version: semantic_version.Version = _current_versio
     return active.get_disabled(id, version=version)
 
 
-def is_disabled(id: str, *, version=_current_version) -> bool:
+def is_disabled(id: str, *, version: semantic_version.Version = _current_version) -> bool:
     """Query the global KillSwitchSet#is_disabled method."""
     return active.is_disabled(id, version=version)
 
 
-def get_reason(id: str, *, version=_current_version) -> str:
+def get_reason(id: str, *, version: semantic_version.Version = _current_version) -> str:
     """Query the global KillSwitchSet#get_reason method."""
     return active.get_reason(id, version=version)
 
 
+def kills_for_version(version: semantic_version.Version = _current_version) -> List[KillSwitch]:
+    """Query the global KillSwitchSet for kills matching a particular version."""
+    return active.kills_for_version(version)
+
+
 if __name__ == "__main__":
     setup_main_list()
     print(f'{_current_version=}')