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", "util"]
13 this.lazyRequire("storage", ["storage"]);
15 function newURI(url, charset, base) {
17 return services.io.newURI(url, charset, base);
24 var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id");
25 var Keyword = Struct("keyword", "title", "icon", "url");
26 Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
27 update(Bookmark.prototype, {
29 ["keyword", this.keyword, "Keyword"],
30 ["tags", this.tags.join(", "), "Tag"]
31 ].filter(function (item) item[1]),
33 get uri() newURI(this.url),
37 services.bookmarks.changeBookmarkURI(this.id, uri);
41 encodeURIComponent: function _encodeURIComponent(str) {
42 if (!this.charset || this.charset === "UTF-8")
43 return encodeURIComponent(str);
44 let conv = services.CharsetConv(this.charset);
45 return escape(conv.ConvertFromUnicode(str) + conv.Finish()).replace(/\+/g, encodeURIComponent);
50 res.toString = function () this.join("/");
52 let id = this.id, parent, title;
53 while ((id = services.bookmarks.getFolderIdForItem(id)) &&
54 (title = services.bookmarks.getItemTitle(id)))
60 Bookmark.prototype.members.uri = Bookmark.prototype.members.url;
61 Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
62 Bookmark.setter("url", function (val) { this.uri = isString(val) ? newURI(val) : val; });
63 Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
64 Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
65 Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
66 Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
67 Bookmark.setter("tags", function (val) {
68 services.tagging.untagURI(this.uri, null);
70 services.tagging.tagURI(this.uri, val);
73 var name = "bookmark-cache";
75 var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
76 POST: "bookmarkProperties/POSTData",
77 CHARSET: "dactyl/charset",
79 init: function init() {
80 services.bookmarks.addObserver(this, false);
83 cleanup: function cleanup() {
84 services.bookmarks.removeObserver(this);
87 __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),
89 bookmarks: Class.Memoize(function () this.load()),
91 keywords: Class.Memoize(function () array.toObject([[b.keyword, b] for (b in this) if (b.keyword)])),
93 rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
94 .map(function (s) services.bookmarks[s]),
96 _deleteBookmark: function deleteBookmark(id) {
97 let result = this.bookmarks[id] || null;
98 delete this.bookmarks[id];
102 _loadBookmark: function loadBookmark(node) {
103 if (node.uri == null) // How does this happen?
106 let uri = newURI(node.uri);
107 let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
109 let tags = "tags" in node ? (node.tags ? node.tags.split(/, /g) : [])
110 : services.tagging.getTagsForURI(uri, {}) || [];
112 let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
113 let charset = BookmarkCache.getAnnotation(node.itemId, this.CHARSET);
114 return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, charset, node.itemId);
117 annotate: function (id, key, val, timespan) {
119 services.annotation.setItemAnnotation(id, key, val, 0,
120 timespan || services.annotation.EXPIRE_NEVER);
121 else if (services.annotation.itemHasAnnotation(id, key))
122 services.annotation.removeItemAnnotation(id, key);
125 get: function (url) {
126 let ids = services.bookmarks.getBookmarkIdsForURI(newURI(url), {});
127 for (let id in values(ids))
128 if (id in this.bookmarks)
129 return this.bookmarks[id];
133 readBookmark: function readBookmark(id) ({
135 uri: services.bookmarks.getBookmarkURI(id).spec,
136 title: services.bookmarks.getItemTitle(id)
139 findRoot: function findRoot(id) {
142 id = services.bookmarks.getFolderIdForItem(id);
143 } while (id != services.bookmarks.placesRoot && id != root);
147 isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
150 * Returns true if the given URL is bookmarked and that bookmark is
151 * not a Live Bookmark.
153 * @param {nsIURI|string} url The URL of which to check the bookmarked
157 isBookmarked: function isBookmarked(uri) {
162 return services.bookmarks
163 .getBookmarkIdsForURI(uri, {})
164 .some(this.closure.isRegularBookmark);
171 isRegularBookmark: function isRegularBookmark(id) {
174 if (services.livemark && services.livemark.isLivemark(id))
176 id = services.bookmarks.getFolderIdForItem(id);
177 } while (id != services.bookmarks.placesRoot && id != root);
178 return this.rootFolders.indexOf(root) >= 0;
181 load: function load() {
184 let query = services.history.getNewQuery();
185 let options = services.history.getNewQueryOptions();
186 options.queryType = options.QUERY_TYPE_BOOKMARKS;
188 // https://bugzil.la/702639
189 options.excludeItemIfParentHasAnnotation = "livemark/feedURI";
193 let { root } = services.history.executeQuery(query, options);
194 root.containerOpen = true;
196 // iterate over the immediate children of this folder
197 for (let i = 0; i < root.childCount; i++) {
198 let node = root.getChild(i);
199 if (node.type == node.RESULT_TYPE_URI) // bookmark
200 bookmarks[node.itemId] = this._loadBookmark(node);
204 root.containerOpen = false;
210 onItemAdded: function onItemAdded(itemId, folder, index) {
211 if (services.bookmarks.getItemType(itemId) == services.bookmarks.TYPE_BOOKMARK) {
212 if (this.isBookmark(itemId)) {
213 let bmark = this._loadBookmark(this.readBookmark(itemId));
214 this.bookmarks[bmark.id] = bmark;
215 storage.fireEvent(name, "add", bmark);
216 delete this.keywords;
220 onItemRemoved: function onItemRemoved(itemId, folder, index) {
221 let result = this._deleteBookmark(itemId);
222 delete this.keywords;
224 storage.fireEvent(name, "remove", result);
226 onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
228 if (property === this.POST)
229 [property, value] = ["post", BookmarkCache.getAnnotation(itemId, property)];
230 else if (property === this.CHARSET)
231 [property, value] = ["charset", BookmarkCache.getAnnotation(itemId, property)];
235 let bookmark = this.bookmarks[itemId];
237 if (property == "keyword")
238 delete this.keywords;
239 if (property == "tags")
240 value = services.tagging.getTagsForURI(bookmark.uri, {});
241 if (property in bookmark) {
242 bookmark[bookmark.members[property]] = value;
243 storage.fireEvent(name, "change", { __proto__: bookmark, changed: property });
248 DEFAULT_FAVICON: "chrome://mozapps/skin/places/defaultFavicon.png",
250 getAnnotation: function getAnnotation(item, anno)
251 services.annotation.itemHasAnnotation(item, anno) ?
252 services.annotation.getItemAnnotation(item, anno) : null,
254 getFavicon: function getFavicon(uri) {
256 return services.favicon.getFaviconImageForPage(newURI(uri)).spec;
266 // vim: set fdm=marker sw=4 sts=4 et ft=javascript: