make button-captions configurable (plugin name|description)
[enigma2-plugins.git] / movieselectionquickbutton / src / plugin.py
1 #
2 #  MovieSelectionQuickButton E2
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 Plugins.Plugin import PluginDescriptor
21 from Screens.Screen import Screen
22 from Screens.MovieSelection import MovieSelection, MovieContextMenu
23 from Components.ActionMap import HelpableActionMap, ActionMap
24 from Components.Button import Button
25 from Components.PluginComponent import plugins
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 Screens.MessageBox import MessageBox
30 # for localized messages
31 from . import _
32
33 config.plugins.MovieSelectionQuickButton = ConfigSubsection()
34 config.plugins.MovieSelectionQuickButton.green = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
35 config.plugins.MovieSelectionQuickButton.yellow = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
36 config.plugins.MovieSelectionQuickButton.blue = ConfigText(default = _("Nothing"), visible_width = 50, fixed_size = False)
37 config.plugins.MovieSelectionQuickButton.buttoncaption = ConfigSelection(default="0", choices = [("0", _("display plugin name")),("1", _("display plugin description"))])
38
39 ###########################################
40 # MovieSelection
41 ###########################################
42 baseMovieSelection__init__ = None
43 baseupdateTags = None
44 def MovieSelectionInit():
45         global baseMovieSelection__init__, baseupdateTags
46         if baseMovieSelection__init__ is None:
47                 baseMovieSelection__init__ = MovieSelection.__init__
48         if baseupdateTags is None:
49                 baseupdateTags = MovieSelection.updateTags
50         MovieSelection.__init__ = MovieSelection__init__
51         MovieSelection.updateTags = noUpdateTages
52         # new methods
53         MovieSelection.redpressed = redpressed
54         MovieSelection.greenpressed = greenpressed
55         MovieSelection.yellowpressed = yellowpressed
56         MovieSelection.bluepressed = bluepressed
57         MovieSelection.getPluginCaption = getPluginCaption
58
59 def MovieSelection__init__(self, session, selectedmovie = None):
60         baseMovieSelection__init__ (self, session, selectedmovie)
61         self["key_red"] = Button(_("Delete"))
62         self["key_green"] = Button(self.getPluginCaption(str(config.plugins.MovieSelectionQuickButton.green.value)))
63         self["key_yellow"] = Button(self.getPluginCaption(str(config.plugins.MovieSelectionQuickButton.yellow.value)))
64         self["key_blue"] = Button(self.getPluginCaption(str(config.plugins.MovieSelectionQuickButton.blue.value)))
65         self["ColorActions"] = HelpableActionMap(self, "ColorActions",
66         {
67                 "red": (self.redpressed, _("delete movie")),
68                 "green": (self.greenpressed, _("Assign plugin to green key pressed")),
69                 "yellow": (self.yellowpressed, _("Assign plugin to yellow key pressed")),
70                 "blue": (self.bluepressed, _("Assign plugin to blue key pressed")),
71         })
72
73 def redpressed(self):
74         current = self.getCurrent()
75         if current is not None:
76                 MCM = MovieContextMenu(self.session,self,current)
77                 MCM.delete()
78
79 def greenpressed(self):
80         startPlugin(self,str(config.plugins.MovieSelectionQuickButton.green.value))
81
82 def yellowpressed(self):
83         startPlugin(self,str(config.plugins.MovieSelectionQuickButton.yellow.value))
84
85 def bluepressed(self):
86         startPlugin(self,str(config.plugins.MovieSelectionQuickButton.blue.value))
87
88 def getPluginCaption(self,pname):
89         if pname != _("Nothing"):
90                 for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_MOVIELIST]):
91                         if pname == str(p.name):
92                                 if config.plugins.MovieSelectionQuickButton.buttoncaption.value == "1":
93                                         return p.description
94                                 else:
95                                         return p.name
96         return ""
97
98 def startPlugin(self,pname):
99         plugin = None
100         no_plugin = True
101         msgText = _("Unknown Error")
102         current = self.getCurrent()
103         if current is not None:
104                 if pname != _("Nothing"):
105                         for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_MOVIELIST]):
106                                 if pname == str(p.name):
107                                         plugin = p
108                         if plugin is not None:
109                                 try:
110                                         plugin(self.session, current)
111                                         no_plugin = False
112                                 except Exception, e:
113                                         msgText = _("Error!\nError Text: %s"%e)
114                         else: 
115                                 msgText = _("Plugin not found!")
116                 else:
117                         msgText = _("No plugin assigned!")
118                 if no_plugin:
119                         self.session.open(MessageBox,msgText, MessageBox.TYPE_INFO)
120
121 def noUpdateTages(self):
122         pass #nothing to do here, just ovewrite the method
123
124 class MovieSelectionButtonSetup(ConfigListScreen, Screen):
125         skin = """
126                 <screen position="center,center" size="550,400" title="MovieSelection QuickButton Setup" >
127                         <widget name="config" position="20,10" size="510,330" scrollbarMode="showOnDemand" />
128                         <widget name="key_red" position="0,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
129                         <widget name="key_green" position="140,350" size="140,40" valign="center" halign="center" zPosition="5" transparent="1" foregroundColor="white" font="Regular;18"/>
130                         <ePixmap name="red" pixmap="skin_default/buttons/red.png" position="0,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
131                         <ePixmap name="green" pixmap="skin_default/buttons/green.png" position="140,350" size="140,40" zPosition="4" transparent="1" alphatest="on"/>
132                 </screen>"""
133
134         def __init__(self, session, args = None):
135                 Screen.__init__(self, session)
136                 self["key_red"] = Button(_("Cancel"))
137                 self["key_green"] = Button(_("OK"))
138                 self.entryguilist = []
139                 self.entryguilist.append(("0",_("Nothing")))
140                 index = 1
141                 green_selectedindex = "0"
142                 yellow_selectedindex = "0"
143                 blue_selectedindex = "0"
144                 for p in plugins.getPlugins(where = [PluginDescriptor.WHERE_MOVIELIST]):
145                         self.entryguilist.append((str(index),str(p.name)))
146                         if config.plugins.MovieSelectionQuickButton.green.value == str(p.name):
147                                 green_selectedindex = str(index)
148                         if config.plugins.MovieSelectionQuickButton.yellow.value == str(p.name):
149                                 yellow_selectedindex = str(index)
150                         if config.plugins.MovieSelectionQuickButton.blue.value == str(p.name):
151                                 blue_selectedindex = str(index)
152                         index = index + 1
153                 self.greenchoice = ConfigSelection(default = green_selectedindex, choices = self.entryguilist)
154                 self.yellowchoice = ConfigSelection(default = yellow_selectedindex, choices = self.entryguilist)
155                 self.bluechoice = ConfigSelection(default = blue_selectedindex, choices = self.entryguilist)
156                 cfglist = [
157                         getConfigListEntry(_("assigned to green"), self.greenchoice),
158                         getConfigListEntry(_("assigned to yellow"), self.yellowchoice),
159                         getConfigListEntry(_("assigned to blue"), self.bluechoice),
160                         getConfigListEntry(_("button caption"), config.plugins.MovieSelectionQuickButton.buttoncaption),
161                         ]
162                 ConfigListScreen.__init__(self, cfglist, session)
163                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
164                 {
165                         "green": self.keySave,
166                         "cancel": self.keyClose,
167                         "ok": self.keySave,
168                 }, -2)
169
170         def keySave(self):
171                 config.plugins.MovieSelectionQuickButton.green.value = self.entryguilist[int(self.greenchoice.value)][1]
172                 config.plugins.MovieSelectionQuickButton.yellow.value = self.entryguilist[int(self.yellowchoice.value)][1]
173                 config.plugins.MovieSelectionQuickButton.blue.value = self.entryguilist[int(self.bluechoice.value)][1]
174                 config.plugins.MovieSelectionQuickButton.save()
175                 configfile.save()
176                 self.close()
177
178         def keyClose(self):
179                 self.close()
180
181 def setup(session,**kwargs):
182         session.open(MovieSelectionButtonSetup)
183
184 def main(session, **kwargs):
185         try: MovieSelectionInit()
186         except: pass
187
188 def Plugins(**kwargs):
189         list = [PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = main)]      
190         list.append(PluginDescriptor(name="Setup MovieSelection QuickButton", description=_("Setup for MovieSelection QuickButton"), where = [PluginDescriptor.WHERE_PLUGINMENU],
191         icon = "plugin.png", fnc=setup))
192         return list
193