global: 'Dream Multimedia' -> 'Dream Property'
[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 Property 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 #config for lastservice
34 config.startupservice = ConfigSubsection()
35 config.startupservice.lastservice = ConfigText(default = "")
36 config.startupservice.lastroot = ConfigText(default = "")
37 config.startupservice.lastmode = ConfigText(default = "tv")
38 config.startupserviceleavingstandbymode = ConfigSubsection()
39 config.startupserviceleavingstandbymode.lastservice = ConfigText(default = "")
40 config.startupserviceleavingstandbymode.lastroot = ConfigText(default = "")
41 config.startupserviceleavingstandbymode.lastmode = ConfigText(default = "tv")
42
43 def leaveStandby():
44         if config.startupserviceleavingstandbymode.lastservice.value != "" and config.startupserviceleavingstandbymode.lastroot.value != "":
45                 from Screens.InfoBar import InfoBar
46                 if config.startupservice.lastmode.value == "tv":
47                         InfoBar.instance.servicelist.setModeTv()
48                 else:
49                         InfoBar.instance.servicelist.setModeRadio()
50
51 def standbyCounterChanged(configElement):
52         if config.startupserviceleavingstandbymode.lastservice.value != "" and config.startupserviceleavingstandbymode.lastroot.value != "":
53                 from Screens.Standby import inStandby
54                 if inStandby.prev_running_service and (inStandby.prev_running_service.getPath() == "" or inStandby.prev_running_service.getPath()[0] != "/"):
55                         inStandby.prev_running_service = eServiceReference(config.startupserviceleavingstandbymode.lastservice.value)
56                         if config.startupserviceleavingstandbymode.lastmode.value == "tv":
57                                 config_last = config.tv
58                         else:
59                                 config_last = config.radio
60                         config_last.lastservice.value = config.startupserviceleavingstandbymode.lastservice.value
61                         config_last.lastroot.value = config.startupserviceleavingstandbymode.lastroot.value
62                         config_last.save()
63                         inStandby.onClose.append(leaveStandby)
64
65 def main(session, **kwargs):
66         # copy startupservice data to config.tv or config.radio if available
67         if config.startupservice.lastservice.value != "" and config.startupservice.lastroot.value != "":
68                 config.servicelist.lastmode.value = config.startupservice.lastmode.value
69                 config.servicelist.lastmode.save()
70                 if config.startupservice.lastmode.value == "tv":
71                         config.tv.lastservice.value = config.startupservice.lastservice.value
72                         config.tv.lastroot.value = config.startupservice.lastroot.value
73                         config.tv.save()
74                 else:
75                         config.radio.lastservice.value = config.startupservice.lastservice.value
76                         config.radio.lastroot.value = config.startupservice.lastroot.value
77                         config.radio.save()
78         try: startUpServiceInit()
79         except: pass
80         config.misc.standbyCounter.addNotifier(standbyCounterChanged, initial_call = False)
81
82 ###########################################
83 # ChannelContextMenu
84 ###########################################
85 baseChannelContextMenu__init__ = None
86 def startUpServiceInit():
87         global baseChannelContextMenu__init__
88         if baseChannelContextMenu__init__ is None:
89                 baseChannelContextMenu__init__ = ChannelContextMenu.__init__
90         ChannelContextMenu.__init__ = startUpService__init__
91         # new methods
92         ChannelContextMenu.startUpServiceContextMenuCallback = startUpServiceContextMenuCallback
93         ChannelContextMenu.startUpServiceMenuCallback = startUpServiceMenuCallback
94         ChannelContextMenu.setStartUpService = setStartUpService
95         ChannelContextMenu.resetStartUpService = resetStartUpService
96
97 def startUpService__init__(self, session, csel):
98         baseChannelContextMenu__init__(self, session, csel)
99         current = csel.getCurrentSelection()
100         current_root = csel.getRoot()
101         inBouquetRootList = current_root and current_root.getPath().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
102         if csel.bouquet_mark_edit == 0 and not csel.movemode:
103                 if not inBouquetRootList:
104                         if not (current.flags & (eServiceReference.isMarker|eServiceReference.isDirectory)):
105                                 self["menu"].list.insert(1, ChoiceEntryComponent(text = (_("set as startup service"), boundFunction(self.startUpServiceContextMenuCallback,True))))
106                                 self["menu"].list.insert(2, ChoiceEntryComponent(text = (_("reset startup service"), boundFunction(self.startUpServiceContextMenuCallback,False))))
107
108 def startUpServiceContextMenuCallback(self, add):
109         if add:
110                 options = [
111                                 (_("set as startup service after booting..."), boundFunction(self.setStartUpService, config.startupservice)),
112                                 (_("set as startup service after leaving standby mode..."), boundFunction(self.setStartUpService, config.startupserviceleavingstandbymode)),
113                         ]
114         else:
115                 options = [
116                                 (_("reset startup service for booting..."), boundFunction(self.resetStartUpService, config.startupservice)),
117                                 (_("reset startup service for leaving standby mode..."), boundFunction(self.resetStartUpService, config.startupserviceleavingstandbymode)),
118                         ]
119         self.session.openWithCallback(self.startUpServiceMenuCallback, ChoiceBox,list = options)
120
121 def startUpServiceMenuCallback(self, ret):
122         ret and ret[1]()
123
124
125 def setStartUpService(self, configElement):
126         current = self.csel.getCurrentSelection()
127         path = ''
128         for i in self.csel.servicePath:
129                  path += i.toString()
130                  path += ';'
131         if path:
132                 if current.type == eServiceReference.idDVB and current.getData(0) in (2, 10):   
133                         configElement.lastroot.value = path
134                         configElement.lastmode.value = "radio"
135                 else:
136                         configElement.lastroot.value = path
137                         configElement.lastmode.value = "tv"
138                 configElement.lastservice.value = current.toString()
139                 configElement.save()
140                 self.close()
141         else:
142                  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)
143
144 def resetStartUpService(self, configElement):
145         configElement.lastroot.value = ""
146         configElement.lastmode.value = "tv"
147         configElement.lastservice.value = ""
148         configElement.save()
149         self.close()
150
151 def Plugins(**kwargs):
152         return [PluginDescriptor(name="StartUpService", description="set startup service", where = PluginDescriptor.WHERE_SESSIONSTART, fnc=main)]
153