[satloader] some improvements
[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="10" font="Regular;22" valign="center" halign="center" />
30                         </screen>"""
31         elif framewidth == 720:
32                 skin = """
33                         <screen position="center,center" size="560,460" title="Satloader Plugin" >
34                                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
35                                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
36                                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
37                                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
38                                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
39                                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
40                                 <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
41                                 <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
42                                 <widget name="list" position="10,50" size="540,350" scrollbarMode="showOnDemand" />
43                                 <widget name="info" position="10,410" size="540,40" zPosition="10" font="Regular;20" valign="center" halign="center" />
44                         </screen>"""
45
46         def __init__(self, session, url=None, path=None):
47                 self.session = session
48
49                 Screen.__init__(self, session)
50                 self.list = SatloaderList([])
51                 self["list"] = self.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 btnRed(self):
70                 print "\n[Satloader] cancel\n"
71                 self.close(None)
72
73         def btnOK(self):
74                 if self["list"].l.getCurrentSelection() is not None:
75                         if self["list"].l.getCurrentSelection()[0][3] == None:
76                                 downloadPage(self["list"].l.getCurrentSelection()[0][1], "/etc/tuxbox/satellites.xml").addCallback(self.downloadListSATCallback).addErrback(self.downloadListError)
77                         else:
78                                 self.list.toggleSelection()
79
80         def btnGreen(self):
81                 if self["list"].l.getCurrentSelection() is not None:
82                         if self["list"].l.getCurrentSelection()[0][3] == None:
83                                 downloadPage(self["list"].l.getCurrentSelection()[0][1], "/etc/tuxbox/satellites.xml").addCallback(self.downloadListSATCallback).addErrback(self.downloadListError)
84                         else:
85                                 list = self.list.getSelectionsList()
86                                 if len(list) is not 0:
87                                         f = open('/etc/tuxbox/satellites.xml', 'w+')
88                                         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')
89                                         for item in list:
90                                                 myfile = open('/tmp/transponder/'+item[1], 'r')
91                                                 f.write(myfile.read())
92                                                 myfile.close()
93                                         f.write('</satellites>')
94                                         f.close()
95                                         
96                                         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)
97                                         restart.setTitle(_("Restart GUI now?"))
98                                 else:
99                                         self["info"].setText("Please select a Transponder")
100
101         def btnYellow(self):
102                 self.list.clearList()
103                 self.list.addSelection("Kingofsat satellites.xml", "http://satellites.satloader.net/satellites.xml", 0, None)
104                 self.list.addSelection("Kingofsat satellites.xml (feed)", "http://satellites.satloader.net/feeds.xml", 1, None)
105                 self.list.addSelection("Satbeams satellites.xml", "http://satellites.satloader.net/satbeam.xml", 2, None)
106                 self["key_green"].setText(_("Install"))
107                 self["info"].setText("Press ok or green button to install satellites.xml")
108
109         def btnBlue(self):
110                 self["info"].setText("Downloading transponders from server ...")
111                 downloadPage("http://satellites.satloader.net/transponder.tar.gz", "/tmp/transponder.tar.gz").addCallback(self.downloadListTPCallback).addErrback(self.downloadListError)
112
113         def downloadListError(self, ret):
114                 self.session.open(MessageBox, text = _("Getting transponders failed!"), type = MessageBox.TYPE_ERROR)
115                 self["info"].setText("Getting transponders failed!!")
116
117         def downloadListSATCallback(self, ret):
118                 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)
119                 restart.setTitle(_("Restart GUI now?"))
120
121         def downloadListTPCallback(self, ret):
122                 self["info"].setText("Downloading succesfull! Parsing ...")
123
124                 try:
125                         if not os.path.exists('/tmp/transponder'):
126                                 os.mkdir('/tmp/transponder', 644)
127
128                         os.system("tar -xf /tmp/transponder.tar.gz -C/tmp/transponder")
129                         self.list.clearList()
130                         idx=0
131                         f = open("/tmp/transponder/transponder.lst", "r")
132                         for line in f.readlines():
133                                 m = line.split(";")
134                                 self.list.addSelection(m[0], m[1], idx, False)
135                                 idx += 1
136                         f.close()
137
138                         if self.list is not None:
139                                 self["key_green"].setText("Build")
140                                 self["info"].setText("Press ok button to select transponders")
141
142                 except Exception, e:
143                         print "Error:", e
144                         self["info"].setText("Parsing failed!:\n"+str(e))
145
146         def restart(self, ret):
147                 if ret is True:
148                         self.session.open(TryQuitMainloop, 3)
149
150
151 class SatloaderList(MenuList):
152         def __init__(self, list):
153                 MenuList.__init__(self, list, False, eListboxPythonMultiContent)
154                 self.l.setItemHeight(25)
155                 self.l.setFont(0, gFont("Regular", 22))
156
157         def clearList(self):
158                 self.list = []
159                 self.setList(self.list)
160
161         def addSelection(self, description, value, index, selected = False):
162                 self.list.append(SatListEntry(description, value, index, selected))
163                 self.setList(self.list)
164
165         def toggleSelection(self):
166                 idx = self.getSelectedIndex()
167                 item = self.list[idx][0]
168                 self.list[idx] = SatListEntry(item[0], item[1], item[2], not item[3])
169                 self.setList(self.list)
170
171         def getSelectionsList(self):
172                 return [ (item[0][0], item[0][1], item[0][2]) for item in self.list if item[0][3] ]
173
174
175 def SatListEntry(description, value, index, selected):
176         if selected == None:
177                 res = [
178                         (description, value, index, selected),
179                         (eListboxPythonMultiContent.TYPE_TEXT, 10, 0, 760, 25, 0, RT_HALIGN_LEFT, description)
180                 ]
181                 return res
182         else:
183                 res = [
184                         (description, value, index, selected),
185                         (eListboxPythonMultiContent.TYPE_TEXT, 40, 0, 730, 25, 0, RT_HALIGN_LEFT, description)
186                 ]
187                 if selected == True:
188                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 0, 25, 24,  LoadPixmap(cached=True, path="/usr/share/enigma2/skin_default/icons/lock_on.png")))
189                 elif selected == False:
190                         res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 10, 0, 25, 24,  LoadPixmap(cached=True, path="/usr/share/enigma2/skin_default/icons/lock_off.png")))
191                 return res
192
193
194 #######################
195
196 def main(session, **kwargs):
197         print "\n[Satloader] start\n"   
198         session.open(Satloader)
199
200 #######################
201
202 def Plugins(**kwargs):
203         return [
204                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
205                 PluginDescriptor(name="Satloader Plugin", description="update satellites.xml", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)
206                 ]