]> git.donarmstrong.com Git - dactyl.git/blob - common/modules/bookmarkcache.jsm
Import 1.0b7.1 supporting Firefox up to 8.*
[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", "storage", "util"]
11 }, this);
12
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, {
17     get extra() [
18         ["keyword", this.keyword,         "Keyword"],
19         ["tags",    this.tags.join(", "), "Tag"]
20     ].filter(function (item) item[1]),
21
22     get uri() util.newURI(this.url),
23
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());
29     }
30 })
31 Bookmark.setter = function (key, func) this.prototype.__defineSetter__(key, func);
32 Bookmark.setter("url", function (val) {
33     if (isString(val))
34         val = util.newURI(val);
35     let tags = this.tags;
36     this.tags = null;
37     services.bookmarks.changeBookmarkURI(this.id, val);
38     this.tags = tags;
39 });
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);
46     if (val)
47         services.tagging.tagURI(this.uri, val);
48 });
49
50 var name = "bookmark-cache";
51
52 var BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
53     POST: "bookmarkProperties/POSTData",
54     CHARSET: "dactyl/charset",
55
56     init: function init() {
57         services.bookmarks.addObserver(this, false);
58     },
59
60     cleanup: function cleanup() {
61         services.bookmarks.removeObserver(this);
62     },
63
64     __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),
65
66     get bookmarks() Class.replaceProperty(this, "bookmarks", this.load()),
67
68     keywords: Class.memoize(function () array.toObject([[b.keyword, b] for (b in this) if (b.keyword)])),
69
70     rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
71         .map(function (s) services.bookmarks[s]),
72
73     _deleteBookmark: function deleteBookmark(id) {
74         let result = this.bookmarks[id] || null;
75         delete this.bookmarks[id];
76         return result;
77     },
78
79     _loadBookmark: function loadBookmark(node) {
80         if (node.uri == null) // How does this happen?
81             return false;
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);
88     },
89
90     annotate: function (id, key, val, timespan) {
91         if (val)
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);
96     },
97
98     get: function (url) {
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];
103         return null;
104     },
105
106     readBookmark: function readBookmark(id) ({
107         itemId: id,
108         uri:    services.bookmarks.getBookmarkURI(id).spec,
109         title:  services.bookmarks.getItemTitle(id)
110     }),
111
112     findRoot: function findRoot(id) {
113         do {
114             var root = id;
115             id = services.bookmarks.getFolderIdForItem(id);
116         } while (id != services.bookmarks.placesRoot && id != root);
117         return root;
118     },
119
120     isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
121
122     /**
123      * Returns true if the given URL is bookmarked and that bookmark is
124      * not a Live Bookmark.
125      *
126      * @param {nsIURI|string} url The URL of which to check the bookmarked
127      *     state.
128      * @returns {boolean}
129      */
130     isBookmarked: function isBookmarked(uri) {
131         if (isString(uri))
132             uri = util.newURI(uri);
133
134         try {
135             return services.bookmarks
136                            .getBookmarkIdsForURI(uri, {})
137                            .some(this.closure.isRegularBookmark);
138         }
139         catch (e) {
140             return false;
141         }
142     },
143
144     isRegularBookmark: function isRegularBookmark(id) {
145         do {
146             var root = id;
147             if (services.livemark && services.livemark.isLivemark(id))
148                 return false;
149             id = services.bookmarks.getFolderIdForItem(id);
150         } while (id != services.bookmarks.placesRoot && id != root);
151         return this.rootFolders.indexOf(root) >= 0;
152     },
153
154     load: function load() {
155         let bookmarks = {};
156
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);
162             folders.shift();
163             let result = services.history.executeQuery(query, options);
164             let folder = result.root;
165             folder.containerOpen = true;
166
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);
174             }
175
176             // close a container after using it!
177             folder.containerOpen = false;
178         }
179
180         return bookmarks;
181     },
182
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;
190             }
191         }
192     },
193     onItemRemoved: function onItemRemoved(itemId, folder, index) {
194         let result = this._deleteBookmark(itemId);
195         delete this.keywords;
196         if (result)
197             storage.fireEvent(name, "remove", result);
198     },
199     onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
200         if (isAnnotation)
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)];
205             else
206                 return;
207
208         let bookmark = this.bookmarks[itemId];
209         if (bookmark) {
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 });
217             }
218         }
219     }
220 }, {
221     getAnnotation: function getAnnotation(item, anno)
222         services.annotation.itemHasAnnotation(item, anno) ?
223         services.annotation.getItemAnnotation(item, anno) : null,
224     getFavicon: function getFavicon(uri) {
225         try {
226             return services.favicon.getFaviconImageForPage(util.newURI(uri)).spec;
227         }
228         catch (e) {
229             return "";
230         }
231     }
232 });
233
234 endModule();
235
236 // vim: set fdm=marker sw=4 sts=4 et ft=javascript: