[satloader] Make MenuList as eListboxPythonMultiContent
[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.MultiContent import MultiContentEntryText, MultiContentEntryPixmapAlphaTest
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_VALIGN_CENTER
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,500" 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="config" position="10,40" size="780,400" scrollbarMode="showOnDemand" />
30                                 <widget name="info" position="10,450" size="780,40" zPosition="10" font="Regular;22" valign="center" halign="center" />
31                         </screen>"""
32         elif framewidth == 720:
33                 skin = """
34                         <screen position="center,center" size="560,440" 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="config" position="10,50" size="540,350" scrollbarMode="showOnDemand" />
44                                 <widget name="info" position="10,410" size="540,30" zPosition="10" font="Regular;22" valign="center" halign="center" />
45                         </screen>"""
46
47         def __init__(self, session, url=None, path=None):
48                 self.session = session
49
50                 Screen.__init__(self, session)
51                 self["config"] = SatloaderList(list)
52                 self["info"] = Label()
53                 self["key_red"] = Label(_("Cancel"))
54                 self["key_green"] = Label()
55                 self["key_yellow"] = Label(_("Satellites"))
56                 self["key_blue"] = Label(_("Transponder"))
57                 self["myActionMap"] = ActionMap(["OkCancelActions", "ColorActions"],
58                 {
59                         "ok": self.btnOK,
60                         "cancel": self.btnRed,
61                         "red": self.btnRed,
62                         "green": self.btnGreen,
63                         "yellow": self.btnYellow,
64                         "blue": self.btnBlue
65                 }, -1)
66
67                 self.onLayoutFinish.append(self.btnYellow)
68
69         def btnOK(self):
70                 if self["key_green"].getText() == "OK":
71                         self.downloadList("sat")
72                 else:
73                         self.downloadList("tp")
74
75         def btnRed(self):
76                 print "\n[Satloader] cancel\n"
77                 self.close(None)
78
79         def btnGreen(self):
80                 if self["key_green"].getText() == "OK":
81                         self.downloadList("sat")
82                 else:
83                         self.buildSatellites()
84
85         def btnYellow(self):
86                 png="/usr/share/enigma2/skin_default/icons/lock_off.png"
87                 list = []
88                 list.append((self, MultiContentEntryPixmapAlphaTest(pos=(0, 0), size=(35, 25), png=LoadPixmap(png)), MultiContentEntryText(pos=(40, 3), size=(750, 25), font=0, flags=RT_VALIGN_CENTER, text="Kingofsat satellites.xml"), "http://satellites.satloader.net/satellites.xml"))
89                 list.append((self, MultiContentEntryPixmapAlphaTest(pos=(0, 0), size=(35, 25), png=LoadPixmap(png)), MultiContentEntryText(pos=(40, 3), size=(750, 25), font=0, flags=RT_VALIGN_CENTER, text="Kingofsat satellites.xml (feed)"), "http://satellites.satloader.net/feeds.xml"))
90                 list.append((self, MultiContentEntryPixmapAlphaTest(pos=(0, 0), size=(35, 25), png=LoadPixmap(png)), MultiContentEntryText(pos=(40, 3), size=(750, 25), font=0, flags=RT_VALIGN_CENTER, text="Satbeams satellites.xml"), "http://satellites.satloader.net/satbeam.xml"))
91
92                 self["key_green"].setText("OK")
93                 self["info"].setText("Press green or ok button to download satellites.xml")
94                 self["config"].setList(list)
95
96         def btnBlue(self):
97                 self["info"].setText("Downloading feeds from server ...")
98                 self.downloadList("entry")
99
100         def downloadList(self, key):
101                 if key == "entry":
102                         downloadPage("http://satellites.satloader.net/transponder/transponder.lst", "/tmp/transponder.lst").addCallback(self.downloadListEntryCallback).addErrback(self.downloadListError)
103                 elif key == "sat":
104                         downloadPage(self["config"].l.getCurrentSelection()[3], "/etc/tuxbox/satellites.xml").addCallback(self.downloadListSATCallback).addErrback(self.downloadListError)
105                 elif key == "tp":
106                         if not os.path.exists("/tmp/transponder"):
107                                 os.mkdirs("/tmp/transponder")
108
109                         satname=self["config"].l.getCurrentSelection()[3].split("/")
110                         downloadPage(self["config"].l.getCurrentSelection()[3], "/tmp/transponder/"+satname[4]).addCallback(self.downloadListTPCallback).addErrback(self.downloadListError)
111
112         def downloadListError(self, ret):
113                 self.session.open(MessageBox, text = _("Error downloading!"), type = MessageBox.TYPE_ERROR)
114                 self["info"].setText("Error downloading!")
115
116         def downloadListEntryCallback(self, ret):
117                 png = "/usr/share/enigma2/skin_default/icons/lock_off.png"
118                 self["info"].setText("Parsing Feeds ...")
119
120                 try:
121                         list = []
122                         f = open('/tmp/transponder.lst', 'r')
123                         for line in f.readlines():
124                                 m = line.split(";")
125                                 list.append((self, MultiContentEntryPixmapAlphaTest(pos=(0, 0), size=(35, 25), png=LoadPixmap(png)), MultiContentEntryText(pos=(40, 3), size=(750, 25), font=0, flags=RT_VALIGN_CENTER, text=m[0]), m[1]))
126                         f.close()
127
128                         if list is not None:
129                                 self["key_green"].setText("Build")
130                                 self["info"].setText("Press ok button to download selected transponders")
131                                 self["config"].setList(list)
132                 except Exception, e:
133                         print "Error:", e
134                         self["info"].setText("Error:", e)
135
136         def downloadListSATCallback(self, ret):
137                 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)
138                 restart.setTitle(_("Restart GUI now?"))
139
140         def downloadListTPCallback(self, ret):
141                 self["info"].setText(self["config"].l.getCurrentSelection()[2][7]+" added.")
142
143         def buildSatellites(self):
144                 f = open('/etc/tuxbox/satellites.xml', 'w+')
145                 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')
146                 
147                 for fname in os.listdir("/tmp/transponder"):
148                         f.write(open("/tmp/transponder/"+fname,"rb").read())
149                         os.remove("/tmp/transponder/"+fname)
150
151                 f.write('</satellites>')
152                 f.close()
153                 
154                 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)
155                 restart.setTitle(_("Restart GUI now?"))
156
157         def restart(self, ret):
158                 if ret is True:
159                         self.session.open(TryQuitMainloop, 3)
160
161 class SatloaderList(MenuList):
162         def __init__(self, list):
163                 MenuList.__init__(self, list, False, eListboxPythonMultiContent)
164                 self.l.setItemHeight(25)
165                 self.l.setFont(0, gFont("Regular", 22))
166
167 #######################
168
169 def main(session, **kwargs):
170         print "\n[Satloader] start\n"   
171         session.open(Satloader)
172
173 #######################
174
175 def Plugins(**kwargs):
176         return [
177                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
178                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)
179                 ]