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