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