add prewidgetremove,
[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
24                 # Keep Cleanup
25                 self.cleanupfnc = cleanupfnc
26
27         def showSelection(self):
28                 # Check if timer is active
29                 if self.zap_timer.isActive():
30                         # Ask if we should stop the running timer
31                         self.session.openWithCallback(
32                                 self.confirmStop,
33                                 MessageBox,
34                                 _("Timer already running.\nStop it?")
35                         )
36                 else:
37                         from Screens.ChoiceBox import ChoiceBox
38
39                         # Select Timer Length
40                         self.session.openWithCallback(
41                                 self.choiceCallback,
42                                 ChoiceBox,
43                                 _("When to Zap back?"),
44                                 [
45                                         (
46                                                 ' '.join([str(x), _("minutes")]),
47                                                 x
48                                         )
49                                         for x in range(1, 10)
50                                  ] + [
51                                         ( _("Custom"), 'custom')
52                                 ]
53                         )
54
55         def confirmStop(self, result):
56                 if result:
57                         # Stop Timer
58                         self.zap_timer.stop()
59
60                         # Reset Vars
61                         self.zap_service = None
62                         self.move_service = None
63
64                         # Clean up if possible
65                         if self.cleanupfnc:
66                                 self.cleanupfnc()
67
68         def choiceCallback(self, result):
69                 result = result and result[1]
70                 if result == "custom":
71                         from Screens.InputBox import InputBox
72                         from Components.Input import Input
73
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