41b3f32eda0c2c2703885050385fe93a631a494a
[vuplus_dvbapp] / lib / python / Screens / PluginBrowser.py
1 from Screen import Screen
2
3 from enigma import eConsoleAppContainer, loadPNG
4
5 from Components.MenuList import MenuList
6 from Components.ActionMap import ActionMap
7 from Components.PluginComponent import plugins
8 from Components.PluginList import *
9 from Components.config import config
10 from Components.Label import Label
11 from Screens.MessageBox import MessageBox
12 from Screens.Console import Console
13 from Plugins.Plugin import PluginDescriptor
14 from Tools.Directories import resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
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                 
35         def save(self):
36                 #self.close()
37                 self.run()
38         
39         def run(self):
40                 plugin = self["list"].l.getCurrentSelection()[0]
41                 
42                 plugin(session=self.session)
43                 
44         def updateList(self):
45                 self.list = [ ]
46                 self.pluginlist = plugins.getPlugins(PluginDescriptor.WHERE_PLUGINMENU)
47                 for plugin in self.pluginlist:
48                         self.list.append(PluginEntryComponent(plugin))
49                 
50                 self["list"].l.setList(self.list)
51
52         def delete(self):
53                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.REMOVE)
54         
55         def download(self):
56                 self.session.openWithCallback(self.updateList, PluginDownloadBrowser, PluginDownloadBrowser.DOWNLOAD)
57
58 class PluginDownloadBrowser(Screen):
59         DOWNLOAD = 0
60         REMOVE = 1
61         
62         def __init__(self, session, type):
63                 Screen.__init__(self, session)
64                 
65                 self.type = type
66                 
67                 self.container = eConsoleAppContainer()
68                 self.container.appClosed.get().append(self.runFinished)
69                 self.container.dataAvail.get().append(self.dataAvail)
70                 self.onLayoutFinish.append(self.startRun)
71                 self.onShown.append(self.setTitle)
72                 
73                 self.list = []
74                 self["list"] = PluginList(self.list)
75                 self.pluginlist = []
76                 self.expanded = []
77                 
78                 if self.type == self.DOWNLOAD:
79                         self["text"] = Label(_("Downloading plugin information. Please wait..."))
80                 elif self.type == self.REMOVE:
81                         self["text"] = Label(_("Getting plugin information. Please wait..."))
82                 
83                 self.run = 0
84                                 
85                 self["actions"] = ActionMap(["WizardActions"], 
86                 {
87                         "ok": self.go,
88                         "back": self.close,
89                 })
90                 
91         def go(self):
92                 if type(self["list"].l.getCurrentSelection()[0]) is str: # category
93                         if self["list"].l.getCurrentSelection()[0] in self.expanded:
94                                 self.expanded.remove(self["list"].l.getCurrentSelection()[0])
95                         else:
96                                 self.expanded.append(self["list"].l.getCurrentSelection()[0])
97                         self.updateList()
98                 else:
99                         if self.type == self.DOWNLOAD:
100                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"" + self["list"].l.getCurrentSelection()[0].name + "\"?"))
101                         elif self.type == self.REMOVE:
102                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"" + self["list"].l.getCurrentSelection()[0].name + "\"?"))
103
104         def runInstall(self, val):
105                 if val:
106                         if self.type == self.DOWNLOAD:
107                                 self.session.openWithCallback(self.installFinished, Console, ["ipkg install " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
108                         elif self.type == self.REMOVE:
109                                 self.session.openWithCallback(self.installFinished, Console, ["ipkg remove " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
110
111         def setTitle(self):
112                 if self.type == self.DOWNLOAD:
113                         self.session.currentDialog.instance.setTitle(_("Downloadable new plugins"))
114                 elif self.type == self.REMOVE:
115                         self.session.currentDialog.instance.setTitle(_("Remove plugins"))
116
117         def startRun(self):
118                 self["list"].instance.hide()
119                 if self.type == self.DOWNLOAD:
120                         self.container.execute("ipkg update")
121                 elif self.type == self.REMOVE:
122                         self.run = 1
123                         self.container.execute("ipkg list_installed enigma2-plugin-*")
124                 
125         def installFinished(self):
126                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
127                 self.close()
128                 
129         def runFinished(self, retval):
130                 if self.run == 0:
131                         self.run = 1
132                         if self.type == self.DOWNLOAD:
133                                 self.container.execute("ipkg list enigma2-plugin-*")
134                 else:
135                         if len(self.pluginlist) > 0:
136                                 self.updateList()
137                                 self["list"].instance.show()
138                         else:
139                                 self["text"].setText("No plugins found")
140
141         def dataAvail(self, str):
142                 for x in str.split('\n'):
143                         plugin = x.split(" - ")
144                         if len(plugin) == 3:
145                                 plugin.append(plugin[0][15:])
146
147                                 self.pluginlist.append(plugin)
148         
149         def updateList(self):
150                 self.list = []
151                 expandableIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "expandable-plugins.png"))
152                 expandedIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "expanded-plugins.png"))
153                 verticallineIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "verticalline-plugins.png"))
154                 
155                 self.plugins = {}
156                 for x in self.pluginlist:
157                         split = x[3].split('-')
158                         if len(split) < 2:
159                                 continue
160                         if not self.plugins.has_key(split[0]):
161                                 self.plugins[split[0]] = []
162                                 
163                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
164                         
165                 for x in self.plugins.keys():
166                         if x in self.expanded:
167                                 self.list.append(PluginCategoryComponent(x, expandedIcon))
168                                 for plugin in self.plugins[x]:
169                                         self.list.append(PluginDownloadComponent(plugin[0], plugin[1]))
170                         else:
171                                 self.list.append(PluginCategoryComponent(x, expandableIcon))
172                 self["list"].l.setList(self.list)
173