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