adjust favorites to interface changes
[enigma2-plugins.git] / netcaster / src / bin / interface / favorites.py
1 from Plugins.Extensions.NETcaster.StreamInterface import StreamInterface
2 from Plugins.Extensions.NETcaster.StreamInterface import Stream
3 from Plugins.Extensions.NETcaster.plugin import myname
4 from ConfigParser import ConfigParser, DuplicateSectionError
5
6 from Tools.BoundFunction import boundFunction
7
8 ####################################################################
9
10 class Interface(StreamInterface):
11     name= "Your saved Favorites"
12     nameshort = "Favorites"
13     description = "you can save Streams in your Favorites in a local list, to exec them directly without search for long time."
14     selectedStream = None
15     def getList(self):
16         list = []
17         for stream in SHOUTcasterFavorites().getStreams():
18             list.append(stream)
19         self.list = list
20         self.OnListLoaded()
21     def getMenuItems(self,selectedStream,generic=False):
22         self.selectedStream = selectedStream
23         list = []
24         if generic is True and selectedStream is not None:
25             if selectedStream.isFavorite() is False:
26                 list.append((_("add stream to favorites"), self.addStream))            
27         elif generic is False and selectedStream is not None:
28             if selectedStream.isFavorite() is False:
29                 list.append((_("add stream to favorites"), self.addStream))
30             if selectedStream.isFavorite() is True:
31                 list.append((_("delete stream from favorites"), self.deleteStream))
32         return list
33     
34     def deleteStream(self):
35         print "favorites deleteStream"
36         if self.selectedStream is not None:
37             SHOUTcasterFavorites().deleteStreamWithName(self.selectedStream.getName())
38         self.getList()
39     def addStream(self):
40         print "favorites addStream"
41         if self.selectedStream is not None:
42             SHOUTcasterFavorites().addStream(self.selectedStream)
43         #self.getList()
44 ############################################################################### 
45 class SHOUTcasterFavorites:
46     configfile = "/etc/NETcaster.conf"
47     def __init__(self):
48         self.configparser = ConfigParser()
49         self.configparser.read(self.configfile)
50     def getStreams(self):
51         streams=[]
52         sections = self.configparser.sections()
53         print sections
54         for section in sections:
55                 stream = self.getStreamByName(section)
56                 streams.append(stream)
57         return streams
58     def isStream(self,streamname):
59         if self.configparser.has_section(streamname) is True:
60             return True
61         else:
62             return False
63     def getStreamByName(self,streamname):
64         print "["+myname+"] load "+streamname+" from config"
65         if self.isStream(streamname) is True:
66             stream = Stream(
67                         streamname,
68                         self.configparser.get(streamname, "description"),
69                         self.configparser.get(streamname, "url"),
70                         type=self.configparser.get(streamname, "type")
71                     )
72             stream.setFavorite(True)
73             return stream
74         else:
75             return False
76
77     def addStream(self, stream):
78         print "["+myname+"] adding "+stream.getName()+" to config"
79         try:
80             self.configparser.add_section(stream.getName())
81         except DuplicateSectionError,e:
82             print "["+myname+"] error while adding stream to config:",e
83             return False,e
84         else:
85             # XXX: I hope this still works properly if we make a optimistic
86             # return here since otherwise the interface would need to be changed
87             # to work with a callback
88             stream.getURL(boundFunction(self.addStreamCb, stream))
89             return True,"Stream added"
90
91     def addStreamCb(self, stream, url = None):
92         self.configparser.set(stream.getName(), "description", stream.getDescription())
93         self.configparser.set(stream.getName(), "url", url)
94         self.configparser.set(stream.getName(), "type", stream.getType())
95         self.writeConfig()
96
97     def changeStream(self,streamold,streamnew):
98         if self.configparser.has_section(streamold.getName()) is False:
99             return False,"stream not found in config"
100         elif self.configparser.has_section(streamnew.getName()) is True:
101             return False,"stream with that name exists already"
102         else:    
103            self.configparser.remove_section(streamold.getName())
104            return self.addStream(streamnew); 
105         
106     def deleteStreamWithName(self,streamname):
107         self.configparser.remove_section(streamname)
108         self.writeConfig()
109         
110     def writeConfig(self):
111         print "["+myname+"] writing config to "+self.configfile
112         
113         fp = open(self.configfile,"w")
114         self.configparser.write(fp)
115         fp.close()
116