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