intial commit of autotimer, epgrefresh and werbezapper to enigma2-plugins
[enigma2-plugins.git] / werbezapper / src / plugin.py
1 # GUI (Screens)
2 from Screens.MessageBox import MessageBox
3 from Screens.ChoiceBox import ChoiceBox
4
5 # Timer
6 from enigma import eTimer
7
8 # Plugin
9 from Plugins.Plugin import PluginDescriptor
10
11 class WerbeZapper:
12         """Simple Plugin to automatically zap back to a Service after a given amount
13            of time."""
14
15         def __init__(self, session, servicelist):
16                 # Save Session&Servicelist
17                 self.session = session
18                 self.servicelist = servicelist
19
20                 # Create Timer
21                 self.zap_timer = eTimer()
22                 self.zap_timer.timeout.get().append(self.zap)
23
24                 # Initialize services
25                 self.zap_service = None
26                 self.move_service = None
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                         # Select Timer Length
39                         self.session.openWithCallback(
40                                 self.confirmStart,
41                                 ChoiceBox,
42                                 "When to Zap back?",
43                                 [
44                                         (
45                                                 ' '.join([str(x), "Min"]),
46                                                 x
47                                         )
48                                         for x in range(1, 10)
49                                  ]
50                         )
51
52         def confirmStop(self, result):
53                 if result:
54                         # Stop Timer
55                         self.zap_timer.stop()
56
57                         # Reset Vars
58                         self.zap_service = None
59                         self.move_service = None
60
61         def confirmStart(self, result):
62                 if result is not None:
63                         # Show Information to User, Save Service, Start Timer
64                         self.session.open(
65                                 MessageBox,
66                                 ' '.join(["Zapping back in", str(result[1]), "Minutes"]),
67                                 type = MessageBox.TYPE_INFO,
68                                 timeout = 3
69                         )
70
71                         # Keep playing and selected service as we might be playing a service not in servicelist
72                         self.zap_service = self.session.nav.getCurrentlyPlayingServiceReference()
73                         self.move_service = self.servicelist.getCurrentSelection()
74
75                         # Start Timer
76                         self.zap_timer.startLongTimer(result[1]*60)
77
78         def zap(self):
79                 if self.zap_service is not None:
80                         # Check if we know where to move in Servicelist
81                         if self.move_service is not None:
82                                 self.servicelist.setCurrentSelection(self.move_service)
83                                 self.servicelist.zap()
84
85                         # Play zap_service if it is different from move_service
86                         if self.zap_service != self.move_service:
87                                 # Play Service
88                                 self.session.nav.playService(self.zap_service)
89
90                         # Reset services
91                         self.zap_service = None
92                         self.move_service = None
93
94 zapperInstance = None
95
96 # Mainfunction
97 def main(session, servicelist, **kwargs):
98         # Create Instance if none present, show Dialog afterwards
99         global zapperInstance
100         if zapperInstance is None:
101                 zapperInstance = WerbeZapper(session, servicelist)
102         zapperInstance.showSelection()
103
104 def Plugins(**kwargs):
105         return [PluginDescriptor(name="Werbezapper", description="Automatically zaps back to current service after given Time", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)]