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