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