[satloader] red button label length correction
[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
10 from Plugins.Plugin import PluginDescriptor
11
12 #######################
13
14 class Satloader(Screen):
15         framewidth = getDesktop(0).size().width()
16         if framewidth == 1280:
17                 skin = """
18                         <screen position="240,130" size="800,450" title="Satloader Plugin" >
19                                 <ePixmap position="8,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_red_sm.png" alphatest="on" />
20                                 <ePixmap position="206,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_green_sm.png" alphatest="on" />
21                                 <ePixmap position="404,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_yellow_sm.png" alphatest="on" />
22                                 <ePixmap position="602,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_blue_sm.png" alphatest="on" />
23                                 <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" />
24                                 <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" />
25                                 <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" />
26                                 <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" />
27                                 <widget name="config" position="10,40" size="780,360" scrollbarMode="showOnDemand" />
28                                 <widget name="info" position="10,410" size="780,30" zPosition="10" font="Regular;22" valign="center" halign="center" />
29                         </screen>"""
30         elif framewidth == 720:
31                 skin = """
32                         <screen position="center,center" size="560,440" title="Satloader Plugin" >
33                                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
34                                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
35                                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
36                                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
37                                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
38                                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
39                                 <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
40                                 <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
41                                 <widget name="config" position="10,50" size="540,350" scrollbarMode="showOnDemand" />
42                                 <widget name="info" position="10,410" size="540,30" zPosition="10" font="Regular;22" valign="center" halign="center" />
43                         </screen>"""
44
45         def __init__(self, session, url=None, path=None):
46                 self.session = session
47
48                 Screen.__init__(self, session)
49                 self["config"] = MenuList(list)
50                 self["info"] = Label()
51                 self["key_red"] = Label(_("Cancel"))
52                 self["key_green"] = Label()
53                 self["key_yellow"] = Label(_("Satellites"))
54                 self["key_blue"] = Label(_("Transponder"))
55                 self["myActionMap"] = ActionMap(["OkCancelActions", "ColorActions"],
56                 {
57                         "ok": self.btnOK,
58                         "cancel": self.btnRed,
59                         "red": self.btnRed,
60                         "green": self.btnGreen,
61                         "yellow": self.btnYellow,
62                         "blue": self.btnBlue
63                 }, -1)
64
65                 self.onLayoutFinish.append(self.btnYellow)
66
67         def btnOK(self):
68                 if self["key_green"].getText() == "OK":
69                         self.downloadList("sat")
70                 else:
71                         self.downloadList("tp")
72
73         def btnRed(self):
74                 print "\n[Satloader] cancel\n"
75                 self.close(None)
76
77         def btnGreen(self):
78                 if self["key_green"].getText() == "OK":
79                         self.downloadList("sat")
80                 else:
81                         self.buildSatellites()
82
83         def btnYellow(self):
84                 list = []
85                 list.append((_("Kingofsat satellites.xml (no feed)"), "http://satellites.satloader.net/satellites.xml"))
86                 list.append((_("Kingofsat satellites.xml (with feed)"), "http://satellites.satloader.net/feeds.xml"))
87                 list.append((_("Satbeams satellites.xml"), "http://satellites.satloader.net/satbeam.xml"))
88
89                 self["key_green"].setText("OK")
90                 self["info"].setText("Press green or ok button to download satellites.xml")
91                 self["config"].setList(list)
92
93         def btnBlue(self):
94                 self["info"].setText("Downloading feeds from server ...")
95                 self.downloadList("list")
96
97         def downloadList(self, key):
98                 if key == "sat":
99                         downloadPage(self["config"].l.getCurrentSelection()[1], "/etc/tuxbox/satellites.xml").addCallback(self.downloadListSATCallback).addErrback(self.downloadListError)
100                 elif key == "tp":
101                         downloadPage(self["config"].l.getCurrentSelection()[1], "/tmp/tp_"+self["config"].l.getCurrentSelection()[0]+".xml").addCallback(self.downloadListBUILDCallback).addErrback(self.downloadListError)
102                 elif key == "list":
103                         downloadPage("http://satellites.satloader.net/transponder/transponder.lst", "/tmp/transponder.lst").addCallback(self.downloadListTPCallback).addErrback(self.downloadListError)
104
105         def downloadListError(self, raw):
106                 self.session.open(MessageBox, text = _("Error downloading!"), type = MessageBox.TYPE_ERROR)
107                 self["info"].setText("Error downloading!")
108
109         def downloadListSATCallback(self, raw):
110                 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)
111                 restart.setTitle(_("Restart GUI now?"))
112
113         def downloadListTPCallback(self, raw):
114                 self["info"].setText("Parsing Feeds ...")
115
116                 try:
117                         list = []
118                         f = open('/tmp/transponder.lst', 'r')
119                         for line in f.readlines():
120                                 m = line.split(";")
121                                 list.append((_(m[0]), m[1]))
122                         f.close()
123                         
124                         if list is not None:
125                                 self["key_green"].setText("Build")
126                                 self["info"].setText("Press ok button to download selected transponders")
127                                 self["config"].setList(list)
128                 except Exception, e:
129                         print "Error:", e
130                         self["info"].setText("Error:", e)
131
132         def downloadListBUILDCallback(self, raw):
133                 self["info"].setText(self["config"].l.getCurrentSelection()[0] + " added.")
134
135         def restart(self, ret):
136                 if ret is True:
137                         self.session.open(TryQuitMainloop, 3)
138
139         def buildSatellites(self):
140                 self.session.open(MessageBox, text = _("coming soon ..."), type = MessageBox.TYPE_INFO)
141
142 #######################
143
144 def main(session, **kwargs):
145         print "\n[Satloader] start\n"   
146         session.open(Satloader)
147
148 #######################
149
150 def Plugins(**kwargs):
151         return [
152                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
153                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)
154                 ]