1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-29 13:02:27 +01:00
Files
pentadactyl-pm/common/modules/bookmarkcache.jsm
Kris Maglione 1557b70f45 Major documentation updates and formatting fixes, and many, many other changes thanks to an MQ glitch, including:
* Significant completion speed improvements
 * Significantly improve startup speed, in large part by lazily
   instantiating Options and Commands, lazily installing highlight
   stylesheets, etc.
 * Update logos and icons, fix atrocious about page
 * Fix Teledactyl
 * JavaScript completion now avoids accessing property values
 * Add Option#persist to define which options are saved with :mkp
 * Add new Dactyl component which holds add-on-specific configuration
   information and removes need for separate components for each dactyl
   host
 * Several fixes for latest nightlies
 * Significant code cleanup and many bug fixes

--HG--
rename : muttator/AUTHORS => teledactyl/AUTHORS
rename : muttator/Donors => teledactyl/Donors
rename : muttator/Makefile => teledactyl/Makefile
rename : muttator/NEWS => teledactyl/NEWS
rename : muttator/TODO => teledactyl/TODO
rename : muttator/chrome.manifest => teledactyl/chrome.manifest
rename : muttator/components/commandline-handler.js => teledactyl/components/commandline-handler.js
rename : muttator/components/protocols.js => teledactyl/components/protocols.js
rename : muttator/content/addressbook.js => teledactyl/content/addressbook.js
rename : muttator/content/compose/compose.js => teledactyl/content/compose/compose.js
rename : muttator/content/compose/compose.xul => teledactyl/content/compose/compose.xul
rename : muttator/content/compose/dactyl.dtd => teledactyl/content/compose/dactyl.dtd
rename : muttator/content/compose/dactyl.xul => teledactyl/content/compose/dactyl.xul
rename : muttator/content/config.js => teledactyl/content/config.js
rename : muttator/content/dactyl.dtd => teledactyl/content/dactyl.dtd
rename : muttator/content/logo.png => teledactyl/content/logo.png
rename : muttator/content/mail.js => teledactyl/content/mail.js
rename : muttator/content/muttator.xul => teledactyl/content/pentadactyl.xul
rename : muttator/contrib/vim/Makefile => teledactyl/contrib/vim/Makefile
rename : muttator/contrib/vim/ftdetect/muttator.vim => teledactyl/contrib/vim/ftdetect/muttator.vim
rename : muttator/contrib/vim/mkvimball.txt => teledactyl/contrib/vim/mkvimball.txt
rename : muttator/contrib/vim/syntax/muttator.vim => teledactyl/contrib/vim/syntax/muttator.vim
rename : muttator/install.rdf => teledactyl/install.rdf
rename : muttator/locale/en-US/Makefile => teledactyl/locale/en-US/Makefile
rename : muttator/locale/en-US/all.xml => teledactyl/locale/en-US/all.xml
rename : muttator/locale/en-US/autocommands.xml => teledactyl/locale/en-US/autocommands.xml
rename : muttator/locale/en-US/gui.xml => teledactyl/locale/en-US/gui.xml
rename : muttator/locale/en-US/intro.xml => teledactyl/locale/en-US/intro.xml
rename : muttator/skin/icon.png => teledactyl/skin/icon.png
2010-09-17 06:21:33 -04:00

162 lines
5.8 KiB
JavaScript

// Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";
Components.utils.import("resource://dactyl/base.jsm");
defmodule("bookmarkcache", this, {
exports: ["Bookmark", "BookmarkCache", "Keyword", "bookmarkcache"],
require: ["services", "storage", "util"]
});
const Bookmark = Struct("url", "title", "icon", "keyword", "tags", "id");
const Keyword = Struct("keyword", "title", "icon", "url");
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
Bookmark.prototype.__defineGetter__("extra", function () [
["keyword", this.keyword, "Keyword"],
["tags", this.tags.join(", "), "Tag"]
].filter(function (item) item[1]));
const bookmarks = services.get("bookmarks");
const history = services.get("history");
const tagging = services.get("tagging");
const name = "bookmark-cache";
const BookmarkCache = Module("BookmarkCache", {
init: function init() {
bookmarks.addObserver(this, false);
},
__iterator__: function () (val for ([, val] in Iterator(this.bookmarks))),
get bookmarks() Class.replaceProperty(this, "bookmarks", this.load()),
rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
.map(function (s) bookmarks[s]),
_deleteBookmark: function deleteBookmark(id) {
let length = this.bookmarks.length;
this.bookmarks = this.bookmarks.filter(function (item) item.id != id);
return this.bookmarks.length < length;
},
_loadBookmark: function loadBookmark(node) {
if (node.uri == null) // How does this happen?
return false;
let uri = util.newURI(node.uri);
let keyword = bookmarks.getKeywordForBookmark(node.itemId);
let tags = tagging.getTagsForURI(uri, {}) || [];
return Bookmark(node.uri, node.title, node.icon && node.icon.spec, keyword, tags, node.itemId);
},
readBookmark: function readBookmark(id) {
return {
itemId: id,
uri: bookmarks.getBookmarkURI(id).spec,
title: bookmarks.getItemTitle(id)
};
},
findRoot: function findRoot(id) {
do {
var root = id;
id = bookmarks.getFolderIdForItem(id);
} while (id != bookmarks.placesRoot && id != root);
return root;
},
isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,
isRegularBookmark: function isRegularBookmark(id) {
do {
var root = id;
if (services.get("livemark") && services.get("livemark").isLivemark(id))
return false;
id = bookmarks.getFolderIdForItem(id);
} while (id != bookmarks.placesRoot && id != root);
return this.rootFolders.indexOf(root) >= 0;
},
get keywords() [Keyword(k.keyword, k.title, k.icon, k.url) for ([, k] in Iterator(this.bookmarks)) if (k.keyword)],
// Should be made thread safe.
load: function load() {
let bookmarks = [];
let folders = this.rootFolders.slice();
let query = history.getNewQuery();
let options = history.getNewQueryOptions();
while (folders.length > 0) {
query.setFolders(folders, 1);
folders.shift();
let result = history.executeQuery(query, options);
let folder = result.root;
folder.containerOpen = true;
// iterate over the immediate children of this folder
for (let i = 0; i < folder.childCount; i++) {
let node = folder.getChild(i);
if (node.type == node.RESULT_TYPE_FOLDER) // folder
folders.push(node.itemId);
else if (node.type == node.RESULT_TYPE_URI) // bookmark
bookmarks.push(this._loadBookmark(node));
}
// close a container after using it!
folder.containerOpen = false;
}
return bookmarks;
},
onBeginUpdateBatch: function onBeginUpdateBatch() {},
onEndUpdateBatch: function onEndUpdateBatch() {},
onItemVisited: function onItemVisited() {},
onItemMoved: function onItemMoved() {},
onItemAdded: function onItemAdded(itemId, folder, index) {
if (bookmarks.getItemType(itemId) == bookmarks.TYPE_BOOKMARK) {
if (this.isBookmark(itemId)) {
let bmark = this._loadBookmark(this.readBookmark(itemId));
this.bookmarks.push(bmark);
storage.fireEvent(name, "add", bmark);
}
}
},
onItemRemoved: function onItemRemoved(itemId, folder, index) {
if (this._deleteBookmark(itemId))
storage.fireEvent(name, "remove", itemId);
},
onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
if (isAnnotation)
return;
let bookmark = bookmarks.filter(function (item) item.id == itemId)[0];
if (bookmark) {
if (property == "tags")
value = tagging.getTagsForURI(util.newURI(bookmark.url), {});
if (property in bookmark)
bookmark[property] = value;
storage.fireEvent(name, "change", itemId);
}
},
QueryInterface: function QueryInterface(iid) {
if (iid.equals(Ci.nsINavBookmarkObserver) || iid.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
}, {
getFavicon: function getFavicon(uri) {
try {
return service.get("favicon").getFaviconImageForPage(util.newURI(uri)).spec;
}
catch (e) {
return "";
}
}
});
endmodule();
// vim: set fdm=marker sw=4 sts=4 et ft=javascript: