[satloader] prepare for transponder selection while adding selected transponder to...
[enigma2-plugins.git] / satloader / src / plugin.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3 from Screens.Standby import TryQuitMainloop
4 from Components.MenuList import MenuList
5 from Components.ActionMap import ActionMap
6 from Components.Label import Label
7 from twisted.web.client import downloadPage
8 from enigma import getDesktop, eListbox, eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT
9 from Tools.LoadPixmap import LoadPixmap
10 from Plugins.Plugin import PluginDescriptor
11 import os
12
13 #######################
14
15 class Satloader(Screen):
16         framewidth = getDesktop(0).size().width()
17         if framewidth == 1280:
18                 skin = """
19                         <screen position="240,130" size="800,510" title="Satloader Plugin" >
20                                 <ePixmap position="8,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_red_sm.png" alphatest="on" />
21                                 <ePixmap position="206,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_green_sm.png" alphatest="on" />
22                                 <ePixmap position="404,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_yellow_sm.png" alphatest="on" />
23                                 <ePixmap position="602,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_blue_sm.png" alphatest="on" />
24                                 <widget name="key_red" position="8,4" size="190,32" valign="center" halign="center" zPosition="1" font="Regular;22" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
25                                 <widget name="key_green" position="206,4" size="190,32" valign="center" halign="center" zPosition="1" font="Regular;22" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
26                                 <widget name="key_yellow" position="404,4" size="190,32" valign="center" halign="center" zPosition="1" font="Regular;22" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
27                                 <widget name="key_blue" position="602,4" size="190,32" valign="center" halign="center" zPosition="1" font="Regular;22" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
28                                 <widget name="list" position="10,40" size="780,400" scrollbarMode="showOnDemand" />
29                                 <widget name="info" position="10,450" size="780,50" zPosition="1" font="Regular;22" valign="center" halign="center" />
30                                 <ePixmap position="730,458" size="50,35" zPosition="2" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/key_info.png" alphatest="on" />
31                         </screen>"""
32         elif framewidth == 720:
33                 skin = """
34                         <screen position="center,center" size="560,460" title="Satloader Plugin" >
35                                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
36                                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
37                                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
38                                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
39                                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
40                                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
41                                 <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
42                                 <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
43                                 <widget name="list" position="10,50" size="540,350" scrollbarMode="showOnDemand" />
44                                 <widget name="info" position="10,410" size="540,40" zPosition="1" font="Regular;20" valign="center" halign="center" />
45                                 <ePixmap position="505,418" size="35,25" zPosition="2" pixmap="/usr/share/enigma2/skin_default/buttons/key_info.png" alphatest="on" />
46                         </screen>"""
47
48         def __init__(self, session, url=None, path=None):
49                 self.session = session
50
51                 Screen.__init__(self, session)
52                 self.list = SatloaderList([])
53                 self["list"] = self.list
54                 self["info"] = Label()
55                 self["key_red"] = Label(_("Cancel"))
56                 self["key_green"] = Label(_("Install"))
57                 self["key_yellow"] = Label(_("Satellites"))
58                 self["key_blue"] = Label(_("Multi Sat"))
59                 self["myActionMap"] = ActionMap(["OkCancelActions", "ColorActions", "EPGSelectActions"],
60                 {
61                         "ok": self.btnOK,
62                         "cancel": self.btnRed,
63                         "red": self.btnRed,
64                         "green": self.btnGreen,
65                         "yellow": self.btnYellow,
66                         "blue": self.btnBlue,
67                         "info": self.btnInfo
68                 }, -1)
69
70                 self.onLayoutFinish.append(self.btnYellow)
71
72         def btnRed(self):
73                 print "\n[Satloader] cancel\n"
74                 self.close(None)
75
76         def btnOK(self):
77                 if self["list"].l.getCurrentSelection() is not None:
78                         if self["list"].l.getCurrentSelection()[0][3] == None:
79                                 downloadPage(self["list"].l.getCurrentSelection()[0][1], "/etc/tuxbox/satellites.xml").addCallback(self.downloadListSATCallback).addErrback(self.downloadListError)
80                         else:
81                                 self.list.toggleSelection()
82
83         def btnGreen(self):
84                 if self["list"].l.getCurrentSelection() is not None:
85                         if self["list"].l.getCurrentSelection()[0][3] == None:
86                                 downloadPage(self["list"].l.getCurrentSelection()[0][1], "/etc/tuxbox/satellites.xml").addCallback(self.downloadListSATCallback).addErrback(self.downloadListError)
87                         else:
88                                 list = self.list.getSelectionsList()
89                                 if len(list) is not 0:
90                                         f = open('/etc/tuxbox/satellites.xml', 'w+')
91                                         f.write('<?xml version="1.0" ?>\n<!--\nsatellites.xml generated by Satloader Plugin\n(c)2009-2012 Ismail Demir www.satloader.net\n-->\n\n<satellites>\n')
92                                         for item in list:
93                                                 myfile = open('/tmp/transponder/'+item[1], 'r')
94                                                 f.write(myfile.read())
95                                                 myfile.close()
96                                         f.write('</satellites>')
97                                         f.close()
98                                         
99                                         restart = self.session.openWithCallback(self.restart,MessageBox,_("satellites.xml building finished!")+"\n\n"+_("GUI needs a restart to apply changes.")+"\n"+_("Do you want to restart the GUI now?"), MessageBox.TYPE_YESNO)
100                                         restart.setTitle(_("Restart GUI now?"))
101                                 else:
102                                         self["info"].setText(_("Please select at least one satellite"))
103
104         def btnYellow(self):
105                 self.list.clearList()
106                 self.list.addSelection("Kingofsat satellites.xml", "http://satellites.satloader.net/satellites.xml", 0, None)
107                 self.list.addSelection("Kingofsat satellites.xml (feed)", "http://satellites.satloader.net/feeds.xml", 1, None)
108                 self.list.addSelection("Satbeams satellites.xml", "http://satellites.satloader.net/satbeam.xml", 2, None)
109                 self["key_green"].setText(_("Install"))
110                 self["info"].setText(_("Press ok or green button to install satellites.xml"))
111
112         def btnBlue(self):
113                 self["info"].setText(_("Downloading satellites from server ..."))
114                 downloadPage("http://satellites.satloader.net/transponder.tar.gz", "/tmp/transponder.tar.gz").addCallback(self.downloadListTPCallback).addErrback(self.downloadListError)
115
116         def btnInfo(self):
117                 if self["list"].l.getCurrentSelection() is not None:
118                         if self["list"].l.getCurrentSelection()[0][3] is not None:
119                                 name = self["list"].l.getCurrentSelection()[0][0]
120                                 url = "/tmp/transponder/"+self["list"].l.getCurrentSelection()[0][1]
121                                 self.session.open(SatloaderConfig, name, url)
122                         else:
123                                 self.session.open(MessageBox, _("Only for Multi Sat"), MessageBox.TYPE_INFO)
124
125         def downloadListError(self, ret):
126                 self.session.open(MessageBox, text = _("Downloading satellites failed!"), type = MessageBox.TYPE_ERROR)
127                 self["info"].setText(_("Downloading satellites failed!"))
128
129         def downloadListSATCallback(self, ret):
130                 restart = self.session.openWithCallback(self.restart, MessageBox, _("satellites.xml is up-to-date")+"\n\n"+_("GUI needs a restart to apply changes.")+"\n"+_("Do you want to restart the GUI now?"), MessageBox.TYPE_YESNO)
131                 restart.setTitle(_("Restart GUI now?"))
132
133         def downloadListTPCallback(self, ret):
134                 self["info"].setText(_("Downloading succesfull! Parsing ..."))
135
136                 try:
137                         if not os.path.exists('/tmp/transponder'):
138                                 os.mkdir('/tmp/transponder', 644)
139
140                         os.system("tar -xzf /tmp/transponder.tar.gz -C/tmp/transponder")
141                         os.system("rm -f /tmp/transponder.tar.gz")
142                         self.list.clearList()
143                         idx = 0
144                         f = open("/tmp/transponder/transponder.lst", "r")
145                         for line in f.readlines():
146                                 m = line.split(";")
147                                 self.list.addSelection(m[0], m[1], idx, False)
148                                 idx += 1
149                         f.close()
150
151                         if self.list is not None:
152                                 self["info"].setText(_("Press ok button to select satellite"))
153
154                 except Exception, e:
155                         print "Error:", e
156                         self["info"].setText(_("Parsing failed!")+"\n"+str(e))
157
158         def restart(self, ret):
159                 if ret is True:
160                         self.session.open(TryQuitMainloop, 3)
161
162
163 class SatloaderConfig(Screen):
164         framewidth = getDesktop(0).size().width()
165         if framewidth == 1280:
166                 skin = """
167                         <screen position="240,130" size="800,510" title="Satloader Config" >
168                                 <ePixmap position="8,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_red_sm.png" alphatest="on" />
169                                 <ePixmap position="206,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_green_sm.png" alphatest="on" />
170                                 <ePixmap position="404,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_yellow_sm.png" alphatest="on" />
171                                 <ePixmap position="602,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_blue_sm.png" alphatest="on" />
172                                 <widget name="key_red" position="8,4" size="190,32" valign="center" halign="center" zPosition="1" font="Regular;22" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
173                                 <widget name="key_green" position="206,4" size="190,32" valign="center" halign="center" zPosition="1" font="Regular;22" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
174                                 <widget name="list" position="10,40" size="780,400" scrollbarMode="showOnDemand" />
175                                 <widget name="info" position="10,450" size="780,50" zPosition="10" font="Regular;22" valign="center" halign="center" />
176                         </screen>"""
177         elif framewidth == 720:
178                 skin = """
179                         <screen position="center,center" size="560,460" title="Satloader Config" >
180                                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
181                                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
182                                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
183                                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
184                                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
185                                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
186                                 <widget name="list" position="10,50" size="540,350" scrollbarMode="showOnDemand" />
187                                 <widget name="info" position="10,410" size="540,40" zPosition="10" font="Regular;20" valign="center" halign="center" />
188                         </screen>"""
189
190         def __init__(self, session, name, url):
191                 Screen.__init__(self, session)
192                 self.list = SatloaderList([])
193                 self["list"] = self.list
194                 self["info"] = Label()
195                 self["key_red"] = Label(_("Cancel"))
196                 self["key_green"] = Label(_("Save"))
197                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
198                 {
199                         "ok": self.btnOK,
200                         "cancel": self.btnRed,
201                         "red": self.btnRed,
202                         "green": self.btnGreen
203                 }, -1)
204
205                 try:
206                         f = open(url, "r")
207                         self.list.clearList()
208                         idx = 0
209                         for line in f.readlines():
210                                 if "transponder" in line:
211                                         m = line.split("\"")
212                                         pol = ""
213                                         if m[5] == "0":
214                                                 pol = "H"
215                                         elif m[5] == "1":
216                                                 pol = "V"
217                                         elif m[5] == "2":
218                                                 pol = "L"
219                                         elif m[5] == "3":
220                                                 pol = "R"
221                                         
222                                         text = "TP:" + " " + str(idx+1).zfill(3) + "   " + _("Frequency:") + " " + str(m[1]).zfill(8)[:5] + " " + pol + "   " + _("Symbol Rate:") + " " + str(m[3]).zfill(8)[:5]
223                                         self.list.addSelection(text, url, idx, True)
224                                         idx += 1
225                         f.close()
226
227                 except Exception, e:
228                         print "Error:", e
229                         self["info"].setText(_("Parsing failed!")+"\n"+str(e))
230
231                 self["info"].setText(name)
232
233         def btnRed(self):
234                 print "\n[SatloaderConfig] cancel\n"
235                 self.close(None)
236
237         def btnOK(self):
238                 if self["list"].l.getCurrentSelection() is not None:
239                         if self["list"].l.getCurrentSelection()[0][3] is not None:
240                                 self.list.toggleSelection()
241
242         def btnGreen(self):
243                 self.session.open(MessageBox, _("Not yet implemented"), MessageBox.TYPE_INFO)
244
245
246 class SatloaderList(MenuList):
247         def __init__(self, list):
248                 MenuList.__init__(self, list, False, eListboxPythonMultiContent)
249                 self.l.setItemHeight(25)
250                 self.l.setFont(0, gFont("Regular", 22))
251
252         def clearList(self):
253                 self.list = []
254                 self.setList(self.list)
255
256         def addSelection(self, description, value, index, selected = False):
257                 self.list.append(SatListEntry(description, value, index, selected))
258                 self.setList(self.list)
259
260         def toggleSelection(self):
261                 idx = self.getSelectedIndex()
262                 item = self.list[idx][0]
263                 self.list[idx] = SatListEntry(item[0], item[1], item[2], not item[3])
264                 self.setList(self.list)
265
266         def getSelectionsList(self):
267                 return [ (item[0][0], item[0][1], item[0][2]) for item in self.list if item[0][3] ]
268
269
270 def SatListEntry(description, value, index, selected):
271         if selected == None:
272                 res = [
273                         (description, value, index, selected),
274                         (eListboxPythonMultiContent.TYPE_TEXT, 10, 0, 760, 25, 0, RT_HALIGN_LEFT, description)
275                 ]
276                 return res
277         else:
278                 res = [
279                         (description, value, index, selected),
280                         (eListboxPythonMultiContent.TYPE_TEXT, 40, 0, 730, 25, 0, RT_HALIGN_LEFT, description)
281                 ]
282                 if selected == True:
283                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 0, 25, 24,  LoadPixmap(cached=True, path="/usr/share/enigma2/skin_default/icons/lock_on.png")))
284                 elif selected == False:
285                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 0, 25, 24,  LoadPixmap(cached=True, path="/usr/share/enigma2/skin_default/icons/lock_off.png")))
286                 return res
287
288
289 #######################
290
291 def main(session, **kwargs):
292         print "\n[Satloader] start\n"   
293         session.open(Satloader)
294
295 #######################
296
297 def Plugins(**kwargs):
298         return [
299                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
300                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)
301                 ]