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