small speedups/cleanups by moritz venn
[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.append(self.runFinished)
78                 self.container.dataAvail.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.remainingdata = ""
96
97                 self["actions"] = ActionMap(["WizardActions"], 
98                 {
99                         "ok": self.go,
100                         "back": self.close,
101                 })
102                 
103         def go(self):
104                 sel = self["list"].l.getCurrentSelection()
105
106                 if sel is None:
107                         return
108
109                 sel = sel[0]
110                 if isinstance(sel, str): # category
111                         if sel in self.expanded:
112                                 self.expanded.remove(sel)
113                         else:
114                                 self.expanded.append(sel)
115                         self.updateList()
116                 else:
117                         if self.type == self.DOWNLOAD:
118                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"%s\"?") % sel.name)
119                         elif self.type == self.REMOVE:
120                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"%s\"?") % sel.name)
121
122         def runInstall(self, val):
123                 if val:
124                         if self.type == self.DOWNLOAD:
125                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg install " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
126                         elif self.type == self.REMOVE:
127                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg remove " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
128
129         def setWindowTitle(self):
130                 if self.type == self.DOWNLOAD:
131                         self.setTitle(_("Downloadable new plugins"))
132                 elif self.type == self.REMOVE:
133                         self.setTitle(_("Remove plugins"))
134
135         def startRun(self):
136                 self["list"].instance.hide()
137                 if self.type == self.DOWNLOAD:
138                         self.container.execute("ipkg update")
139                 elif self.type == self.REMOVE:
140                         self.run = 1
141                         self.container.execute("ipkg list_installed enigma2-plugin-*")
142                 
143         def installFinished(self):
144                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
145                 self.container.appClosed.remove(self.runFinished)
146                 self.container.dataAvail.remove(self.dataAvail)
147                 self.close()
148
149         def runFinished(self, retval):
150                 self.remainingdata = ""
151                 if self.run == 0:
152                         self.run = 1
153                         if self.type == self.DOWNLOAD:
154                                 self.container.execute("ipkg list_installed enigma2-plugin-*")
155                 elif self.run == 1 and self.type == self.DOWNLOAD:
156                         self.run = 2
157                         self.container.execute("ipkg list enigma2-plugin-*")
158                 else:
159                         if len(self.pluginlist) > 0:
160                                 self.updateList()
161                                 self["list"].instance.show()
162                         else:
163                                 self["text"].setText("No new plugins found")
164
165         def dataAvail(self, str):
166                 #prepend any remaining data from the previous call
167                 str = self.remainingdata + str
168                 #split in lines
169                 lines = str.split('\n')
170                 #'str' should end with '\n', so when splitting, the last line should be empty. If this is not the case, we received an incomplete line
171                 if len(lines[-1]):
172                         #remember this data for next time
173                         self.remainingdata = lines[-1]
174                         lines = lines[0:-1]
175                 else:
176                         self.remainingdata = ""
177
178                 for x in lines:
179                         plugin = x.split(" - ")
180                         if len(plugin) == 3:
181                                 if self.run == 1 and self.type == self.DOWNLOAD:
182                                         self.installedplugins.append(plugin[0])
183                                 else:
184                                         if plugin[0] not in self.installedplugins:
185                                                 plugin.append(plugin[0][15:])
186
187                                                 self.pluginlist.append(plugin)
188         
189         def updateList(self):
190                 self.list = []
191                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expandable-plugins.png"))
192                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expanded-plugins.png"))
193                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/verticalline-plugins.png"))
194                 
195                 self.plugins = {}
196                 for x in self.pluginlist:
197                         split = x[3].split('-')
198                         if len(split) < 2:
199                                 continue
200                         if not self.plugins.has_key(split[0]):
201                                 self.plugins[split[0]] = []
202                                 
203                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
204                         
205                 for x in self.plugins.keys():
206                         if x in self.expanded:
207                                 self.list.append(PluginCategoryComponent(x, expandedIcon))
208                                 for plugin in self.plugins[x]:
209                                         self.list.append(PluginDownloadComponent(plugin[0], plugin[1]))
210                         else:
211                                 self.list.append(PluginCategoryComponent(x, expandableIcon))
212                 self["list"].l.setList(self.list)
213