new option: set/reset startservice for leaving standby-mode
[enigma2-plugins.git] / startupservice / src / plugin.py
1 #
2 #  StartUpService E2
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2009
7 #  Support: www.dreambox-tools.info
8 #
9 #  This plugin is licensed under the Creative Commons 
10 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
11 #  License. To view a copy of this license, visit
12 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
13 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
14 #
15 #  Alternatively, this plugin may be distributed and executed on hardware which
16 #  is licensed by Dream Multimedia GmbH.
17
18 #  This plugin is NOT free software. It is open source, you are allowed to
19 #  modify it (if you keep the license), but it may not be commercially 
20 #  distributed other than under the conditions noted above.
21 #
22
23 from Plugins.Plugin import PluginDescriptor
24 from Components.config import config, ConfigSubsection, ConfigText
25 from Screens.Screen import Screen
26 from Screens.ChannelSelection import ChannelContextMenu
27 from Screens.ChoiceBox import ChoiceBox
28 from enigma import eServiceReference
29 from Components.ChoiceList import ChoiceEntryComponent
30 from Screens.MessageBox import MessageBox
31 from Tools.BoundFunction import boundFunction
32
33 # for localized messages
34 from . import _
35
36 #config for lastservice
37 config.startupservice = ConfigSubsection()
38 config.startupservice.lastservice = ConfigText(default = "")
39 config.startupservice.lastroot = ConfigText(default = "")
40 config.startupservice.lastmode = ConfigText(default = "tv")
41 config.startupserviceleavingstandbymode = ConfigSubsection()
42 config.startupserviceleavingstandbymode.lastservice = ConfigText(default = "")
43 config.startupserviceleavingstandbymode.lastroot = ConfigText(default = "")
44 config.startupserviceleavingstandbymode.lastmode = ConfigText(default = "tv")
45
46 def leaveStandby():
47         if config.startupserviceleavingstandbymode.lastservice.value != "" and config.startupserviceleavingstandbymode.lastroot.value != "":
48                 from Screens.InfoBar import InfoBar
49                 if config.startupservice.lastmode.value == "tv":
50                         InfoBar.instance.servicelist.setModeTv()
51                 else:
52                         InfoBar.instance.servicelist.setModeRadio()
53
54 def standbyCounterChanged(configElement):
55         if config.startupserviceleavingstandbymode.lastservice.value != "" and config.startupserviceleavingstandbymode.lastroot.value != "":
56                 from Screens.Standby import inStandby
57                 if inStandby.prev_running_service and inStandby.prev_running_service.getPath() == "":
58                         inStandby.prev_running_service = eServiceReference(config.startupserviceleavingstandbymode.lastservice.value)
59                         if config.startupserviceleavingstandbymode.lastmode.value == "tv":
60                                 config_last = config.tv
61                         else:
62                                 config_last = config.radio
63                         config_last.lastservice.value = config.startupserviceleavingstandbymode.lastservice.value
64                         config_last.lastroot.value = config.startupserviceleavingstandbymode.lastroot.value
65                         config_last.save()
66                         inStandby.onClose.append(leaveStandby)
67
68 def main(session, **kwargs):
69         # copy startupservice data to config.tv or config.radio if available
70         if config.startupservice.lastservice.value != "" and config.startupservice.lastroot.value != "":
71                 config.servicelist = ConfigSubsection()
72                 config.servicelist.lastmode = ConfigText(default = "tv")
73                 config.servicelist.lastmode.value = config.startupservice.lastmode.value
74                 config.servicelist.lastmode.save()
75                 if config.startupservice.lastmode.value == "tv":
76                         config.tv = ConfigSubsection()
77                         config.tv.lastservice = ConfigText()
78                         config.tv.lastroot = ConfigText()
79                         config.tv.lastservice.value = config.startupservice.lastservice.value
80                         config.tv.lastroot.value = config.startupservice.lastroot.value
81                         config.tv.save()
82                 else:
83                         config.radio = ConfigSubsection()
84                         config.radio.lastservice = ConfigText()
85                         config.radio.lastroot = ConfigText()
86                         config.radio.lastservice.value = config.startupservice.lastservice.value
87                         config.radio.lastroot.value = config.startupservice.lastroot.value
88                         config.radio.save()
89         try: startUpServiceInit()
90         except: pass
91         config.misc.standbyCounter.addNotifier(standbyCounterChanged, initial_call = False)
92
93 ###########################################
94 # ChannelContextMenu
95 ###########################################
96 baseChannelContextMenu__init__ = None
97 def startUpServiceInit():
98         global baseChannelContextMenu__init__
99         if baseChannelContextMenu__init__ is None:
100                 baseChannelContextMenu__init__ = ChannelContextMenu.__init__
101         ChannelContextMenu.__init__ = startUpService__init__
102         # new methods
103         ChannelContextMenu.startUpServiceContextMenuCallback = startUpServiceContextMenuCallback
104         ChannelContextMenu.startUpServiceMenuCallback = startUpServiceMenuCallback
105         ChannelContextMenu.setStartUpService = setStartUpService
106         ChannelContextMenu.resetStartUpService = resetStartUpService
107
108 def startUpService__init__(self, session, csel):
109         baseChannelContextMenu__init__(self, session, csel)
110         current = csel.getCurrentSelection()
111         current_root = csel.getRoot()
112         inBouquetRootList = current_root and current_root.getPath().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
113         if csel.bouquet_mark_edit == 0 and not csel.movemode:
114                 if not inBouquetRootList:
115                         if not (current.flags & (eServiceReference.isMarker|eServiceReference.isDirectory)):
116                                 self["menu"].list.insert(1, ChoiceEntryComponent(text = (_("set as startup service"), boundFunction(self.startUpServiceContextMenuCallback,True))))
117                                 self["menu"].list.insert(2, ChoiceEntryComponent(text = (_("reset startup service"), boundFunction(self.startUpServiceContextMenuCallback,False))))
118
119 def startUpServiceContextMenuCallback(self, add):
120         if add:
121                 options = [
122                                 (_("set as startup service after booting..."), boundFunction(self.setStartUpService, config.startupservice)),
123                                 (_("set as startup service after leaving standby mode..."), boundFunction(self.setStartUpService, config.startupserviceleavingstandbymode)),
124                         ]
125         else:
126                 options = [
127                                 (_("reset startup service for booting..."), boundFunction(self.resetStartUpService, config.startupservice)),
128                                 (_("reset startup service for leaving standby mode..."), boundFunction(self.resetStartUpService, config.startupserviceleavingstandbymode)),
129                         ]
130         self.session.openWithCallback(self.startUpServiceMenuCallback, ChoiceBox,list = options)
131
132 def startUpServiceMenuCallback(self, ret):
133         ret and ret[1]()
134
135
136 def setStartUpService(self, configElement):
137         current = self.csel.getCurrentSelection()
138         path = ''
139         for i in self.csel.servicePath:
140                  path += i.toString()
141                  path += ';'
142         if path:
143                 if current.type == eServiceReference.idDVB and current.getData(0) in (2, 10):   
144                         configElement.lastroot.value = path
145                         configElement.lastmode.value = "radio"
146                 else:
147                         configElement.lastroot.value = path
148                         configElement.lastmode.value = "tv"
149                 configElement.lastservice.value = current.toString()
150                 configElement.save()
151                 self.close()
152         else:
153                  self.session.openWithCallback(self.close, MessageBox, _("If you see this message, please switch to the service you want to mark as startservice and try again."), MessageBox.TYPE_ERROR)
154
155 def resetStartUpService(self, configElement):
156         configElement.lastroot.value = ""
157         configElement.lastmode.value = "tv"
158         configElement.lastservice.value = ""
159         configElement.save()
160         self.close()
161
162 def Plugins(**kwargs):
163         return [PluginDescriptor(name="StartUpService", description="set startup service", where = PluginDescriptor.WHERE_SESSIONSTART, fnc=main)]
164