use Input component to change the ipkg feed
[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                                 
110                 self["actions"] = ActionMap(["WizardActions", "DirectionActions"], 
111                 {
112                         "ok": self.go,
113                         "back": self.close,
114                         "up": self["text"].pageUp,
115                         "down": self["text"].pageDown
116                 }, -1)
117                 
118                 self.delayTimer = eTimer()
119                 self.delayTimer.timeout.get().append(self.doUpdateDelay)
120                 # WARNING! Don't copy this code! this code could harm your children! It is ugly, bad and must be banned from this world!
121                 # it only exists due to some lack of competence by the core system designers.
122                 self.delayTimer.start(1, 1)
123                 
124         def go(self):
125                 self.close()
126         
127         def doUpdateDelay(self):
128                 lines = os.popen("ipkg update && ipkg upgrade -force-defaults -force-overwrite", "r").readlines()
129                 string = ""
130                 for x in lines:
131                         string += x
132                 self["text"].setText(_("Updating finished. Here is the result:") + "\n\n" + string)
133                         
134 RT_HALIGN_LEFT = 0
135 RT_HALIGN_RIGHT = 1
136 RT_HALIGN_CENTER = 2
137 RT_HALIGN_BLOCK = 4
138
139 RT_VALIGN_TOP = 0
140 RT_VALIGN_CENTER = 8
141 RT_VALIGN_BOTTOM = 16
142
143 def PacketEntryComponent(packet):
144         res = [ packet ]
145         
146         res.append((eListboxPythonMultiContent.TYPE_TEXT, 0, 0,250, 30, 0, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[0]))
147         res.append((eListboxPythonMultiContent.TYPE_TEXT, 250, 0, 200, 30, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[1]))
148         res.append((eListboxPythonMultiContent.TYPE_TEXT, 450, 0, 100, 30, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, packet[2]))
149         return res
150
151 class PacketList(GUIComponent):
152         def __init__(self, list):
153                 GUIComponent.__init__(self)
154                 self.l = eListboxPythonMultiContent()
155                 self.l.setList(list)
156                 self.l.setFont(0, gFont("Regular", 20))
157                 self.l.setFont(1, gFont("Regular", 18))
158         
159         def getCurrent(self):
160                 return self.l.getCurrentSelection()
161         
162         def GUIcreate(self, parent):
163                 self.instance = eListbox(parent)
164                 self.instance.setContent(self.l)
165                 self.instance.setItemHeight(30)
166         
167         def GUIdelete(self):
168                 self.instance.setContent(None)
169                 self.instance = None
170
171         def invalidate(self):
172                 self.l.invalidate()
173
174 class Ipkg(Screen):
175         skin = """
176                 <screen position="100,100" size="550,400" title="IPKG upgrade..." >
177                         <widget name="list" position="0,0" size="550,400" scrollbarMode="showOnDemand" />
178                 </screen>"""
179                 
180         def __init__(self, session, args = None):
181                 self.skin = Ipkg.skin
182                 Screen.__init__(self, session)
183         
184                 list = []
185                 self.list = list
186                 self.fillPacketList()
187
188                 self["list"] = PacketList(self.list)
189                                 
190                 self["actions"] = ActionMap(["WizardActions"], 
191                 {
192                         "ok": self.close,
193                         "back": self.close
194                 }, -1)
195                 
196
197         def fillPacketList(self):
198                 lines = os.popen("ipkg list", "r").readlines()
199                 packetlist = []
200                 for x in lines:
201                         split = x.split(' - ')
202                         packetlist.append([split[0].strip(), split[1].strip()])
203                 
204                 lines = os.popen("ipkg list_installed", "r").readlines()
205                 
206                 installedlist = {}
207                 for x in lines:
208                         split = x.split(' - ')
209                         installedlist[split[0].strip()] = split[1].strip()
210                 
211                 for x in packetlist:
212                         status = ""
213                         if installedlist.has_key(x[0]):
214                                 if installedlist[x[0]] == x[1]:
215                                         status = "installed"
216                                 else:
217                                         status = "upgradable"
218                         self.list.append(PacketEntryComponent([x[0], x[1], status]))
219                 
220         def go(self):
221                 if self.update:
222                         self.session.openWithCallback(self.doUpdate, MessageBox, _("Do you want to update your Dreambox?\nAfter pressing OK, please wait!"))            
223                 else:
224                         self.close()
225         
226         def doUpdateDelay(self):
227                 lines = os.popen("ipkg update && ipkg upgrade", "r").readlines()
228                 string = ""
229                 for x in lines:
230                         string += x
231                 self["text"].setText(_("Updating finished. Here is the result:") + "\n\n" + string)
232                 self.update = False
233                         
234         
235         def doUpdate(self, val = False):
236                 if val == True:
237                         self["text"].setText(_("Updating... Please wait... This can take some minutes..."))
238                         self.delayTimer.start(0, 1)
239                 else:
240                         self.close()
241
242 def UpgradeMain(session):
243         session.open(UpdatePluginMenu)
244
245 def Plugins():
246         return PluginDescriptor(name="Softwareupdate", description="Updates your receiver's software", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=UpgradeMain)