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