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