8c2715bf4405d3867e799e694256df9ee8dbb2df
[enigma2.git] / lib / python / Plugins / update / plugin.py
1 from enigma import *
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap, NumberActionMap
5 from Components.ScrollLabel import ScrollLabel
6 from Components.GUIComponent import *
7 from Components.MenuList import MenuList
8 from Components.Input import Input
9 from Plugins.Plugin import PluginDescriptor
10
11 import os
12
13 class UpdatePluginMenu(Screen):
14         skin = """
15                 <screen position="200,100" size="300,250" title="Update..." >
16                         <widget name="menu" position="10,10" size="290,175" scrollbarMode="showOnDemand" />
17                 </screen>"""
18                 
19         def __init__(self, session, args = None):
20                 self.skin = UpdatePluginMenu.skin
21                 Screen.__init__(self, session)
22                 
23                 list = []
24                 list.append((_("Upgrade"), "upgrade"))
25                 list.append((_("Choose source"), "source"))
26                 list.append((_("Packet management"), "ipkg"))
27                 
28                 self["menu"] = MenuList(list)
29                                 
30                 self["actions"] = ActionMap(["WizardActions", "DirectionActions"], 
31                 {
32                         "ok": self.go,
33                         "back": self.close,
34                 }, -1)
35                 
36         def go(self):
37                 if (self["menu"].l.getCurrentSelection()[1] == "upgrade"):
38                         self.session.openWithCallback(self.runUpgrade, MessageBox, _("Do you want to update your Dreambox?\nAfter pressing OK, please wait!"))
39                 elif (self["menu"].l.getCurrentSelection()[1] == "source"):
40                         self.session.open(IPKGSource)
41                 elif (self["menu"].l.getCurrentSelection()[1] == "ipkg"):
42                         self.session.open(Ipkg)
43         
44         def runUpgrade(self, result):
45                 if result:
46                         self.session.open(Upgrade)
47
48 class IPKGSource(Screen):
49         skin = """
50                 <screen position="100,100" size="550,60" title="IPKG source" >
51                         <widget name="text" position="0,0" size="550,25" font="Regular;20" />
52                 </screen>"""
53                 
54         def __init__(self, session, args = None):
55                 self.skin = IPKGSource.skin
56                 Screen.__init__(self, session)
57                 
58                 fp = file('/etc/ipkg/official-feed.conf', 'r')
59                 sources = fp.readlines()
60                 fp.close()
61                 
62                 self["text"] = Input(sources[0], maxSize=False, type=Input.TEXT)
63                                 
64                 self["actions"] = NumberActionMap(["WizardActions", "InputActions"], 
65                 {
66                         "ok": self.go,
67                         "back": self.close,
68                         "left": self.keyLeft,
69                         "right": self.keyRight,
70                         "1": self.keyNumberGlobal,
71                         "2": self.keyNumberGlobal,
72                         "3": self.keyNumberGlobal,
73                         "4": self.keyNumberGlobal,
74                         "5": self.keyNumberGlobal,
75                         "6": self.keyNumberGlobal,
76                         "7": self.keyNumberGlobal,
77                         "8": self.keyNumberGlobal,
78                         "9": self.keyNumberGlobal,
79                         "0": self.keyNumberGlobal
80                 }, -1)
81                 
82         def go(self):
83                 fp = file('/etc/ipkg/official-feed.conf', 'w')
84                 fp.write(self["text"].getText())
85                 fp.close()
86                 self.close()
87                 
88         def keyLeft(self):
89                 self["text"].left()
90         
91         def keyRight(self):
92                 self["text"].right()
93         
94         def keyNumberGlobal(self, number):
95                 print "pressed", number
96                 self["text"].number(number)
97
98 class Upgrade(Screen):
99         skin = """
100                 <screen position="100,100" size="550,400" title="IPKG upgrade..." >
101                         <widget name="text" position="0,0" size="550,400" font="Regular;15" />
102                 </screen>"""
103                 
104         def __init__(self, session, args = None):
105                 self.skin = Upgrade.skin
106                 Screen.__init__(self, session)
107
108                 self["text"] = ScrollLabel(_("Updating... Please wait... This can take some minutes..."))
109                 self["actions"] = ActionMap(["WizardActions", "DirectionActions"], 
110                 {
111                         "ok": self.cancel,
112                         "back": self.cancel,
113                         "up": self["text"].pageUp,
114                         "down": self["text"].pageDown
115                 }, -1)
116                 
117                 self.container = eConsoleAppContainer()
118                 self.run = 0
119                 self.container.appClosed.get().append(self.updateFinished)
120                 self.container.dataAvail.get().append(self.dataAvail)
121                 self.onLayoutFinish.append(self.startUpdate) # dont start before gui is finished
122
123         def startUpdate(self):
124                 self["text"].setText(_("Update Progress:") + "\n\n")
125                 self.container.execute("ipkg update")
126
127         def updateFinished(self, retval):
128                 if self.run == 0:
129                         self.run = 1
130                         self.container.execute("ipkg upgrade")
131                 elif self.run == 1:
132                         str = self["text"].getText()
133                         str += _("Updating finished!!");
134                         self["text"].setText(str)
135                         self.run = 2
136                         
137         def cancel(self):
138                 if self.run == 2:
139                         self.close()
140
141         def dataAvail(self, str):
142                 self["text"].setText(self["text"].getText() + str)
143
144 RT_HALIGN_LEFT = 0
145 RT_HALIGN_RIGHT = 1
146 RT_HALIGN_CENTER = 2
147 RT_HALIGN_BLOCK = 4
148
149 RT_VALIGN_TOP = 0
150 RT_VALIGN_CENTER = 8
151 RT_VALIGN_BOTTOM = 16
152
153 def PacketEntryComponent(packet):
154         res = [ packet ]
155         
156         res.append((eListboxPythonMultiContent.TYPE_TEXT, 0, 0,250, 30, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[0]))
157         res.append((eListboxPythonMultiContent.TYPE_TEXT, 250, 0, 200, 30, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[1]))
158         res.append((eListboxPythonMultiContent.TYPE_TEXT, 450, 0, 100, 30, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[2]))
159         return res
160
161 class PacketList(GUIComponent):
162         def __init__(self, list):
163                 GUIComponent.__init__(self)
164                 self.l = eListboxPythonMultiContent()
165                 self.l.setList(list)
166                 self.l.setFont(0, gFont("Regular", 20))
167                 self.l.setFont(1, gFont("Regular", 18))
168         
169         def getCurrent(self):
170                 return self.l.getCurrentSelection()
171         
172         def GUIcreate(self, parent):
173                 self.instance = eListbox(parent)
174                 self.instance.setContent(self.l)
175                 self.instance.setItemHeight(30)
176         
177         def GUIdelete(self):
178                 self.instance.setContent(None)
179                 self.instance = None
180
181         def invalidate(self):
182                 self.l.invalidate()
183
184 class Ipkg(Screen):
185         skin = """
186                 <screen position="100,100" size="550,400" title="IPKG upgrade..." >
187                         <widget name="list" position="0,0" size="550,400" scrollbarMode="showOnDemand" />
188                 </screen>"""
189                 
190         def __init__(self, session, args = None):
191                 self.skin = Ipkg.skin
192                 Screen.__init__(self, session)
193         
194                 list = []
195                 self.list = list
196                 self.fillPacketList()
197
198                 self["list"] = PacketList(self.list)
199                                 
200                 self["actions"] = ActionMap(["WizardActions"], 
201                 {
202                         "ok": self.close,
203                         "back": self.close
204                 }, -1)
205                 
206
207         def fillPacketList(self):
208                 lines = os.popen("ipkg list", "r").readlines()
209                 packetlist = []
210                 for x in lines:
211                         split = x.split(' - ')
212                         packetlist.append([split[0].strip(), split[1].strip()])
213                 
214                 lines = os.popen("ipkg list_installed", "r").readlines()
215                 
216                 installedlist = {}
217                 for x in lines:
218                         split = x.split(' - ')
219                         installedlist[split[0].strip()] = split[1].strip()
220                 
221                 for x in packetlist:
222                         status = ""
223                         if installedlist.has_key(x[0]):
224                                 if installedlist[x[0]] == x[1]:
225                                         status = "installed"
226                                 else:
227                                         status = "upgradable"
228                         self.list.append(PacketEntryComponent([x[0], x[1], status]))
229                 
230         def go(self):
231                 if self.update:
232                         self.session.openWithCallback(self.doUpdate, MessageBox, _("Do you want to update your Dreambox?\nAfter pressing OK, please wait!"))            
233                 else:
234                         self.close()
235         
236         def doUpdateDelay(self):
237                 lines = os.popen("ipkg update && ipkg upgrade", "r").readlines()
238                 string = ""
239                 for x in lines:
240                         string += x
241                 self["text"].setText(_("Updating finished. Here is the result:") + "\n\n" + string)
242                 self.update = False
243                         
244         
245         def doUpdate(self, val = False):
246                 if val == True:
247                         self["text"].setText(_("Updating... Please wait... This can take some minutes..."))
248                         self.delayTimer.start(0, 1)
249                 else:
250                         self.close()
251
252 def UpgradeMain(session):
253         session.open(UpdatePluginMenu)
254
255 def Plugins():
256         return PluginDescriptor(name="Softwareupdate", description="Updates your receiver's software", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=UpgradeMain)