factor out loadPNG into generic LoadPixmap
[enigma2.git] / lib / python / Screens / PluginBrowser.py
1 from Screen import Screen
2
3 from enigma import eConsoleAppContainer
4
5 from Components.ActionMap import ActionMap
6 from Components.PluginComponent import plugins
7 from Components.PluginList import *
8 from Components.Label import Label
9 from Screens.MessageBox import MessageBox
10 from Screens.Console import Console
11 from Plugins.Plugin import PluginDescriptor
12 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
13 from Tools.LoadPixmap import LoadPixmap
14
15
16 class PluginBrowser(Screen):
17         def __init__(self, session):
18                 Screen.__init__(self, session)
19                 
20                 self["red"] = Label(_("Remove Plugins"))
21                 self["green"] = Label(_("Download Plugins"))
22                 
23                 self.list = []
24                 self["list"] = PluginList(self.list)
25                 self.updateList()
26                 
27                 self["actions"] = ActionMap(["WizardActions", "ColorActions"],
28                 {
29                         "ok": self.save,
30                         "back": self.close,
31                         "red": self.delete,
32                         "green": self.download
33                 })
34                 self.onExecBegin.append(self.checkWarnings)
35         
36         def checkWarnings(self):
37                 if len(plugins.warnings):
38                         text = _("Some plugins are not available:\n")
39                         for (pluginname, error) in plugins.warnings:
40                                 text += _("%s (%s)\n") % (pluginname, error)
41                         plugins.resetWarnings()
42                         self.session.open(MessageBox, text = text, type = MessageBox.TYPE_WARNING)
43
44         def save(self):
45                 #self.close()
46                 self.run()
47         
48         def run(self):
49                 plugin = self["list"].l.getCurrentSelection()[0]
50                 
51                 plugin(session=self.session)
52                 
53         def updateList(self):
54                 self.list = [ ]
55                 self.pluginlist = plugins.getPlugins(PluginDescriptor.WHERE_PLUGINMENU)
56                 for plugin in self.pluginlist:
57                         self.list.append(PluginEntryComponent(plugin))
58                 
59                 self["list"].l.setList(self.list)
60
61         def delete(self):
62                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.REMOVE)
63         
64         def download(self):
65                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.DOWNLOAD)
66
67 class PluginDownloadBrowser(Screen):
68         DOWNLOAD = 0
69         REMOVE = 1
70         
71         def __init__(self, session, type):
72                 Screen.__init__(self, session)
73                 
74                 self.type = type
75                 
76                 self.container = eConsoleAppContainer()
77                 self.container.appClosed.get().append(self.runFinished)
78                 self.container.dataAvail.get().append(self.dataAvail)
79                 self.onLayoutFinish.append(self.startRun)
80                 self.onShown.append(self.setWindowTitle)
81                 
82                 self.list = []
83                 self["list"] = PluginList(self.list)
84                 self.pluginlist = []
85                 self.expanded = []
86                 self.installedplugins = []
87                 
88                 if self.type == self.DOWNLOAD:
89                         self["text"] = Label(_("Downloading plugin information. Please wait..."))
90                 elif self.type == self.REMOVE:
91                         self["text"] = Label(_("Getting plugin information. Please wait..."))
92                 
93                 self.run = 0
94                                 
95                 self["actions"] = ActionMap(["WizardActions"], 
96                 {
97                         "ok": self.go,
98                         "back": self.close,
99                 })
100                 
101         def go(self):
102                 sel = self["list"].l.getCurrentSelection()
103
104                 if sel is None:
105                         return
106
107                 if type(sel[0]) is str: # category
108                         if sel[0] in self.expanded:
109                                 self.expanded.remove(sel[0])
110                         else:
111                                 self.expanded.append(sel[0])
112                         self.updateList()
113                 else:
114                         if self.type == self.DOWNLOAD:
115                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"" + sel[0].name + "\"?"))
116                         elif self.type == self.REMOVE:
117                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"" + sel[0].name + "\"?"))
118
119         def runInstall(self, val):
120                 if val:
121                         if self.type == self.DOWNLOAD:
122                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg install " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
123                         elif self.type == self.REMOVE:
124                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg remove " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
125
126         def setWindowTitle(self):
127                 if self.type == self.DOWNLOAD:
128                         self.setTitle(_("Downloadable new plugins"))
129                 elif self.type == self.REMOVE:
130                         self.setTitle(_("Remove plugins"))
131
132         def startRun(self):
133                 self["list"].instance.hide()
134                 if self.type == self.DOWNLOAD:
135                         self.container.execute("ipkg update")
136                 elif self.type == self.REMOVE:
137                         self.run = 1
138                         self.container.execute("ipkg list_installed enigma2-plugin-*")
139                 
140         def installFinished(self):
141                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
142                 self.close()
143                 
144         def runFinished(self, retval):
145                 if self.run == 0:
146                         self.run = 1
147                         if self.type == self.DOWNLOAD:
148                                 self.container.execute("ipkg list_installed enigma2-plugin-*")
149                 elif self.run == 1 and self.type == self.DOWNLOAD:
150                         self.run = 2
151                         self.container.execute("ipkg list enigma2-plugin-*")
152                 else:
153                         if len(self.pluginlist) > 0:
154                                 self.updateList()
155                                 self["list"].instance.show()
156                         else:
157                                 self["text"].setText("No new plugins found")
158
159         def dataAvail(self, str):
160                 for x in str.split('\n'):
161                         plugin = x.split(" - ")
162                         if len(plugin) == 3:
163                                 if self.run == 1 and self.type == self.DOWNLOAD:
164                                         self.installedplugins.append(plugin[0])
165                                 else:
166                                         if plugin[0] not in self.installedplugins:
167                                                 plugin.append(plugin[0][15:])
168
169                                                 self.pluginlist.append(plugin)
170         
171         def updateList(self):
172                 self.list = []
173                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "expandable-plugins.png"))
174                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "expanded-plugins.png"))
175                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "verticalline-plugins.png"))
176                 
177                 self.plugins = {}
178                 for x in self.pluginlist:
179                         split = x[3].split('-')
180                         if len(split) < 2:
181                                 continue
182                         if not self.plugins.has_key(split[0]):
183                                 self.plugins[split[0]] = []
184                                 
185                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
186                         
187                 for x in self.plugins.keys():
188                         if x in self.expanded:
189                                 self.list.append(PluginCategoryComponent(x, expandedIcon))
190                                 for plugin in self.plugins[x]:
191                                         self.list.append(PluginDownloadComponent(plugin[0], plugin[1]))
192                         else:
193                                 self.list.append(PluginCategoryComponent(x, expandableIcon))
194                 self["list"].l.setList(self.list)
195