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