initial checkin of MerlinSkinThemes
[enigma2-plugins.git] / quickbutton / src / plugin.py
1 #  Quickbutton
2 #
3 #  $Id$
4 #
5 #  Coded by Dr.Best (c) 2009
6 #  Support: www.dreambox-tools.info
7 #
8 #  This program is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU General Public License
10 #  as published by the Free Software Foundation; either version 2
11 #  of the License, or (at your option) any later version.
12 #
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17 #
18
19 from Screens.Screen import Screen
20 from Screens.ChannelSelection import ChannelSelection
21 from Plugins.Plugin import PluginDescriptor
22 from Components.ActionMap import ActionMap, HelpableActionMap
23 from Components.PluginComponent import plugins
24 from Plugins.Plugin import PluginDescriptor
25 from Components.ConfigList import ConfigList, ConfigListScreen
26 from Components.config import ConfigSubsection, ConfigText, configfile, ConfigSelection, getConfigListEntry, ConfigYesNo
27 from Components.config import config
28 from Components.Button import Button
29 from Screens.MessageBox import MessageBox
30
31 config.plugins.Quickbutton = ConfigSubsection()
32 config.plugins.Quickbutton.overwritehbbtvredbutton = ConfigYesNo(default = False)
33 config.plugins.Quickbutton.red = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
34 config.plugins.Quickbutton.red_b = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
35 config.plugins.Quickbutton.green = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
36 config.plugins.Quickbutton.yellow = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
37 config.plugins.Quickbutton.blue = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
38
39
40 from  Screens.InfoBarGenerics import InfoBarPlugins
41 baseInfoBarPlugins__init__ = None
42 ENABLE_RED_BUTTON = False
43
44 def getHBBTVInstalled():
45         try:
46                 from Plugins.Extensions.HbbTV.HbbTV import HbbTV
47                 return config.plugins.hbbtv.enabled.value
48         except ImportError:
49                 return False
50
51 def autostart(reason, **kwargs):
52         global baseInfoBarPlugins__init__,ENABLE_RED_BUTTON
53         if "session" in kwargs:
54                 session = kwargs["session"]
55                 if baseInfoBarPlugins__init__ is None:
56                         baseInfoBarPlugins__init__ = InfoBarPlugins.__init__
57                 InfoBarPlugins.__init__ = InfoBarPlugins__init__
58                 InfoBarPlugins.greenlong = greenlong
59                 InfoBarPlugins.yellowlong = yellowlong
60                 InfoBarPlugins.redlong = redlong
61                 InfoBarPlugins.bluelong = bluelong
62                 if config.misc.rcused.value != 1:
63                         ENABLE_RED_BUTTON = 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], icon = "setup_quickbutton.png", fnc=setup))
73         return list
74
75 def InfoBarPlugins__init__(self):
76         from Screens.InfoBarGenerics import InfoBarEPG, InfobarHbbtvPlugin
77         if isinstance(self, InfoBarEPG):
78                 x = {   "green_l": (self.greenlong, _("Assign plugin to long green key pressed")),
79                         "yellow_l": (self.yellowlong, _("Assign plugin to long yellow key pressed")),
80                         "red_l": (self.redlong, _("Assign plugin to long red key pressed")),
81                         "blue_l": (self.bluelong, _("Assign plugin to long blue key pressed"))}
82                 if ENABLE_RED_BUTTON:
83                         x["red_b"] = (self.red, _("Assign plugin to red key pressed"))
84                         hbbtvinstalled = getHBBTVInstalled()
85                         if isinstance(self, InfobarHbbtvPlugin) and config.plugins.Quickbutton.overwritehbbtvredbutton.value and hbbtvinstalled:
86                                 self["HbbtvActions"].setEnabled(False)
87                 self["QuickbuttonActions"] = HelpableActionMap(self, "QuickbuttonActions",x)
88
89                         
90         else:
91                 InfoBarPlugins.__init__ = InfoBarPlugins.__init__
92                 InfoBarPlugins.greenlong = None
93                 InfoBarPlugins.yellowlong = None
94                 InfoBarPlugins.redlong = None
95                 InfoBarPlugins.bluelong = None
96                 if ENABLE_RED_BUTTON:
97                         InfoBarPlugins.red = None
98         baseInfoBarPlugins__init__(self)
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 == _("Single EPG"):
120                         from Screens.InfoBarGenerics import InfoBarEPG
121                         if isinstance(self, InfoBarEPG):
122                                 self.openSingleServiceEPG()
123                         no_plugin = False
124                 elif pname == _("Multi EPG"):
125                         from Screens.InfoBarGenerics import InfoBarEPG
126                         if isinstance(self, InfoBarEPG):
127                                 self.openMultiServiceEPG()
128                         no_plugin = False
129                 elif pname == _("MediaPlayer"):
130                         try: # falls es nicht installiert ist
131                                 from Plugins.Extensions.MediaPlayer.plugin import MediaPlayer
132                                 self.session.open(MediaPlayer)
133                                 no_plugin = False
134                         except Exception, e:
135                                 msgText = _("Error!\nError Text: %s"%e)
136                 elif pname == _("Plugin browser"):
137                         from Screens.PluginBrowser import PluginBrowser
138                         self.session.open(PluginBrowser)
139                         no_plugin = False
140                 elif pname == _("switch 4:3 content display"):
141                         ar = {  "pillarbox": _("Pillarbox"), 
142                                 "panscan": _("Pan&Scan"),  
143                                 "scale": _("Just Scale")}
144                         switch = { "pillarbox":"panscan", "panscan":"scale", "scale":"pillarbox" }
145                         config.av.policy_43.value =  switch[config.av.policy_43.value]
146                         config.av.policy_43.save()
147                         self.session.open(MessageBox,_("Display 4:3 content as") + " " + ar[config.av.policy_43.value], MessageBox.TYPE_INFO, timeout = 3)
148                         no_plugin = False
149                 elif pname == _("Timer"):
150                         from Screens.TimerEdit import TimerEditList
151                         self.session.open(TimerEditList)
152                         no_plugin = False
153                 elif pname == _("HbbTV Applications"):
154                         try:
155                                 from Plugins.Extensions.HbbTV.HbbTV import HbbTV
156                                 no_plugin = not config.plugins.hbbtv.enabled.value
157                         except ImportError:
158                                 no_plugin = True
159                         finally:
160                                 if not no_plugin:
161                                         hbbtv_instance = HbbTV.instance
162                                         if hbbtv_instance:
163                                                 hbbtv_instance._showApplicationList()
164                 else:
165                         plugin = None
166                         for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU]):
167                                 if pname == str(p.name):
168                                         plugin = p
169                         if plugin is not None:
170                                 try:
171                                         self.runPlugin(plugin)
172                                         no_plugin = False
173                                 except Exception, e:
174                                         msgText = _("Error!\nError Text: %s"%e)
175                         else: 
176                                 msgText = _("Plugin not found!")
177         else:
178                 msgText = _("No plugin assigned!")
179         if no_plugin:
180                 self.session.open(MessageBox,msgText, MessageBox.TYPE_INFO)
181
182
183 class QuickbuttonSetup(ConfigListScreen, Screen):
184         skin = """
185                 <screen position="center,center" size="720,310" title="Quickbutton Setup">
186                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,5" size="200,40" alphatest="on" />
187                         <ePixmap pixmap="skin_default/buttons/green.png" position="210,5" size="200,40" alphatest="on" />
188                         <widget name="key_red" position="10,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
189                         <widget name="key_green" position="210,5" size="200,40" zPosition="1" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" shadowColor="black" shadowOffset="-2,-2" />
190                         <eLabel position="10,50" size="700,1" backgroundColor="grey" />
191                         <widget name="config" position="10,60" size="700,240" enableWrapAround="1" scrollbarMode="showOnDemand" />
192                 </screen>"""
193
194         def __init__(self, session, args = None):
195                 Screen.__init__(self, session)
196                 self["key_red"] = Button(_("Cancel"))
197                 self["key_green"] = Button(_("OK"))
198                 self.entryguilist = []
199                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
200                 {
201                         "green": self.keySave,
202                         "cancel": self.keyClose,
203                         "ok": self.keySave,
204                         "left": self.keyLeft,
205                         "right": self.keyRight,
206                 }, -2)
207                 ConfigListScreen.__init__(self, [], session = session)
208                 self.hbbtvinstalled = getHBBTVInstalled()
209                 self.overwriteHBBTVButton = config.plugins.Quickbutton.overwritehbbtvredbutton
210                 self.createSetup("config")
211
212         def createSetup(self, widget):
213                 cfglist = []
214                 red_b_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.red_b.value)
215                 red_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.red.value)
216                 green_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.green.value)
217                 yellow_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.yellow.value)
218                 blue_selectedindex = self.getStaticPluginName(config.plugins.Quickbutton.blue.value)
219                 # feste Vorgaben...koennte man noch erweitern, da hole ich mir sinnvolle Vorschlaege aus Foren noch ein...
220                 self.entryguilist.append(("0",_("Nothing")))
221                 self.entryguilist.append(("1",_("Single EPG")))
222                 self.entryguilist.append(("2",_("Multi EPG")))
223                 self.entryguilist.append(("3",_("MediaPlayer")))
224                 self.entryguilist.append(("4",_("Plugin browser")))
225                 self.entryguilist.append(("5",_("switch 4:3 content display")))
226                 self.entryguilist.append(("6",_("Timer")))
227                 if self.hbbtvinstalled:
228                         self.entryguilist.append(("7",_("HbbTV Applications")))
229                         index = 8
230                 else:
231                         index = 7
232                 # Vorgaben aus EXTENSIONSMENU, PLUGINMENU
233                 for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU]):
234                         self.entryguilist.append((str(index),str(p.name)))
235                         if config.plugins.Quickbutton.red.value == str(p.name):
236                                 red_selectedindex = str(index)
237                         if config.plugins.Quickbutton.red_b.value == str(p.name):
238                                 red_b_selectedindex = str(index)
239                         if config.plugins.Quickbutton.green.value == str(p.name):
240                                 green_selectedindex = str(index)
241                         if config.plugins.Quickbutton.yellow.value == str(p.name):
242                                 yellow_selectedindex = str(index)
243                         if config.plugins.Quickbutton.blue.value == str(p.name):
244                                 blue_selectedindex = str(index)
245                         index = index + 1
246                 self.overwriteHBBTVButtonEntry = None
247                 if self.hbbtvinstalled and ENABLE_RED_BUTTON:
248                         self.overwriteHBBTVButtonEntry = getConfigListEntry(_("Overwrite HBBTV-red-button"), self.overwriteHBBTVButton)
249                         cfglist.append(self.overwriteHBBTVButtonEntry)
250                 self.redchoice = ConfigSelection(default = red_selectedindex, choices = self.entryguilist)
251                 self.greenchoice = ConfigSelection(default = green_selectedindex, choices = self.entryguilist)
252                 self.yellowchoice = ConfigSelection(default = yellow_selectedindex, choices = self.entryguilist)
253                 self.bluechoice = ConfigSelection(default = blue_selectedindex, choices = self.entryguilist)
254                 cfglist.append(getConfigListEntry(_("assigned to long red"), self.redchoice))
255                 cfglist.append(getConfigListEntry(_("assigned to long green"), self.greenchoice))
256                 cfglist.append(getConfigListEntry(_("assigned to long yellow"), self.yellowchoice))
257                 cfglist.append(getConfigListEntry(_("assigned to long blue"), self.bluechoice))
258                 if ENABLE_RED_BUTTON and (self.overwriteHBBTVButton.value or not self.hbbtvinstalled):
259                         self.red_b_choice = ConfigSelection(default = red_b_selectedindex, choices = self.entryguilist)
260                         cfglist.append(getConfigListEntry(_("assigned to red"), self.red_b_choice))
261                 self[widget].list = cfglist
262                 self[widget].l.setList(cfglist)
263
264         def getStaticPluginName(self,value):
265                 if value == _("Single EPG"):
266                         return "1"
267                 elif value == _("Multi EPG"):
268                         return "2"
269                 elif value == _("MediaPlayer"):
270                         return "3"
271                 elif value == _("Plugin browser"):
272                         return "4"
273                 elif value == _("switch 4:3 content display"):
274                         return "5"
275                 elif value == _("Timer"):
276                         return "6"
277                 elif value == _("HbbTV Applications"):
278                         return "7"
279                 else:
280                         return "0"
281
282         def keyLeft(self):
283                 ConfigListScreen.keyLeft(self)
284                 self.newConfig()
285
286         def keyRight(self):
287                 ConfigListScreen.keyRight(self)
288                 self.newConfig()
289
290         def newConfig(self):
291                 cur = self["config"].getCurrent()
292                 if cur and (cur == self.overwriteHBBTVButtonEntry):
293                         self.createSetup("config")
294
295         def keySave(self):
296                 config.plugins.Quickbutton.red.value = self.entryguilist[int(self.redchoice.value)][1]
297                 config.plugins.Quickbutton.green.value = self.entryguilist[int(self.greenchoice.value)][1]
298                 config.plugins.Quickbutton.yellow.value = self.entryguilist[int(self.yellowchoice.value)][1]
299                 config.plugins.Quickbutton.blue.value = self.entryguilist[int(self.bluechoice.value)][1]
300                 if ENABLE_RED_BUTTON and (self.overwriteHBBTVButton.value or not self.hbbtvinstalled):
301                         config.plugins.Quickbutton.red_b.value = self.entryguilist[int(self.red_b_choice.value)][1]
302                 self.overwriteHBBTVButton.save()
303                 config.plugins.Quickbutton.save()
304                 configfile.save()
305                 self.close()
306
307         def keyClose(self):
308                 self.close()