2017-02-11 20:52:22 +00:00
|
|
|
/*
|
|
|
|
***** BEGIN LICENSE BLOCK *****
|
|
|
|
|
|
|
|
Copyright (c) 2009 Zotero
|
|
|
|
Center for History and New Media
|
|
|
|
George Mason University, Fairfax, Virginia, USA
|
|
|
|
http://zotero.org
|
|
|
|
|
|
|
|
Zotero is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Zotero is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
***** END LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A common installer interface used by word processor plugins to make themselves
|
|
|
|
* installable and available in the cite preferences pane.
|
|
|
|
*/
|
|
|
|
|
2018-02-24 09:54:53 +00:00
|
|
|
var EXPORTED_SYMBOLS = ["ZoteroPluginInstaller"];
|
2017-02-11 20:52:22 +00:00
|
|
|
|
2024-03-28 08:45:18 +00:00
|
|
|
var { Zotero } = ChromeUtils.importESModule("chrome://zotero/content/zotero.mjs");
|
2024-08-16 13:51:07 +00:00
|
|
|
var { setTimeout } = ChromeUtils.importESModule("resource://gre/modules/Timer.sys.mjs");
|
2017-02-23 20:10:36 +00:00
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2017-02-11 20:52:22 +00:00
|
|
|
|
|
|
|
var installationInProgress = false;
|
|
|
|
|
2024-08-16 13:51:07 +00:00
|
|
|
// Prefs:
|
|
|
|
// version - last successfully installed version
|
|
|
|
// lastAttemptedVersion - last version that was attempted to automatically install
|
|
|
|
// skipInstallation - if user cancels an attempt to automatically install the plugin, we do not
|
|
|
|
// automatically install it again until a successful manual installation
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic plugin installation orchestrator for word processor installers
|
|
|
|
* @param addon {Object}
|
|
|
|
* @param failSilently {Boolean} whether the installation should not throw errors (typically when installing automatically)
|
|
|
|
* @param force {Boolean} force install even if the plugin version is up-to-date
|
|
|
|
* @constructor
|
|
|
|
*/
|
2024-08-14 06:47:11 +00:00
|
|
|
var ZoteroPluginInstaller = function (addon, failSilently, force) {
|
2017-02-11 20:52:22 +00:00
|
|
|
this._addon = addon;
|
|
|
|
this.failSilently = failSilently;
|
|
|
|
this.force = force;
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
this.prefBranch = Services.prefs.getBranch(this._addon.EXTENSION_PREF_BRANCH);
|
2017-03-20 17:22:24 +00:00
|
|
|
|
|
|
|
this.prefPaneDoc = null;
|
2024-08-16 13:51:07 +00:00
|
|
|
this._errorDisplayed = false;
|
2017-02-11 20:52:22 +00:00
|
|
|
|
2022-10-06 04:17:28 +00:00
|
|
|
this.init();
|
2017-02-11 20:52:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ZoteroPluginInstaller.prototype = {
|
2024-08-14 06:47:11 +00:00
|
|
|
init: async function () {
|
2024-08-16 13:51:07 +00:00
|
|
|
this.debug('Fetching addon info');
|
2022-10-06 04:17:28 +00:00
|
|
|
|
2024-08-16 13:51:07 +00:00
|
|
|
this._currentPluginVersion = (await Zotero.File.getContentsFromURLAsync(this._addon.VERSION_FILE)).trim();
|
|
|
|
let lastInstalledVersion = this.prefBranch.getCharPref("version");
|
|
|
|
let lastAttemptedVersion = this.prefBranch.getCharPref("lastAttemptedVersion", "");
|
|
|
|
let lastPluginFileVersion = this._addon.LAST_INSTALLED_FILE_UPDATE;
|
2024-08-24 10:28:02 +00:00
|
|
|
this.debug(`Addon info fetched. version: ${this._currentPluginVersion}, `
|
|
|
|
+ `file version: ${lastPluginFileVersion}, installed version: ${lastInstalledVersion}`
|
|
|
|
+ `attempted: ${lastAttemptedVersion}`);
|
2024-08-16 13:51:07 +00:00
|
|
|
const newVersionSinceLastInstall = Services.vc.compare(lastInstalledVersion, lastPluginFileVersion) < 0;
|
|
|
|
const newVersionSinceLastAttempt = Services.vc.compare(lastAttemptedVersion, lastPluginFileVersion) < 0;
|
|
|
|
const shouldSkipInstallation = this.prefBranch.getBoolPref("skipInstallation");
|
|
|
|
if (this.force) {
|
|
|
|
this.debug('Force-installing');
|
|
|
|
// Should never fail silently
|
|
|
|
this.failSilently = false;
|
|
|
|
return this.install();
|
|
|
|
}
|
|
|
|
else if (shouldSkipInstallation) {
|
|
|
|
this.debug('Skipping automatic installation because skipInstallation is true');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newVersionSinceLastAttempt) {
|
|
|
|
this.debug('New version since last attempt to install. Will display prompt upon failure.');
|
|
|
|
this.failSilently = false;
|
|
|
|
this.prefBranch.setCharPref("lastAttemptedVersion", this._currentPluginVersion);
|
|
|
|
return this.install();
|
|
|
|
}
|
|
|
|
else if (newVersionSinceLastInstall) {
|
|
|
|
this.debug('New version since last successful install. Attempting to install silently.');
|
|
|
|
return this.install();
|
|
|
|
}
|
|
|
|
this.debug('No new updates');
|
|
|
|
},
|
|
|
|
|
|
|
|
install: async function () {
|
|
|
|
if (installationInProgress) {
|
|
|
|
this.debug('Extension installation is already in progress');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
installationInProgress = true;
|
2017-02-11 20:52:22 +00:00
|
|
|
try {
|
2024-08-19 11:52:40 +00:00
|
|
|
if (!this._addon.DISABLE_PROGRESS_WINDOW && !this.failSilently) {
|
2024-08-16 13:51:07 +00:00
|
|
|
this._progressWindow = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowWatcher)
|
|
|
|
.openWindow(null, "chrome://zotero/content/progressWindow.xhtml", '',
|
|
|
|
"chrome,resizable=no,close=no,centerscreen", null);
|
|
|
|
this._progressWindow.addEventListener("load", () => this._firstRunListener(), false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let result = this._addon.install(this);
|
|
|
|
if (result.then) await result;
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
2024-08-14 06:47:11 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-02-11 20:52:22 +00:00
|
|
|
Zotero.logError(e);
|
2024-08-16 13:51:07 +00:00
|
|
|
this.error(e);
|
2024-08-14 06:47:11 +00:00
|
|
|
}
|
|
|
|
finally {
|
2017-08-08 09:46:20 +00:00
|
|
|
installationInProgress = false;
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
isInstalled: function () {
|
|
|
|
return !!this.prefBranch.getCharPref("version");
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
setProgressWindowLabel: function (value) {
|
2022-10-06 04:17:28 +00:00
|
|
|
if (this._progressWindow) this._progressWindowLabel.value = value;
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
closeProgressWindow: function () {
|
2022-10-06 04:17:28 +00:00
|
|
|
if (this._progressWindow) this._progressWindow.close();
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
success: function () {
|
2024-08-16 13:51:07 +00:00
|
|
|
this.debug(`Installation was successful. Version ${this._currentPluginVersion}`);
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = false;
|
|
|
|
this.closeProgressWindow();
|
2024-08-16 13:51:07 +00:00
|
|
|
this.prefBranch.setCharPref("version", this._currentPluginVersion);
|
2024-08-14 06:47:11 +00:00
|
|
|
this.updateInstallStatus();
|
2017-02-11 20:52:22 +00:00
|
|
|
this.prefBranch.setBoolPref("skipInstallation", false);
|
2024-08-14 06:47:11 +00:00
|
|
|
if (this.force && !this._addon.DISABLE_PROGRESS_WINDOW) {
|
2024-08-16 13:51:07 +00:00
|
|
|
setTimeout(() => {
|
2023-12-04 10:19:57 +00:00
|
|
|
Services.prompt.alert(
|
|
|
|
null,
|
2024-08-16 13:51:07 +00:00
|
|
|
this._addon.EXTENSION_STRING,
|
2023-12-04 10:19:57 +00:00
|
|
|
Zotero.getString("zotero.preferences.wordProcessors.installationSuccess")
|
|
|
|
);
|
2024-08-14 06:47:11 +00:00
|
|
|
});
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-08-16 13:51:07 +00:00
|
|
|
error: async function (error, notFailure) {
|
|
|
|
this.debug(`Installation failed with error ${error}`);
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = false;
|
|
|
|
this.closeProgressWindow();
|
2024-08-16 13:51:07 +00:00
|
|
|
if (notFailure) {
|
|
|
|
this.prefBranch.setCharPref("version", this._currentPluginVersion);
|
2024-08-14 06:47:11 +00:00
|
|
|
this.updateInstallStatus();
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
2024-08-16 13:51:07 +00:00
|
|
|
if (this.failSilently) {
|
|
|
|
this.debug('Not displaying error because failSilently is true');
|
|
|
|
return;
|
|
|
|
}
|
2024-08-14 06:47:11 +00:00
|
|
|
if (this._errorDisplayed) return;
|
2017-02-11 20:52:22 +00:00
|
|
|
this._errorDisplayed = true;
|
2024-08-16 13:51:07 +00:00
|
|
|
let errorMessage = await Zotero.getString("zotero.preferences.wordProcessors.installationError", [
|
|
|
|
this._addon.APP,
|
|
|
|
Zotero.appName
|
|
|
|
]);
|
|
|
|
if (error) {
|
|
|
|
errorMessage += "\n\n" + error;
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
2022-03-23 09:07:22 +00:00
|
|
|
var ps = Services.prompt;
|
2024-08-16 13:51:07 +00:00
|
|
|
var buttonFlags = (ps.BUTTON_POS_0 * ps.BUTTON_TITLE_IS_STRING) + ps.BUTTON_POS_0_DEFAULT
|
|
|
|
+ (ps.BUTTON_POS_1 * ps.BUTTON_TITLE_CANCEL);
|
2022-03-23 09:07:22 +00:00
|
|
|
var result = ps.confirmEx(null,
|
2024-08-16 13:51:07 +00:00
|
|
|
this._addon.EXTENSION_STRING,
|
|
|
|
errorMessage,
|
|
|
|
buttonFlags,
|
|
|
|
Zotero.getString('general-view-troubleshooting-instructions'),
|
|
|
|
null, null, null, {});
|
|
|
|
if (result == 0) {
|
|
|
|
Zotero.launchURL("https://www.zotero.org/support/kb/word_processor_plugin_installation_error");
|
2024-08-14 06:47:11 +00:00
|
|
|
}
|
|
|
|
});
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
cancelled: function (dontSkipInstallation) {
|
2024-08-16 13:51:07 +00:00
|
|
|
this.debug('Installation cancelled');
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = false;
|
|
|
|
this.closeProgressWindow();
|
2024-08-14 06:47:11 +00:00
|
|
|
if (!this.force && !dontSkipInstallation) this.prefBranch.setBoolPref("skipInstallation", true);
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
showPreferences: function (document) {
|
2017-03-20 17:22:24 +00:00
|
|
|
this.prefPaneDoc = document;
|
2017-02-11 20:52:22 +00:00
|
|
|
var isInstalled = this.isInstalled(),
|
2022-06-29 14:16:49 +00:00
|
|
|
groupbox = document.createXULElement("groupbox");
|
2017-02-11 20:52:22 +00:00
|
|
|
groupbox.id = this._addon.EXTENSION_DIR;
|
|
|
|
|
2023-06-05 14:52:04 +00:00
|
|
|
var label = document.createXULElement("label");
|
|
|
|
var h2 = document.createElement('h2');
|
|
|
|
h2.textContent = this._addon.APP;
|
|
|
|
label.appendChild(h2);
|
|
|
|
groupbox.appendChild(label);
|
2017-02-11 20:52:22 +00:00
|
|
|
|
2022-06-29 14:16:49 +00:00
|
|
|
var description = document.createXULElement("description");
|
2024-08-14 06:47:11 +00:00
|
|
|
description.appendChild(document.createTextNode(isInstalled
|
|
|
|
? Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP)
|
|
|
|
: Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)));
|
2017-02-11 20:52:22 +00:00
|
|
|
groupbox.appendChild(description);
|
|
|
|
|
2022-06-29 14:16:49 +00:00
|
|
|
var hbox = document.createXULElement("hbox");
|
2017-02-11 20:52:22 +00:00
|
|
|
hbox.setAttribute("pack", "center");
|
2022-06-29 14:16:49 +00:00
|
|
|
var button = document.createXULElement("button"),
|
2017-02-11 20:52:22 +00:00
|
|
|
addon = this._addon;
|
2024-08-14 06:47:11 +00:00
|
|
|
button.setAttribute("label", isInstalled
|
|
|
|
? Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP)
|
|
|
|
: Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP));
|
|
|
|
|
2024-08-16 13:51:07 +00:00
|
|
|
button.addEventListener("command", () => {
|
|
|
|
this.debug('Install button pressed');
|
2017-07-22 14:28:15 +00:00
|
|
|
try {
|
|
|
|
var zpi = new ZoteroPluginInstaller(addon, false, true);
|
|
|
|
zpi.showPreferences(document);
|
2024-08-14 06:47:11 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-07-22 14:28:15 +00:00
|
|
|
Zotero.logError(e);
|
|
|
|
}
|
2017-02-11 20:52:22 +00:00
|
|
|
}, false);
|
|
|
|
hbox.appendChild(button);
|
|
|
|
groupbox.appendChild(hbox);
|
|
|
|
|
2023-06-05 14:52:04 +00:00
|
|
|
var container = document.getElementById("wordProcessorInstallers"),
|
2017-02-11 20:52:22 +00:00
|
|
|
old = document.getElementById(this._addon.EXTENSION_DIR);
|
2024-08-14 06:47:11 +00:00
|
|
|
if (old) {
|
2023-06-05 14:52:04 +00:00
|
|
|
container.replaceChild(groupbox, old);
|
2024-08-14 06:47:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-06-05 14:52:04 +00:00
|
|
|
container.appendChild(groupbox);
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-08-14 06:47:11 +00:00
|
|
|
updateInstallStatus: function () {
|
|
|
|
if (!this.prefPaneDoc) return;
|
2017-03-20 17:22:24 +00:00
|
|
|
var isInstalled = this.isInstalled();
|
|
|
|
var description = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} description`);
|
2024-08-14 06:47:11 +00:00
|
|
|
description.replaceChild(this.prefPaneDoc.createTextNode(isInstalled
|
|
|
|
? Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP)
|
|
|
|
: Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)
|
|
|
|
), description.childNodes[0]);
|
2017-03-20 17:22:24 +00:00
|
|
|
var button = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} button`);
|
2024-08-14 06:47:11 +00:00
|
|
|
button.setAttribute("label", isInstalled
|
|
|
|
? Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP)
|
|
|
|
: Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP));
|
|
|
|
},
|
2017-03-20 17:22:24 +00:00
|
|
|
|
2024-08-16 13:51:07 +00:00
|
|
|
_firstRunListener: async function () {
|
2017-02-11 20:52:22 +00:00
|
|
|
this._progressWindowLabel = this._progressWindow.document.getElementById("progress-label");
|
|
|
|
this._progressWindowLabel.value = Zotero.getString('zotero.preferences.wordProcessors.installing', this._addon.EXTENSION_STRING);
|
2024-04-04 12:24:46 +00:00
|
|
|
this._progressWindow.sizeToContent();
|
2024-08-16 13:51:07 +00:00
|
|
|
await Zotero.Promise.delay(100);
|
|
|
|
this._progressWindow.focus();
|
|
|
|
await Zotero.Promise.delay(500);
|
|
|
|
this._progressWindow.focus();
|
|
|
|
try {
|
|
|
|
await this._addon.install(this);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
Zotero.logError(e);
|
|
|
|
this.error(e);
|
|
|
|
}
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
2024-08-16 13:51:07 +00:00
|
|
|
|
|
|
|
debug: function (message) {
|
|
|
|
Zotero.debug(`PluginInstaller ${this._addon.APP}: ${message}`);
|
|
|
|
}
|
2023-06-05 14:52:04 +00:00
|
|
|
};
|