[InternetRadio] initial commit of InternetRadio plugin (with music-visualization...
[enigma2-plugins.git] / internetradio / src / InternetRadioFavoriteConfig.py
1 #
2 # InternetRadio E2
3 #
4 # Coded by Dr.Best (c) 2012
5 # Support: www.dreambox-tools.info
6 # E-Mail: dr.best@dreambox-tools.info
7 #
8 # This plugin is open source but it is NOT free software.
9 #
10 # This plugin may only be distributed to and executed on hardware which
11 # is licensed by Dream Multimedia GmbH.
12 # In other words:
13 # It's NOT allowed to distribute any parts of this plugin or its source code in ANY way
14 # to hardware which is NOT licensed by Dream Multimedia GmbH.
15 # It's NOT allowed to execute this plugin and its source code or even parts of it in ANY way
16 # on hardware which is NOT licensed by Dream Multimedia GmbH.
17 #
18 # If you want to use or modify the code or parts of it,
19 # you have to keep MY license and inform me about the modifications by mail.
20 #
21
22 import os
23 from Components.config import config, ConfigSubsection, Config, ConfigInteger, ConfigSubList, ConfigText
24 from enigma import eEnv
25
26 class Favorite:
27         def __init__(self, configItem = None):
28                 self.configItem = configItem
29
30 class InternetRadioFavoriteConfig(object):
31         FAVORITE_FILE_DEFAULT =  eEnv.resolve('${libdir}/enigma2/python/Plugins/Extensions/InternetRadio/internetradio_favorites') #'/usr/lib/enigma2/python/Plugins/Extensions/InternetRadio/internetradio_favorites'
32         FAVORITE_FILE = eEnv.resolve("${sysconfdir}/enigma2/internetradio_favorites.user") #'/etc/enigma2/internetradio_favorites.user'
33         def __init__(self):
34                 self.loadFavoriteConfig()
35
36         def loadFavoriteConfig(self):
37                 self.favoriteConfig = Config()
38                 if os.path.exists(self.FAVORITE_FILE):
39                         self.favoriteConfig.loadFromFile(self.FAVORITE_FILE)
40                 else:
41                         self.favoriteConfig.loadFromFile(self.FAVORITE_FILE_DEFAULT)
42                 self.favoriteConfig.entriescount =  ConfigInteger(0)
43                 self.favoriteConfig.Entries = ConfigSubList()
44                 self.initFavouriteConfig()
45                 
46         def initFavouriteEntryConfig(self):
47                 self.favoriteConfig.Entries.append(ConfigSubsection())
48                 i = len(self.favoriteConfig.Entries) -1
49                 self.favoriteConfig.Entries[i].name = ConfigText(default = "")
50                 self.favoriteConfig.Entries[i].text = ConfigText(default = "")
51                 self.favoriteConfig.Entries[i].type = ConfigInteger(0)
52                 self.favoriteConfig.Entries[i].tags = ConfigText(default = "")
53                 self.favoriteConfig.Entries[i].country = ConfigText(default = "")
54                 self.favoriteConfig.Entries[i].homepage = ConfigText(default = "")
55                 return self.favoriteConfig.Entries[i]
56
57         def initFavouriteConfig(self):
58                 count = self.favoriteConfig.entriescount.value
59                 if count != 0:
60                         i = 0
61                         while i < count:
62                                 self.initFavouriteEntryConfig()
63                                 i += 1
64                                 
65         def addFavorite(self, name = "", text = "", favoritetype = "", tags = "", country = "", homepage = ""):
66                 self.favoriteConfig.entriescount.value = self.favoriteConfig.entriescount.value + 1
67                 self.favoriteConfig.entriescount.save()
68                 newFavorite = self.initFavouriteEntryConfig()
69                 newFavorite.name.value = name
70                 newFavorite.text.value = text
71                 newFavorite.type.value = favoritetype
72                 newFavorite.tags.value = tags
73                 newFavorite.country.value = country
74                 newFavorite.homepage.value = homepage
75                 newFavorite.save()
76                 self.favoriteConfig.saveToFile(self.FAVORITE_FILE)
77                 
78         def renameFavorite(self, configItem, text):
79                         configItem.name.value = text
80                         configItem.save()
81                         self.favoriteConfig.saveToFile(self.FAVORITE_FILE)
82                         
83         def removeFavorite(self, configItem):
84                 if configItem is not None:
85                         self.favoriteConfig.entriescount.value = self.favoriteConfig.entriescount.value - 1
86                         self.favoriteConfig.entriescount.save()
87                         self.favoriteConfig.Entries.remove(configItem)
88                         self.favoriteConfig.Entries.save()
89                         self.favoriteConfig.saveToFile(self.FAVORITE_FILE)
90                         
91         def removeFavoriteHTML(self, name, text, favoritetype):
92                 result = 0
93                 for item in self.favoriteConfig.Entries:
94                         if item.name.value == name and item.text.value == text and item.type.value == favoritetype:
95                                 result = 1
96                                 self.removeFavorite(item)
97                                 break
98                 return result
99                 
100         def renameFavoriteHTML(self, name, text, favoritetype, newname):
101                 result = 0
102                 for item in self.favoriteConfig.Entries:
103                         if item.name.value == name and item.text.value == text and item.type.value == favoritetype:
104                                 result = 1
105                                 self.renameFavorite(item, newname)
106                                 break
107                 return result
108                         
109         def getFavoriteList(self, html = False):
110                 favoriteList = []
111                 for item in self.favoriteConfig.Entries:
112                         if html == True:
113                                 favoriteList.append((item.name.value, item.text.value, item.type.value, item.tags.value, item.country.value, item.homepage.value))
114                         else:
115                                 favoriteList.append(((Favorite(item)),))
116                 return favoriteList