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