Merge branch 'vuplus_experimental' of code.vuplus.com:/opt/repository/dvbapp into...
[vuplus_dvbapp] / lib / python / Plugins / Extensions / HbbTV / bookmark.py
1 class BookmarkData:
2         def __init__(self, _id, _title, _url, _parent, _type):
3                 self.mId        = _id
4                 self.mTitle     = _title
5                 self.mUrl       = _url
6                 self.mParent    = _parent
7                 self.mType      = _type
8         def dump(self, _intent='  '):
9                 print "%s-> %d, %s, %s, %d, %d" % (_intent, self.mId, self.mTitle, self.mUrl, self.mParent, self.mType)
10
11 class CategoryData:
12         def __init__(self, _id, _name):
13                 self.mId        = _id
14                 self.mName      = _name
15                 self.mBookmarks = {}
16
17         def setBookmarks(self, _bookmarks):
18                 self.mBookmarks = _bookmarks
19
20         def appendBookmark(self, _bookmark):
21                 self.mBookmarks[_bookmark.mId] = _bookmark
22
23         def dump(self):
24                 print "  -> %d, %s" % (self.mId, self.mName)
25                 for key in self.mBookmarks.iterkeys():
26                         self.mBookmarks[key].dump('      ')
27
28 import ConfigParser
29 class SimpleConfigParser:
30         def __init__(self):
31                 self.mFileName = None
32                 self.mConfig = None
33                 self.mCategoryCurrentIdx = 0
34                 self.mBookmarkCurrentIdx = 0
35                 self.mDataValid = False
36                 self.mPopulateValid = False
37
38         def _read(self):
39                 if self.mDataValid:
40                         return
41                 print "populate!!"
42                 self.mConfig.read(self.mFileName)
43
44                 self.mCategoryCurrentIdx = self.getNumber('__SYS__', 'category_current_idx')
45                 self.mBookmarkCurrentIdx = self.getNumber('__SYS__', 'bookmark_current_idx')
46                 self.mDataValid = True
47
48         def _save(self):
49                 with open(self.mFileName, 'wb') as bookmarkFile:
50                         self.mConfig.write(bookmarkFile)
51                 self.mDataValid = False
52                 self.mPopulateValid = False
53
54         def _del(self, _section, _option=None):
55                 #print _section, ' :', _option
56                 if _option is None:
57                         if not self.exist(_section):
58                                 return
59                         self.mConfig.remove_section(_section)
60                         return
61                 if not self.exist(_section, _option):
62                         return
63                 self.mConfig.remove_option(_section, _option)
64
65         def _get(self, _section, _option, _default):
66                 try:
67                         data = self.mConfig.get(_section, _option)
68                 except Exception, e:
69                         #print e
70                         return _default
71                 else :  return data
72
73         def _set(self, _section, _option, _value):
74                 self.mConfig.set(_section, _option, _value)
75
76         def exist(self, _section, _option=None):
77                 if _option is None:
78                         return self.mConfig.has_section(_section)
79                 return self.mConfig.has_option(_section, _option)
80
81         def setNumber(self, _section, _option, _value):
82                 self._set(_section, _option, str(_value))
83
84         def setString(self, _section, _option, _value):
85                 self._set(_section, _option, _value)
86
87         def getNumber(self, _section, _option, _default=0):
88                 return int(self._get(_section, _option, _default))
89
90         def getString(self, _section, _option, _default=''):
91                 return self._get(_section, _option, _default)
92
93         def delOption(self, _section, _option):
94                 self._del(_section, _option)
95
96         def addSection(self, _section):
97                 self.mConfig.add_section(_section)
98
99         def delSection(self, _section):
100                 self._del(_section)
101
102         def init(self, _fileName):
103                 self.mFileName = _fileName
104                 self.mConfig = ConfigParser.RawConfigParser()
105                 if self.mConfig is None:
106                         return False
107                 self._read()
108                 return True
109
110 class BookmarkManager(SimpleConfigParser):
111         _instance = None
112         def __new__(cls, *args, **kwargs):
113                 if not cls._instance:
114                         cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
115                 return cls._instance
116
117         def __init__(self, _dbFileName):
118                 SimpleConfigParser.__init__(self)
119                 self.mBookmarkRoot = None
120                 self.mDebugEnable = True
121
122                 import os
123                 if not os.path.exists(_dbFileName):
124                         f = file('/proc/stb/info/vumodel')
125                         model = f.read().strip()
126                         f.close()
127                         manualmode = (model == "solo2" or model == "duo2" or model == "solose")
128
129                         os.system('echo "[__SYS__]" > %s'%(_dbFileName))
130                         os.system('echo "category_current_idx = 1" >> %s'%(_dbFileName))
131                         if manualmode :
132                                 os.system('echo "bookmark_current_idx = 2" >> %s'%(_dbFileName))
133                         else:   os.system('echo "bookmark_current_idx = 1" >> %s'%(_dbFileName))
134                         os.system('echo "[c-1]" >> %s'%(_dbFileName))
135                         os.system('echo "id = 1" >> %s'%(_dbFileName))
136                         os.system('echo "name = My favorite" >> %s'%(_dbFileName))
137                         os.system('echo "[b-1]" >> %s'%(_dbFileName))
138                         os.system('echo "url = http://www.vuplus.com/" >> %s'%(_dbFileName))
139                         os.system('echo "id = 1" >> %s'%(_dbFileName))
140                         os.system('echo "parent = 1" >> %s'%(_dbFileName))
141                         os.system('echo "title = Vuplus Home" >> %s'%(_dbFileName))
142                         os.system('echo "type = 0" >> %s'%(_dbFileName))
143                         if manualmode :
144                                 os.system('echo "[b-2]" >> %s'%(_dbFileName))
145                                 os.system('echo "url = file:///usr/local/manual/main.html" >> %s'%(_dbFileName))
146                                 os.system('echo "id = 2" >> %s'%(_dbFileName))
147                                 os.system('echo "parent = 1" >> %s'%(_dbFileName))
148                                 os.system('echo "title = User Manual" >> %s'%(_dbFileName))
149                                 os.system('echo "type = 1" >> %s'%(_dbFileName))
150                 self.init(_dbFileName)
151
152         def message(self, format, params=None):
153                 if not self.mDebugEnable:
154                         return
155                 if params is None:
156                         print format
157                 else:   print format % (params)
158
159         def getBookmark(self, _title):
160                 self.populate()
161                 for key in self.mBookmarkRoot.iterkeys():
162                         for key2 in self.mBookmarkRoot[key].mBookmarks.iterkeys():
163                                 if self.mBookmarkRoot[key].mBookmarks[key2].mTitle == _title:
164                                         return 'b-%d' % (self.mBookmarkRoot[key].mBookmarks[key2].mId)
165                 return None
166
167         def addBookmark(self, _title, _url, _parent, _type):
168                 if self.getBookmark(_title) is not None:
169                         return False
170                 i = self.mBookmarkCurrentIdx + 1
171                 s = "b-%d" % (i,)
172                 self.message("add bookmark : %s, %s, %d, %d", (_title, _url, _parent, _type,))
173
174                 self.mConfig.add_section(s)
175                 self.setNumber(s, 'id', i)
176                 self.setString(s, 'title', _title)
177                 self.setString(s, 'url', _url)
178                 self.setNumber(s, 'parent', _parent)
179                 self.setNumber(s, 'type', _type)
180                 self.setNumber('__SYS__', 'bookmark_current_idx', i)
181                 self._save()
182
183                 return True
184
185         def deleteBookmark(self, _id):
186                 self.populate()
187                 self.message("delete bookmark : %d", (_id,))
188                 self.delSection('b-%d' % (_id,))
189                 self._save()
190
191         def updateBookmark(self, _bookmark):
192                 self.populate()
193                 s = "b-%d" % (_bookmark.mId)
194                 self.message("update bookmark : %s, %s, %d, %d", (_bookmark.mTitle, _bookmark.mUrl, _bookmark.mParent, _bookmark.mType,))
195                 self.setString(s, 'title', _bookmark.mTitle)
196                 self.setString(s, 'url', _bookmark.mUrl)
197                 self.setNumber(s, 'parent', _bookmark.mParent)
198                 self.setNumber(s, 'type', _bookmark.mType)
199                 self._save()
200
201         def getCategory(self, _name):
202                 self.populate()
203                 for key in self.mBookmarkRoot.iterkeys():
204                         if self.mBookmarkRoot[key].mName == _name:
205                                 return 'c-%d' % (self.mBookmarkRoot[key].mId)
206                 return None
207
208         def addCategory(self, _name):
209                 if self.getCategory(_name) is not None:
210                         return False
211                 self.message("add category : %s", (_name,))
212                 i = self.mCategoryCurrentIdx + 1
213                 s = "c-%d" % (i)
214
215                 self.mConfig.add_section(s)
216                 self.setNumber(s, 'id', i)
217                 self.setNumber(s, 'name', _name)
218                 self.setNumber('__SYS__', 'category_current_idx', i)
219                 self._save()
220
221                 return True
222
223         def deleteCategory(self, _id):
224                 self.populate()
225                 self.message("delete category : %d", (_id,))
226                 try:
227                         for key in self.mBookmarkRoot[_id].mBookmarks.iterkeys():
228                                 self.delSection('b-%d' % (key,))
229                 except: pass
230                 self.delSection('c-%d' % (_id,))
231                 self._save()
232
233         def updateCategory(self, _category):
234                 self.populate()
235                 self.message("update category : %s", (_category.mName,))
236                 s = "c-%d" % (_category.mId)
237                 self.setNumber(s, 'name', _category.mName)
238                 self._save()
239
240         def populate(self):
241                 cx, bx = 0, 0
242                 categoryList = {}
243                 self.message("populate : %d, %d", (self.mPopulateValid, self.mDataValid))
244
245                 self._read()
246                 if self.mPopulateValid:
247                         return
248
249                 while cx <= self.mCategoryCurrentIdx:
250                         s = 'c-%d' % (cx,)
251                         i = self.getNumber(s, 'id', -1)
252                         if i != -1:
253                                 n = self.getString(s, 'name')
254                                 categoryList[i] = CategoryData(i, n)
255                         cx += 1
256                 sorted(categoryList)
257                 while bx <= self.mBookmarkCurrentIdx:
258                         s = 'b-%d' % (bx,)
259                         i = self.getNumber(s, 'id', -1)
260                         if i != -1:
261                                 t = self.getString(s, 'title')
262                                 u = self.getString(s, 'url')
263                                 p = self.getNumber(s, 'parent')
264                                 e = self.getNumber(s, 'type')
265                                 try:
266                                         categoryList[p].appendBookmark(BookmarkData(i, t, u, p, e))
267                                 except Exception, e: self._del(s)
268                         bx += 1
269                 for key in categoryList.iterkeys():
270                         sorted(categoryList[key].mBookmarks)
271                 self.mBookmarkRoot = categoryList
272                 self.mPopulateValid = True
273                 self.dump()
274
275         def getBookmarkRoot(self):
276                 self.populate()
277                 return self.mBookmarkRoot
278
279         def dump(self):
280                 if not self.mDebugEnable:
281                         return
282                 self.populate()
283                 print "-- snapshot --"
284                 for key in self.mBookmarkRoot.iterkeys():
285                         self.mBookmarkRoot[key].dump()
286                 print "--------------"
287
288         @staticmethod
289         def getInstance():
290                 return BookmarkManager('/usr/lib/enigma2/python/Plugins/Extensions/HbbTV/bookmark.ini')
291
292