Partnerbox: add handling for channels with alternatives on Partnerbox client
[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         # new methods
62         EPGSelection.NewPartnerBoxSelected = NewPartnerBoxSelected
63         EPGSelection.GetPartnerboxTimerlistCallback = GetPartnerboxTimerlistCallback
64         EPGSelection.GetPartnerboxTimerlistCallbackError = GetPartnerboxTimerlistCallbackError
65         EPGSelection.CheckRemoteTimer = CheckRemoteTimer
66         EPGSelection.DeleteTimerConfirmed = DeleteTimerConfirmed
67         EPGSelection.DeleteTimerCallback = DeleteTimerCallback
68         EPGSelection.GetPartnerboxTimerlist = GetPartnerboxTimerlist
69         EPGSelection.PartnerboxInit = PartnerboxInit
70
71 def Partnerbox_EPGSelection__init__(self, session, service, zapFunc=None, eventid=None, bouquetChangeCB=None, serviceChangeCB=None):
72         #check if alternatives are defined
73         if service.flags & (eServiceReference.isGroup):
74                 service = eServiceCenter.getInstance().list(eServiceReference("%s" %(service.toString()))).getContent("S")[0]
75         baseEPGSelection__init__(self, session, service, zapFunc, eventid, bouquetChangeCB, serviceChangeCB)
76         self.PartnerboxInit(True)
77
78 def PartnerboxInit(self, filterRef):
79         self.filterRef = filterRef
80         self.partnerboxentry = None
81         partnerboxfunctions.remote_timer_list = []
82         if int(config.plugins.Partnerbox.entriescount.value) >= 1:
83                 try: 
84                         self.partnerboxentry = config.plugins.Partnerbox.Entries[0]
85                         partnerboxfunctions.CurrentIP = self.partnerboxentry.ip.value
86                 except: self.partnerboxentry = None
87         try:self["key_red"].setText(config.plugins.Partnerbox.Entries[0].name.value)
88         except: pass
89         
90
91 def Partnerbox_EPGSelection_zapTo(self): # just used in multiepg
92         if not (self.zapFunc and self.key_red_choice == self.ZAP):
93                 self.session.openWithCallback(self.NewPartnerBoxSelected, PartnerboxEntriesListConfigScreen, 0)
94         else:
95                 baseEPGSelection_zapTo(self)
96
97 def NewPartnerBoxSelected(self, session, what, partnerboxentry = None):
98         if partnerboxentry is not None:
99                 self.partnerboxentry = partnerboxentry
100                 curService = None
101                 if self.type == EPG_TYPE_SINGLE and self.filterRef:
102                         curService = self.currentService.ref.toString()
103                 SetPartnerboxTimerlist(partnerboxentry, curService)
104                 Partnerbox_onSelectionChanged(self)
105                 self["key_red"].setText(partnerboxentry.name.value)
106                 self["list"].l.invalidate() # immer zeichnen, da neue Box ausgewaehlt wurde
107
108 def Partnerbox_onSelectionChanged(self):
109         baseonSelectionChanged(self)
110         self.CheckRemoteTimer()
111
112 def Partnerbox_timerAdd(self):
113         proceed = True
114         if self.key_green_choice == self.REMOVE_TIMER:
115                 cur = self["list"].getCurrent()
116                 event = cur[0]
117                 serviceref = cur[1]
118                 if event is not None:
119                         timerentry = isInTimerList(event.getBeginTime(), event.getDuration(),serviceref.ref.toString(), event.getEventId(), partnerboxfunctions.remote_timer_list)
120                         if timerentry is not None:
121                                 proceed = False
122                                 if int(self.partnerboxentry.enigma.value) == 0:
123                                         name = timerentry.name
124                                 else:
125                                         name = timerentry.description
126                                 self.session.openWithCallback(boundFunction(self.DeleteTimerConfirmed,timerentry), MessageBox, _("Do you really want to delete the timer \n%s ?") % name)
127         if proceed:basetimerAdd(self)
128
129 def Partnerbox_finishedAdd(self, answer):
130         basefinishedAdd(self,answer)
131         self.CheckRemoteTimer()
132
133 def Partnerbox_onCreate(self):
134         baseonCreate(self)
135         self.GetPartnerboxTimerlist()
136
137 def GetPartnerboxTimerlist(self):
138         if self.partnerboxentry is not None:
139                 ip = "%d.%d.%d.%d" % tuple(self.partnerboxentry.ip.value)
140                 port = self.partnerboxentry.port.value
141                 http = "http://%s:%d" % (ip,port)
142                 if int(self.partnerboxentry.enigma.value) == 0:
143                         sCommand = http + "/web/timerlist"
144                 else:
145                         sCommand = http + "/xml/timers"
146                 print "[Partnerbox] %s"%sCommand
147                 sendPartnerBoxWebCommand(sCommand, None,3, "root", self.partnerboxentry.password.value).addCallback(self.GetPartnerboxTimerlistCallback).addErrback(GetPartnerboxTimerlistCallbackError)
148
149
150 def GetPartnerboxTimerlistCallback(self, sxml = None):
151         if sxml is not None:
152                 curService = None
153                 if self.type == EPG_TYPE_SINGLE and self.filterRef:
154                         curService = self.currentService.ref.toString()
155                 if int(self.partnerboxentry.enigma.value) == 0:
156                         partnerboxfunctions.remote_timer_list = FillE2TimerList(sxml, curService)
157                 else:
158                         partnerboxfunctions.remote_timer_list = FillE1TimerList(sxml, curService)
159         if len(partnerboxfunctions.remote_timer_list) != 0:
160                 Partnerbox_onSelectionChanged(self)
161                 self["list"].l.invalidate()
162
163 def GetPartnerboxTimerlistCallbackError(self, error = None):
164         if error is not None:
165                 print str(error.getErrorMessage())
166
167 def CheckRemoteTimer(self):
168         if self.key_green_choice != self.REMOVE_TIMER:
169                 cur = self["list"].getCurrent()
170                 if cur is None:
171                         return
172                 event = cur[0]
173                 serviceref = cur[1]
174                 if event is not None:
175                         timerentry = isInTimerList(event.getBeginTime(), event.getDuration(),serviceref.ref.toString(),event.getEventId(), partnerboxfunctions.remote_timer_list)
176                         if timerentry is not None:
177                                 self["key_green"].setText(_("Remove timer"))
178                                 self.key_green_choice = self.REMOVE_TIMER
179
180 def DeleteTimerConfirmed (self, timerentry, answer):
181         if answer:
182                 ip = "%d.%d.%d.%d" % tuple(self.partnerboxentry.ip.value)
183                 port = self.partnerboxentry.port.value
184                 http = "http://%s:%d" % (ip,port)
185                 if int(self.partnerboxentry.enigma.value) == 0:
186                         sCommand = http + "/web/timerdelete?sRef=" + timerentry.servicereference + "&begin=" + ("%s"%(timerentry.timebegin)) + "&end=" +("%s"%(timerentry.timeend))
187                 else:
188                         sCommand = http + "/deleteTimerEvent?ref=" + timerentry.servicereference + "&start=" + ("%s"%(timerentry.timebegin)) + "&type=" +("%s"%(timerentry.type)) + "&force=yes"
189                 sendPartnerBoxWebCommand(sCommand, None,3, "root", self.partnerboxentry.password.value).addCallback(self.DeleteTimerCallback).addErrback(DeleteTimerCallbackError)
190
191 def DeleteTimerCallback(self, callback = None):
192         curService = None
193         if self.type == EPG_TYPE_SINGLE and self.filterRef:
194                 curService = self.currentService.ref.toString()
195         SetPartnerboxTimerlist(self.partnerboxentry, curService)
196         Partnerbox_onSelectionChanged(self)
197         self["list"].l.invalidate() # immer zeichnen, da ja was geloescht wurde
198
199 def DeleteTimerCallbackError(self, error = None):
200         if error is not None:
201                 self.session.open(MessageBox, str(error.getErrorMessage()),MessageBox.TYPE_INFO)
202