]> git.donarmstrong.com Git - dactyl.git/blob - common/modules/bookmarkcache.jsm
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / common / modules / bookmarkcache.jsm
1 // Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail>
2 //
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
5 /* use strict */
6
7 Components.utils.import("resource://dactyl/bootstrap.jsm");
8 defineModule("bookmarkcache", {
9     exports: ["Bookmark", "BookmarkCache", "Keyword", "bookmarkcache"],
10     require: ["services", "util"]
11 }, this);
12
13 this.lazyRequire("storage", ["storage"]);
14
15 function newURI(url, charset, base) {
16     try {
17         return services.io.newURI(url, charset, base);
18     }
19     catch (e) {
20         throw Error(e);
21     }
22 }
23
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, {
28     get extra() [
29         ["keyword", this.keyword,         "Keyword"],
30         ["tags",    this.tags.join(", "), "Tag"]
31     ].filter(function (item) item[1]),
32
33     get uri() newURI(this.url),
34     set uri(uri) {
35         let tags = this.tags;
36         this.tags = null;
37         services.bookmarks.changeBookmarkURI(this.id, uri);
38         this.tags = tags;
39     },
40
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());
46     }
47 })
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);
57     if (val)
58         services.tagging.tagURI(this.uri, val);
59 });
60
61 var name = "bookmark-cache";
62
63 var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
64     POST: "bookmarkProperties/POSTData",
65     CHARSET: "dactyl/charset",
66
67     init: function init() {
68         services.bookmarks.addObserver(this, false);
69     },
70
71     cleanup: function cleanup() {
72         services.bookmarks.removeObserver(this);
73     },
74
75     __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),
76
77     bookmarks: Class.Memoize(function () this.load()),
78
79     keywords: Class.Memoize(function () array.toObject([[b.keyword, b] for (b in this) if (b.keyword)])),
80
81     rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
82         .map(function (s) services.bookmarks[s]),
83
84     _deleteBookmark: function deleteBookmark(id) {
85         let result = this.bookmarks[id] || null;
86         delete this.bookmarks[id];
87         return result;
88     },
89
90     _loadBookmark: function loadBookmark(node) {
91         if (node.uri == null) // How does this happen?
92             return false;
93
94         let uri = newURI(node.uri);
95         let keyword = services.bookmarks.getKeywordForBookmark(node.itemId);
96
97         let tags = tags in node ? (node.tags ? node.tags.split(/, /g) : [])
98                                 : services.tagging.getTagsForURI(uri, {}) || [];
99
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);
103     },
104
105     annotate: function (id, key, val, timespan) {
106         if (val)
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);
111     },
112
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];
118         return null;
119     },
120
121     readBookmark: function readBookmark(id) ({
122         itemId: id,
123         uri:    services.bookmarks.getBookmarkURI(id).spec,
124         title:  services.bookmarks.getItemTitle(id)
125     }),
126
127     findRoot: function findRoot(id) {
128         do {
129             var root = id;
130             id = services.bookmarks.getFolderIdForItem(id);
131         } while (id != services.bookmarks.placesRoot && id != root);
132         return root;
133     },
134
135     isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
136
137     /**
138      * Returns true if the given URL is bookmarked and that bookmark is
139      * not a Live Bookmark.
140      *
141      * @param {nsIURI|string} url The URL of which to check the bookmarked
142      *     state.
143      * @returns {boolean}
144      */
145     isBookmarked: function isBookmarked(uri) {
146         if (isString(uri))
147             uri = newURI(uri);
148
149         try {
150             return services.bookmarks
151                            .getBookmarkIdsForURI(uri, {})
152                            .some(this.closure.isRegularBookmark);
153         }
154         catch (e) {
155             return false;
156         }
157     },
158
159     isRegularBookmark: function isRegularBookmark(id) {
160         do {
161             var root = id;
162             if (services.livemark && services.livemark.isLivemark(id))
163                 return false;
164             id = services.bookmarks.getFolderIdForItem(id);
165         } while (id != services.bookmarks.placesRoot && id != root);
166         return this.rootFolders.indexOf(root) >= 0;
167     },
168
169     load: function load() {
170         let bookmarks = {};
171
172         let query = services.history.getNewQuery();
173         let options = services.history.getNewQueryOptions();
174         options.queryType = options.QUERY_TYPE_BOOKMARKS;
175         options.excludeItemIfParentHasAnnotation = "livemark/feedURI";
176
177         let { root } = services.history.executeQuery(query, options);
178         root.containerOpen = true;
179         try {
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);
185             }
186         }
187         finally {
188             root.containerOpen = false;
189         }
190
191         return bookmarks;
192     },
193
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;
201             }
202         }
203     },
204     onItemRemoved: function onItemRemoved(itemId, folder, index) {
205         let result = this._deleteBookmark(itemId);
206         delete this.keywords;
207         if (result)
208             storage.fireEvent(name, "remove", result);
209     },
210     onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
211         if (isAnnotation)
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)];
216             else
217                 return;
218
219         let bookmark = this.bookmarks[itemId];
220         if (bookmark) {
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 });
228             }
229         }
230     }
231 }, {
232     DEFAULT_FAVICON: "chrome://mozapps/skin/places/defaultFavicon.png",
233
234     getAnnotation: function getAnnotation(item, anno)
235         services.annotation.itemHasAnnotation(item, anno) ?
236         services.annotation.getItemAnnotation(item, anno) : null,
237
238     getFavicon: function getFavicon(uri) {
239         try {
240             return services.favicon.getFaviconImageForPage(newURI(uri)).spec;
241         }
242         catch (e) {
243             return "";
244         }
245     }
246 });
247
248 endModule();
249
250 // vim: set fdm=marker sw=4 sts=4 et ft=javascript: