pluginhider: fix possible crash when aborting setup
[enigma2-plugins.git] / pluginhider / src / PluginHiderSetup.py
1 # GUI (Screens)
2 from Screens.Screen import Screen
3 from Components.ConfigList import ConfigListScreen
4
5 # GUI (Summary)
6 from Screens.Setup import SetupSummary
7
8 # GUI (Components)
9 from Components.ActionMap import ActionMap
10 from Components.SelectionList import SelectionList, SelectionEntryComponent
11 from Components.Sources.StaticText import StaticText
12
13 # Configuration
14 from Components.config import config
15
16 from Components.PluginComponent import plugins
17 from Plugins.Plugin import PluginDescriptor
18
19 LIST_PLUGINS = 0
20 LIST_EXTENSIONS = 1
21 class PluginHiderSetup(Screen):
22         skin = """<screen name="PluginHiderSetup" title="PluginHider Setup" position="center,center" size="565,290">
23                 <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
24                 <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
25                 <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
26                 <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
27                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
28                 <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
29                 <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
30                 <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
31                 <widget name="list" position="5,45" size="555,240" scrollbarMode="showOnDemand" />
32         </screen>"""
33
34         def __init__(self, session):
35                 Screen.__init__(self, session)
36
37                 # Initialize widgets
38                 self["key_green"] = StaticText(_("OK"))
39                 self["key_red"] = StaticText(_("Cancel"))
40                 self["key_yellow"] = StaticText(_("Plugins"))
41                 self["key_blue"] = StaticText(_("Extensions"))
42
43                 self["list"] = SelectionList([])
44                 self.selectedList = LIST_PLUGINS
45                 self.updateList()
46
47                 self["ColorActions"] = ActionMap(["OkCancelActions", "ColorActions"],
48                         {
49                                 "ok": self["list"].toggleSelection,
50                                 "cancel": self.cancel,
51                                 "red": self.cancel,
52                                 "green": self.save,
53                                 "yellow": self.plugins,
54                                 "blue": self.extensions,
55                         }, -1
56                 )
57
58                 self.onLayoutFinish.append(self.setCustomTitle)
59
60         def cancel(self):
61                 config.plugins.pluginhider.hideplugins.cancel()
62                 config.plugins.pluginhider.hideextensions.cancel()
63                 self.close()
64
65         def save(self):
66                 self.keepCurrent()
67                 config.plugins.pluginhider.save()
68                 self.close()
69
70         def plugins(self):
71                 self.keepCurrent()
72                 self.selectedList = LIST_PLUGINS
73                 self.updateList()
74
75         def extensions(self):
76                 self.keepCurrent()
77                 self.selectedList = LIST_EXTENSIONS
78                 self.updateList()
79
80         def setCustomTitle(self):
81                 self.setTitle(_("PluginHider Setup"))
82
83         def updateList(self):
84                 if hasattr(plugins, 'pluginHider_baseGetPlugins'):
85                         fnc = plugins.pluginHider_baseGetPlugins
86                 else:
87                         fnc = plugins.getPlugins
88
89                 if self.selectedList == LIST_PLUGINS:
90                         list = fnc([PluginDescriptor.WHERE_PLUGINMENU])
91                         selected = config.plugins.pluginhider.hideplugins.value
92                 else:
93                         list = fnc([PluginDescriptor.WHERE_EXTENSIONSMENU])
94                         selected = config.plugins.pluginhider.hideextensions.value
95
96                 res = []
97                 i = 0
98                 for plugin in list:
99                         if plugin.description:
100                                 name = "%s (%s)" % (plugin.name, plugin.description)
101                         else:
102                                 name = plugin.name
103
104                         res.append(SelectionEntryComponent(
105                                         name,
106                                         plugin.name,
107                                         i,
108                                         plugin.name in selected,
109                         ))
110                         i += 1
111                 self["list"].setList(res)
112                 if res:
113                         self["list"].moveToIndex(0)
114
115         def keepCurrent(self):
116                 selected = self["list"].getSelectionsList()
117                 if self.selectedList == LIST_PLUGINS:
118                         config.plugins.pluginhider.hideplugins.value = [x[1] for x in selected]
119                 else:
120                         config.plugins.pluginhider.hideextensions.value = [x[1] for x in selected]