png cleanup by nemesis, source changes.
[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.list = [ ]
55                 self.pluginlist = plugins.getPlugins(PluginDescriptor.WHERE_PLUGINMENU)
56                 for plugin in self.pluginlist:
57                         self.list.append(PluginEntryComponent(plugin))
58                 
59                 self["list"].l.setList(self.list)
60
61         def delete(self):
62                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.REMOVE)
63         
64         def download(self):
65                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.DOWNLOAD)
66
67 class PluginDownloadBrowser(Screen):
68         DOWNLOAD = 0
69         REMOVE = 1
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.get().append(self.runFinished)
78                 self.container.dataAvail.get().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                 if type(sel[0]) is str: # category
110                         if sel[0] in self.expanded:
111                                 self.expanded.remove(sel[0])
112                         else:
113                                 self.expanded.append(sel[0])
114                         self.updateList()
115                 else:
116                         if self.type == self.DOWNLOAD:
117                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"" + sel[0].name + "\"?"))
118                         elif self.type == self.REMOVE:
119                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"" + sel[0].name + "\"?"))
120
121         def runInstall(self, val):
122                 if val:
123                         if self.type == self.DOWNLOAD:
124                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg install " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
125                         elif self.type == self.REMOVE:
126                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg remove " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
127
128         def setWindowTitle(self):
129                 if self.type == self.DOWNLOAD:
130                         self.setTitle(_("Downloadable new plugins"))
131                 elif self.type == self.REMOVE:
132                         self.setTitle(_("Remove plugins"))
133
134         def startRun(self):
135                 self["list"].instance.hide()
136                 if self.type == self.DOWNLOAD:
137                         self.container.execute("ipkg update")
138                 elif self.type == self.REMOVE:
139                         self.run = 1
140                         self.container.execute("ipkg list_installed enigma2-plugin-*")
141                 
142         def installFinished(self):
143                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
144                 self.container.appClosed.get().remove(self.runFinished)
145                 self.container.dataAvail.get().remove(self.dataAvail)
146                 self.close()
147
148         def runFinished(self, retval):
149                 self.remainingdata = ""
150                 if self.run == 0:
151                         self.run = 1
152                         if self.type == self.DOWNLOAD:
153                                 self.container.execute("ipkg list_installed enigma2-plugin-*")
154                 elif self.run == 1 and self.type == self.DOWNLOAD:
155                         self.run = 2
156                         self.container.execute("ipkg list enigma2-plugin-*")
157                 else:
158                         if len(self.pluginlist) > 0:
159                                 self.updateList()
160                                 self["list"].instance.show()
161                         else:
162                                 self["text"].setText("No new plugins found")
163
164         def dataAvail(self, str):
165                 #prepend any remaining data from the previous call
166                 str = self.remainingdata + str
167                 #split in lines
168                 lines = str.split('\n')
169                 #'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
170                 if len(lines[-1]):
171                         #remember this data for next time
172                         self.remainingdata = lines[-1]
173                         lines = lines[0:-1]
174                 else:
175                         self.remainingdata = ""
176
177                 for x in lines:
178                         plugin = x.split(" - ")
179                         if len(plugin) == 3:
180                                 if self.run == 1 and self.type == self.DOWNLOAD:
181                                         self.installedplugins.append(plugin[0])
182                                 else:
183                                         if plugin[0] not in self.installedplugins:
184                                                 plugin.append(plugin[0][15:])
185
186                                                 self.pluginlist.append(plugin)
187         
188         def updateList(self):
189                 self.list = []
190                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expandable-plugins.png"))
191                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expanded-plugins.png"))
192                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/verticalline-plugins.png"))
193                 
194                 self.plugins = {}
195                 for x in self.pluginlist:
196                         split = x[3].split('-')
197                         if len(split) < 2:
198                                 continue
199                         if not self.plugins.has_key(split[0]):
200                                 self.plugins[split[0]] = []
201                                 
202                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
203                         
204                 for x in self.plugins.keys():
205                         if x in self.expanded:
206                                 self.list.append(PluginCategoryComponent(x, expandedIcon))
207                                 for plugin in self.plugins[x]:
208                                         self.list.append(PluginDownloadComponent(plugin[0], plugin[1]))
209                         else:
210                                 self.list.append(PluginCategoryComponent(x, expandableIcon))
211                 self["list"].l.setList(self.list)
212