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