add custom localization support including german translation
[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                                         (
50                                                 ' '.join([str(x), _("minutes")]),
51                                                 x
52                                         )
53                                         for x in range(1, 10)
54                                  ] + [
55                                         ( _("Custom"), 'custom')
56                                 ]
57                         )
58
59         def confirmStop(self, result):
60                 if result:
61                         # Stop Timer
62                         self.zap_timer.stop()
63
64                         # Reset Vars
65                         self.zap_service = None
66                         self.move_service = None
67
68                         # Clean up if possible
69                         if self.cleanupfnc:
70                                 self.cleanupfnc()
71
72         def choiceCallback(self, result):
73                 result = result and result[1]
74                 if result == "custom":
75                         from Screens.InputBox import InputBox
76                         from Components.Input import Input
77
78                         self.session.openWithCallback(
79                                 self.inputCallback,
80                                 InputBox,
81                                 title=_("How many minutes to wait until zapping back?"),
82                                 text="10",
83                                 maxSize=False,
84                                 type=Input.NUMBER
85                         )
86                 elif result is not None:
87                         self.confirmStart(result)
88                 # Clean up if possible
89                 elif self.cleanupfnc:
90                         self.cleanupfnc()
91
92         def inputCallback(self, result):
93                 if result is not None:
94                         self.confirmStart(int(result))
95                 # Clean up if possible
96                 elif self.cleanupfnc:
97                         self.cleanupfnc()
98
99
100         def confirmStart(self, duration):
101                 # Remind the User of what he just did
102                 self.session.open(
103                         MessageBox,
104                         _("Zapping back in %d Minutes") % (duration),
105                         type = MessageBox.TYPE_INFO,
106                         timeout = 3
107                 )
108
109                 # Keep any service related information (zap_service might not equal move_service -> subservices)
110                 self.zap_service = self.session.nav.getCurrentlyPlayingServiceReference()
111                 self.move_service = self.servicelist.getCurrentSelection()
112                 self.root = self.servicelist.getRoot()
113
114                 #import ServiceReference
115                 #print [str(ServiceReference.ServiceReference(x)) for x in self.servicelist.getCurrentServicePath()]
116                 #print ServiceReference.ServiceReference(self.servicelist.getRoot())
117
118                 # Start Timer
119                 self.zap_timer.startLongTimer(duration*60)
120
121         def zap(self):
122                 if self.zap_service is not None:
123                         if self.root:
124                                 import ServiceReference
125                                 if not self.servicelist.preEnterPath(str(ServiceReference.ServiceReference(self.root))):
126                                         if self.servicelist.isBasePathEqual(self.root):
127                                                 self.servicelist.pathUp()
128                                                 self.servicelist.enterPath(self.root)
129                                         else:
130                                                 currentRoot = self.servicelist.getRoot()
131                                                 if currentRoot is None or currentRoot != self.root:
132                                                         self.servicelist.clearPath()
133                                                         self.servicelist.enterPath(self.root)
134
135                         if self.move_service:
136                                 self.servicelist.setCurrentSelection(self.move_service)
137                                 self.servicelist.zap()
138
139                         # Play zap_service (won't rezap if service equals to move_service)
140                         self.session.nav.playService(self.zap_service)
141
142                         # Reset services
143                         self.zap_service = None
144                         self.move_service = None
145                         self.root = None
146
147                 # Clean up if possible
148                 if self.cleanupfnc:
149                         self.cleanupfnc()
150
151         def shutdown(self):
152                 self.zap_timer.callback.remove(self.zap)
153                 self.zap_timer = None
154