diff options
Diffstat (limited to 'common/modules/bookmarkcache.jsm')
-rw-r--r-- | common/modules/bookmarkcache.jsm | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/common/modules/bookmarkcache.jsm b/common/modules/bookmarkcache.jsm index b2f3d621..cf73277f 100644 --- a/common/modules/bookmarkcache.jsm +++ b/common/modules/bookmarkcache.jsm @@ -10,7 +10,7 @@ defineModule("bookmarkcache", { require: ["services", "storage", "util"] }); -const Bookmark = Struct("url", "title", "icon", "keyword", "tags", "id"); +const Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "id"); const Keyword = Struct("keyword", "title", "icon", "url"); Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url)); Bookmark.prototype.__defineGetter__("extra", function () [ @@ -18,12 +18,15 @@ Bookmark.prototype.__defineGetter__("extra", function () [ ["tags", this.tags.join(", "), "Tag"] ].filter(function (item) item[1])); -const bookmarks = services.get("bookmarks"); -const history = services.get("history"); -const tagging = services.get("tagging"); -const name = "bookmark-cache"; +const annotation = services.get("annotation"); +const bookmarks = services.get("bookmarks"); +const history = services.get("history"); +const tagging = services.get("tagging"); +const name = "bookmark-cache"; const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), { + POST: "bookmarkProperties/POSTData", + init: function init() { bookmarks.addObserver(this, false); }, @@ -32,17 +35,14 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), get bookmarks() Class.replaceProperty(this, "bookmarks", this.load()), + get keywords() array.toObject([[b.keyword, b] for (b in this) if (b.keyword)]), + rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"] .map(function (s) bookmarks[s]), _deleteBookmark: function deleteBookmark(id) { - let length = this.bookmarks.length; - let result; - this.bookmarks = this.bookmarks.filter(function (item) { - if (item.id == id) - result = item; - return item.id != id; - }); + let result = this.bookmarks[item.id] || null; + delete this.bookmarks[id]; return result; }, @@ -52,7 +52,8 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), let uri = util.newURI(node.uri); let keyword = bookmarks.getKeywordForBookmark(node.itemId); let tags = tagging.getTagsForURI(uri, {}) || []; - return Bookmark(node.uri, node.title, node.icon && node.icon.spec, keyword, tags, node.itemId); + let post = BookmarkCache.getAnnotation(node.itemId, this.POST); + return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, node.itemId); }, readBookmark: function readBookmark(id) { @@ -83,11 +84,9 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), return this.rootFolders.indexOf(root) >= 0; }, - get keywords() [Keyword(k.keyword, k.title, k.icon, k.url) for ([, k] in Iterator(this.bookmarks)) if (k.keyword)], - // Should be made thread safe. load: function load() { - let bookmarks = []; + let bookmarks = {}; let folders = this.rootFolders.slice(); let query = history.getNewQuery(); @@ -105,7 +104,7 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), if (node.type == node.RESULT_TYPE_FOLDER) // folder folders.push(node.itemId); else if (node.type == node.RESULT_TYPE_URI) // bookmark - bookmarks.push(this._loadBookmark(node)); + bookmarks[node.itemId] = this._loadBookmark(node); } // close a container after using it! @@ -119,7 +118,7 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), if (bookmarks.getItemType(itemId) == bookmarks.TYPE_BOOKMARK) { if (this.isBookmark(itemId)) { let bmark = this._loadBookmark(this.readBookmark(itemId)); - this.bookmarks.push(bmark); + this.bookmarks[bmark.id] = bmark; storage.fireEvent(name, "add", bmark); } } @@ -131,8 +130,12 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), }, onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) { if (isAnnotation) - return; - let bookmark = bookmarkcache.bookmarks.filter(function (item) item.id == itemId)[0]; + if (property === this.POST) + [property, value] = ["post", BookmarkCache.getAnnotation(itemId, this.POST)]; + else + return; + + let bookmark = this.bookmarks[itemId]; if (bookmark) { if (property == "tags") value = tagging.getTagsForURI(util.newURI(bookmark.url), {}); @@ -143,6 +146,9 @@ const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), } } }, { + getAnnotation: function getAnnotation(item, anno) + annotation.itemHasAnnotation(item, anno) ? + annotation.getItemAnnotation(item, anno) : null, getFavicon: function getFavicon(uri) { try { return service.get("favicon").getFaviconImageForPage(util.newURI(uri)).spec; |