PluginBrowser.py: update pluginlist max every 60 minutes (thx to
[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 startRun(self):
139                 self["list"].instance.hide()
140                 if self.type == self.DOWNLOAD:
141                         if not PluginDownloadBrowser.lastDownloadDate or (time() - PluginDownloadBrowser.lastDownloadDate) > 3600:
142                                 # Only update from internet once per hour
143                                 self.container.execute("ipkg update")
144                                 PluginDownloadBrowser.lastDownloadDate = time()
145                         else:
146                                 self.startIpkgListInstalled()
147                 elif self.type == self.REMOVE:
148                         self.run = 1
149                         self.startIpkgListInstalled()
150
151         def installFinished(self):
152                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
153                 self.container.appClosed.remove(self.runFinished)
154                 self.container.dataAvail.remove(self.dataAvail)
155                 self.close()
156
157         def runFinished(self, retval):
158                 self.remainingdata = ""
159                 if self.run == 0:
160                         self.run = 1
161                         if self.type == self.DOWNLOAD:
162                                 self.startIpkgListInstalled()
163                 elif self.run == 1 and self.type == self.DOWNLOAD:
164                         self.run = 2
165                         self.container.execute("ipkg list enigma2-plugin-*")
166                 else:
167                         if len(self.pluginlist) > 0:
168                                 self.updateList()
169                                 self["list"].instance.show()
170                         else:
171                                 self["text"].setText("No new plugins found")
172
173         def dataAvail(self, str):
174                 #prepend any remaining data from the previous call
175                 str = self.remainingdata + str
176                 #split in lines
177                 lines = str.split('\n')
178                 #'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
179                 if len(lines[-1]):
180                         #remember this data for next time
181                         self.remainingdata = lines[-1]
182                         lines = lines[0:-1]
183                 else:
184                         self.remainingdata = ""
185
186                 for x in lines:
187                         plugin = x.split(" - ")
188                         if len(plugin) == 3:
189                                 if self.run == 1 and self.type == self.DOWNLOAD:
190                                         self.installedplugins.append(plugin[0])
191                                 else:
192                                         if plugin[0] not in self.installedplugins:
193                                                 plugin.append(plugin[0][15:])
194
195                                                 self.pluginlist.append(plugin)
196         
197         def updateList(self):
198                 list = []
199                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expandable-plugins.png"))
200                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expanded-plugins.png"))
201                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/verticalline-plugins.png"))
202                 
203                 self.plugins = {}
204                 for x in self.pluginlist:
205                         split = x[3].split('-')
206                         if len(split) < 2:
207                                 continue
208                         if not self.plugins.has_key(split[0]):
209                                 self.plugins[split[0]] = []
210                                 
211                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
212                         
213                 for x in self.plugins.keys():
214                         if x in self.expanded:
215                                 list.append(PluginCategoryComponent(x, expandedIcon))
216                                 list.extend([PluginDownloadComponent(plugin[0], plugin[1]) for plugin in self.plugins[x]])
217                         else:
218                                 list.append(PluginCategoryComponent(x, expandableIcon))
219                 self.list = list
220                 self["list"].l.setList(list)
221