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 defineModule("bookmarkcache", {
8 exports: ["Bookmark", "BookmarkCache", "Keyword", "bookmarkcache"],
9 require: ["services", "util"]
12 this.lazyRequire("storage", ["storage"]);
14 function newURI(url, charset, base) {
16 return services.io.newURI(url, charset, base);
23 var Bookmark = Struct("url", "title", "icon", "post", "keyword", "tags", "charset", "id");
24 var Keyword = Struct("keyword", "title", "icon", "url");
25 Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
26 update(Bookmark.prototype, {
28 ["keyword", this.keyword, "Keyword"],
29 ["tags", this.tags.join(", "), "Tag"]
30 ].filter(function (item) item[1]),
32 get uri() newURI(this.url),
36 services.bookmarks.changeBookmarkURI(this.id, uri);
40 encodeURIComponent: function _encodeURIComponent(str) {
41 if (!this.charset || this.charset === "UTF-8")
42 return encodeURIComponent(str);
43 let conv = services.CharsetConv(this.charset);
44 return escape(conv.ConvertFromUnicode(str) + conv.Finish()).replace(/\+/g, encodeURIComponent);
49 res.toString = function () this.join("/");
51 let id = this.id, parent, title;
52 while ((id = services.bookmarks.getFolderIdForItem(id)) &&
53 (title = services.bookmarks.getItemTitle(id)))
59 Bookmark.prototype.members.uri = Bookmark.prototype.members.url;
60 Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
61 Bookmark.setter("url", function (val) { this.uri = isString(val) ? newURI(val) : val; });
62 Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
63 Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
64 Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
65 Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
66 Bookmark.setter("tags", function (val) {
67 services.tagging.untagURI(this.uri, null);
69 services.tagging.tagURI(this.uri, val);
72 var name = "bookmark-cache";
74 var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
75 POST: "bookmarkProperties/POSTData",
76 CHARSET: "dactyl/charset",
78 init: function init() {
79 services.bookmarks.addObserver(this, false);
82 cleanup: function cleanup() {
83 services.bookmarks.removeObserver(this);
86 __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),
88 bookmarks: Class.Memoize(function () this.load()),
90 keywords: Class.Memoize(function () array.toObject([[b.keyword, b] for (b in this) if (b.keyword)])),
92 rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
93 .map(function (s) services.bookmarks[s]),
95 _deleteBookmark: function deleteBookmark(id) {
96 let result = this.bookmarks[id] || null;
97 delete this.bookmarks[id];
101 _loadBookmark: function loadBookmark(node) {
102 if (node.uri == null) // How does this happen?
105 let uri = newURI(node.uri);
106 let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
108 let tags = "tags" in node ? (node.tags ? node.tags.split(/, /g) : [])
109 : services.tagging.getTagsForURI(uri, {}) || [];
111 let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
112 let charset = BookmarkCache.getAnnotation(node.itemId, this.CHARSET);
113 return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, charset, node.itemId);
116 annotate: function (id, key, val, timespan) {
118 services.annotation.setItemAnnotation(id, key, val, 0,
119 timespan || services.annotation.EXPIRE_NEVER);
120 else if (services.annotation.itemHasAnnotation(id, key))
121 services.annotation.removeItemAnnotation(id, key);
124 get: function (url) {
125 let ids = services.bookmarks.getBookmarkIdsForURI(newURI(url), {});
126 for (let id in values(ids))
127 if (id in this.bookmarks)
128 return this.bookmarks[id];
132 readBookmark: function readBookmark(id) ({
134 uri: services.bookmarks.getBookmarkURI(id).spec,
135 title: services.bookmarks.getItemTitle(id)
138 findRoot: function findRoot(id) {
141 id = services.bookmarks.getFolderIdForItem(id);
142 } while (id != services.bookmarks.placesRoot && id != root);
146 isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
149 * Returns true if the given URL is bookmarked and that bookmark is
150 * not a Live Bookmark.
152 * @param {nsIURI|string} url The URL of which to check the bookmarked
156 isBookmarked: function isBookmarked(uri) {
161 return services.bookmarks
162 .getBookmarkIdsForURI(uri, {})
163 .some(this.closure.isRegularBookmark);
170 isRegularBookmark: function isRegularBookmark(id) {
173 if (services.livemark && services.livemark.isLivemark(id))
175 id = services.bookmarks.getFolderIdForItem(id);
176 } while (id != services.bookmarks.placesRoot && id != root);
177 return this.rootFolders.indexOf(root) >= 0;
180 load: function load() {
183 let query = services.history.getNewQuery();
184 let options = services.history.getNewQueryOptions();
185 options.queryType = options.QUERY_TYPE_BOOKMARKS;
187 // https://bugzil.la/702639
188 options.excludeItemIfParentHasAnnotation = "livemark/feedURI";
192 let { root } = services.history.executeQuery(query, options);
193 root.containerOpen = true;
195 // iterate over the immediate children of this folder
196 for (let i = 0; i < root.childCount; i++) {
197 let node = root.getChild(i);
198 if (node.type == node.RESULT_TYPE_URI) // bookmark
199 bookmarks[node.itemId] = this._loadBookmark(node);
203 root.containerOpen = false;
209 onItemAdded: function onItemAdded(itemId, folder, index) {
210 if (services.bookmarks.getItemType(itemId) == services.bookmarks.TYPE_BOOKMARK) {
211 if (this.isBookmark(itemId)) {
212 let bmark = this._loadBookmark(this.readBookmark(itemId));
213 this.bookmarks[bmark.id] = bmark;
214 storage.fireEvent(name, "add", bmark);
215 delete this.keywords;
219 onItemRemoved: function onItemRemoved(itemId, folder, index) {
220 let result = this._deleteBookmark(itemId);
221 delete this.keywords;
223 storage.fireEvent(name, "remove", result);
225 onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
227 if (property === this.POST)
228 [property, value] = ["post", BookmarkCache.getAnnotation(itemId, property)];
229 else if (property === this.CHARSET)
230 [property, value] = ["charset", BookmarkCache.getAnnotation(itemId, property)];
234 let bookmark = this.bookmarks[itemId];
236 if (property == "keyword")
237 delete this.keywords;
238 if (property == "tags")
239 value = services.tagging.getTagsForURI(bookmark.uri, {});
240 if (property in bookmark) {
241 bookmark[bookmark.members[property]] = value;
242 storage.fireEvent(name, "change", { __proto__: bookmark, changed: property });
247 DEFAULT_FAVICON: "chrome://mozapps/skin/places/defaultFavicon.png",
249 getAnnotation: function getAnnotation(item, anno)
250 services.annotation.itemHasAnnotation(item, anno) ?
251 services.annotation.getItemAnnotation(item, anno) : null,
253 getFavicon: function getFavicon(uri) {
255 return services.favicon.getFaviconImageForPage(newURI(uri)).spec;
265 // vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: