-center configscreen
[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 # for localized messages
33 from . import _
34
35 config.plugins.Quickbutton = ConfigSubsection()
36 config.plugins.Quickbutton.red = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
37 config.plugins.Quickbutton.red_b = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
38 config.plugins.Quickbutton.green = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
39 config.plugins.Quickbutton.yellow = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
40 config.plugins.Quickbutton.blue = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
41
42
43 from  Screens.InfoBarGenerics import InfoBarPlugins
44 baseInfoBarPlugins__init__ = None
45 baserunPlugin = None
46 StartOnlyOneTime = False
47 DM8000 = False
48
49
50 def autostart(reason, **kwargs):
51         global baseInfoBarPlugins__init__, baserunPlugin, DM8000
52         if "session" in kwargs:
53                 session = kwargs["session"]
54                 if baseInfoBarPlugins__init__ is None:
55                         baseInfoBarPlugins__init__ = InfoBarPlugins.__init__
56                 if baserunPlugin is None:
57                         baserunPlugin = InfoBarPlugins.runPlugin
58                 InfoBarPlugins.__init__ = InfoBarPlugins__init__
59                 InfoBarPlugins.runPlugin = runPlugin
60                 InfoBarPlugins.greenlong = greenlong
61                 InfoBarPlugins.yellowlong = yellowlong
62                 InfoBarPlugins.redlong = redlong
63                 InfoBarPlugins.bluelong = bluelong
64                 if HardwareInfo().get_device_name() == "dm8000":
65                         DM8000 = True           
66                         InfoBarPlugins.red = red
67
68 def setup(session,**kwargs):
69         session.open(QuickbuttonSetup)
70
71 def Plugins(**kwargs):
72
73         list = [PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = autostart)] 
74         list.append(PluginDescriptor(name="Setup Quickbutton", description=_("setup for Quickbutton"), where = [PluginDescriptor.WHERE_PLUGINMENU], fnc=setup))
75         return list
76
77 def InfoBarPlugins__init__(self):
78         global StartOnlyOneTime
79         if not StartOnlyOneTime: 
80                 StartOnlyOneTime = True
81                 x = {   "green_l": (self.greenlong, _("Assign plugin to long green key pressed")),
82                         "yellow_l": (self.yellowlong, _("Assign plugin to long yellow key pressed")),
83                         "red_l": (self.redlong, _("Assign plugin to long red key pressed")),
84                         "blue_l": (self.bluelong, _("Assign plugin to long blue key pressed"))}
85                 if DM8000:
86                         x["red_b"] = (self.red, _("Assign plugin to red key pressed"))
87                 self["QuickbuttonActions"] = HelpableActionMap(self, "QuickbuttonActions",x)
88         else:
89                 InfoBarPlugins.__init__ = InfoBarPlugins.__init__
90                 InfoBarPlugins.runPlugin = InfoBarPlugins.runPlugin
91                 InfoBarPlugins.greenlong = None
92                 InfoBarPlugins.yellowlong = None
93                 InfoBarPlugins.redlong = None
94                 InfoBarPlugins.bluelong = None
95                 if DM8000:
96                         InfoBarPlugins.red = None
97         baseInfoBarPlugins__init__(self)
98
99 def runPlugin(self, plugin):
100         baserunPlugin(self,plugin)
101
102 def greenlong(self):
103         startPlugin(self,str(config.plugins.Quickbutton.green.value))
104
105 def yellowlong(self):
106         startPlugin(self, str(config.plugins.Quickbutton.yellow.value))
107
108 def redlong(self):
109         startPlugin(self, str(config.plugins.Quickbutton.red.value))
110
111 def bluelong(self):
112         startPlugin(self, str(config.plugins.Quickbutton.blue.value))
113
114 def red(self):
115         startPlugin(self, str(config.plugins.Quickbutton.red_b.value))
116
117 def startPlugin(self,pname):
118         msgText = _("Unknown Error")
119         no_plugin = True
120         if pname != _("Nothing"):
121                 if pname == _("EPG List"):
122                         from Screens.EpgSelection import EPGSelection
123                         self.session.open(EPGSelection, self.session.nav.getCurrentlyPlayingServiceReference())
124                         no_plugin = False
125                 elif pname == _("MediaPlayer"):
126                         try: # falls es nicht installiert ist
127                                 from Plugins.Extensions.MediaPlayer.plugin import MediaPlayer
128                                 self.session.open(MediaPlayer)
129                                 no_plugin = False
130                         except Exception, e:
131                                 msgText = _("Error!\nError Text: %s"%e)
132                 elif pname == _("Plugin browser"):
133                         from Screens.PluginBrowser import PluginBrowser
134                         self.session.open(PluginBrowser)
135                         no_plugin = False
136                 elif pname == _("switch 4:3 content display"):
137                         ar = {  "pillarbox": _("Pillarbox"), 
138                                 "panscan": _("Pan&Scan"),  
139                                 "scale": _("Just Scale")}
140                         switch = { "pillarbox":"panscan", "panscan":"scale", "scale":"pillarbox" }
141                         config.av.policy_43.value =  switch[config.av.policy_43.value]
142                         config.av.policy_43.save()
143                         self.session.open(MessageBox,_("Display 4:3 content as") + " " + ar[config.av.policy_43.value], MessageBox.TYPE_INFO, timeout = 3)
144                         no_plugin = False
145                 elif pname == _("Timer"):
146                         from Screens.TimerEdit import TimerEditList
147                         self.session.open(TimerEditList)
148                         no_plugin = False
149                 else:
150                         plugin = None
151                         for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU]):
152                                 if pname == str(p.name):
153                                         plugin = p
154                         if plugin is not None:
155                                 try:
156                                         runPlugin(self,plugin)
157                                         no_plugin = False
158                                 except Exception, e:
159                                         msgText = _("Error!\nError Text: %s"%e)
160                         else: 
161                                 msgText = _("Plugin not found!")
162         else:
163                 msgText = _("No plugin assigned!")
164         if no_plugin:
165                 self.session.open(MessageBox,msgText, MessageBox.TYPE_INFO)
166
167
168 class QuickbuttonSetup(ConfigListScreen, Screen):
169         skin = """
170                 <screen position="center,center" size="550,400" title="Quickbutton Setup" >
171                         <widget name="config" position="20,10" size="510,330" scrollbarMode="showOnDemand" />
172                         <widget name="key_red" position="0,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
173                         <widget name="key_green" position="140,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
174                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
175                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
176                 </screen>"""
177
178         def __init__(self, session, args = None):
179                 Screen.__init__(self, session)
180                 self["key_red"] = Button(_("Cancel"))
181                 self["key_green"] = Button(_("OK"))
182                 self.entryguilist = []
183                 red_b_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.red_b.value)
184                 red_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.red.value)
185                 green_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.green.value)
186                 yellow_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.yellow.value)
187                 blue_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.blue.value)
188                 # feste Vorgaben...koennte man noch erweitern, da hole ich mir sinnvolle Vorschlaege aus Foren noch ein...
189                 self.entryguilist.append(("0",_("Nothing")))
190                 self.entryguilist.append(("1",_("EPG List")))
191                 self.entryguilist.append(("2",_("MediaPlayer")))
192                 self.entryguilist.append(("3",_("Plugin browser")))
193                 self.entryguilist.append(("4",_("switch 4:3 content display")))
194                 self.entryguilist.append(("5",_("Timer")))
195                 # Vorgaben aus EXTENSIONSMENU, PLUGINMENU
196                 index = 6
197                 for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU]):
198                         self.entryguilist.append((str(index),str(p.name)))
199                         if config.plugins.Quickbutton.red.value == str(p.name):
200                                 red_selectedindex = str(index)
201                         if config.plugins.Quickbutton.red_b.value == str(p.name):
202                                 red_b_selectedindex = str(index)
203                         if config.plugins.Quickbutton.green.value == str(p.name):
204                                 green_selectedindex = str(index)
205                         if config.plugins.Quickbutton.yellow.value == str(p.name):
206                                 yellow_selectedindex = str(index)
207                         if config.plugins.Quickbutton.blue.value == str(p.name):
208                                 blue_selectedindex = str(index)
209                         index = index + 1
210
211                 self.redchoice = ConfigSelection(default = red_selectedindex, choices = self.entryguilist)
212                 self.greenchoice = ConfigSelection(default = green_selectedindex, choices = self.entryguilist)
213                 self.yellowchoice = ConfigSelection(default = yellow_selectedindex, choices = self.entryguilist)
214                 self.bluechoice = ConfigSelection(default = blue_selectedindex, choices = self.entryguilist)
215                 cfglist = [
216                         getConfigListEntry(_("assigned to long red"), self.redchoice),
217                         getConfigListEntry(_("assigned to long green"), self.greenchoice),
218                         getConfigListEntry(_("assigned to long yellow"), self.yellowchoice),
219                         getConfigListEntry(_("assigned to long blue"), self.bluechoice)
220
221                         ]
222                 if DM8000:
223                         self.red_b_choice = ConfigSelection(default = red_b_selectedindex, choices = self.entryguilist)
224                         cfglist.append(getConfigListEntry(_("assigned to red"), self.red_b_choice))
225                 ConfigListScreen.__init__(self, cfglist, session)
226                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
227                 {
228                         "green": self.keySave,
229                         "cancel": self.keyClose,
230                         "ok": self.keySave,
231                 }, -2)
232
233         def getStaticPluginName(self,value):
234                 if value == _("EPG List"):
235                         return "1"
236                 elif value == _("MediaPlayer"):
237                         return "2"
238                 elif value == _("Plugin browser"):
239                         return "3"
240                 elif value == _("switch 4:3 content display"):
241                         return "4"
242                 if value == _("Timer"):
243                         return "5"
244                 else:
245                         return "0"
246
247         def keySave(self):
248                 config.plugins.Quickbutton.red.value = self.entryguilist[int(self.redchoice.value)][1]
249                 config.plugins.Quickbutton.green.value = self.entryguilist[int(self.greenchoice.value)][1]
250                 config.plugins.Quickbutton.yellow.value = self.entryguilist[int(self.yellowchoice.value)][1]
251                 config.plugins.Quickbutton.blue.value = self.entryguilist[int(self.bluechoice.value)][1]
252                 if DM8000:
253                         config.plugins.Quickbutton.red_b.value = self.entryguilist[int(self.red_b_choice.value)][1]
254                 config.plugins.Quickbutton.save()
255                 configfile.save()
256                 self.close()
257
258         def keyClose(self):
259                 self.close()