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