1from fsbc.desktop import open_url_in_browser
2from launcher.launcher_config import LauncherConfig
3from fsui import Image, HorizontalLayout, Menu
4from ...i18n import gettext
5from .StatusElement import StatusElement
6
7
8url_descriptions = [
9    ("database_url", "Game Database Entry"),
10    ("homepage_url", "Game Home Page"),
11    ("longplay_url", "Longplay Video"),
12    ("hol_url", "Hall of Light Entry"),
13    ("lemon_url", "LemonAmiga Entry"),
14    ("wikipedia_url", "Wikipedia Entry"),
15    ("mobygames_url", "MobyGames Entry"),
16    ("amigamemo_url", "AmigaMemo Entry"),
17    ("thelegacy_url", "TheLegacy Entry"),
18    ("whdload_url", "WHDLoad Slave Info"),
19]
20
21url_keys = set([x[0] for x in url_descriptions])
22
23
24class WebLinkElement(StatusElement):
25    def __init__(self, parent):
26        StatusElement.__init__(self, parent)
27        # self.set_min_width(140)
28        self.layout = HorizontalLayout()
29        self.icon = Image("launcher:res/16x16/world_link.png")
30        self.right_icon = Image("launcher:res/16x16/drop_down_arrow.png")
31        self.right_icon_disabled = Image(
32            "launcher:res/16x16/drop_down_arrow_disabled.png"
33        )
34        # self.inactive_icon = self.active_icon.grey_scale()
35
36        self.text = gettext("Web Links")
37
38        LauncherConfig.add_listener(self)
39        self.on_config("protection", LauncherConfig.get("protection"))
40
41        self.have = set()
42        for key in url_keys:
43            self.on_config(key, LauncherConfig.get(key))
44
45    def on_left_down(self):
46        if len(self.have) == 0:
47            return
48        menu = Menu()
49        for key, description in url_descriptions:
50            value = LauncherConfig.get(key, "")
51            if value:
52                menu.add_item(description, create_open_url_function(value))
53        self.popup_menu(menu)
54
55    def on_destroy(self):
56        LauncherConfig.remove_listener(self)
57
58    def on_config(self, key, value):
59        if key in url_keys:
60            if value:
61                self.have.add(key)
62            elif key in self.have:
63                self.have.remove(key)
64            old_active = self.active
65            self.active = len(self.have) > 0
66            if old_active != self.active:
67                self.refresh()
68
69
70def create_open_url_function(value):
71    def open_url():
72        open_url_in_browser(value)
73
74    return open_url
75