1 // Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
7 Components.utils.import("resource://dactyl/bootstrap.jsm");
8 defineModule("bookmarkcache", {
9 exports: ["Bookmark", "BookmarkCache", "Keyword", "bookmarkcache"],
10 require: ["services", "storage", "util"]
13 var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id");
14 var Keyword = Struct("keyword", "title", "icon", "url");
15 Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
16 update(Bookmark.prototype, {
18 ["keyword", this.keyword, "Keyword"],
19 ["tags", this.tags.join(", "), "Tag"]
20 ].filter(function (item) item[1]),
22 get uri() util.newURI(this.url),
24 encodeURIComponent: function _encodeURIComponent(str) {
25 if (!this.charset || this.charset === "UTF-8")
26 return encodeURIComponent(str);
27 let conv = services.CharsetConv(this.charset);
28 return escape(conv.ConvertFromUnicode(str) + conv.Finish());
31 Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
32 Bookmark.setter("url", function (val) {
34 val = util.newURI(val);
37 services.bookmarks.changeBookmarkURI(this.id, val);
40 Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
41 Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
42 Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
43 Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
44 Bookmark.setter("tags", function (val) {
45 services.tagging.untagURI(this.uri, null);
47 services.tagging.tagURI(this.uri, val);
50 var name = "bookmark-cache";
52 var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
53 POST: "bookmarkProperties/POSTData",
54 CHARSET: "dactyl/charset",
56 init: function init() {
57 services.bookmarks.addObserver(this, false);
60 cleanup: function cleanup() {
61 services.bookmarks.removeObserver(this);
64 __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),
66 get bookmarks() Class.replaceProperty(this, "bookmarks", this.load()),
68 keywords: Class.memoize(function () array.toObject([[b.keyword, b] for (b in this) if (b.keyword)])),
70 rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
71 .map(function (s) services.bookmarks[s]),
73 _deleteBookmark: function deleteBookmark(id) {
74 let result = this.bookmarks[id] || null;
75 delete this.bookmarks[id];
79 _loadBookmark: function loadBookmark(node) {
80 if (node.uri == null) // How does this happen?
82 let uri = util.newURI(node.uri);
83 let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
84 let tags = services.tagging.getTagsForURI(uri, {}) || [];
85 let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
86 let charset = BookmarkCache.getAnnotation(node.itemId, this.CHARSET);
87 return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, charset, node.itemId);
90 annotate: function (id, key, val, timespan) {
92 services.annotation.setItemAnnotation(id, key, val, 0,
93 timespan || services.annotation.EXPIRE_NEVER);
94 else if (services.annotation.itemHasAnnotation(id, key))
95 services.annotation.removeItemAnnotation(id, key);
99 let ids = services.bookmarks.getBookmarkIdsForURI(util.newURI(url), {});
100 for (let id in values(ids))
101 if (id in this.bookmarks)
102 return this.bookmarks[id];
106 readBookmark: function readBookmark(id) ({
108 uri: services.bookmarks.getBookmarkURI(id).spec,
109 title: services.bookmarks.getItemTitle(id)
112 findRoot: function findRoot(id) {
115 id = services.bookmarks.getFolderIdForItem(id);
116 } while (id != services.bookmarks.placesRoot && id != root);
120 isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
123 * Returns true if the given URL is bookmarked and that bookmark is
124 * not a Live Bookmark.
126 * @param {nsIURI|string} url The URL of which to check the bookmarked
130 isBookmarked: function isBookmarked(uri) {
132 uri = util.newURI(uri);
135 return services.bookmarks
136 .getBookmarkIdsForURI(uri, {})
137 .some(this.closure.isRegularBookmark);
144 isRegularBookmark: function isRegularBookmark(id) {
147 if (services.livemark && services.livemark.isLivemark(id))
149 id = services.bookmarks.getFolderIdForItem(id);
150 } while (id != services.bookmarks.placesRoot && id != root);
151 return this.rootFolders.indexOf(root) >= 0;
154 load: function load() {
157 let folders = this.rootFolders.slice();
158 let query = services.history.getNewQuery();
159 let options = services.history.getNewQueryOptions();
160 while (folders.length > 0) {
161 query.setFolders(folders, 1);
163 let result = services.history.executeQuery(query, options);
164 let folder = result.root;
165 folder.containerOpen = true;
167 // iterate over the immediate children of this folder
168 for (let i = 0; i < folder.childCount; i++) {
169 let node = folder.getChild(i);
170 if (node.type == node.RESULT_TYPE_FOLDER) // folder
171 folders.push(node.itemId);
172 else if (node.type == node.RESULT_TYPE_URI) // bookmark
173 bookmarks[node.itemId] = this._loadBookmark(node);
176 // close a container after using it!
177 folder.containerOpen = false;
183 onItemAdded: function onItemAdded(itemId, folder, index) {
184 if (services.bookmarks.getItemType(itemId) == services.bookmarks.TYPE_BOOKMARK) {
185 if (this.isBookmark(itemId)) {
186 let bmark = this._loadBookmark(this.readBookmark(itemId));
187 this.bookmarks[bmark.id] = bmark;
188 storage.fireEvent(name, "add", bmark);
189 delete this.keywords;
193 onItemRemoved: function onItemRemoved(itemId, folder, index) {
194 let result = this._deleteBookmark(itemId);
195 delete this.keywords;
197 storage.fireEvent(name, "remove", result);
199 onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
201 if (property === this.POST)
202 [property, value] = ["post", BookmarkCache.getAnnotation(itemId, property)];
203 else if (property === this.CHARSET)
204 [property, value] = ["charset", BookmarkCache.getAnnotation(itemId, property)];
208 let bookmark = this.bookmarks[itemId];
210 if (property == "keyword")
211 delete this.keywords;
212 if (property == "tags")
213 value = services.tagging.getTagsForURI(bookmark.uri, {});
214 if (property in bookmark) {
215 bookmark[bookmark.members[property]] = value;
216 storage.fireEvent(name, "change", { __proto__: bookmark, changed: property });
221 getAnnotation: function getAnnotation(item, anno)
222 services.annotation.itemHasAnnotation(item, anno) ?
223 services.annotation.getItemAnnotation(item, anno) : null,
224 getFavicon: function getFavicon(uri) {
226 return services.favicon.getFaviconImageForPage(util.newURI(uri)).spec;
236 // vim: set fdm=marker sw=4 sts=4 et ft=javascript: