1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 17:27:57 +01:00

Update :extensions with live update and buttons like :downloads. Rename to :addons.

--HG--
branch : key-processing
This commit is contained in:
Kris Maglione
2011-01-24 03:56:27 -05:00
parent 37719fdf1f
commit 93b9aade1d
9 changed files with 544 additions and 361 deletions

View File

@@ -1527,14 +1527,6 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
},
commands: function () {
commands.add(["addo[ns]"],
"Manage available Extensions and Themes",
function () {
dactyl.open("chrome://mozapps/content/extensions/extensions.xul",
{ from: "addons" });
},
{ argCount: "0" });
commands.add(["dia[log]"],
"Open a " + config.appName + " dialog",
function (args) {
@@ -1592,255 +1584,6 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
literal: 0
});
///////////////////////////////////////////////////////////////////////////
const addonErrors = array.toObject([
[AddonManager.ERROR_NETWORK_FAILURE, "A network error occurred"],
[AddonManager.ERROR_INCORRECT_HASH, "The downloaded file did not match the expected hash"],
[AddonManager.ERROR_CORRUPT_FILE, "The file appears to be corrupt"],
[AddonManager.ERROR_FILE_ACCESS, "There was an error accessing the filesystem"]]);
function listener(action, event)
function addonListener(install) {
if (typeof dactyl !== "undefined")
dactyl[install.error ? "echoerr" : "echomsg"](
"Add-on " + action + " " + event + ": " + (install.name || install.sourceURI.spec) +
(install.error ? ": " + addonErrors[install.error] : ""));
}
const addonListener = {
onNewInstall: function (install) {},
onExternalInstall: function (addon, existingAddon, needsRestart) {},
onDownloadStarted: listener("download", "started"),
onDownloadEnded: listener("download", "complete"),
onDownloadCancelled: listener("download", "cancelled"),
onDownloadFailed: listener("download", "failed"),
onDownloadProgress: function (install) {},
onInstallStarted: function (install) {},
onInstallEnded: listener("installation", "complete"),
onInstallCancelled: listener("installation", "cancelled"),
onInstallFailed: listener("installation", "failed")
};
const updateAddons = Class("UpgradeListener", {
init: function init(addons) {
dactyl.assert(!addons.length || addons[0].findUpdates,
"Not available on " + config.host + " " + services.runtime.version);
this.remaining = addons;
this.upgrade = [];
dactyl.echomsg("Checking updates for addons: " + addons.map(function (a) a.name).join(", "));
for (let addon in values(addons))
addon.findUpdates(this, AddonManager.UPDATE_WHEN_USER_REQUESTED, null, null);
},
addonListener: {
__proto__: addonListener,
onDownloadStarted: function () {},
onDownloadEnded: function () {}
},
onUpdateAvailable: function (addon, install) {
this.upgrade.push(addon);
install.addListener(this.addonListener);
install.install();
},
onUpdateFinished: function (addon, error) {
this.remaining = this.remaining.filter(function (a) a != addon);
if (!this.remaining.length)
dactyl.echomsg(
this.upgrade.length
? "Installing updates for addons: " + this.upgrade.map(function (i) i.name).join(", ")
: "No addon updates found");
}
});
///////////////////////////////////////////////////////////////////////////
function callResult(method) {
let args = Array.slice(arguments, 1);
return function (result) { result[method].apply(result, args); };
}
commands.add(["exta[dd]"],
"Install an extension",
function (args) {
let url = args[0];
let file = io.File(url);
function install(addonInstall) {
addonInstall.addListener(addonListener);
addonInstall.install();
}
if (!file.exists())
AddonManager.getInstallForURL(url, install, "application/x-xpinstall");
else if (file.isReadable() && file.isFile())
AddonManager.getInstallForFile(file, install, "application/x-xpinstall");
else if (file.isDirectory())
dactyl.echoerr("Cannot install a directory: " + file.path.quote());
else
dactyl.echoerr("E484: Can't open file " + file.path);
}, {
argCount: "1",
completer: function (context) {
context.filters.push(function ({ item }) item.isDirectory() || /\.xpi$/.test(item.leafName));
completion.file(context);
},
literal: 0
});
// TODO: handle extension dependencies
[
{
name: "extde[lete]",
description: "Uninstall an extension",
action: callResult("uninstall"),
perm: "uninstall"
},
{
name: "exte[nable]",
description: "Enable an extension",
action: function (addon) addon.userDisabled = false,
filter: function ({ item }) item.userDisabled,
perm: "enable"
},
{
name: "extd[isable]",
description: "Disable an extension",
action: function (addon) addon.userDisabled = true,
filter: function ({ item }) !item.userDisabled,
perm: "disable"
},
{
name: "extr[ehash]",
description: "Reload an extension",
action: function (addon) {
dactyl.assert(dactyl.has("Gecko2"), "This command is not useful in this version of " + config.host);
util.timeout(function () {
addon.userDisabled = true;
addon.userDisabled = false;
});
},
get filter() {
let ids = set(keys(JSON.parse(prefs.get("extensions.bootstrappedAddons", "{}"))));
return function ({ item }) !item.userDisabled && set.has(ids, item.id);
},
perm: "disable"
},
{
name: "extt[oggle]",
description: "Toggle an extension's enabled status",
action: function (addon) addon.userDisabled = !addon.userDisabled
},
{
name: "extu[pdate]",
description: "Update an extension",
actions: updateAddons,
perm: "upgrade"
}
].forEach(function (command) {
let perm = command.perm && AddonManager["PERM_CAN_" + command.perm.toUpperCase()];
function ok(addon) !perm || addon.permissions & perm;
commands.add([command.name],
command.description,
function (args) {
let name = args[0];
if (args.bang)
dactyl.assert(!name, "E488: Trailing characters");
else
dactyl.assert(name, "E471: Argument required");
AddonManager.getAddonsByTypes(["extension"], dactyl.wrapCallback(function (list) {
if (!args.bang) {
list = list.filter(function (extension) extension.name == name);
if (list.length == 0)
return void dactyl.echoerr("E475: Invalid argument: " + name);
if (!list.every(ok))
return void dactyl.echoerr("Permission denied");
}
if (command.actions)
command.actions(list);
else
list.forEach(command.action);
}));
}, {
argCount: "?", // FIXME: should be "1"
bang: true,
completer: function (context) {
completion.extension(context);
context.filters.push(function ({ item }) ok(item));
if (command.filter)
context.filters.push(command.filter);
},
literal: 0
});
});
commands.add(["exto[ptions]", "extp[references]"],
"Open an extension's preference dialog",
function (args) {
AddonManager.getAddonsByTypes(["extension"], dactyl.wrapCallback(function (list) {
list = list.filter(function (extension) extension.name == args[0]);
if (!list.length || !list[0].optionsURL)
dactyl.echoerr("E474: Invalid argument");
else if (args.bang)
window.openDialog(list[0].optionsURL, "_blank", "chrome");
else
dactyl.open(list[0].optionsURL, { from: "extoptions" });
}));
}, {
argCount: "1",
bang: true,
completer: function (context) {
completion.extension(context);
context.filters.push(function ({ item }) item.isActive && item.optionsURL);
},
literal: 0
});
commands.add(["extens[ions]", "exts"],
"List available extensions",
function (args) {
function addonExtra(e) {
let extra;
if (e.pendingOperations & AddonManager.PENDING_UNINSTALL)
extra = ["Disabled", "uninstalled"];
else if (e.pendingOperations & AddonManager.PENDING_DISABLE)
extra = ["Disabled", "disabled"];
else if (e.pendingOperations & AddonManager.PENDING_INSTALL)
extra = ["Enabled", "installed"];
else if (e.pendingOperations & AddonManager.PENDING_ENABLE)
extra = ["Enabled", "enabled"];
else if (e.pendingOperations & AddonManager.PENDING_UPGRADE)
extra = ["Enabled", "upgraded"];
if (extra)
return <>&#xa0;(<span highlight={extra[0]}>{extra[1]}</span>
&#xa0;on restart)</>;
return <></>;
}
let waiting = true;
AddonManager.getAddonsByTypes(["extension"], function (extensions) {
if (args[0])
extensions = extensions.filter(function (extension) extension.name.indexOf(args[0]) >= 0);
extensions.sort(function (a, b) String.localeCompare(a.name, b.name));
if (extensions.length > 0)
commandline.commandOutput(
template.tabular(["Name", "Version", "Status", "Description"], [],
([template.icon({ icon: e.iconURL }, e.name),
e.version,
(e.isActive ? <span highlight="Enabled">enabled</span>
: <span highlight="Disabled">disabled</span>) +
addonExtra(e),
e.description]
for ([, e] in Iterator(extensions)))));
else if (filter)
dactyl.echoerr("Exxx: No extension matching " + filter.quote());
else
dactyl.echoerr("No extensions installed");
waiting = false;
});
if (commandline.savingOutput)
util.waitFor(function () !waiting);
},
{ argCount: "?" });
[
{
name: "h[elp]",