Use red button (b-flag) as quickbutton for DM8000
[enigma2-plugins.git] / quickbutton / src / plugin.py
1
2 #  Quickbutton
3 #
4 #  $Id$
5 #
6 #  Coded by Dr.Best (c) 2009
7 #  Support: www.dreambox-tools.info
8 #
9 #  This program is free software; you can redistribute it and/or
10 #  modify it under the terms of the GNU General Public License
11 #  as published by the Free Software Foundation; either version 2
12 #  of the License, or (at your option) any later version.
13 #
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19
20 from Screens.Screen import Screen
21 from Screens.ChannelSelection import ChannelSelection
22 from Plugins.Plugin import PluginDescriptor
23 from Components.ActionMap import ActionMap, HelpableActionMap
24 from Components.PluginComponent import plugins
25 from Plugins.Plugin import PluginDescriptor
26 from Components.ConfigList import ConfigList, ConfigListScreen
27 from Components.config import ConfigSubsection, ConfigText, configfile, ConfigSelection, getConfigListEntry
28 from Components.config import config
29 from Components.Button import Button
30 from Screens.MessageBox import MessageBox
31 from Tools.HardwareInfo import HardwareInfo
32
33 config.plugins.Quickbutton = ConfigSubsection()
34 config.plugins.Quickbutton.red = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
35 config.plugins.Quickbutton.red_b = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
36 config.plugins.Quickbutton.green = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
37 config.plugins.Quickbutton.yellow = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
38 config.plugins.Quickbutton.blue = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
39
40
41 from  Screens.InfoBarGenerics import InfoBarPlugins
42 baseInfoBarPlugins__init__ = None
43 baserunPlugin = None
44 StartOnlyOneTime = False
45 DM8000 = False
46
47
48 def autostart(reason, **kwargs):
49         global baseInfoBarPlugins__init__, baserunPlugin, DM8000
50         if "session" in kwargs:
51                 session = kwargs["session"]
52                 if baseInfoBarPlugins__init__ is None:
53                         baseInfoBarPlugins__init__ = InfoBarPlugins.__init__
54                 if baserunPlugin is None:
55                         baserunPlugin = InfoBarPlugins.runPlugin
56                 InfoBarPlugins.__init__ = InfoBarPlugins__init__
57                 InfoBarPlugins.runPlugin = runPlugin
58                 InfoBarPlugins.greenlong = greenlong
59                 InfoBarPlugins.yellowlong = yellowlong
60                 InfoBarPlugins.redlong = redlong
61                 InfoBarPlugins.bluelong = bluelong
62                 if HardwareInfo().get_device_name() == "dm8000":
63                         DM8000 = True           
64                         InfoBarPlugins.red = red
65
66 def setup(session,**kwargs):
67         session.open(QuickbuttonSetup)
68
69 def Plugins(**kwargs):
70
71         list = [PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = autostart)] 
72         list.append(PluginDescriptor(name="Setup Quickbutton", description=_("setup for Quickbutton"), where = [PluginDescriptor.WHERE_PLUGINMENU], fnc=setup))
73         return list
74
75 def InfoBarPlugins__init__(self):
76         global StartOnlyOneTime
77         if not StartOnlyOneTime: 
78                 StartOnlyOneTime = True
79                 x = {   "green_l": (self.greenlong, _("Assign plugin to long green key pressed")),
80                         "yellow_l": (self.yellowlong, _("Assign plugin to long yellow key pressed")),
81                         "red_l": (self.redlong, _("Assign plugin to long red key pressed")),
82                         "blue_l": (self.bluelong, _("Assign plugin to long blue key pressed"))}
83                 if DM8000:
84                         x["red_b"] = (self.red, _("Assign plugin to red key pressed"))
85                 self["QuickbuttonActions"] = HelpableActionMap(self, "QuickbuttonActions",x)
86         else:
87                 InfoBarPlugins.__init__ = InfoBarPlugins.__init__
88                 InfoBarPlugins.runPlugin = InfoBarPlugins.runPlugin
89                 InfoBarPlugins.greenlong = None
90                 InfoBarPlugins.yellowlong = None
91                 InfoBarPlugins.redlong = None
92                 InfoBarPlugins.bluelong = None
93                 if DM8000:
94                         InfoBarPlugins.red = None
95         baseInfoBarPlugins__init__(self)
96
97 def runPlugin(self, plugin):
98         baserunPlugin(self,plugin)
99
100 def greenlong(self):
101         startPlugin(self,str(config.plugins.Quickbutton.green.value))
102
103 def yellowlong(self):
104         startPlugin(self, str(config.plugins.Quickbutton.yellow.value))
105
106 def redlong(self):
107         startPlugin(self, str(config.plugins.Quickbutton.red.value))
108
109 def bluelong(self):
110         startPlugin(self, str(config.plugins.Quickbutton.blue.value))
111
112 def red(self):
113         startPlugin(self, str(config.plugins.Quickbutton.red_b.value))
114
115 def startPlugin(self,pname):
116         msgText = _("Unknown Error")
117         no_plugin = True
118         if pname != _("Nothing"):
119                 if pname == _("EPG List"):
120                         from Screens.EpgSelection import EPGSelection
121                         self.session.open(EPGSelection, self.session.nav.getCurrentlyPlayingServiceReference())
122                         no_plugin = False
123                 elif pname == _("MediaPlayer"):
124                         try: # falls es nicht installiert ist
125                                 from Plugins.Extensions.MediaPlayer.plugin import MediaPlayer
126                                 self.session.open(MediaPlayer)
127                                 no_plugin = False
128                         except Exception, e:
129                                 msgText = _("Error!\nError Text: %s"%e)
130                 elif pname == _("Plugin browser"):
131                         from Screens.PluginBrowser import PluginBrowser
132                         self.session.open(PluginBrowser)
133                         no_plugin = False
134                 elif pname == _("switch 4:3 content display"):
135                         ar = {  "pillarbox": _("Pillarbox"), 
136                                 "panscan": _("Pan&Scan"),  
137                                 "scale": _("Just Scale")}
138                         switch = { "pillarbox":"panscan", "panscan":"scale", "scale":"pillarbox" }
139                         config.av.policy_43.value =  switch[config.av.policy_43.value]
140                         config.av.policy_43.save()
141                         self.session.open(MessageBox,_("Display 4:3 content as") + " " + ar[config.av.policy_43.value], MessageBox.TYPE_INFO, timeout = 3)
142                         no_plugin = False
143                 else:
144                         plugin = None
145                         for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU]):
146                                 if pname == str(p.name):
147                                         plugin = p
148                         if plugin is not None:
149                                 try:
150                                         runPlugin(self,plugin)
151                                         no_plugin = False
152                                 except Exception, e:
153                                         msgText = _("Error!\nError Text: %s"%e)
154                         else: 
155                                 msgText = _("Plugin not found!")
156         else:
157                 msgText = _("No plugin assigned!")
158         if no_plugin:
159                 self.session.open(MessageBox,msgText, MessageBox.TYPE_INFO)
160
161
162 class QuickbuttonSetup(ConfigListScreen, Screen):
163         skin = """
164                 <screen position="100,100" size="550,400" title="Quickbutton Setup" >
165                         <widget name="config" position="20,10" size="510,330" scrollbarMode="showOnDemand" />
166                         <widget name="key_red" position="0,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
167                         <widget name="key_green" position="140,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
168                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
169                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
170                 </screen>"""
171
172         def __init__(self, session, args = None):
173                 Screen.__init__(self, session)
174                 self["key_red"] = Button(_("Cancel"))
175                 self["key_green"] = Button(_("OK"))
176                 self.entryguilist = []
177                 red_b_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.red_b.value)
178                 red_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.red.value)
179                 green_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.green.value)
180                 yellow_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.yellow.value)
181                 blue_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.blue.value)
182                 # feste Vorgaben...koennte man noch erweitern, da hole ich mir sinnvolle Vorschlaege aus Foren noch ein...
183                 self.entryguilist.append(("0",_("Nothing")))
184                 self.entryguilist.append(("1",_("EPG List")))
185                 self.entryguilist.append(("2",_("MediaPlayer")))
186                 self.entryguilist.append(("3",_("Plugin browser")))
187                 self.entryguilist.append(("4",_("switch 4:3 content display")))
188                 # Vorgaben aus EXTENSIONSMENU, PLUGINMENU
189                 index = 5
190                 for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU]):
191                         self.entryguilist.append((str(index),str(p.name)))
192                         if config.plugins.Quickbutton.red.value == str(p.name):
193                                 red_selectedindex = str(index)
194                         if config.plugins.Quickbutton.red_b.value == str(p.name):
195                                 red_b_selectedindex = str(index)
196                         if config.plugins.Quickbutton.green.value == str(p.name):
197                                 green_selectedindex = str(index)
198                         if config.plugins.Quickbutton.yellow.value == str(p.name):
199                                 yellow_selectedindex = str(index)
200                         if config.plugins.Quickbutton.blue.value == str(p.name):
201                                 blue_selectedindex = str(index)
202                         index = index + 1
203
204                 self.redchoice = ConfigSelection(default = red_selectedindex, choices = self.entryguilist)
205                 self.greenchoice = ConfigSelection(default = green_selectedindex, choices = self.entryguilist)
206                 self.yellowchoice = ConfigSelection(default = yellow_selectedindex, choices = self.entryguilist)
207                 self.bluechoice = ConfigSelection(default = blue_selectedindex, choices = self.entryguilist)
208                 cfglist = [
209                         getConfigListEntry(_("assigned to long red"), self.redchoice),
210                         getConfigListEntry(_("assigned to long green"), self.greenchoice),
211                         getConfigListEntry(_("assigned to long yellow"), self.yellowchoice),
212                         getConfigListEntry(_("assigned to long blue"), self.bluechoice)
213
214                         ]
215                 if DM8000:
216                         self.red_b_choice = ConfigSelection(default = red_b_selectedindex, choices = self.entryguilist)
217                         cfglist.append(getConfigListEntry(_("assigned to red"), self.red_b_choice))
218                 ConfigListScreen.__init__(self, cfglist, session)
219                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
220                 {
221                         "green": self.keySave,
222                         "cancel": self.keyClose,
223                         "ok": self.keySave,
224                 }, -2)
225
226         def getStaticPluginName(self,value):
227                 if value == _("EPG List"):
228                         return "1"
229                 elif value == _("MediaPlayer"):
230                         return "2"
231                 elif value == _("Plugin browser"):
232                         return "3"
233                 elif value == _("switch 4:3 content display"):
234                         return "4"
235                 else:
236                         return "0"
237
238         def keySave(self):
239                 config.plugins.Quickbutton.red.value = self.entryguilist[int(self.redchoice.value)][1]
240                 config.plugins.Quickbutton.green.value = self.entryguilist[int(self.greenchoice.value)][1]
241                 config.plugins.Quickbutton.yellow.value = self.entryguilist[int(self.yellowchoice.value)][1]
242                 config.plugins.Quickbutton.blue.value = self.entryguilist[int(self.bluechoice.value)][1]
243                 if DM8000:
244                         config.plugins.Quickbutton.red_b.value = self.entryguilist[int(self.red_b_choice.value)][1]
245                 config.plugins.Quickbutton.save()
246                 configfile.save()
247                 self.close()
248
249         def keyClose(self):
250                 self.close()