using iNetwork instead of Network, because we cant handle the asyncron way of loading...
[enigma2-plugins.git] / werbezapper / src / WerbeZapper.py
1 # GUI (Screens)
2 from Screens.MessageBox import MessageBox
3
4 # Timer
5 from enigma import eTimer
6
7 class WerbeZapper:
8         """Simple Plugin to automatically zap back to a Service after a given amount
9            of time."""
10
11         def __init__(self, session, servicelist, cleanupfnc = None):
12                 # Save Session&Servicelist
13                 self.session = session
14                 self.servicelist = servicelist
15
16                 # Create Timer
17                 self.zap_timer = eTimer()
18                 self.zap_timer.callback.append(self.zap)
19
20                 # Initialize services
21                 self.zap_service = None
22                 self.move_service = None
23                 self.root = None
24
25                 # Keep Cleanup
26                 self.cleanupfnc = cleanupfnc
27
28         def showSelection(self):
29                 # Check if timer is active
30                 if self.zap_timer.isActive():
31                         # Ask if we should stop the running timer
32                         self.session.openWithCallback(
33                                 self.confirmStop,
34                                 MessageBox,
35                                 _("Timer already running.\nStop it?")
36                         )
37                 else:
38                         from Screens.ChoiceBox import ChoiceBox
39
40                         # Select Timer Length
41                         self.session.openWithCallback(
42                                 self.choiceCallback,
43                                 ChoiceBox,
44                                 _("When to Zap back?"),
45                                 [
46                                         (
47                                                 ' '.join([str(x), _("minutes")]),
48                                                 x
49                                         )
50                                         for x in range(1, 10)
51                                  ] + [
52                                         ( _("Custom"), 'custom')
53                                 ]
54                         )
55
56         def confirmStop(self, result):
57                 if result:
58                         # Stop Timer
59                         self.zap_timer.stop()
60
61                         # Reset Vars
62                         self.zap_service = None
63                         self.move_service = None
64
65                         # Clean up if possible
66                         if self.cleanupfnc:
67                                 self.cleanupfnc()
68
69         def choiceCallback(self, result):
70                 result = result and result[1]
71                 if result == "custom":
72                         from Screens.InputBox import InputBox
73                         from Components.Input import Input
74
75                         self.session.openWithCallback(
76                                 self.inputCallback,
77                                 InputBox,
78                                 title=_("How many minutes to wait until zapping back?"),
79                                 text="10",
80                                 maxSize=False,
81                                 type=Input.NUMBER
82                         )
83                 elif result is not None:
84                         self.confirmStart(result)
85                 # Clean up if possible
86                 elif self.cleanupfnc:
87                         self.cleanupfnc()
88
89         def inputCallback(self, result):
90                 if result is not None:
91                         self.confirmStart(int(result))
92                 # Clean up if possible
93                 elif self.cleanupfnc:
94                         self.cleanupfnc()
95
96
97         def confirmStart(self, duration):
98                 # Remind the User of what he just did
99                 self.session.open(
100                         MessageBox,
101                         _("Zapping back in %d Minutes") % (duration),
102                         type = MessageBox.TYPE_INFO,
103                         timeout = 3
104                 )
105
106                 # Keep any service related information (zap_service might not equal move_service -> subservices)
107                 self.zap_service = self.session.nav.getCurrentlyPlayingServiceReference()
108                 self.move_service = self.servicelist.getCurrentSelection()
109                 self.root = self.servicelist.getRoot()
110                 
111                 import ServiceReference
112                 print [str(ServiceReference.ServiceReference(x)) for x in self.servicelist.getCurrentServicePath()]
113                 print ServiceReference.ServiceReference(self.servicelist.getRoot())
114
115                 # Start Timer
116                 self.zap_timer.startLongTimer(duration*60)
117
118         def zap(self):
119                 if self.zap_service is not None:
120                         if self.root:
121                                 import ServiceReference
122                                 if not self.servicelist.preEnterPath(str(ServiceReference.ServiceReference(self.root))):
123                                         if self.servicelist.isBasePathEqual(self.root):
124                                                 self.servicelist.pathUp()
125                                                 self.servicelist.enterPath(self.root)
126                                         else:
127                                                 currentRoot = self.servicelist.getRoot()
128                                                 if currentRoot is None or currentRoot != self.root:
129                                                         self.servicelist.clearPath()
130                                                         self.servicelist.enterPath(self.root)
131
132                         if self.move_service:
133                                 self.servicelist.setCurrentSelection(self.move_service)
134                                 self.servicelist.zap()
135
136                         # Play zap_service (won't rezap if service equals to move_service)
137                         self.session.nav.playService(self.zap_service)
138
139                         # Reset services
140                         self.zap_service = None
141                         self.move_service = None
142                         self.root = None
143
144                 # Clean up if possible
145                 if self.cleanupfnc:
146                         self.cleanupfnc()
147
148         def shutdown(self):
149                 self.zap_timer.callback.remove(self.zap)
150                 self.zap_timer = None