Merge branch 'vuplus_experimental' of code.vuplus.com:/opt/repository/dvbapp into...
[vuplus_dvbapp] / lib / python / Plugins / Extensions / Vbrowser / bookmark.py
1 import vbcfg
2
3 class BookmarkData:
4         def __init__(self, _id, _title, _url, _parent, _type):
5                 self.mId        = _id
6                 self.mTitle     = _title
7                 self.mUrl       = _url
8                 self.mParent    = _parent
9                 self.mType      = _type
10         def dump(self, _intent='  '):
11                 print "%s-> %d, %s, %s, %d, %d" % (_intent, self.mId, self.mTitle, self.mUrl, self.mParent, self.mType)
12
13 class CategoryData:
14         def __init__(self, _id, _name):
15                 self.mId        = _id
16                 self.mName      = _name
17                 self.mBookmarks = {}
18
19         def setBookmarks(self, _bookmarks):
20                 self.mBookmarks = _bookmarks
21
22         def appendBookmark(self, _bookmark):
23                 self.mBookmarks[_bookmark.mId] = _bookmark
24
25         def dump(self):
26                 print "  -> %d, %s" % (self.mId, self.mName)
27                 for key in self.mBookmarks.iterkeys():
28                         self.mBookmarks[key].dump('      ')
29
30 import ConfigParser
31 class SimpleConfigParser:
32         def __init__(self):
33                 self.mFileName = None
34                 self.mConfig = None
35                 self.mCategoryCurrentIdx = 0
36                 self.mBookmarkCurrentIdx = 0
37                 self.mDataValid = False
38                 self.mPopulateValid = False
39
40         def _read(self):
41                 if self.mDataValid:
42                         return
43                 self.mConfig.read(self.mFileName)
44
45                 self.mCategoryCurrentIdx = self.getNumber('__SYS__', 'category_current_idx')
46                 self.mBookmarkCurrentIdx = self.getNumber('__SYS__', 'bookmark_current_idx')
47                 self.mDataValid = True
48
49         def _save(self):
50                 with open(self.mFileName, 'wb') as bookmarkFile:
51                         self.mConfig.write(bookmarkFile)
52                 self.mDataValid = False
53                 self.mPopulateValid = False
54
55         def _del(self, _section, _option=None):
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                         vbcfg.ERR(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                         vbcfg.DEBUG(format)
157                 else:
158                         vbcfg.DEBUG(format % (params))
159
160         def getBookmark(self, _title):
161                 self.populate()
162                 for key in self.mBookmarkRoot.iterkeys():
163                         for key2 in self.mBookmarkRoot[key].mBookmarks.iterkeys():
164                                 if self.mBookmarkRoot[key].mBookmarks[key2].mTitle == _title:
165                                         return 'b-%d' % (self.mBookmarkRoot[key].mBookmarks[key2].mId)
166                 return None
167
168         def addBookmark(self, _title, _url, _parent, _type):
169                 if self.getBookmark(_title) is not None:
170                         return False
171                 i = self.mBookmarkCurrentIdx + 1
172                 s = "b-%d" % (i,)
173                 self.message("add bookmark : %s, %s, %d, %d", (_title, _url, _parent, _type,))
174
175                 self.mConfig.add_section(s)
176                 self.setNumber(s, 'id', i)
177                 self.setString(s, 'title', _title)
178                 self.setString(s, 'url', _url)
179                 self.setNumber(s, 'parent', _parent)
180                 self.setNumber(s, 'type', _type)
181                 self.setNumber('__SYS__', 'bookmark_current_idx', i)
182                 self._save()
183
184                 return True
185
186         def deleteBookmark(self, _id):
187                 self.populate()
188                 self.message("delete bookmark : %d", (_id,))
189                 self.delSection('b-%d' % (_id,))
190                 self._save()
191
192         def updateBookmark(self, _bookmark):
193                 self.populate()
194                 s = "b-%d" % (_bookmark.mId)
195                 self.message("update bookmark : %s, %s, %d, %d", (_bookmark.mTitle, _bookmark.mUrl, _bookmark.mParent, _bookmark.mType,))
196                 self.setString(s, 'title', _bookmark.mTitle)
197                 self.setString(s, 'url', _bookmark.mUrl)
198                 self.setNumber(s, 'parent', _bookmark.mParent)
199                 self.setNumber(s, 'type', _bookmark.mType)
200                 self._save()
201
202         def getCategory(self, _name):
203                 self.populate()
204                 for key in self.mBookmarkRoot.iterkeys():
205                         if self.mBookmarkRoot[key].mName == _name:
206                                 return 'c-%d' % (self.mBookmarkRoot[key].mId)
207                 return None
208
209         def addCategory(self, _name):
210                 if self.getCategory(_name) is not None:
211                         return False
212                 self.message("add category : %s", (_name,))
213                 i = self.mCategoryCurrentIdx + 1
214                 s = "c-%d" % (i)
215
216                 self.mConfig.add_section(s)
217                 self.setNumber(s, 'id', i)
218                 self.setNumber(s, 'name', _name)
219                 self.setNumber('__SYS__', 'category_current_idx', i)
220                 self._save()
221
222                 return True
223
224         def deleteCategory(self, _id):
225                 self.populate()
226                 self.message("delete category : %d", (_id,))
227                 try:
228                         for key in self.mBookmarkRoot[_id].mBookmarks.iterkeys():
229                                 self.delSection('b-%d' % (key,))
230                 except: pass
231                 self.delSection('c-%d' % (_id,))
232                 self._save()
233
234         def updateCategory(self, _category):
235                 self.populate()
236                 self.message("update category : %s", (_category.mName,))
237                 s = "c-%d" % (_category.mId)
238                 self.setNumber(s, 'name', _category.mName)
239                 self._save()
240
241         def populate(self):
242                 cx, bx = 0, 0
243                 categoryList = {}
244                 self.message("populate : %d, %d", (self.mPopulateValid, self.mDataValid))
245
246                 self._read()
247                 if self.mPopulateValid:
248                         return
249
250                 while cx <= self.mCategoryCurrentIdx:
251                         s = 'c-%d' % (cx,)
252                         i = self.getNumber(s, 'id', -1)
253                         if i != -1:
254                                 n = self.getString(s, 'name')
255                                 categoryList[i] = CategoryData(i, n)
256                         cx += 1
257                 sorted(categoryList)
258                 while bx <= self.mBookmarkCurrentIdx:
259                         s = 'b-%d' % (bx,)
260                         i = self.getNumber(s, 'id', -1)
261                         if i != -1:
262                                 t = self.getString(s, 'title')
263                                 u = self.getString(s, 'url')
264                                 p = self.getNumber(s, 'parent')
265                                 e = self.getNumber(s, 'type')
266                                 try:
267                                         categoryList[p].appendBookmark(BookmarkData(i, t, u, p, e))
268                                 except Exception, e: self._del(s)
269                         bx += 1
270                 for key in categoryList.iterkeys():
271                         sorted(categoryList[key].mBookmarks)
272                 self.mBookmarkRoot = categoryList
273                 self.mPopulateValid = True
274                 self.dump()
275
276         def getBookmarkRoot(self):
277                 self.populate()
278                 return self.mBookmarkRoot
279
280         def dump(self):
281                 if not self.mDebugEnable:
282                         return
283                 self.populate()
284                 print "-- snapshot --"
285                 for key in self.mBookmarkRoot.iterkeys():
286                         self.mBookmarkRoot[key].dump()
287                 print "--------------"
288
289         @staticmethod
290         def getInstance():
291                 return BookmarkManager(vbcfg.PLUGINROOT + "/bookmark.ini")
292
293