movietagger: fix imports, remove unused assignment
[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,360" scrollbarMode="showOnDemand" />
47         <widget name="metadata"     position="0,360" size="560,40" transparent="1" valign="left" halign="center" zPosition="5"  foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" font="Regular;21" />
48         <ePixmap name="red"    position="0,400"   zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
49         <ePixmap name="green"  position="140,400" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
50         <ePixmap name="yellow" position="280,400" zPosition="4" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
51         <ePixmap name="blue"   position="420,400" zPosition="4" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
52         <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" />
53         <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"/>
54         <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" />
55         <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" />
56         </screen>"""
57
58     streamlist = []
59     currentPlugin = None
60     def __init__(self, session, args = 0):
61         self.skin = NETcasterScreenBrowser.skin
62         self.session = session
63         Screen.__init__(self, session)
64         self["streamlist"] = StreamMenu([])
65         self["metadata"] = Label("")
66         self["pixred"] = Label("")
67         self["pixgreen"] = Label(_("Play"))
68         self["pixyellow"] = Label("")
69         self["pixblue"] = Label(_("Select"))
70         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","ShortcutActions","GlobalActions","HelpActions"],
71             {
72              "ok": self.ok,
73              "back": self.exit,
74              "red": self.stream_stop,
75              "green": self.stream_start,
76              "yellow": self.yellow,
77              "blue": self.selectPlugin,
78              "menu": self.showMainMenu,
79              "info": self.showAbout,
80              "displayHelp": self.showHelp,
81              }, -1)
82
83         self.onClose.append(self.exit)
84         self.getInterfaceList()
85         for plugin in self.pluginlist:
86             if plugin.nameshort == "Favorites":
87                 self.setCurrentPlugin(plugin)
88         self.onShown.append(self.updateTitle)
89
90         self.onClose.append(self.disconnectFromMetadataUpdates)
91
92     def connectToMetadataUpdates(self):
93         global streamplayer
94         if streamplayer is not None:
95              streamplayer.metadatachangelisteners.append(self.onMetadataChanged)
96
97     def disconnectFromMetadataUpdates(self):
98         global streamplayer
99         try:
100              streamplayer.metadatachangelisteners.remove(self.onMetadataChanged)
101         except Exception,e:
102             pass
103
104     def onMetadataChanged(self,title):
105         try:
106              self["metadata"].setText(title)
107         except Exception,e:
108             self.disconnectFromMetadataUpdates()
109
110     def getInterfaceList(self):
111         self.pluginlist = []
112         global plugin_path,myname
113         interfacepath = plugin_path+"/interface"
114         for iface in os_listdir(interfacepath):
115             if iface.endswith(".py") and not iface.startswith("_"):
116                 pluginp = '.'.join(["Plugins", "Extensions", myname, "interface",iface.replace(".py","")])
117                 plugin = my_import(pluginp)
118                 self.pluginlist.append(plugin.Interface(self.session,cbListLoaded=self.onStreamlistLoaded))
119
120     def updateTitle(self):
121         self.setTitle("%s (%s)"%(myname,self.currentPlugin.nameshort))
122
123     def selectPlugin(self):
124         glist=[]
125         for i in self.pluginlist:
126             glist.append((i.name,i))
127         self.session.openWithCallback(self.selectedPlugin,ChoiceBox,_("select Plugin"),glist)
128
129     def selectedPlugin(self,splugin):
130         if splugin is not None:
131             self.setCurrentPlugin(splugin[1])
132             self.updateTitle()
133
134     def setCurrentPlugin(self, plugin):
135         self.currentPlugin = plugin
136         plugin.getList()
137
138     def ok(self):
139         if self.shown is False:
140             self.show()
141
142     def exit(self):
143         global streamplayer
144         streamplayer.exit()
145         self.close()
146
147     def yellow(self):
148         pass
149
150     def stream_stop(self):
151         global streamplayer
152         if streamplayer.is_playing:
153             print "[",myname,"] stream_startstop -> stop"
154             streamplayer.stop()
155             self.disconnectFromMetadataUpdates()
156             self["pixred"].setText("")
157             self.setTitle("%s (%s)"%(myname,self.currentPlugin.nameshort))
158
159     def stream_start(self):
160         global streamplayer
161         if self["streamlist"].l.getCurrentSelection() is not None:
162             stream = self["streamlist"].l.getCurrentSelection()[0]
163             self.connectToMetadataUpdates()
164             streamplayer.play(stream)
165             self["pixred"].setText(_("Stop"))
166             self.setTitle("%s"%(stream.getName()))
167
168     def onStreamlistLoaded(self,list):
169        self["streamlist"].buildList(list)
170
171     def showMainMenu(self):
172         menu = []
173         if self["streamlist"].l.getCurrentSelection() is not None:
174              selectedStream = self["streamlist"].l.getCurrentSelection()[0]
175         else:
176              selectedStream = None
177         # generic menuitems
178         for p in self.pluginlist:
179             for i in p.getMenuItems(selectedStream,generic=True):
180                 menu.append((i[0],i[1]))
181
182         # non generic menuitems
183         if self.currentPlugin is not None:
184             for i in self.currentPlugin.getMenuItems(selectedStream):
185                 menu.append((i[0],i[1]))
186
187         # std menuitems
188         menu.append((_("hide"), self.hide))
189         menu.append((_("info"), self.showAbout));
190         menu.append((_("help"), self.showHelp));
191         self.session.openWithCallback(self.menuCallback, ChoiceBox, title=_("Menu"), list=menu)
192
193     def menuCallback(self,choice):
194         if choice is not None:
195             choice[1]()
196
197     def showAbout(self):
198         self.session.open(MessageBox,_("%s Enigma2 Plugin V%s (Patched)" % (myname,myversion)), MessageBox.TYPE_INFO)
199
200     def showHelp(self):
201         self.session.open(NETcasterScreenHelp)
202
203
204
205 ###############################################################################
206 class NETcasterScreenHelp(Screen):
207     skin = """
208         <screen position="103,73" size="500,400" title="NETcaster Help" >
209             <widget name="help" position="0,0" size="500,400" font="Regular;18"/>
210         </screen>"""
211
212     def __init__(self, session, args = 0):
213         self.skin = NETcasterScreenHelp.skin
214         Screen.__init__(self, session)
215         global plugin_path
216         readme = plugin_path+"/readme.txt"
217         if os_path.exists(readme):
218             fp = open(readme)
219             text = fp.read()
220             fp.close()
221         else:
222             text = "sorry, cant load helptext from file "+readme
223         self["help"] = ScrollLabel(text)
224         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions"],
225             {
226              "ok": self.close,
227              "back": self.close,
228              "up": self["help"].pageUp,
229              "down": self["help"].pageDown
230              }, -1)
231
232 ###############################################################################
233 class StreamMenu(MenuList):
234     def __init__(self, list, enableWrapAround = False):
235         MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
236         self.l.setFont(0, gFont("Regular", 20))
237         self.l.setFont(1, gFont("Regular", 18))
238
239     def postWidgetCreate(self, instance):
240         MenuList.postWidgetCreate(self, instance)
241         instance.setItemHeight(50)
242
243     def buildList(self,listnew):
244         list=[]
245         for stream in listnew:
246             res = [ stream ]
247             res.append(MultiContentEntryText(pos=(5, 5), size=(500, 25), font=0, text=stream.getName()))
248             res.append(MultiContentEntryText(pos=(5, 26), size=(500, 24), font=1, text=stream.getDescription()))
249             list.append(res)
250         self.l.setList(list)
251         self.moveToIndex(0)
252
253 ###############################################################################
254 class NETcasterScreenStreamDelete(Screen):
255     def __init__(self, session):
256         self.session = session
257         self.config = NETcasterConfig()
258         streams = self.config.getStreams()
259         streamlist = []
260         for stream in streams:
261             streamlist.append((_(stream.getName()),stream.getName()))
262         self.session.openWithCallback(self.stream2deleteSelected,ChoiceBox,_("select stream to delete"),streamlist)
263
264     def stream2deleteSelected(self,selectedstreamname):
265         if selectedstreamname is not None:
266             self.stream2delete = selectedstreamname[1]
267             self.session.openWithCallback(self.userIsSure,MessageBox,_("are you shure to delete the stream?\n\n%s" % self.stream2delete), MessageBox.TYPE_YESNO)
268
269     def userIsSure(self,answer):
270         if answer is None:
271             self.cancelWizzard()
272         if answer is False:
273             self.cancelWizzard()
274         else:
275             self.config.deleteStreamWithName(self.stream2delete)
276
277 ###############################################################################