PluginBrowser.py: dont use "ipkg list_installed" on second run if "Download Plugins...
[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 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                 self.updateList()
27                 
28                 self["actions"] = ActionMap(["WizardActions", "ColorActions"],
29                 {
30                         "ok": self.save,
31                         "back": self.close,
32                         "red": self.delete,
33                         "green": self.download
34                 })
35                 self.onExecBegin.append(self.checkWarnings)
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                 
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
58                 self["list"].l.setList(self.list)
59
60         def delete(self):
61                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.REMOVE)
62         
63         def download(self):
64                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.DOWNLOAD)
65
66 class PluginDownloadBrowser(Screen):
67         DOWNLOAD = 0
68         REMOVE = 1
69         lastDownloadDate = None
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 startIpkgListInstalled(self):
136                 self.container.execute("ipkg list_installed enigma2-plugin-*")
137
138         def startIpkgListAvailable(self):
139                 self.container.execute("ipkg list enigma2-plugin-*")
140
141         def startRun(self):
142                 self["list"].instance.hide()
143                 if self.type == self.DOWNLOAD:
144                         if not PluginDownloadBrowser.lastDownloadDate or (time() - PluginDownloadBrowser.lastDownloadDate) > 3600:
145                                 # Only update from internet once per hour
146                                 self.container.execute("ipkg update")
147                                 PluginDownloadBrowser.lastDownloadDate = time()
148                         else:
149                                 self.startIpkgListAvailable()
150                 elif self.type == self.REMOVE:
151                         self.run = 1
152                         self.startIpkgListInstalled()
153
154         def installFinished(self):
155                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
156                 self.container.appClosed.remove(self.runFinished)
157                 self.container.dataAvail.remove(self.dataAvail)
158                 self.close()
159
160         def runFinished(self, retval):
161                 self.remainingdata = ""
162                 if self.run == 0:
163                         self.run = 1
164                         if self.type == self.DOWNLOAD:
165                                 self.startIpkgListInstalled()
166                 elif self.run == 1 and self.type == self.DOWNLOAD:
167                         self.run = 2
168                         self.startIpkgListAvailable()
169                 else:
170                         if len(self.pluginlist) > 0:
171                                 self.updateList()
172                                 self["list"].instance.show()
173                         else:
174                                 self["text"].setText("No new plugins found")
175
176         def dataAvail(self, str):
177                 #prepend any remaining data from the previous call
178                 str = self.remainingdata + str
179                 #split in lines
180                 lines = str.split('\n')
181                 #'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
182                 if len(lines[-1]):
183                         #remember this data for next time
184                         self.remainingdata = lines[-1]
185                         lines = lines[0:-1]
186                 else:
187                         self.remainingdata = ""
188
189                 for x in lines:
190                         plugin = x.split(" - ", 2)
191                         if len(plugin) == 3:
192                                 if self.run == 1 and self.type == self.DOWNLOAD:
193                                         self.installedplugins.append(plugin[0])
194                                 else:
195                                         if plugin[0] not in self.installedplugins:
196                                                 plugin.append(plugin[0][15:])
197
198                                                 self.pluginlist.append(plugin)
199         
200         def updateList(self):
201                 list = []
202                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expandable-plugins.png"))
203                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expanded-plugins.png"))
204                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/verticalline-plugins.png"))
205                 
206                 self.plugins = {}
207                 for x in self.pluginlist:
208                         split = x[3].split('-', 1)
209                         if len(split) < 2:
210                                 continue
211                         if not self.plugins.has_key(split[0]):
212                                 self.plugins[split[0]] = []
213                                 
214                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
215                         
216                 for x in self.plugins.keys():
217                         if x in self.expanded:
218                                 list.append(PluginCategoryComponent(x, expandedIcon))
219                                 list.extend([PluginDownloadComponent(plugin[0], plugin[1]) for plugin in self.plugins[x]])
220                         else:
221                                 list.append(PluginCategoryComponent(x, expandableIcon))
222                 self.list = list
223                 self["list"].l.setList(list)
224