a21dde8041f5b9dff088bf5e870c7c354f7bfcc9
[enigma2.git] / lib / python / Screens / PluginBrowser.py
1 from Screen import Screen
2
3 from enigma import eConsoleAppContainer
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
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                 
77                 if self.type == self.DOWNLOAD:
78                         self["text"] = Label(_("Downloading plugin information. Please wait..."))
79                 elif self.type == self.REMOVE:
80                         self["text"] = Label(_("Getting plugin information. Please wait..."))
81                 
82                 self.run = 0
83                                 
84                 self["actions"] = ActionMap(["WizardActions"], 
85                 {
86                         "ok": self.go,
87                         "back": self.close,
88                 })
89                 
90         def go(self):
91                 print "plugin: installing:", self.pluginlist[self["list"].l.getCurrentSelectionIndex()]
92                 if self.type == self.DOWNLOAD:
93                         self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"" + self.pluginlist[self["list"].l.getCurrentSelectionIndex()][3] + "\"?"))
94                 elif self.type == self.REMOVE:
95                         self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"" + self.pluginlist[self["list"].l.getCurrentSelectionIndex()][3] + "\"?"))
96
97         def runInstall(self, val):
98                 if val:
99                         if self.type == self.DOWNLOAD:
100                                 self.session.openWithCallback(self.installFinished, Console, ["ipkg install " + self.pluginlist[self["list"].l.getCurrentSelectionIndex()][0]])
101                         elif self.type == self.REMOVE:
102                                 self.session.openWithCallback(self.installFinished, Console, ["ipkg remove " + self.pluginlist[self["list"].l.getCurrentSelectionIndex()][0]])
103
104         def setTitle(self):
105                 if self.type == self.DOWNLOAD:
106                         self.session.currentDialog.instance.setTitle(_("Downloadable new plugins"))
107                 elif self.type == self.REMOVE:
108                         self.session.currentDialog.instance.setTitle(_("Remove plugins"))
109
110         def startRun(self):
111                 self["list"].instance.hide()
112                 self.container.execute("ipkg update")
113                 
114         def installFinished(self):
115                 plugins.reReadPluginList(resolveFilename(SCOPE_PLUGINS))
116                 self.close()
117                 
118         def runFinished(self, retval):
119                 if self.run == 0:
120                         self.run = 1
121                         if self.type == self.DOWNLOAD:
122                                 self.container.execute("ipkg list enigma2-plugin-*")
123                         elif self.type == self.REMOVE:
124                                 self.container.execute("ipkg list_installed enigma2-plugin-*")
125                 else:
126                         if len(self.pluginlist) > 0:
127                                 self.updateList()
128                                 self["list"].instance.show()
129                         else:
130                                 self["text"].setText("No plugins found")
131
132         def dataAvail(self, str):
133                 for x in str.split('\n'):
134                         plugin = x.split(" - ")
135                         if len(plugin) == 3:
136                                 plugin.append(plugin[0][15:])
137
138                                 self.pluginlist.append(plugin)
139         
140         def updateList(self):
141                 self.list = []
142                 for x in self.pluginlist:
143                         plugin = PluginDescriptor(name = x[3], description = x[2])
144                         self.list.append(PluginEntryComponent(plugin))
145                 
146                 self["list"].l.setList(self.list)
147