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