Patch of Mike Looijmans
[enigma2-plugins.git] / netcaster / src / bin / plugin.py
1 from Screens.Screen import Screen
2 from Screens.ChoiceBox import ChoiceBox
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.ScrollLabel import ScrollLabel
6 from Components.Label import Label
7 from Components.MenuList import MenuList
8 from Components.MultiContent import MultiContentEntryText
9 from enigma import eListboxPythonMultiContent, eListbox, gFont
10
11 from Plugins.Plugin import PluginDescriptor
12 from os import path as os_path, listdir as os_listdir
13 from StreamPlayer import StreamPlayer
14 from Tools.Import import my_import
15
16 ###############################################################################        
17 myname = "NETcaster"     
18 myversion = "0.2"
19 streamingtargetfile = "/tmp/streaming.mpg"
20 valid_types = ("MP3")
21 #
22 streamplayer = None
23 plugin_path = ""
24
25 ###############################################################################        
26 def main(session,**kwargs):
27     session.open(NETcasterScreenBrowser)
28     global streamplayer
29     streamplayer = StreamPlayer(session)
30  
31 def Plugins(path,**kwargs):
32     global plugin_path
33     plugin_path = path
34     return PluginDescriptor(
35         name=myname, 
36         description="play Network and Internet Streams", 
37         where = PluginDescriptor.WHERE_EXTENSIONSMENU,
38         icon = "NETcaster.png",
39         fnc = main
40         )
41         
42 ############################################################################### 
43 class NETcasterScreenBrowser(Screen):
44     skin = """
45         <screen position="80,73" size="560,440" title="SHOUTcaster" >
46                 <widget name="streamlist" position="0,0" size="560,400" scrollbarMode="showOnDemand" />            
47                 <ePixmap name="red"    position="0,400"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
48                 <ePixmap name="green"  position="140,400" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
49                 <ePixmap name="yellow" position="280,400" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
50                 <ePixmap name="blue"   position="420,400" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
51                 <widget name="pixred"    position="0,400" size="140,40" transparent="1" valign="center" halign="center" zPosition="5"  foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" font="Regular;21" />          
52                 <widget name="pixgreen"  position="140,400" size="140,40" transparent="1" valign="center" halign="center" zPosition="5"  foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" font="Regular;21"/>            
53                 <widget name="pixyellow" position="280,400" size="140,40" transparent="1" valign="center" halign="center" zPosition="5"  foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" font="Regular;21" />            
54                 <widget name="pixblue"   position="420,400" size="140,40" transparent="1" valign="center" halign="center" zPosition="5"  foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" font="Regular;21" />            
55         </screen>"""
56   
57     streamlist = []
58     currentPlugin = None
59     def __init__(self, session, args = 0):
60         self.skin = NETcasterScreenBrowser.skin
61         self.session = session
62         Screen.__init__(self, session)
63         self["streamlist"] = StreamMenu([])
64         self["pixred"] = Label("")
65         self["pixgreen"] = Label(_("Play"))
66         self["pixyellow"] = Label("")
67         self["pixblue"] = Label(_("Select"))
68         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","ShortcutActions","GlobalActions","HelpActions"], 
69             {
70              "ok": self.ok,
71              "back": self.exit,
72              "red": self.stream_stop,
73              "green": self.stream_start,
74              "yellow": self.yellow,
75              "blue": self.selectPlugin,
76              "menu": self.showMainMenu,
77              "info": self.showAbout,
78              "displayHelp": self.showHelp,
79              }, -1)
80         
81         self.onClose.append(self.exit)
82         self.getInterfaceList()
83         for plugin in self.pluginlist:
84             if plugin.nameshort == "Favorites":
85                 self.setCurrentPlugin(plugin)
86         self.onShown.append(self.updateTitle)
87
88     def getInterfaceList(self):
89         self.pluginlist = []
90         global plugin_path,myname
91         interfacepath = plugin_path+"/interface"
92         for iface in os_listdir(interfacepath):
93             if iface.endswith(".py") and not iface.startswith("_"):
94                 pluginp = '.'.join(["Plugins", "Extensions", myname, "interface",iface.replace(".py","")])
95                 plugin = my_import(pluginp)
96                 self.pluginlist.append(plugin.Interface(self.session,cbListLoaded=self.onStreamlistLoaded))
97
98     def updateTitle(self):
99         self.setTitle("%s (%s)"%(myname,self.currentPlugin.nameshort))
100
101     def selectPlugin(self):
102         glist=[]
103         for i in self.pluginlist:
104             glist.append((i.name,i))
105         self.session.openWithCallback(self.selectedPlugin,ChoiceBox,_("select Plugin"),glist)
106
107     def selectedPlugin(self,splugin):
108         if splugin is not None:
109             self.setCurrentPlugin(splugin[1])
110             self.updateTitle()
111
112     def setCurrentPlugin(self, plugin):
113         self.currentPlugin = plugin
114         plugin.getList()
115
116     def ok(self):
117         if self.shown is False:
118             self.show()
119
120     def exit(self):
121         global streamplayer 
122         streamplayer.exit()       
123         self.close()
124
125     def yellow(self):
126         pass
127
128     def stream_stop(self):
129         global streamplayer
130         if streamplayer.is_playing:
131             print "[",myname,"] stream_startstop -> stop"
132             streamplayer.stop()
133             self["pixred"].setText("")
134             self.setTitle("%s (%s)"%(myname,self.currentPlugin.nameshort))
135
136     def stream_start(self):
137         global streamplayer
138         if self["streamlist"].l.getCurrentSelection() is not None:
139             stream = self["streamlist"].l.getCurrentSelection()[0]
140             print "[",myname,"] stream_start",stream.getURL()
141             streamplayer.play(stream)
142             self["pixred"].setText(_("Stop"))
143             self.setTitle("%s"%(stream.getName()))
144
145     def onStreamlistLoaded(self,list):
146        self["streamlist"].buildList(list)
147
148     def showMainMenu(self):
149         menu = []
150         if self["streamlist"].l.getCurrentSelection() is not None:
151              selectedStream = self["streamlist"].l.getCurrentSelection()[0]
152         else:
153              selectedStream = None
154         # generic menuitems
155         for p in self.pluginlist:
156             for i in p.getMenuItems(selectedStream,generic=True):
157                 menu.append((i[0],i[1]))
158
159         # non generic menuitems
160         if self.currentPlugin is not None:
161             for i in self.currentPlugin.getMenuItems(selectedStream):
162                 menu.append((i[0],i[1]))
163
164         # std menuitems    
165         menu.append((_("hide"), self.hide))
166         menu.append((_("info"), self.showAbout));
167         menu.append((_("help"), self.showHelp));
168         self.session.openWithCallback(self.menuCallback, ChoiceBox, title=_("Menu"), list=menu)
169
170     def menuCallback(self,choice):
171         if choice is not None:
172             choice[1]()
173
174     def showAbout(self):
175         self.session.open(MessageBox,_("%s Enigma2 Plugin V%s (Patched)" % (myname,myversion)), MessageBox.TYPE_INFO)
176
177     def showHelp(self):
178         self.session.open(NETcasterScreenHelp)
179
180
181
182 ############################################################################### 
183 class NETcasterScreenHelp(Screen):
184     skin = """
185         <screen position="103,73" size="500,400" title="NETcaster Help" >
186             <widget name="help" position="0,0" size="500,400" font="Regular;18"/>            
187         </screen>""" 
188
189     def __init__(self, session, args = 0):
190         self.skin = NETcasterScreenHelp.skin
191         Screen.__init__(self, session)
192         global plugin_path
193         readme = plugin_path+"/readme.txt"
194         if os_path.exists(readme):
195             fp = open(readme)
196             text = fp.read()
197             fp.close()
198         else:
199             text = "sorry, cant load helptext from file "+readme
200         self["help"] = ScrollLabel(text)
201         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions"], 
202             {
203              "ok": self.close,
204              "back": self.close,
205              "up": self["help"].pageUp,
206              "down": self["help"].pageDown
207              }, -1)
208         
209 ############################################################################### 
210 class StreamMenu(MenuList):
211     def __init__(self, list, enableWrapAround = False):
212         MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
213         self.l.setFont(0, gFont("Regular", 20))
214         self.l.setFont(1, gFont("Regular", 18))
215
216     def postWidgetCreate(self, instance):
217         MenuList.postWidgetCreate(self, instance)
218         instance.setItemHeight(50)
219     
220     def buildList(self,listnew):
221         list=[]
222         for stream in listnew:
223             res = [ stream ]
224             res.append(MultiContentEntryText(pos=(5, 5), size=(500, 25), font=0, text=stream.getName()))
225             res.append(MultiContentEntryText(pos=(5, 26), size=(500, 24), font=1, text=stream.getDescription()))
226             list.append(res)
227         self.l.setList(list)
228         self.moveToIndex(0)
229
230 ###############################################################################        
231 class NETcasterScreenStreamDelete(Screen):
232     def __init__(self, session):
233         self.session = session
234         self.config = NETcasterConfig()
235         streams = self.config.getStreams()
236         streamlist = []
237         for stream in streams:
238             streamlist.append((_(stream.getName()),stream.getName()))
239         self.session.openWithCallback(self.stream2deleteSelected,ChoiceBox,_("select stream to delete"),streamlist)
240         
241     def stream2deleteSelected(self,selectedstreamname):
242         if selectedstreamname is not None:
243             self.stream2delete = selectedstreamname[1]
244             self.session.openWithCallback(self.userIsSure,MessageBox,_("are you shure to delete the stream?\n\n%s" % self.stream2delete), MessageBox.TYPE_YESNO)
245
246     def userIsSure(self,answer):
247         if answer is None:
248             self.cancelWizzard()
249         if answer is False:
250             self.cancelWizzard()
251         else:
252             self.config.deleteStreamWithName(self.stream2delete)
253             
254 ###############################################################################