2011-01-08 07:03:27 -08:00
|
|
|
var BookmarkHelper = {
|
|
|
|
_editor: null,
|
|
|
|
|
2011-05-10 11:57:46 -07:00
|
|
|
logging: false,
|
|
|
|
|
|
|
|
log: function(msg) {
|
|
|
|
if (this.logging) {
|
|
|
|
Services.console.logStringMessage(msg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-01-17 11:21:38 -08:00
|
|
|
get box() {
|
|
|
|
delete this.box;
|
|
|
|
this.box = document.getElementById("bookmark-container");
|
|
|
|
return this.box;
|
|
|
|
},
|
|
|
|
|
2011-01-08 07:03:27 -08:00
|
|
|
edit: function BH_edit(aURI) {
|
|
|
|
if (!aURI)
|
|
|
|
aURI = getBrowser().currentURI;
|
|
|
|
|
|
|
|
let itemId = PlacesUtils.getMostRecentBookmarkForURI(aURI);
|
|
|
|
if (itemId == -1)
|
|
|
|
return;
|
|
|
|
|
2011-03-08 12:44:04 -08:00
|
|
|
// When opening the bookmark helper dialog be sure there is not others
|
2011-04-11 16:35:00 -07:00
|
|
|
// popup opened like the bookmark popup
|
2011-03-08 12:44:04 -08:00
|
|
|
BookmarkPopup.hide();
|
|
|
|
|
2011-01-08 07:03:27 -08:00
|
|
|
let title = PlacesUtils.bookmarks.getItemTitle(itemId);
|
|
|
|
let tags = PlacesUtils.tagging.getTagsForURI(aURI, {});
|
|
|
|
|
|
|
|
const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
this._editor = document.createElementNS(XULNS, "placeitem");
|
|
|
|
this._editor.setAttribute("id", "bookmark-item");
|
|
|
|
this._editor.setAttribute("flex", "1");
|
|
|
|
this._editor.setAttribute("type", "bookmark");
|
|
|
|
this._editor.setAttribute("ui", "manage");
|
|
|
|
this._editor.setAttribute("title", title);
|
|
|
|
this._editor.setAttribute("uri", aURI.spec);
|
|
|
|
this._editor.setAttribute("itemid", itemId);
|
|
|
|
this._editor.setAttribute("tags", tags.join(", "));
|
2011-03-08 12:44:04 -08:00
|
|
|
this._editor.setAttribute("onclose", "BookmarkHelper.close()");
|
2011-01-08 07:03:27 -08:00
|
|
|
document.getElementById("bookmark-form").appendChild(this._editor);
|
|
|
|
|
2011-01-17 11:21:38 -08:00
|
|
|
this.box.hidden = false;
|
2011-03-08 12:44:04 -08:00
|
|
|
BrowserUI.pushDialog(this);
|
2011-01-08 07:03:27 -08:00
|
|
|
|
2011-01-17 11:21:38 -08:00
|
|
|
function waitForWidget(self) {
|
|
|
|
try {
|
|
|
|
self._editor.startEditing();
|
|
|
|
} catch(e) {
|
2011-01-18 07:31:59 -08:00
|
|
|
setTimeout(waitForWidget, 0, self);
|
2011-01-17 11:21:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(waitForWidget, 0, this);
|
2011-01-08 07:03:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
save: function BH_save() {
|
|
|
|
this._editor.stopEditing(true);
|
|
|
|
},
|
|
|
|
|
2011-03-08 12:44:04 -08:00
|
|
|
close: function BH_close() {
|
2011-05-10 11:57:46 -07:00
|
|
|
this.log("Bookmark helper: closing");
|
2011-01-08 07:03:27 -08:00
|
|
|
BrowserUI.updateStar();
|
|
|
|
|
|
|
|
// Note: the _editor will have already saved the data, if needed, by the time
|
|
|
|
// this method is called, since this method is called via the "close" event.
|
|
|
|
this._editor.parentNode.removeChild(this._editor);
|
|
|
|
this._editor = null;
|
|
|
|
|
2011-03-08 12:44:04 -08:00
|
|
|
BrowserUI.popDialog();
|
2011-01-17 11:21:38 -08:00
|
|
|
this.box.hidden = true;
|
2011-01-08 07:03:27 -08:00
|
|
|
},
|
|
|
|
|
2011-08-09 18:32:22 -07:00
|
|
|
createShortcut: function BH_createShortcut(aTitle, aURL, aIconURL) {
|
2011-08-12 11:00:14 -07:00
|
|
|
// The background images are 72px, but Android will resize as needed.
|
|
|
|
// Bigger is better than too small.
|
|
|
|
const kIconSize = 72;
|
|
|
|
const kOverlaySize = 32;
|
|
|
|
const kOffset = 20;
|
2011-08-09 18:32:22 -07:00
|
|
|
|
2011-08-12 11:00:14 -07:00
|
|
|
// We have to fallback to something
|
2011-08-12 11:00:12 -07:00
|
|
|
aTitle = aTitle || aURL;
|
|
|
|
|
2011-08-09 18:32:22 -07:00
|
|
|
let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
|
|
|
|
canvas.setAttribute("style", "display: none");
|
|
|
|
|
2011-08-12 11:00:14 -07:00
|
|
|
function _createShortcut() {
|
2011-08-09 18:32:22 -07:00
|
|
|
let icon = canvas.toDataURL("image/png", "");
|
|
|
|
canvas = null;
|
|
|
|
try {
|
|
|
|
let shell = Cc["@mozilla.org/browser/shell-service;1"].createInstance(Ci.nsIShellService);
|
|
|
|
shell.createShortcut(aTitle, aURL, icon, "bookmark");
|
|
|
|
} catch(e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 11:00:14 -07:00
|
|
|
// Load the main background image first
|
|
|
|
let image = new Image();
|
|
|
|
image.onload = function() {
|
|
|
|
canvas.width = canvas.height = kIconSize;
|
|
|
|
let ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(image, 0, 0, kIconSize, kIconSize);
|
|
|
|
|
|
|
|
// If we have a favicon, lets draw it next
|
|
|
|
if (aIconURL) {
|
|
|
|
let favicon = new Image();
|
|
|
|
favicon.onload = function() {
|
|
|
|
// Center the favicon and overlay it on the background
|
|
|
|
ctx.drawImage(favicon, kOffset, kOffset, kOverlaySize, kOverlaySize);
|
|
|
|
_createShortcut();
|
|
|
|
}
|
|
|
|
|
|
|
|
favicon.onerror = function() {
|
|
|
|
Cu.reportError("CreateShortcut: favicon image load error");
|
|
|
|
}
|
|
|
|
|
|
|
|
favicon.src = aIconURL;
|
|
|
|
} else {
|
|
|
|
_createShortcut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 11:00:12 -07:00
|
|
|
image.onerror = function() {
|
2011-08-12 11:00:14 -07:00
|
|
|
Cu.reportError("CreateShortcut: background image load error");
|
2011-08-12 11:00:12 -07:00
|
|
|
}
|
|
|
|
|
2011-08-12 11:00:14 -07:00
|
|
|
// Pick the right background
|
|
|
|
image.src = aIconURL ? "chrome://browser/skin/images/homescreen-blank-hdpi.png"
|
|
|
|
: "chrome://browser/skin/images/homescreen-default-hdpi.png";
|
2011-08-09 18:32:22 -07:00
|
|
|
},
|
|
|
|
|
2011-01-08 07:03:27 -08:00
|
|
|
removeBookmarksForURI: function BH_removeBookmarksForURI(aURI) {
|
|
|
|
//XXX blargle xpconnect! might not matter, but a method on
|
|
|
|
// nsINavBookmarksService that takes an array of items to
|
|
|
|
// delete would be faster. better yet, a method that takes a URI!
|
|
|
|
let itemIds = PlacesUtils.getBookmarksForURI(aURI);
|
|
|
|
itemIds.forEach(PlacesUtils.bookmarks.removeItem);
|
|
|
|
|
|
|
|
BrowserUI.updateStar();
|
|
|
|
}
|
|
|
|
};
|