* Remove triple dot "..." from main menu entries and main menu plugin names.
[enigma2.git] / lib / python / Screens / PluginBrowser.py
1 from Screen import Screen
2 from Components.Language import language
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, fileExists, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
13 from Tools.LoadPixmap import LoadPixmap
14
15 from time import time
16
17 def languageChanged():
18         plugins.clearPluginList()
19         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
20
21 class PluginBrowser(Screen):
22         def __init__(self, session):
23                 Screen.__init__(self, session)
24                 
25                 self["red"] = Label()
26                 self["green"] = Label()
27                 
28                 self.list = []
29                 self["list"] = PluginList(self.list)
30                 
31                 self["actions"] = ActionMap(["WizardActions"],
32                 {
33                         "ok": self.save,
34                         "back": self.close,
35                 })
36                 self["PluginDownloadActions"] = ActionMap(["ColorActions"],
37                 {
38                         "red": self.delete,
39                         "green": self.download
40                 })
41                 self["SoftwareActions"] = ActionMap(["ColorActions"],
42                 {
43                         "red": self.openExtensionmanager
44                 })
45                 self["PluginDownloadActions"].setEnabled(False)
46                 self["SoftwareActions"].setEnabled(False)
47                 self.onFirstExecBegin.append(self.checkWarnings)
48                 self.onShown.append(self.updateList)
49         
50         def checkWarnings(self):
51                 if len(plugins.warnings):
52                         text = _("Some plugins are not available:\n")
53                         for (pluginname, error) in plugins.warnings:
54                                 text += _("%s (%s)\n") % (pluginname, error)
55                         plugins.resetWarnings()
56                         self.session.open(MessageBox, text = text, type = MessageBox.TYPE_WARNING)
57
58         def save(self):
59                 self.run()
60         
61         def run(self):
62                 plugin = self["list"].l.getCurrentSelection()[0]
63                 plugin(session=self.session)
64                 
65         def updateList(self):
66                 self.pluginlist = plugins.getPlugins(PluginDescriptor.WHERE_PLUGINMENU)
67                 self.list = [PluginEntryComponent(plugin) for plugin in self.pluginlist]
68                 self["list"].l.setList(self.list)
69                 if fileExists(resolveFilename(SCOPE_PLUGINS, "SystemPlugins/SoftwareManager/plugin.py")):
70                         self["red"].setText(_("Manage extensions"))
71                         self["green"].setText("")
72                         self["SoftwareActions"].setEnabled(True)
73                         self["PluginDownloadActions"].setEnabled(False)
74                 else:
75                         self["red"].setText(_("Remove Plugins"))
76                         self["green"].setText(_("Download Plugins"))
77                         self["SoftwareActions"].setEnabled(False)
78                         self["PluginDownloadActions"].setEnabled(True)
79                         
80         def delete(self):
81                 self.session.openWithCallback(self.PluginDownloadBrowserClosed, PluginDownloadBrowser, PluginDownloadBrowser.REMOVE)
82         
83         def download(self):
84                 self.session.openWithCallback(self.PluginDownloadBrowserClosed, PluginDownloadBrowser, PluginDownloadBrowser.DOWNLOAD)
85
86         def PluginDownloadBrowserClosed(self):
87                 self.updateList()
88                 self.checkWarnings()
89
90         def openExtensionmanager(self):
91                 if fileExists(resolveFilename(SCOPE_PLUGINS, "SystemPlugins/SoftwareManager/plugin.py")):
92                         try:
93                                 from Plugins.SystemPlugins.SoftwareManager.plugin import PluginManager
94                         except ImportError:
95                                 self.session.open(MessageBox, _("The Softwaremanagement extension is not installed!\nPlease install it."), type = MessageBox.TYPE_INFO,timeout = 10 )
96                         else:
97                                 self.session.openWithCallback(self.PluginDownloadBrowserClosed, PluginManager)
98
99 class PluginDownloadBrowser(Screen):
100         DOWNLOAD = 0
101         REMOVE = 1
102         lastDownloadDate = None
103
104         def __init__(self, session, type):
105                 Screen.__init__(self, session)
106                 
107                 self.type = type
108                 
109                 self.container = eConsoleAppContainer()
110                 self.container.appClosed.append(self.runFinished)
111                 self.container.dataAvail.append(self.dataAvail)
112                 self.onLayoutFinish.append(self.startRun)
113                 self.onShown.append(self.setWindowTitle)
114                 
115                 self.list = []
116                 self["list"] = PluginList(self.list)
117                 self.pluginlist = []
118                 self.expanded = []
119                 self.installedplugins = []
120                 
121                 if self.type == self.DOWNLOAD:
122                         self["text"] = Label(_("Downloading plugin information. Please wait..."))
123                 elif self.type == self.REMOVE:
124                         self["text"] = Label(_("Getting plugin information. Please wait..."))
125                 
126                 self.run = 0
127
128                 self.remainingdata = ""
129
130                 self["actions"] = ActionMap(["WizardActions"], 
131                 {
132                         "ok": self.go,
133                         "back": self.close,
134                 })
135                 
136         def go(self):
137                 sel = self["list"].l.getCurrentSelection()
138
139                 if sel is None:
140                         return
141
142                 sel = sel[0]
143                 if isinstance(sel, str): # category
144                         if sel in self.expanded:
145                                 self.expanded.remove(sel)
146                         else:
147                                 self.expanded.append(sel)
148                         self.updateList()
149                 else:
150                         if self.type == self.DOWNLOAD:
151                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to download\nthe plugin \"%s\"?") % sel.name)
152                         elif self.type == self.REMOVE:
153                                 self.session.openWithCallback(self.runInstall, MessageBox, _("Do you really want to REMOVE\nthe plugin \"%s\"?") % sel.name)
154
155         def runInstall(self, val):
156                 if val:
157                         if self.type == self.DOWNLOAD:
158                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg install " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
159                         elif self.type == self.REMOVE:
160                                 self.session.openWithCallback(self.installFinished, Console, cmdlist = ["ipkg remove " + "enigma2-plugin-" + self["list"].l.getCurrentSelection()[0].name])
161
162         def setWindowTitle(self):
163                 if self.type == self.DOWNLOAD:
164                         self.setTitle(_("Downloadable new plugins"))
165                 elif self.type == self.REMOVE:
166                         self.setTitle(_("Remove plugins"))
167
168         def startIpkgListInstalled(self):
169                 self.container.execute("ipkg list_installed enigma2-plugin-*")
170
171         def startIpkgListAvailable(self):
172                 self.container.execute("ipkg list enigma2-plugin-*")
173
174         def startRun(self):
175                 self["list"].instance.hide()
176                 if self.type == self.DOWNLOAD:
177                         if not PluginDownloadBrowser.lastDownloadDate or (time() - PluginDownloadBrowser.lastDownloadDate) > 3600:
178                                 # Only update from internet once per hour
179                                 self.container.execute("ipkg update")
180                                 PluginDownloadBrowser.lastDownloadDate = time()
181                         else:
182                                 self.startIpkgListAvailable()
183                 elif self.type == self.REMOVE:
184                         self.run = 1
185                         self.startIpkgListInstalled()
186
187         def installFinished(self):
188                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
189                 self.container.appClosed.remove(self.runFinished)
190                 self.container.dataAvail.remove(self.dataAvail)
191                 self.close()
192
193         def runFinished(self, retval):
194                 self.remainingdata = ""
195                 if self.run == 0:
196                         self.run = 1
197                         if self.type == self.DOWNLOAD:
198                                 self.startIpkgListInstalled()
199                 elif self.run == 1 and self.type == self.DOWNLOAD:
200                         self.run = 2
201                         self.startIpkgListAvailable()
202                 else:
203                         if len(self.pluginlist) > 0:
204                                 self.updateList()
205                                 self["list"].instance.show()
206                         else:
207                                 self["text"].setText("No new plugins found")
208
209         def dataAvail(self, str):
210                 #prepend any remaining data from the previous call
211                 str = self.remainingdata + str
212                 #split in lines
213                 lines = str.split('\n')
214                 #'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
215                 if len(lines[-1]):
216                         #remember this data for next time
217                         self.remainingdata = lines[-1]
218                         lines = lines[0:-1]
219                 else:
220                         self.remainingdata = ""
221
222                 for x in lines:
223                         plugin = x.split(" - ", 2)
224                         if len(plugin) == 3:
225                                 if self.run == 1 and self.type == self.DOWNLOAD:
226                                         if plugin[0] not in self.installedplugins:
227                                                 self.installedplugins.append(plugin[0])
228                                 else:
229                                         if plugin[0] not in self.installedplugins:
230                                                 plugin.append(plugin[0][15:])
231
232                                                 self.pluginlist.append(plugin)
233         
234         def updateList(self):
235                 list = []
236                 expandableIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expandable-plugins.png"))
237                 expandedIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/expanded-plugins.png"))
238                 verticallineIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "skin_default/verticalline-plugins.png"))
239                 
240                 self.plugins = {}
241                 for x in self.pluginlist:
242                         split = x[3].split('-', 1)
243                         if len(split) < 2:
244                                 continue
245                         if not self.plugins.has_key(split[0]):
246                                 self.plugins[split[0]] = []
247                                 
248                         self.plugins[split[0]].append((PluginDescriptor(name = x[3], description = x[2], icon = verticallineIcon), split[1]))
249                         
250                 for x in self.plugins.keys():
251                         if x in self.expanded:
252                                 list.append(PluginCategoryComponent(x, expandedIcon))
253                                 list.extend([PluginDownloadComponent(plugin[0], plugin[1]) for plugin in self.plugins[x]])
254                         else:
255                                 list.append(PluginCategoryComponent(x, expandableIcon))
256                 self.list = list
257                 self["list"].l.setList(list)
258
259 language.addCallback(languageChanged)