diff options
author | Sayamindu Dasgupta <sayamindu@gmail.com> | 2009-02-15 23:28:29 (GMT) |
---|---|---|
committer | Sayamindu Dasgupta <sayamindu@gmail.com> | 2009-02-15 23:28:29 (GMT) |
commit | ac6ac17eaad5859e570d82470d7c7dbb00d02477 (patch) | |
tree | 1aa3b0ec0856ce4d4ff2adc1f152f811a2718fb7 /readdb.py | |
parent | ad9a4d59d2a62dac7d3f1768d9dc1be37cd9b666 (diff) |
Bookmark support
Diffstat (limited to 'readdb.py')
-rw-r--r-- | readdb.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/readdb.py b/readdb.py new file mode 100644 index 0000000..592313b --- /dev/null +++ b/readdb.py @@ -0,0 +1,65 @@ +import logging + +import sqlite3 +import time + +import gconf + +from readbookmark import Bookmark + +_logger = logging.getLogger('read-activity') + +class BookmarkManager: + def __init__(self, hash, dbpath='read.db'): + self._hash = hash + self._conn = sqlite3.connect(dbpath) + self._cur = self._conn.cursor() + + self._bookmarks = [] + self._populate_bookmarks() + + def add_bookmark(self, page, title, local=1): + # locale = 0 means that this is a bookmark originally + # created by the person who originally shared the file + timestamp = time.time() + client = gconf.client_get_default() + user = client.get_string("/desktop/sugar/user/nick") + color = client.get_string("/desktop/sugar/user/color") + + t = (self._hash, page, title, timestamp, user, color, local) + self._cur.execute('insert into bookmarks values (?, ?, ?, ?, ?, ?, ?)', t) + self._conn.commit() + + self._resync_bookmark_cache() + + def del_bookmark(self, page): + client = gconf.client_get_default() + user = client.get_string("/desktop/sugar/user/nick") + + # We delete only the locally made bookmark + + t = (self._hash, page, user) + self._cur.execute('delete from bookmarks where md5=? and page=? and user=?', t) + self._conn.commit() + + self._resync_bookmark_cache() + + def _populate_bookmarks(self): + # TODO: Figure out if caching the entire set of bookmarks is a good idea or not + self._cur.execute('select * from bookmarks where md5=?', (self._hash,)) + + for row in self._cur: + self._bookmarks.append(Bookmark(row)) + + def get_bookmarks_for_page(self, page): + bookmarks = [] + for bookmark in self._bookmarks: + if bookmark.belongstopage(page): + bookmarks.append(bookmark) + + return bookmarks + + def _resync_bookmark_cache(self): + # To be called when a new bookmark has been added/removed + self._bookmarks = [] + self._populate_bookmarks()
\ No newline at end of file |