1import fsui
2from launcher.i18n import gettext
3from launcher.launcher_settings import LauncherSettings
4from launcher.ui.IconButton import IconButton
5from fsbc.util import unused
6from fsgs.FSGSDirectories import FSGSDirectories
7
8
9class ScanPathsGroup(fsui.Group):
10    def __init__(self, parent):
11        fsui.Group.__init__(self, parent)
12        self.layout = fsui.HorizontalLayout()
13
14        self.layout2 = fsui.VerticalLayout()
15        self.layout.add(self.layout2, fill=True, expand=True)
16
17        layout3 = fsui.HorizontalLayout()
18        self.layout2.add(layout3, fill=True, expand=True)
19
20        self.list_view = fsui.ListView(self)
21        self.list_view.set_min_height(130)
22        self.default_icon = fsui.Image("launcher:res/folder_16.png")
23        layout3.add(self.list_view, expand=True, fill=True)
24        layout3.add_spacer(10)
25
26        vlayout = fsui.VerticalLayout()
27        layout3.add(vlayout, fill=True)
28
29        add_button = IconButton(self, "add_button.png")
30        add_button.set_tooltip(gettext("Add Directory to Search Path"))
31        # add_button.disable()
32        add_button.activated.connect(self.on_add_button)
33        vlayout.add(add_button)
34        vlayout.add_spacer(10)
35
36        self.remove_button = IconButton(self, "remove_button.png")
37        self.remove_button.set_tooltip(
38            gettext("Remove Directory from Search Path")
39        )
40        self.remove_button.disable()
41        self.remove_button.activated.connect(self.on_remove_button)
42        vlayout.add(self.remove_button)
43
44        # self.list_view.set_items(self.get_search_path())
45        self.repopulate_list()
46        self.list_view.on_select_item = self.on_select_item
47        LauncherSettings.add_listener(self)
48
49    def on_destroy(self):
50        LauncherSettings.remove_listener(self)
51
52    def on_setting(self, key, value):
53        unused(value)
54        if key == "search_path":
55            self.repopulate_list()
56
57    def on_select_item(self, index):
58        unused(index)
59        self.remove_button.enable()
60
61    def repopulate_list(self):
62        self.list_view.clear()
63        for item in self.get_search_path():
64            self.list_view.add_item(item, self.default_icon)
65
66    @classmethod
67    def get_search_path(cls):
68        paths = FSGSDirectories.get_default_search_path()
69        search_path = LauncherSettings.get("search_path")
70        for p in search_path.split(";"):
71            p = p.strip()
72            if not p:
73                continue
74            elif p[0] == "-":
75                p = p[1:]
76                if p in paths:
77                    paths.remove(p)
78            else:
79                if p not in paths:
80                    paths.append(p)
81        # The Configurations dir is always scanned on startup (whenever
82        # modification time has changed). If we don't include it here too
83        # always, the result will be that the configs sometimes appear (on
84        # startup) and disappear (on scan).
85        if not FSGSDirectories.get_configurations_dir() in paths:
86            paths.append(FSGSDirectories.get_configurations_dir())
87        # Likewise, we force the Kickstarts directory to always be scanned.
88        if not FSGSDirectories.get_kickstarts_dir() in paths:
89            paths.append(FSGSDirectories.get_kickstarts_dir())
90        return paths
91
92    def on_add_button(self):
93        search_path = LauncherSettings.get("search_path")
94        search_path = [x.strip() for x in search_path.split(";") if x.strip()]
95        path = fsui.pick_directory(parent=self.get_window())
96        if path:
97            for i in range(len(search_path)):
98                if search_path[i].startswith("-"):
99                    if path == search_path[i][1:]:
100                        search_path.remove(search_path[i])
101                        break
102                else:
103                    if search_path[i] == path:
104                        # Already added.
105                        break
106            else:
107                default_paths = FSGSDirectories.get_default_search_path()
108                if path not in default_paths:
109                    search_path.append(path)
110            LauncherSettings.set("search_path", ";".join(search_path))
111
112    def on_remove_button(self):
113        path = self.list_view.get_item(self.list_view.get_index())
114        search_path = LauncherSettings.get("search_path")
115        search_path = [x.strip() for x in search_path.split(";") if x.strip()]
116        for i in range(len(search_path)):
117            if search_path[i].startswith("-"):
118                if path == search_path[i][1:]:
119                    # Already removed.
120                    break
121            else:
122                if search_path[i] == path:
123                    search_path.remove(search_path[i])
124                    break
125        default_paths = FSGSDirectories.get_default_search_path()
126        if path in default_paths:
127            search_path.append("-" + path)
128        LauncherSettings.set("search_path", ";".join(search_path))
129