New plugin: Quickbutton - assign long key-press (red/green/yellow/blue) to plugins...
[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
33 config.plugins.Quickbutton = ConfigSubsection()
34 config.plugins.Quickbutton.red = ConfigText(default = "", visible_width = 50, fixed_size = False)
35 config.plugins.Quickbutton.green = ConfigText(default = "", visible_width = 50, fixed_size = False)
36 config.plugins.Quickbutton.yellow = ConfigText(default = "", visible_width = 50, fixed_size = False)
37 config.plugins.Quickbutton.blue = ConfigText(default = "", visible_width = 50, fixed_size = False)
38
39 EPGListTitle = _("EPG List")
40
41 def autostart(reason, **kwargs):
42         if "session" in kwargs:
43                 session = kwargs["session"]
44                 Quickbutton(session)
45
46 def setup(session,**kwargs):
47         session.open(QuickbuttonSetup)
48
49 def Plugins(**kwargs):
50
51         list = [PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = autostart)] 
52         list.append(PluginDescriptor(name="Setup Quickbutton", description=_("setup for Quickbutton"), where = [PluginDescriptor.WHERE_PLUGINMENU], fnc=setup))
53         return list
54
55 class QuickbuttonSetup(ConfigListScreen, Screen):
56         skin = """
57                 <screen position="100,100" size="550,400" title="Quickbutton Setup" >
58                         <widget name="config" position="20,10" size="510,330" scrollbarMode="showOnDemand" />
59                         <widget name="key_red" position="0,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
60                         <widget name="key_green" position="140,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
61                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
62                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
63                 </screen>"""
64
65         def __init__(self, session, args = None):
66                 Screen.__init__(self, session)
67                 self["key_red"] = Button(_("Cancel"))
68                 self["key_green"] = Button(_("OK"))
69                 self.entryguilist = []
70                 red_selectedindex = "0"
71                 if config.plugins.Quickbutton.red.value == EPGListTitle:
72                         red_selectedindex = "1"
73                 green_selectedindex = "0"
74                 if config.plugins.Quickbutton.green.value == EPGListTitle:
75                         green_selectedindex = "1"
76                 yellow_selectedindex = "0"
77                 if config.plugins.Quickbutton.yellow.value == EPGListTitle:
78                         yellow_selectedindex = "1"
79                 blue_selectedindex = "0"
80                 if config.plugins.Quickbutton.blue.value == EPGListTitle:
81                         blue_selectedindex = "1"
82                 # feste Vorgaben...koennte man noch erweitern, da hole ich mir sinnvolle Vorschlaege aus Foren noch ein...
83                 self.entryguilist.append(("0",_("Nothing")))
84                 self.entryguilist.append(("1",EPGListTitle))
85                 # Vorgaben aus EXTENSIONSMENU
86                 index = 2
87                 for p in plugins.getPlugins(where = PluginDescriptor.WHERE_EXTENSIONSMENU):
88                         self.entryguilist.append((str(index),str(p.name)))
89                         if config.plugins.Quickbutton.red.value == str(p.name):
90                                 red_selectedindex = str(index)
91                         if config.plugins.Quickbutton.green.value == str(p.name):
92                                 green_selectedindex = str(index)
93                         if config.plugins.Quickbutton.yellow.value == str(p.name):
94                                 yellow_selectedindex = str(index)
95                         if config.plugins.Quickbutton.blue.value == str(p.name):
96                                 blue_selectedindex = str(index)
97                         index = index + 1
98
99                 self.redchoice = ConfigSelection(default = red_selectedindex, choices = self.entryguilist)
100                 self.greenchoice = ConfigSelection(default = green_selectedindex, choices = self.entryguilist)
101                 self.yellowchoice = ConfigSelection(default = yellow_selectedindex, choices = self.entryguilist)
102                 self.bluechoice = ConfigSelection(default = blue_selectedindex, choices = self.entryguilist)
103
104                 cfglist = [
105                         getConfigListEntry(_("assigned to long red"), self.redchoice),
106                         getConfigListEntry(_("assigned to long green"), self.greenchoice),
107                         getConfigListEntry(_("assigned to long yellow"), self.yellowchoice),
108                         getConfigListEntry(_("assigned to long blue"), self.bluechoice)
109
110                         ]
111                 ConfigListScreen.__init__(self, cfglist, session)
112                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
113                 {
114                         "green": self.keySave,
115                         "cancel": self.keyClose,
116                         "ok": self.keySave,
117                 }, -2)
118
119         def keySave(self):
120                 config.plugins.Quickbutton.red.value = self.entryguilist[int(self.redchoice.value)][1]
121                 config.plugins.Quickbutton.green.value = self.entryguilist[int(self.greenchoice.value)][1]
122                 config.plugins.Quickbutton.yellow.value = self.entryguilist[int(self.yellowchoice.value)][1]
123                 config.plugins.Quickbutton.blue.value = self.entryguilist[int(self.bluechoice.value)][1]
124                 config.plugins.Quickbutton.save()
125                 configfile.save()
126                 self.close()
127
128         def keyClose(self):
129                 self.close()
130
131 class Quickbutton(object):
132         def __init__(self, session):
133                 self.session = session
134                 QuickbuttonActionMap = ActionMap(["QuickbuttonActions"])
135                 QuickbuttonActionMap.execBegin()
136                 QuickbuttonActionMap.actions["green_l"] = self.greenlong
137                 QuickbuttonActionMap.actions["yellow_l"] = self.yellowlong
138                 QuickbuttonActionMap.actions["red_l"] = self.redlong 
139                 QuickbuttonActionMap.actions["blue_l"] = self.bluelong
140
141         def greenlong(self):
142                 self.getPlugin(str(config.plugins.Quickbutton.green.value))
143         
144         def yellowlong(self):
145                 self.getPlugin(str(config.plugins.Quickbutton.yellow.value))
146         
147         def redlong(self):
148                 self.getPlugin(str(config.plugins.Quickbutton.red.value))
149
150         def bluelong(self):
151                 self.getPlugin(str(config.plugins.Quickbutton.blue.value))
152         
153         def getPlugin(self, pname):
154                 msgText = _("Unknown Error")
155                 error = True
156                 if pname != "":
157                         if pname == EPGListTitle:
158                                 print "[Quickbutton] EPG List"
159                                 from Screens.EpgSelection import EPGSelection
160                                 self.session.open(EPGSelection, self.session.nav.getCurrentlyPlayingServiceReference())
161                                 error = False
162                         else:
163                                 ca = None
164                                 for p in plugins.getPlugins(where = PluginDescriptor.WHERE_EXTENSIONSMENU):
165                                         if pname == str(p.name):
166                                                 print "[Quickbutton] %s"%p.name
167                                                 ca = p
168                                 if ca is not None:
169                                         try: 
170                                                 servicelist = self.session.instantiateDialog(ChannelSelection)
171                                                 ca(session = self.session, servicelist = servicelist)
172                                                 error = 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 error:
180                         self.session.open(MessageBox,msgText, MessageBox.TYPE_INFO)
181