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