a25eeb7e6694c8ebef7ef749e0219f74c05db14f
[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
5 from Components.ScrollLabel import ScrollLabel
6 from Components.GUIComponent import *
7 from Components.MenuList import MenuList
8 from Plugins.Plugin import PluginDescriptor
9
10 import os
11
12 class UpdatePluginMenu(Screen):
13         skin = """
14                 <screen position="200,100" size="300,250" title="Update..." >
15                         <widget name="menu" position="10,10" size="290,175" scrollbarMode="showOnDemand" />
16                 </screen>"""
17                 
18         def __init__(self, session, args = None):
19                 self.skin = UpdatePluginMenu.skin
20                 Screen.__init__(self, session)
21                 
22                 list = []
23                 list.append((_("Upgrade"), "upgrade"))
24                 list.append((_("Choose source"), None))
25                 list.append((_("Packet management"), "ipkg"))
26                 
27                 self["menu"] = MenuList(list)
28                                 
29                 self["actions"] = ActionMap(["WizardActions", "DirectionActions"], 
30                 {
31                         "ok": self.go,
32                         "back": self.close,
33                 }, -1)
34                 
35         def go(self):
36                 if (self["menu"].l.getCurrentSelection()[1] == "upgrade"):
37                         self.session.openWithCallback(self.runUpgrade, MessageBox, _("Do you want to update your Dreambox?\nAfter pressing OK, please wait!"))
38                 elif (self["menu"].l.getCurrentSelection()[1] == "ipkg"):
39                         self.session.open(Ipkg)
40         
41         def runUpgrade(self, result):
42                 if result:
43                         self.session.open(Upgrade)
44         
45 class Upgrade(Screen):
46         skin = """
47                 <screen position="100,100" size="550,400" title="IPKG upgrade..." >
48                         <widget name="text" position="0,0" size="550,400" font="Regular;15" />
49                 </screen>"""
50                 
51         def __init__(self, session, args = None):
52                 self.skin = Upgrade.skin
53                 Screen.__init__(self, session)
54
55                 self["text"] = ScrollLabel(_("Updating... Please wait... This can take some minutes..."))
56                                 
57                 self["actions"] = ActionMap(["WizardActions", "DirectionActions"], 
58                 {
59                         "ok": self.go,
60                         "back": self.close,
61                         "up": self["text"].pageUp,
62                         "down": self["text"].pageDown
63                 }, -1)
64                 
65                 self.delayTimer = eTimer()
66                 self.delayTimer.timeout.get().append(self.doUpdateDelay)
67                 # WARNING! Don't copy this code! this code could harm your children! It is ugly, bad and must be banned from this world!
68                 # it only exists due to some lack of competence by the core system designers.
69                 self.delayTimer.start(1, 1)
70                 
71         def go(self):
72                 self.close()
73         
74         def doUpdateDelay(self):
75                 lines = os.popen("ipkg update && ipkg upgrade -force-defaults -force-overwrite", "r").readlines()
76                 string = ""
77                 for x in lines:
78                         string += x
79                 self["text"].setText(_("Updating finished. Here is the result:") + "\n\n" + string)
80                         
81 RT_HALIGN_LEFT = 0
82 RT_HALIGN_RIGHT = 1
83 RT_HALIGN_CENTER = 2
84 RT_HALIGN_BLOCK = 4
85
86 RT_VALIGN_TOP = 0
87 RT_VALIGN_CENTER = 8
88 RT_VALIGN_BOTTOM = 16
89
90 def PacketEntryComponent(packet):
91         res = [ packet ]
92         
93         res.append((eListboxPythonMultiContent.TYPE_TEXT, 0, 0,250, 30, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[0]))
94         res.append((eListboxPythonMultiContent.TYPE_TEXT, 250, 0, 200, 30, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[1]))
95         res.append((eListboxPythonMultiContent.TYPE_TEXT, 450, 0, 100, 30, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[2]))
96         return res
97
98 class PacketList(GUIComponent):
99         def __init__(self, list):
100                 GUIComponent.__init__(self)
101                 self.l = eListboxPythonMultiContent()
102                 self.l.setList(list)
103                 self.l.setFont(0, gFont("Regular", 20))
104                 self.l.setFont(1, gFont("Regular", 18))
105         
106         def getCurrent(self):
107                 return self.l.getCurrentSelection()
108         
109         def GUIcreate(self, parent):
110                 self.instance = eListbox(parent)
111                 self.instance.setContent(self.l)
112                 self.instance.setItemHeight(30)
113         
114         def GUIdelete(self):
115                 self.instance.setContent(None)
116                 self.instance = None
117
118         def invalidate(self):
119                 self.l.invalidate()
120
121 class Ipkg(Screen):
122         skin = """
123                 <screen position="100,100" size="550,400" title="IPKG upgrade..." >
124                         <widget name="list" position="0,0" size="550,400" scrollbarMode="showOnDemand" />
125                 </screen>"""
126                 
127         def __init__(self, session, args = None):
128                 self.skin = Ipkg.skin
129                 Screen.__init__(self, session)
130         
131                 list = []
132                 self.list = list
133                 self.fillPacketList()
134
135                 self["list"] = PacketList(self.list)
136                                 
137                 self["actions"] = ActionMap(["WizardActions"], 
138                 {
139                         "ok": self.close,
140                         "back": self.close
141                 }, -1)
142                 
143
144         def fillPacketList(self):
145                 lines = os.popen("ipkg list", "r").readlines()
146                 packetlist = []
147                 for x in lines:
148                         split = x.split(' - ')
149                         packetlist.append([split[0].strip(), split[1].strip()])
150                 
151                 lines = os.popen("ipkg list_installed", "r").readlines()
152                 
153                 installedlist = {}
154                 for x in lines:
155                         split = x.split(' - ')
156                         installedlist[split[0].strip()] = split[1].strip()
157                 
158                 for x in packetlist:
159                         status = ""
160                         if installedlist.has_key(x[0]):
161                                 if installedlist[x[0]] == x[1]:
162                                         status = "installed"
163                                 else:
164                                         status = "upgradable"
165                         self.list.append(PacketEntryComponent([x[0], x[1], status]))
166                 
167         def go(self):
168                 if self.update:
169                         self.session.openWithCallback(self.doUpdate, MessageBox, _("Do you want to update your Dreambox?\nAfter pressing OK, please wait!"))            
170                 else:
171                         self.close()
172         
173         def doUpdateDelay(self):
174                 lines = os.popen("ipkg update && ipkg upgrade", "r").readlines()
175                 string = ""
176                 for x in lines:
177                         string += x
178                 self["text"].setText(_("Updating finished. Here is the result:") + "\n\n" + string)
179                 self.update = False
180                         
181         
182         def doUpdate(self, val = False):
183                 if val == True:
184                         self["text"].setText(_("Updating... Please wait... This can take some minutes..."))
185                         self.delayTimer.start(0, 1)
186                 else:
187                         self.close()
188
189 def UpgradeMain(session):
190         session.open(UpdatePluginMenu)
191
192 def Plugins():
193         return PluginDescriptor(name="Softwareupdate", description="Updates your receiver's software", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=UpgradeMain)