[satloader]add restart function
[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
9 from Plugins.Plugin import PluginDescriptor
10
11 ###########################################################################
12
13 class Satloader(Screen):
14         framewidth = getDesktop(0).size().width()
15         if framewidth == 1280:
16                 skin = """
17                         <screen position="240,130" size="800,450" title="Satloader Plugin" >
18                                 <ePixmap position="8,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_red_sm.png" alphatest="on" />
19                                 <ePixmap position="206,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_green_sm.png" alphatest="on" />
20                                 <ePixmap position="404,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_yellow_sm.png" alphatest="on" />
21                                 <ePixmap position="602,4" size="190,32" pixmap="/usr/lib/enigma2/python/Plugins/Extensions/Satloader/button_blue_sm.png" alphatest="on" />
22                                 <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" />
23                                 <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" />
24                                 <widget name="config" position="10,40" size="780,360" scrollbarMode="showOnDemand" />
25                                 <widget name="introduction" position="10,410" size="780,30" zPosition="10" font="Regular;22" valign="center" halign="center" />
26                         </screen>"""
27         elif framewidth == 720:
28                 skin = """
29                         <screen position="center,center" size="560,440" title="Satloader Plugin" >
30                                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
31                                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
32                                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
33                                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
34                                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
35                                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
36                                 <widget name="config" position="10,50" size="540,350" scrollbarMode="showOnDemand" />
37                                 <widget name="introduction" position="10,410" size="540,30" zPosition="10" font="Regular;22" valign="center" halign="center" />
38                         </screen>"""
39
40         
41         def __init__(self, session, url=None, path=None):
42                 self.session = session
43
44                 list = []
45                 list.append((_("Kingofsat satellites.xml (no feed)"), "http://satellites.satloader.net/satellites.xml"))
46                 list.append((_("Kingofsat satellites.xml (with feed)"), "http://satellites.satloader.net/feeds.xml"))
47                 list.append((_("Satbeams satellites.xml"), "http://satellites.satloader.net/satbeam.xml"))
48
49                 Screen.__init__(self, session)
50                 self["config"] = MenuList(list)
51                 self["introduction"] = Label(_("Press green or ok button to download satellites.xml"))
52                 self["key_red"] = Label(_("Cancel"))
53                 self["key_green"] = Label(_("OK"))
54                 self["myActionMap"] = ActionMap(["OkCancelActions", "ColorActions"],
55                 {
56                         "ok": self.start,
57                         "cancel": self.cancel,
58                         "red": self.cancel,
59                         "green": self.start,
60                 }, -1)
61
62         def start(self):
63                 getSatfromUrl(self.session, self["config"].l.getCurrentSelection()[1], "/etc/tuxbox/satellites.xml")
64
65         def cancel(self):
66                 print "\n[Satloader] cancel\n"
67                 self.close(None)
68
69 ###########################################################################
70
71 class getSatfromUrl(object):
72         def __init__(self, session, url=None, path=None):
73                 self.session = session
74                 self.download(url, path)
75
76         def download(self, url, path):
77                 downloadPage(url, path).addCallback(self.downloadDone).addErrback(self.downloadError)
78
79         def downloadError(self, raw):
80                 self.session.open(MessageBox, text = _("Error downloading"), type = MessageBox.TYPE_ERROR)
81
82         def downloadDone(self,raw):
83                 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)
84                 restart.setTitle(_("Restart GUI now?"))
85
86         def restart(self, ret):
87                 if ret is True:
88                         self.session.open(TryQuitMainloop, 3)
89                 
90 ###########################################################################
91
92 def main(session, **kwargs):
93         print "\n[Satloader] start\n"   
94         session.open(Satloader)
95
96 ###########################################################################
97
98 def Plugins(**kwargs):
99         return PluginDescriptor(
100                         name="Satloader Plugin",
101                         description="update satellites.xml",
102                         where = PluginDescriptor.WHERE_PLUGINMENU,
103                         fnc=main)