setup icons for PiPServiceRelation, Partnerbox and Quickbutton added
[enigma2-plugins.git] / pipservicerelation / src / plugin.py
1 # -*- coding: utf-8 -*-
2 #
3 #  PipServiceRelation E2
4 #
5 #  Coded by Dr.Best (c) 2011
6 #  Support: www.dreambox-tools.info
7 #
8 #  This plugin is licensed under the Creative Commons 
9 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
10 #  License. To view a copy of this license, visit
11 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
12 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
13 #
14 #  Alternatively, this plugin may be distributed and executed on hardware which
15 #  is licensed by Dream Multimedia GmbH.
16
17 #  This plugin is NOT free software. It is open source, you are allowed to
18 #  modify it (if you keep the license), but it may not be commercially 
19 #  distributed other than under the conditions noted above.
20 #
21 from Plugins.Plugin import PluginDescriptor
22 from Screens.PictureInPicture import PictureInPicture
23 from Screens.Screen import Screen
24 from Screens.MessageBox import MessageBox
25 from Screens.ChannelSelection import SimpleChannelSelection
26 from enigma import eServiceCenter, getBestPlayableServiceReference, eServiceReference, eListboxPythonMultiContent, gFont, RT_HALIGN_LEFT, RT_VALIGN_CENTER
27 from Components.MenuList import MenuList
28 from Components.Sources.StaticText import StaticText
29 from Components.ActionMap import ActionMap
30 from ServiceReference import ServiceReference
31 from Tools.BoundFunction import boundFunction
32 from Components.ConfigList import ConfigListScreen
33 from Components.config import getConfigListEntry, config, ConfigDirectory, NoSave
34
35 from os import path as os_path
36 from pickle import load as pickle_load, dump as pickle_dump
37 from enigma import eEnv
38
39 basePictureInPicture__init__ = None
40 CONFIG_FILE = eEnv.resolve('${libdir}/enigma2/python/Plugins/SystemPlugins/PiPServiceRelation/psr_config')
41
42 def getRelationDict():
43         if os_path.exists(CONFIG_FILE):
44                 pkl_file = open(CONFIG_FILE, 'rb')
45                 if pkl_file:
46                         volumedict = pickle_load(pkl_file)
47                         pkl_file.close()
48                         return volumedict
49         return {}
50
51 def saveRelationDict(dict):
52         pkl_file = open(CONFIG_FILE, 'wb')
53         if pkl_file:
54                 pickle_dump(dict, pkl_file)
55                 pkl_file.close()
56
57 def autostart_PictureInPicture(session, **kwargs):
58         init_pipservicerelation()
59
60 def setup(session,**kwargs):
61         session.open(PipServiceRelationSetup)
62
63
64 class PipServiceRelationSetup(Screen):
65         skin = """
66                 <screen position="center,center" size="655,400">
67                         <widget render="Label" source="name" position="5,0" size="320,50" font="Regular;20" halign="left"/>
68                         <widget render="Label" source="relationname" position="330,0" size="320,50" font="Regular;20" halign="left"/>
69                         <widget name="entrylist" position="0,50" size="655,300" scrollbarMode="showOnDemand"/>
70                         <widget render="Label" source="key_red" position="0,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="red" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
71                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
72                         <widget render="Label" source="key_yellow" position="280,350" size="140,40" zPosition="5" valign="center" halign="center" backgroundColor="yellow" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
73                         <widget render="Label" source="key_blue" position="420,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
74                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
75                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
76                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
77                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
78                 </screen>"""
79
80         def __init__(self, session):
81                 Screen.__init__(self, session)
82                 self.title = _("PipServiceRelation - Config")
83                 self["name"] = StaticText(_("Service"))
84                 self["relationname"] = StaticText(_("Related PiP Service"))
85                 self["key_red"] = StaticText(_("Delete"))
86                 self["key_green"] = StaticText(_("Close"))
87                 self["key_yellow"] = StaticText(_("Add"))
88                 self["key_blue"] = StaticText(_("Edit"))
89                 self["entrylist"] = PipServiceRelationEntryList([])
90                 self["actions"] = ActionMap(["WizardActions","MenuActions","ShortcutActions"],
91                         {
92                          "ok"   :       self.keyBlue,
93                          "back" :       self.keyClose,
94                          "red"  :       self.keyDelete,
95                          "green":       self.keyClose,
96                          "yellow":      self.keyYellow,
97                          "blue":        self.keyBlue,
98                          }, -1)
99                 self["entrylist"].setConfig(getRelationDict())
100                 self.updateList()
101
102         def updateList(self):
103                 self["entrylist"].buildList()
104
105         def keyClose(self):
106                 self.close()
107
108         def keyBlue(self):
109                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
110                 except: sel = None
111                 if sel is None:
112                         return
113                 self.session.openWithCallback(self.updateList,PipServiceRelationEntryConfigScreen,sel, self["entrylist"].configPSR)
114
115         def keyYellow(self):
116                 self.session.openWithCallback(self.updateList,PipServiceRelationEntryConfigScreen,None, self["entrylist"].configPSR)
117
118         def keyDelete(self):
119                 try:sel = self["entrylist"].l.getCurrentSelection()[0]
120                 except: sel = None
121                 if sel is None:
122                         return
123                 self.session.openWithCallback(self.deleteConfirm, MessageBox, _("Do you really want to delete this entry?"))
124
125         def deleteConfirm(self, result):
126                 if not result:
127                         return
128                 sel = self["entrylist"].l.getCurrentSelection()[0]
129                 del self["entrylist"].configPSR[sel[0]]
130                 saveRelationDict(self["entrylist"].configPSR)
131                 self.updateList()
132
133 class PipServiceRelationEntryList(MenuList):
134         def __init__(self, list, enableWrapAround = True):
135                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
136                 self.l.setFont(0, gFont("Regular", 20))
137                 self.l.setFont(1, gFont("Regular", 18))
138                 self.configPSR = None
139
140         def postWidgetCreate(self, instance):
141                 MenuList.postWidgetCreate(self, instance)
142                 instance.setItemHeight(20)
143
144         def getCurrentIndex(self):
145                 return self.instance.getCurrentIndex()
146                 
147         def setConfig(self, configPSR):
148                 self.configPSR = configPSR
149                 
150         def buildList(self):
151                 list = []
152                 for c in self.configPSR.items():
153                         res = [
154                                 c,
155                                 (eListboxPythonMultiContent.TYPE_TEXT, 5, 0, 320, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, ServiceReference(eServiceReference(c[0])).getServiceName()),
156                                 (eListboxPythonMultiContent.TYPE_TEXT, 330, 0,320, 20, 1, RT_HALIGN_LEFT|RT_VALIGN_CENTER, ServiceReference(eServiceReference(c[1])).getServiceName()),
157                         ]
158                         list.append(res)
159                 self.list = list
160                 self.l.setList(list)
161                 self.moveToIndex(0)
162
163
164 class PipServiceRelationEntryConfigScreen(ConfigListScreen, Screen):
165         skin = """
166                 <screen name="PipServiceRelationEntryConfigScreen" position="center,center" size="550,400">
167                         <widget name="config" position="20,10" size="520,330" scrollbarMode="showOnDemand" />
168                         <ePixmap position="0,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
169                         <ePixmap position="140,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
170                         <ePixmap position="280,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
171                         <ePixmap position="420,350" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
172
173                         <widget source="key_red" render="Label" position="0,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
174                         <widget source="key_green" render="Label" position="140,350" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
175                 </screen>"""
176
177         def __init__(self, session, entry, configPSR):  
178                 self.session = session
179                 Screen.__init__(self, session)
180                 self.title = _("PipServiceRelation - Entry Config")
181                 self["actions"] = ActionMap(["SetupActions", "ColorActions"],
182                 {
183                         "green": self.keySave,
184                         "red": self.keyCancel,
185                         "cancel": self.keyCancel,
186                         "ok": self.keySelect,
187                 }, -2)
188                 self["key_red"] = StaticText(_("Cancel"))
189                 self["key_green"] = StaticText(_("OK"))
190                 self.configPSR = configPSR
191                 self.entry = entry
192                 if entry is None:
193                         self.currentKey = None
194                         self.ref1 =  NoSave(ConfigDirectory(default = _("Press OK to select a service")))
195                         self.ref2 =  NoSave(ConfigDirectory(default = _("Press OK to select a related PiP service")))
196                 else:
197                         self.currentKey = entry[0]
198                         self.ref1 =  NoSave(ConfigDirectory(default = ServiceReference(eServiceReference(entry[0])).getServiceName()))
199                         self.ref2 =  NoSave(ConfigDirectory(default = ServiceReference(eServiceReference(entry[1])).getServiceName()))
200                 self.list = [ ]
201                 self.serviceref1 =  getConfigListEntry(_("Service"), self.ref1)
202                 self.serviceref2 =  getConfigListEntry(_("Related Pip Service"), self.ref2)
203                 self.list.append(self.serviceref1)
204                 self.list.append(self.serviceref2)
205                 ConfigListScreen.__init__(self, self.list, session)
206                 
207         def keySelect(self):
208                 cur = self["config"].getCurrent()
209                 if cur == self.serviceref1:
210                         index = 1
211                         descr = _("Channel Selection")
212                 else:
213                         index = 2
214                         if self.entry and self.entry[0]:
215                                 sname = ServiceReference(eServiceReference(self.entry[0])).getServiceName()
216                         else:
217                                 sname = ""
218                         descr = _("Related PiP service for %s") % sname
219                 self.session.openWithCallback(boundFunction(self.channelSelected,index), SimpleChannelSelection, descr)
220                         
221         def channelSelected(self, index, ref = None):
222                 if ref:
223                         if self.entry:
224                                 val1 = self.entry[0]
225                                 val2 = self.entry[1]
226                         else:
227                                 val1 = val2 = None
228                         if index == 1:
229                                 self.entry = (ref.toString(), val2)
230                                 self.ref1.value = ServiceReference(ref).getServiceName()
231                         else:
232                                 self.entry = (val1, ref.toString())
233                                 self.ref2.value = ServiceReference(ref).getServiceName()
234
235         def keySave(self):
236                 if self.entry and self.entry[0] and self.entry[1]:
237                         if self.currentKey is not None:
238                                 del self.configPSR[self.currentKey]
239                         self.configPSR[self.entry[0]] = self.entry[1]
240                         saveRelationDict(self.configPSR)
241                 self.close()
242
243         def keyCancel(self):
244                 ConfigListScreen.cancelConfirm(self, True)
245
246 def init_pipservicerelation():
247         global basePictureInPicture__init__, basePictureInPicture_playService
248         if basePictureInPicture__init__ is None:
249                 basePictureInPicture__init__ = PictureInPicture.__init__
250         PictureInPicture.__init__ = PictureInPicture__init__
251         PictureInPicture.playService = playService
252
253
254 def PictureInPicture__init__(self, session):
255         basePictureInPicture__init__(self, session)
256         self.pipServiceRelation = getRelationDict()
257
258 def playService(self, service):
259         current_service = service
260         n_service = self.pipServiceRelation.get(service.toString(),None)
261         if n_service is not None:
262                 service = eServiceReference(n_service)
263         if service and (service.flags & eServiceReference.isGroup):
264                 ref = getBestPlayableServiceReference(service, eServiceReference())
265         else:
266                 ref = service
267         if ref:
268                 self.pipservice = eServiceCenter.getInstance().play(ref)
269                 if self.pipservice and not self.pipservice.setTarget(1):
270                         self.pipservice.start()
271                         self.currentService = current_service
272                         return True
273                 else:
274                         self.pipservice = None
275         return False
276
277 def Plugins(**kwargs):
278         list = []
279         list.append(PluginDescriptor(name="Setup PiPServiceRelation", description=_("setup for PiPServiceRelation"), where = [PluginDescriptor.WHERE_PLUGINMENU], icon = "PiPServiceRelation.png", fnc=setup))
280         list.append(PluginDescriptor(where = PluginDescriptor.WHERE_SESSIONSTART, fnc = autostart_PictureInPicture))
281         return list
282