NFIFlash/plugin.py: move NFI Flash Utility inside Softwaremanager.
[vuplus_dvbapp] / 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 from time import time
16
17 class PluginBrowser(Screen):
18         def __init__(self, session):
19                 Screen.__init__(self, session)
20                 
21                 self["red"] = Label(_("Remove Plugins"))
22                 self["green"] = Label(_("Download Plugins"))
23                 
24                 self.list = []
25                 self["list"] = PluginList(self.list)
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.onFirstExecBegin.append(self.checkWarnings)
35                 self.onShown.append(self.updateList)
36         
37         def checkWarnings(self):
38                 if len(plugins.warnings):
39                         text = _("Some plugins are not available:\n")
40                         for (pluginname, error) in plugins.warnings:
41                                 text += _("%s (%s)\n") % (pluginname, error)
42                         plugins.resetWarnings()
43                         self.session.open(MessageBox, text = text, type = MessageBox.TYPE_WARNING)
44
45         def save(self):
46                 #self.close()
47                 self.run()
48         
49         def run(self):
50                 plugin = self["list"].l.getCurrentSelection()[0]
51                 plugin(session=self.session)
52                 
53         def updateList(self):
54                 self.pluginlist = plugins.getPlugins(PluginDescriptor.WHERE_PLUGINMENU)
55                 self.list = [PluginEntryComponent(plugin) for plugin in self.pluginlist]
56                 self["list"].l.setList(self.list)
57
58         def delete(self):
59                 self.session.openWithCallback(self.PluginDownloadBrowserClosed, PluginDownloadBrowser, PluginDownloadBrowser.REMOVE)
60         
61         def download(self):
62                 self.session.openWithCallback(self.PluginDownloadBrowserClosed, PluginDownloadBrowser, PluginDownloadBrowser.DOWNLOAD)
63
64         def PluginDownloadBrowserClosed(self):
65                 self.updateList()
66                 self.checkWarnings()
67
68
69 class PluginDownloadBrowser(Screen):
70         DOWNLOAD = 0
71         REMOVE = 1
72         lastDownloadDate = None
73
74         def __init__(self, session, type):
75                 Screen.__init__(self, session)
76                 
77                 self.type = type
78                 
79                 self.container = eConsoleAppContainer()
80                 self.container.appClosed.append(self.runFinished)
81                 self.container.dataAvail.append(self.dataAvail)
82                 self.onLayoutFinish.append(self.startRun)
83                 self.onShown.append(self.setWindowTitle)
84                 
85                 self.list = []
86                 self["list"] = PluginList(self.list)
87                 self.pluginlist = []
88                 self.expanded = []
89                 self.installedplugins = []
90                 
91                 if self.type == self.DOWNLOAD:
92                         self["text"] = Label(_("Downloading plugin information. Please wait..."))
93                 elif self.type == self.REMOVE:
94                         self["text"] = Label(_("Getting plugin information. Please wait..."))
95                 
96                 self.run = 0
97
98                 self.remainingdata = ""
99
100                 self["actions"] = ActionMap(["WizardActions"], 
101                 {
102                         "ok": self.go,
103                         "back": self.close,
104                 })
105                 
106         def go(self):
107                 sel = self["list"].l.getCurrentSelection()
108
109                 if sel is None:
110                         return
111
112                 sel = sel[0]
113                 if isinstance(sel, str): # category
114                         if sel in self.expanded:
115                                 self.expanded.remove(sel)
116                         else:
117                                 self.expanded.append(sel)
118                         self.updateList()
119                 else:
120                         if self.type == self.DOWNLOAD:
121                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"%s\"?") % sel.name)
122                         elif self.type == self.REMOVE:
123                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"%s\"?") % sel.name)
124
125         def runInstall(self, val):
126                 if val:
127                         if self.type == self.DOWNLOAD:
128                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg install " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
129                         elif self.type == self.REMOVE:
130                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg remove " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
131
132         def setWindowTitle(self):
133                 if self.type == self.DOWNLOAD:
134                         self.setTitle(_("Downloadable new plugins"))
135                 elif self.type == self.REMOVE:
136                         self.setTitle(_("Remove plugins"))
137
138         def startIpkgListInstalled(self):
139                 self.container.execute("ipkg list_installed enigma2-plugin-*")
140
141         def startIpkgListAvailable(self):
142                 self.container.execute("ipkg list enigma2-plugin-*")
143
144         def startRun(self):
145                 self["list"].instance.hide()
146                 if self.type == self.DOWNLOAD:
147                         if not PluginDownloadBrowser.lastDownloadDate or (time() - PluginDownloadBrowser.lastDownloadDate) > 3600:
148                                 # Only update from internet once per hour
149                                 self.container.execute("ipkg update")
150                                 PluginDownloadBrowser.lastDownloadDate = time()
151                         else:
152                                 self.startIpkgListAvailable()
153                 elif self.type == self.REMOVE:
154                         self.run = 1
155                         self.startIpkgListInstalled()
156
157         def installFinished(self):
158                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
159                 self.container.appClosed.remove(self.runFinished)
160                 self.container.dataAvail.remove(self.dataAvail)
161                 self.close()
162
163         def runFinished(self, retval):
164                 self.remainingdata = ""
165                 if self.run == 0:
166                         self.run = 1
167                         if self.type == self.DOWNLOAD:
168                                 self.startIpkgListInstalled()
169                 elif self.run == 1 and self.type == self.DOWNLOAD:
170                         self.run = 2
171                         self.startIpkgListAvailable()
172                 else:
173                         if len(self.pluginlist) > 0:
174                                 self.updateList()
175                                 self["list"].instance.show()
176                         else:
177                                 self["text"].setText("No new plugins found")
178
179         def dataAvail(self, str):
180                 #prepend any remaining data from the previous call
181                 str = self.remainingdata + str
182                 #split in lines
183                 lines = str.split('\n')
184                 #'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
185                 if len(lines[-1]):
186                         #remember this data for next time
187                         self.remainingdata = lines[-1]
188                         lines = lines[0:-1]
189                 else:
190                         self.remainingdata = ""
191
192                 for x in lines:
193                         plugin = x.split(" - ", 2)
194                         if len(plugin) == 3:
195                                 if self.run == 1 and self.type == self.DOWNLOAD:
196                                         if plugin[0] not in self.installedplugins:
197                                                 self.installedplugins.append(plugin[0])
198                                 else:
199                                         if plugin[0] not in self.installedplugins:
200                                                 plugin.append(plugin[0][15:])
201
202                                                 self.pluginlist.append(plugin)
203         
204         def updateList(self):
205                 list = []
206                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expandable-plugins.png"))
207                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expanded-plugins.png"))
208                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/verticalline-plugins.png"))
209                 
210                 self.plugins = {}
211                 for x in self.pluginlist:
212                         split = x[3].split('-', 1)
213                         if len(split) < 2:
214                                 continue
215                         if not self.plugins.has_key(split[0]):
216                                 self.plugins[split[0]] = []
217                                 
218                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
219                         
220                 for x in self.plugins.keys():
221                         if x in self.expanded:
222                                 list.append(PluginCategoryComponent(x, expandedIcon))
223                                 list.extend([PluginDownloadComponent(plugin[0], plugin[1]) for plugin in self.plugins[x]])
224                         else:
225                                 list.append(PluginCategoryComponent(x, expandableIcon))
226                 self.list = list
227                 self["list"].l.setList(list)
228