fix typo
[enigma2-plugins.git] / dreammediathek / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Tools.BoundFunction import boundFunction
3 from Screens.MessageBox import MessageBox
4 from Screens.Screen import Screen
5 from Screens.ChoiceBox import ChoiceBox
6 from Components.ActionMap import ActionMap, NumberActionMap
7 from Components.Sources.StaticText import StaticText
8 from Components.Sources.List import List
9 from Components.AVSwitch import AVSwitch
10 from Components.config import config, Config, ConfigSelection, ConfigSubsection, ConfigText, getConfigListEntry, ConfigYesNo, ConfigIP, ConfigNumber,ConfigLocations
11 from Components.config import KEY_DELETE, KEY_BACKSPACE, KEY_LEFT, KEY_RIGHT, KEY_HOME, KEY_END, KEY_TOGGLEOW, KEY_ASCII, KEY_TIMEOUT
12 from Components.ConfigList import ConfigListScreen
13 from Components.ServiceEventTracker import ServiceEventTracker, InfoBarBase
14
15 from Tools.Directories import pathExists, fileExists, resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE, SCOPE_HDD, SCOPE_CURRENT_PLUGIN, SCOPE_CURRENT_SKIN
16 from Tools.LoadPixmap import LoadPixmap
17 from enigma import eTimer, quitMainloop,eListbox,ePoint, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_VALIGN_CENTER, eListboxPythonMultiContent, eListbox, gFont, getDesktop, ePicLoad, eServiceCenter, iServiceInformation, eServiceReference,iSeekableService,iServiceInformation, iPlayableService, iPlayableServicePtr
18 from os import path as os_path, system as os_system, unlink, stat, mkdir, popen, makedirs, listdir, access, rename, remove, W_OK, R_OK, F_OK
19 from twisted.web import client
20 from twisted.internet import reactor
21 from time import time
22
23 from Screens.InfoBarGenerics import InfoBarShowHide, InfoBarSeek, InfoBarNotifications, InfoBarServiceNotifications
24
25 from ServiceXML import iWebTVStations
26 from MoviePlayer import dreamMediathekPlayer
27
28 config.plugins.dreamMediathek = ConfigSubsection()
29 config.plugins.dreamMediathek.general = ConfigSubsection()
30 config.plugins.dreamMediathek.general.on_movie_stop = ConfigSelection(default = "ask", choices = [
31         ("ask", _("Ask user")), ("quit", _("Return to movie list")), ("playnext", _("Play next video")), ("playagain", _("Play video again")) ])
32 config.plugins.dreamMediathek.general.on_exit = ConfigSelection(default = "ask", choices = [
33         ("ask", _("Ask user")), ("quit", _("Return to movie list"))])
34
35
36 class dreamMediathekStationsScreen(Screen):
37         Details = {}
38         skin = """
39                 <screen name="dreamMediathekStationsScreen" position="center,center" size="560,440" title="dreamMediathekStationsScreen" >
40                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
41                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
42                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
43                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
44                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
45                         <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
46                         <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
47                         <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
48                         <widget source="streamlist" render="Listbox" position="5,50" size="550,280" zPosition="1" scrollbarMode="showOnDemand" transparent="1" >
49                                 <convert type="TemplatedMultiContent">
50                                 {"templates":
51                                         {"default": (55,[
52                                                         MultiContentEntryText(pos = (5, 1), size = (540, 28), font=2, flags = RT_HALIGN_LEFT | RT_VALIGN_TOP| RT_WRAP, text = 1), # provider, title, streamurl
53                                                         MultiContentEntryText(pos = (5, 28), size = (540, 18), font=3, flags = RT_HALIGN_LEFT | RT_VALIGN_TOP| RT_WRAP, text = 0), # provider, title, streamurl
54                                                 ]),
55                                         "state": (55,[
56                                                         MultiContentEntryText(pos = (10, 1), size = (560, 28), font=2, flags = RT_HALIGN_LEFT | RT_VALIGN_TOP| RT_WRAP, text = 0), # index 0 is the name
57                                                         MultiContentEntryText(pos = (10, 22), size = (560, 46), font=3, flags = RT_HALIGN_LEFT | RT_VALIGN_TOP| RT_WRAP, text = 1), # index 2 is the description
58                                                 ])
59                                         },
60                                         "fonts": [gFont("Regular", 22),gFont("Regular", 18),gFont("Regular", 26),gFont("Regular", 20)],
61                                         "itemHeight": 55
62                                 }
63                                 </convert>
64                         </widget>
65                 </screen>"""
66                 
67         def __init__(self, session, skin_path = None):
68                 Screen.__init__(self, session)
69                 self.session = session
70                 self.skin_path = skin_path
71                 if self.skin_path == None:
72                         self.skin_path = resolveFilename(SCOPE_CURRENT_PLUGIN, "Extensions/dreamMediathek")
73
74                 self.lastservice = session.nav.getCurrentlyPlayingServiceReference()
75                 self.streamlist = []
76                 self.currentList = "streamlist"
77                 self.oldList = None
78                 self.tvstations = None
79
80                 self["streamlist"] = List(self.streamlist)
81                 self["key_red"] = StaticText(_("Cancel"))
82                 self["key_green"] = StaticText(_("OK"))
83                 self["key_yellow"] = StaticText()
84                 self["key_blue"] = StaticText() 
85
86                 self["FredMainActions"] = ActionMap(["ShortcutActions", "WizardActions"],
87                 {
88                         "ok": self.keyOK,
89                         "back": self.leavePlayer,
90                         "red": self.leavePlayer,
91                 }, -1)
92
93                 #self["videoactions"].setEnabled(True)
94                 self.onLayoutFinish.append(self.layoutFinished)
95                 self.onShown.append(self.setWindowTitle)
96                 self.onClose.append(self.__onClose)
97                 
98         def __onClose(self):
99                 self.Details = {}
100                 self.session.nav.playService(self.lastservice)
101                 
102         def layoutFinished(self):
103                 self.currentList = "streamlist"
104                 self.getStationsList()
105                 #self["videoactions"].setEnabled(False)
106                 
107         def setWindowTitle(self):
108                 self.setTitle(_("dreamMediathek TV Stations"))
109
110         def handleLeave(self, how):
111                 self.is_closing = True
112                 if how == "ask":
113                         list = (
114                                 (_("Yes"), "quit"),
115                                 (_("No"), "continue")
116                         )                                       
117                         self.session.openWithCallback(self.leavePlayerConfirmed, ChoiceBox, title=_("Really quit dreamMediathek ?"), list = list)
118                 else:
119                         self.leavePlayerConfirmed([True, how])
120
121         def leavePlayer(self):
122                 print "leavePlayer"
123                 self.handleLeave(config.plugins.dreamMediathek.general.on_exit.value)
124
125         def leavePlayerConfirmed(self, answer):
126                 answer = answer and answer[1]
127                 if answer == "quit":
128                         self.doQuit()
129                 else:
130                         return
131
132         def doQuit(self):
133                 config.plugins.dreamMediathek.general.save()
134                 config.plugins.dreamMediathek.save()
135                 self.close()
136                         
137         def keyOK(self):
138                 print "self.currentList im KeyOK",self.currentList
139                 if self.currentList == "streamlist":
140                         current = self["streamlist"].getCurrent()
141                         if current:
142                                 print current
143                                 url = current[2]
144                                 title = current[1]
145                                 myreference = eServiceReference(4097,0,url)
146                                 myreference.setName(title)
147                                 #self.session.open(dreamMediathekPlayer, myreference, self.lastservice, infoCallback = self.showVideoInfo, nextCallback = self.getNextEntry, prevCallback = self.getPrevEntry )
148                                 self.session.open(dreamMediathekPlayer, myreference, self.lastservice)
149
150         def getStationsList(self):
151                 print "getStationsList"
152                 iWebTVStations.getWebTVStations()
153                 self.buildStationsList()
154
155         def buildStationsComponent(self, station):
156                 provider = None
157                 title = None
158                 streamurl = None
159                 if iWebTVStations.webtv_stations[station].has_key("provider"):
160                         provider = iWebTVStations.webtv_stations[station]["provider"]
161                 if iWebTVStations.webtv_stations[station].has_key("title"):
162                         title = iWebTVStations.webtv_stations[station]["title"]
163                 if iWebTVStations.webtv_stations[station].has_key("streamurl"):
164                         streamurl = iWebTVStations.webtv_stations[station]["streamurl"]                 
165                 return((provider, title, streamurl ))   
166
167         def buildStationsList(self):
168                 self.tvstations = None
169                 self.tvstations = iWebTVStations.getWebTVStationsList()
170                 if self.tvstations and len(self.tvstations):
171                         self.streamlist = []
172                         for station in self.tvstations:
173                                 print "GOT station:",station
174                                 self.streamlist.append(self.buildStationsComponent(station))
175                         if len(self.streamlist):
176                                 self["streamlist"].setList(self.streamlist)
177                                 self["streamlist"].style = "default"
178
179 def dreamMediathekMain(session, **kwargs):
180         session.open(dreamMediathekStationsScreen)
181
182
183 def Plugins(path, **kwargs):
184         global plugin_path
185         plugin_path = path
186         return PluginDescriptor(
187                 name=_("DreamMediathek"),
188                 description=_("Play Web and ipTV streams"),
189                 where = [ PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU ],
190                 fnc = dreamMediathekMain)