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());
48 Bookmark.prototype.members.uri = Bookmark.prototype.members.url;
49 Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
50 Bookmark.setter("url", function (val) { this.uri = isString(val) ? newURI(val) : val; });
51 Bookmark.setter("title", function (val) { services.bookmarks.setItemTitle(this.id, val); });
52 Bookmark.setter("post", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.POST, val); });
53 Bookmark.setter("charset", function (val) { bookmarkcache.annotate(this.id, bookmarkcache.CHARSET, val); });
54 Bookmark.setter("keyword", function (val) { services.bookmarks.setKeywordForBookmark(this.id, val); });
55 Bookmark.setter("tags", function (val) {
56 services.tagging.untagURI(this.uri, null);
58 services.tagging.tagURI(this.uri, val);
61 var name = "bookmark-cache";
63 var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
64 POST: "bookmarkProperties/POSTData",
65 CHARSET: "dactyl/charset",
67 init: function init() {
68 services.bookmarks.addObserver(this, false);
71 cleanup: function cleanup() {
72 services.bookmarks.removeObserver(this);
75 __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),
77 bookmarks: Class.Memoize(function () this.load()),
79 keywords: Class.Memoize(function () array.toObject([[b.keyword, b] for (b in this) if (b.keyword)])),
81 rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
82 .map(function (s) services.bookmarks[s]),
84 _deleteBookmark: function deleteBookmark(id) {
85 let result = this.bookmarks[id] || null;
86 delete this.bookmarks[id];
90 _loadBookmark: function loadBookmark(node) {
91 if (node.uri == null) // How does this happen?
94 let uri = newURI(node.uri);
95 let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
97 let tags = tags in node ? (node.tags ? node.tags.split(/, /g) : [])
98 : services.tagging.getTagsForURI(uri, {}) || [];
100 let post = BookmarkCache.getAnnotation(node.itemId, this.POST);
101 let charset = BookmarkCache.getAnnotation(node.itemId, this.CHARSET);
102 return Bookmark(node.uri, node.title, node.icon && node.icon.spec, post, keyword, tags, charset, node.itemId);
105 annotate: function (id, key, val, timespan) {
107 services.annotation.setItemAnnotation(id, key, val, 0,
108 timespan || services.annotation.EXPIRE_NEVER);
109 else if (services.annotation.itemHasAnnotation(id, key))
110 services.annotation.removeItemAnnotation(id, key);
113 get: function (url) {
114 let ids = services.bookmarks.getBookmarkIdsForURI(newURI(url), {});
115 for (let id in values(ids))
116 if (id in this.bookmarks)
117 return this.bookmarks[id];
121 readBookmark: function readBookmark(id) ({
123 uri: services.bookmarks.getBookmarkURI(id).spec,
124 title: services.bookmarks.getItemTitle(id)
127 findRoot: function findRoot(id) {
130 id = services.bookmarks.getFolderIdForItem(id);
131 } while (id != services.bookmarks.placesRoot && id != root);
135 isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
138 * Returns true if the given URL is bookmarked and that bookmark is
139 * not a Live Bookmark.
141 * @param {nsIURI|string} url The URL of which to check the bookmarked
145 isBookmarked: function isBookmarked(uri) {
150 return services.bookmarks
151 .getBookmarkIdsForURI(uri, {})
152 .some(this.closure.isRegularBookmark);
159 isRegularBookmark: function isRegularBookmark(id) {
162 if (services.livemark && services.livemark.isLivemark(id))
164 id = services.bookmarks.getFolderIdForItem(id);
165 } while (id != services.bookmarks.placesRoot && id != root);
166 return this.rootFolders.indexOf(root) >= 0;
169 load: function load() {
172 let query = services.history.getNewQuery();
173 let options = services.history.getNewQueryOptions();
174 options.queryType = options.QUERY_TYPE_BOOKMARKS;
175 options.excludeItemIfParentHasAnnotation = "livemark/feedURI";
177 let { root } = services.history.executeQuery(query, options);
178 root.containerOpen = true;
180 // iterate over the immediate children of this folder
181 for (let i = 0; i < root.childCount; i++) {
182 let node = root.getChild(i);
183 if (node.type == node.RESULT_TYPE_URI) // bookmark
184 bookmarks[node.itemId] = this._loadBookmark(node);
188 root.containerOpen = false;
194 onItemAdded: function onItemAdded(itemId, folder, index) {
195 if (services.bookmarks.getItemType(itemId) == services.bookmarks.TYPE_BOOKMARK) {
196 if (this.isBookmark(itemId)) {
197 let bmark = this._loadBookmark(this.readBookmark(itemId));
198 this.bookmarks[bmark.id] = bmark;
199 storage.fireEvent(name, "add", bmark);
200 delete this.keywords;
204 onItemRemoved: function onItemRemoved(itemId, folder, index) {
205 let result = this._deleteBookmark(itemId);
206 delete this.keywords;
208 storage.fireEvent(name, "remove", result);
210 onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
212 if (property === this.POST)
213 [property, value] = ["post", BookmarkCache.getAnnotation(itemId, property)];
214 else if (property === this.CHARSET)
215 [property, value] = ["charset", BookmarkCache.getAnnotation(itemId, property)];
219 let bookmark = this.bookmarks[itemId];
221 if (property == "keyword")
222 delete this.keywords;
223 if (property == "tags")
224 value = services.tagging.getTagsForURI(bookmark.uri, {});
225 if (property in bookmark) {
226 bookmark[bookmark.members[property]] = value;
227 storage.fireEvent(name, "change", { __proto__: bookmark, changed: property });
232 DEFAULT_FAVICON: "chrome://mozapps/skin/places/defaultFavicon.png",
234 getAnnotation: function getAnnotation(item, anno)
235 services.annotation.itemHasAnnotation(item, anno) ?
236 services.annotation.getItemAnnotation(item, anno) : null,
238 getFavicon: function getFavicon(uri) {
240 return services.favicon.getFaviconImageForPage(newURI(uri)).spec;
250 // vim: set fdm=marker sw=4 sts=4 et ft=javascript: