Merge branch 'master' of ssh://drmichael@scm.schwerkraft.elitedvb.net/scmrepos/git...
[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() == "" or inStandby.prev_running_service.getPath()[0] != "/"):
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.lastmode.value = config.startupservice.lastmode.value
72                 config.servicelist.lastmode.save()
73                 if config.startupservice.lastmode.value == "tv":
74                         config.tv.lastservice.value = config.startupservice.lastservice.value
75                         config.tv.lastroot.value = config.startupservice.lastroot.value
76                         config.tv.save()
77                 else:
78                         config.radio.lastservice.value = config.startupservice.lastservice.value
79                         config.radio.lastroot.value = config.startupservice.lastroot.value
80                         config.radio.save()
81         try: startUpServiceInit()
82         except: pass
83         config.misc.standbyCounter.addNotifier(standbyCounterChanged, initial_call = False)
84
85 ###########################################
86 # ChannelContextMenu
87 ###########################################
88 baseChannelContextMenu__init__ = None
89 def startUpServiceInit():
90         global baseChannelContextMenu__init__
91         if baseChannelContextMenu__init__ is None:
92                 baseChannelContextMenu__init__ = ChannelContextMenu.__init__
93         ChannelContextMenu.__init__ = startUpService__init__
94         # new methods
95         ChannelContextMenu.startUpServiceContextMenuCallback = startUpServiceContextMenuCallback
96         ChannelContextMenu.startUpServiceMenuCallback = startUpServiceMenuCallback
97         ChannelContextMenu.setStartUpService = setStartUpService
98         ChannelContextMenu.resetStartUpService = resetStartUpService
99
100 def startUpService__init__(self, session, csel):
101         baseChannelContextMenu__init__(self, session, csel)
102         current = csel.getCurrentSelection()
103         current_root = csel.getRoot()
104         inBouquetRootList = current_root and current_root.getPath().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
105         if csel.bouquet_mark_edit == 0 and not csel.movemode:
106                 if not inBouquetRootList:
107                         if not (current.flags & (eServiceReference.isMarker|eServiceReference.isDirectory)):
108                                 self["menu"].list.insert(1, ChoiceEntryComponent(text = (_("set as startup service"), boundFunction(self.startUpServiceContextMenuCallback,True))))
109                                 self["menu"].list.insert(2, ChoiceEntryComponent(text = (_("reset startup service"), boundFunction(self.startUpServiceContextMenuCallback,False))))
110
111 def startUpServiceContextMenuCallback(self, add):
112         if add:
113                 options = [
114                                 (_("set as startup service after booting..."), boundFunction(self.setStartUpService, config.startupservice)),
115                                 (_("set as startup service after leaving standby mode..."), boundFunction(self.setStartUpService, config.startupserviceleavingstandbymode)),
116                         ]
117         else:
118                 options = [
119                                 (_("reset startup service for booting..."), boundFunction(self.resetStartUpService, config.startupservice)),
120                                 (_("reset startup service for leaving standby mode..."), boundFunction(self.resetStartUpService, config.startupserviceleavingstandbymode)),
121                         ]
122         self.session.openWithCallback(self.startUpServiceMenuCallback, ChoiceBox,list = options)
123
124 def startUpServiceMenuCallback(self, ret):
125         ret and ret[1]()
126
127
128 def setStartUpService(self, configElement):
129         current = self.csel.getCurrentSelection()
130         path = ''
131         for i in self.csel.servicePath:
132                  path += i.toString()
133                  path += ';'
134         if path:
135                 if current.type == eServiceReference.idDVB and current.getData(0) in (2, 10):   
136                         configElement.lastroot.value = path
137                         configElement.lastmode.value = "radio"
138                 else:
139                         configElement.lastroot.value = path
140                         configElement.lastmode.value = "tv"
141                 configElement.lastservice.value = current.toString()
142                 configElement.save()
143                 self.close()
144         else:
145                  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)
146
147 def resetStartUpService(self, configElement):
148         configElement.lastroot.value = ""
149         configElement.lastmode.value = "tv"
150         configElement.lastservice.value = ""
151         configElement.save()
152         self.close()
153
154 def Plugins(**kwargs):
155         return [PluginDescriptor(name="StartUpService", description="set startup service", where = PluginDescriptor.WHERE_SESSIONSTART, fnc=main)]
156