center screens
[enigma2-plugins.git] / partnerbox / src / PartnerboxEPGSelection.py
1
2 #  Partnerbox E2
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2009
7 #  Support: www.dreambox-tools.info
8 #
9 #  This program is free software; you can redistribute it and/or
10 #  modify it under the terms of the GNU General Public License
11 #  as published by the Free Software Foundation; either version 2
12 #  of the License, or (at your option) any later version.
13 #
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19
20 from Screens.Screen import Screen
21 from Screens.MessageBox import MessageBox
22 from Components.config import config
23 from PartnerboxSetup import PartnerboxEntriesListConfigScreen
24 from Screens.EpgSelection import EPGSelection
25 from Components.EpgList import EPG_TYPE_SINGLE, EPG_TYPE_SIMILAR, EPG_TYPE_MULTI
26 from Tools.BoundFunction import boundFunction
27 from PartnerboxFunctions import  SetPartnerboxTimerlist, isInTimerList, sendPartnerBoxWebCommand, FillE1TimerList, FillE2TimerList
28 import PartnerboxFunctions as partnerboxfunctions
29
30 # for localized messages
31 from . import _
32
33 baseEPGSelection__init__ = None
34 baseEPGSelection_zapTo = None
35 baseonSelectionChanged = None
36 basetimerAdd = None
37 basefinishedAdd = None
38 baseonCreate = None
39
40 def Partnerbox_EPGSelectionInit():
41         global baseEPGSelection__init__, baseEPGSelection_zapTo, baseonSelectionChanged, basetimerAdd, basefinishedAdd, baseonCreate
42         if baseEPGSelection__init__ is None:
43                 baseEPGSelection__init__ = EPGSelection.__init__
44         if baseEPGSelection_zapTo is None:
45                 baseEPGSelection_zapTo = EPGSelection.zapTo
46         if baseonSelectionChanged is None:
47                 baseonSelectionChanged = EPGSelection.onSelectionChanged
48         if basetimerAdd is None:
49                 basetimerAdd = EPGSelection.timerAdd
50         if basefinishedAdd is None:
51                 basefinishedAdd = EPGSelection.finishedAdd
52         if baseonCreate is None:
53                 baseonCreate = EPGSelection.onCreate
54
55         EPGSelection.__init__ = Partnerbox_EPGSelection__init__
56         EPGSelection.zapTo = Partnerbox_EPGSelection_zapTo
57         EPGSelection.onSelectionChanged = Partnerbox_onSelectionChanged
58         EPGSelection.timerAdd = Partnerbox_timerAdd
59         EPGSelection.finishedAdd = Partnerbox_finishedAdd
60         EPGSelection.onCreate = Partnerbox_onCreate
61
62
63 def Partnerbox_EPGSelection__init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None, serviceChangeCB=None):
64         self.partnerboxentry = None
65         partnerboxfunctions.remote_timer_list = []
66         if int(config.plugins.Partnerbox.entriescount.value) >= 1:
67                 try: 
68                         self.partnerboxentry = config.plugins.Partnerbox.Entries[0]
69                         partnerboxfunctions.CurrentIP = self.partnerboxentry.ip.value
70                 except: self.partnerboxentry = None
71         baseEPGSelection__init__(self, session, service, zapFunc, eventid, bouquetChangeCB, serviceChangeCB)
72         try:self["key_red"].setText(config.plugins.Partnerbox.Entries[0].name.value)
73         except: pass
74         
75
76 def Partnerbox_EPGSelection_zapTo(self): # just used in multiepg
77         if not (self.zapFunc and self.key_red_choice == self.ZAP):
78                 self.session.openWithCallback(boundFunction(NewPartnerBoxSelected,self), PartnerboxEntriesListConfigScreen, 0)
79         else:
80                 baseEPGSelection_zapTo(self)
81
82 def NewPartnerBoxSelected(self, session, what, partnerboxentry = None):
83         if partnerboxentry is not None:
84                 self.partnerboxentry = partnerboxentry
85                 curService = None
86                 if self.type == EPG_TYPE_SINGLE:
87                         curService = self.currentService.ref.toString()
88                 SetPartnerboxTimerlist(partnerboxentry, curService)
89                 Partnerbox_onSelectionChanged(self)
90                 self["key_red"].setText(partnerboxentry.name.value)
91                 self["list"].l.invalidate() # immer zeichnen, da neue Box ausgewaehlt wurde
92
93 def Partnerbox_onSelectionChanged(self):
94         baseonSelectionChanged(self)
95         CheckRemoteTimer(self)
96
97 def Partnerbox_timerAdd(self):
98         proceed = True
99         if self.key_green_choice == self.REMOVE_TIMER:
100                 cur = self["list"].getCurrent()
101                 event = cur[0]
102                 serviceref = cur[1]
103                 if event is not None:
104                         timerentry = isInTimerList(event.getBeginTime(), event.getDuration(),serviceref.ref.toString(), event.getEventId(), partnerboxfunctions.remote_timer_list)
105                         if timerentry is not None:
106                                 proceed = False
107                                 if int(self.partnerboxentry.enigma.value) == 0:
108                                         name = timerentry.name
109                                 else:
110                                         name = timerentry.description
111                                 self.session.openWithCallback(boundFunction(DeleteTimerConfirmed,self, timerentry), MessageBox, _("Do you really want to delete the timer \n%s ?") % name)
112         if proceed:basetimerAdd(self)
113
114 def Partnerbox_finishedAdd(self, answer):
115         basefinishedAdd(self,answer)
116         CheckRemoteTimer(self)
117
118 def Partnerbox_onCreate(self):
119         baseonCreate(self)
120         if self.partnerboxentry is not None:
121                 ip = "%d.%d.%d.%d" % tuple(self.partnerboxentry.ip.value)
122                 port = self.partnerboxentry.port.value
123                 http = "http://%s:%d" % (ip,port)
124                 if int(self.partnerboxentry.enigma.value) == 0:
125                         sCommand = http + "/web/timerlist"
126                 else:
127                         sCommand = http + "/xml/timers"
128                 print "[Partnerbox] %s"%sCommand
129                 sendPartnerBoxWebCommand(sCommand, None,3, "root", self.partnerboxentry.password.value).addCallback(boundFunction(GetPartnerboxTimerlistCallback,self, int(self.partnerboxentry.enigma.value))).addErrback(boundFunction(GetPartnerboxTimerlistCallbackError,self))
130
131
132 def GetPartnerboxTimerlistCallback(self, enigma_type, sxml = None):
133         if sxml is not None:
134                 curService = None
135                 if self.type == EPG_TYPE_SINGLE:
136                         curService = self.currentService.ref.toString()
137                 if enigma_type == 0:
138                         partnerboxfunctions.remote_timer_list = FillE2TimerList(sxml, curService)
139                 else:
140                         partnerboxfunctions.remote_timer_list = FillE1TimerList(sxml, curService)
141         if len(partnerboxfunctions.remote_timer_list) != 0:
142                 Partnerbox_onSelectionChanged(self)
143                 self["list"].l.invalidate()
144
145 def GetPartnerboxTimerlistCallbackError(self, error = None):
146         if error is not None:
147                 print str(error.getErrorMessage())
148
149 def CheckRemoteTimer(self):
150         if self.key_green_choice != self.REMOVE_TIMER:
151                 cur = self["list"].getCurrent()
152                 if cur is None:
153                         return
154                 event = cur[0]
155                 serviceref = cur[1]
156                 if event is not None:
157                         timerentry = isInTimerList(event.getBeginTime(), event.getDuration(),serviceref.ref.toString(),event.getEventId(), partnerboxfunctions.remote_timer_list)
158                         if timerentry is not None:
159                                 self["key_green"].setText(_("Remove timer"))
160                                 self.key_green_choice = self.REMOVE_TIMER
161
162 def DeleteTimerConfirmed (self, timerentry, answer):
163         if answer:
164                 ip = "%d.%d.%d.%d" % tuple(self.partnerboxentry.ip.value)
165                 port = self.partnerboxentry.port.value
166                 http = "http://%s:%d" % (ip,port)
167                 if int(self.partnerboxentry.enigma.value) == 0:
168                         sCommand = http + "/web/timerdelete?sRef=" + timerentry.servicereference + "&begin=" + ("%s"%(timerentry.timebegin)) + "&end=" +("%s"%(timerentry.timeend))
169                 else:
170                         sCommand = http + "/deleteTimerEvent?ref=" + timerentry.servicereference + "&start=" + ("%s"%(timerentry.timebegin)) + "&type=" +("%s"%(timerentry.type)) + "&force=yes"
171                 sendPartnerBoxWebCommand(sCommand, None,3, "root", self.partnerboxentry.password.value).addCallback(boundFunction(DeleteTimerCallback,self)).addErrback(boundFunction(DeleteTimerCallbackError,self))
172
173 def DeleteTimerCallback(self, callback = None):
174         curService = None
175         if self.type == EPG_TYPE_SINGLE:
176                 curService = self.currentService.ref.toString()
177         SetPartnerboxTimerlist(self.partnerboxentry, curService)
178         Partnerbox_onSelectionChanged(self)
179         self["list"].l.invalidate() # immer zeichnen, da ja was geloescht wurde
180
181 def DeleteTimerCallbackError(self, error = None):
182         if error is not None:
183                 self.session.open(MessageBox, str(error.getErrorMessage()),MessageBox.TYPE_INFO)
184