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