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
|
|
|
|
|
|
|
var Zotero = Components.classes["@zotero.org/Zotero;1"]
|
|
|
|
// Currently uses only nsISupports
|
|
|
|
//.getService(Components.interfaces.chnmIZoteroService).
|
|
|
|
.getService(Components.interfaces.nsISupports)
|
|
|
|
.wrappedJSObject;
|
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;
|
|
|
|
var _runningTimers = [];
|
|
|
|
function setTimeout(func, ms) {
|
|
|
|
var timer = Components.classes["@mozilla.org/timer;1"].
|
|
|
|
createInstance(Components.interfaces.nsITimer);
|
2017-03-17 12:27:18 +00:00
|
|
|
var timerCallback = {notify: function() {
|
2017-02-11 20:52:22 +00:00
|
|
|
_runningTimers.splice(_runningTimers.indexOf(timer), 1);
|
|
|
|
func();
|
|
|
|
}};
|
|
|
|
timer.initWithCallback(timerCallback, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
// add timer to global scope so that it doesn't get garbage collected before it completes
|
|
|
|
_runningTimers.push(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
var ZoteroPluginInstaller = function(addon, failSilently, force) {
|
|
|
|
this._addon = addon;
|
|
|
|
this.failSilently = failSilently;
|
|
|
|
this.force = force;
|
|
|
|
|
|
|
|
var prefService = Components.classes["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Components.interfaces.nsIPrefService);
|
|
|
|
this.prefBranch = prefService.getBranch(this._addon.EXTENSION_PREF_BRANCH);
|
2017-03-20 17:22:24 +00:00
|
|
|
|
|
|
|
this.prefPaneDoc = null;
|
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 = {
|
2022-10-06 04:17:28 +00:00
|
|
|
init: async function() {
|
|
|
|
if (this._initialized) return;
|
|
|
|
Zotero.debug("PluginInstaller: fetching addon info");
|
|
|
|
Zotero.debug("PluginInstaller: addon info fetched");
|
|
|
|
|
2017-02-11 20:52:22 +00:00
|
|
|
try {
|
2022-10-06 04:17:28 +00:00
|
|
|
this._version = (await Zotero.File.getContentsFromURLAsync(this._addon.VERSION_FILE)).trim();
|
|
|
|
var version = this.prefBranch.getCharPref("version");
|
|
|
|
if (this.force || (Services.vc.compare(version, this._addon.LAST_INSTALLED_FILE_UPDATE) < 0
|
|
|
|
&& !this.prefBranch.getBoolPref("skipInstallation"))) {
|
2017-08-08 09:46:20 +00:00
|
|
|
if (installationInProgress) {
|
|
|
|
Zotero.debug(`${this._addon.APP} extension installation is already in progress`);
|
|
|
|
return;
|
|
|
|
}
|
2022-10-06 04:17:28 +00:00
|
|
|
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = true;
|
|
|
|
if(!this._addon.DISABLE_PROGRESS_WINDOW) {
|
|
|
|
this._progressWindow = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowWatcher)
|
2022-10-06 04:17:28 +00:00
|
|
|
.openWindow(null, "chrome://zotero/content/progressWindow.xhtml", '',
|
|
|
|
"chrome,resizable=no,close=no,centerscreen", null);
|
|
|
|
this._progressWindow.addEventListener("load", () => { this._firstRunListener() }, false);
|
2017-02-11 20:52:22 +00:00
|
|
|
} else {
|
|
|
|
this._addon.install(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
Zotero.logError(e);
|
2017-08-08 09:46:20 +00:00
|
|
|
} finally {
|
|
|
|
installationInProgress = false;
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
2022-10-06 04:17:28 +00:00
|
|
|
|
|
|
|
this._initialized = true;
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
2022-10-06 04:17:28 +00:00
|
|
|
_errorDisplayed: false,
|
2017-02-11 20:52:22 +00:00
|
|
|
|
2017-03-17 12:27:18 +00:00
|
|
|
isInstalled: function() {
|
2021-06-25 07:36:20 +00:00
|
|
|
return this.prefBranch.getBoolPref("installed");
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +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
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +00:00
|
|
|
closeProgressWindow: function(value) {
|
2022-10-06 04:17:28 +00:00
|
|
|
if (this._progressWindow) this._progressWindow.close();
|
2017-02-11 20:52:22 +00:00
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +00:00
|
|
|
success: function() {
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = false;
|
|
|
|
this.closeProgressWindow();
|
|
|
|
this.prefBranch.setCharPref("version", this._version);
|
2017-03-20 17:22:24 +00:00
|
|
|
this.updateInstallStatus(true);
|
2017-02-11 20:52:22 +00:00
|
|
|
this.prefBranch.setBoolPref("skipInstallation", false);
|
|
|
|
if(this.force && !this._addon.DISABLE_PROGRESS_WINDOW) {
|
|
|
|
var addon = this._addon;
|
|
|
|
setTimeout(function() {
|
|
|
|
Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPromptService)
|
|
|
|
.alert(null, addon.EXTENSION_STRING,
|
|
|
|
Zotero.getString("zotero.preferences.wordProcessors.installationSuccess"));
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +00:00
|
|
|
error: function(error, notFailure) {
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = false;
|
|
|
|
this.closeProgressWindow();
|
|
|
|
if(!notFailure) {
|
|
|
|
this.prefBranch.setCharPref("version", this._version);
|
2017-03-20 17:22:24 +00:00
|
|
|
this.updateInstallStatus(false);
|
2017-02-11 20:52:22 +00:00
|
|
|
}
|
|
|
|
if(this.failSilently) return;
|
|
|
|
if(this._errorDisplayed) return;
|
|
|
|
this._errorDisplayed = true;
|
|
|
|
var addon = this._addon;
|
|
|
|
setTimeout(function() {
|
2022-03-23 09:07:22 +00:00
|
|
|
var ps = Services.prompt;
|
|
|
|
var buttonFlags = ps.BUTTON_POS_0 * ps.BUTTON_TITLE_OK
|
|
|
|
+ (ps.BUTTON_POS_1) * (ps.BUTTON_TITLE_IS_STRING);
|
|
|
|
var result = ps.confirmEx(null,
|
|
|
|
addon.EXTENSION_STRING,
|
|
|
|
(error ? error : Zotero.getString("zotero.preferences.wordProcessors.installationError", [addon.APP, Zotero.appName])),
|
|
|
|
buttonFlags, null,
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.manualInstallation.button'),
|
|
|
|
null, null, {});
|
|
|
|
if (result == 1) {
|
|
|
|
Zotero.launchURL("https://www.zotero.org/support/word_processor_plugin_manual_installation");
|
|
|
|
}
|
2017-02-11 20:52:22 +00:00
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +00:00
|
|
|
cancelled: function(dontSkipInstallation) {
|
2017-02-11 20:52:22 +00:00
|
|
|
installationInProgress = false;
|
|
|
|
this.closeProgressWindow();
|
|
|
|
if(!this.force && !dontSkipInstallation) this.prefBranch.setBoolPref("skipInstallation", true);
|
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +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;
|
|
|
|
|
2022-06-29 14:16:49 +00:00
|
|
|
var caption = document.createXULElement("caption");
|
2017-02-11 20:52:22 +00:00
|
|
|
caption.setAttribute("label", this._addon.APP);
|
|
|
|
groupbox.appendChild(caption);
|
|
|
|
|
2022-06-29 14:16:49 +00:00
|
|
|
var description = document.createXULElement("description");
|
2017-02-11 20:52:22 +00:00
|
|
|
description.style.width = "45em";
|
|
|
|
description.appendChild(document.createTextNode(
|
|
|
|
isInstalled ?
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP) :
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)));
|
|
|
|
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;
|
|
|
|
button.setAttribute("label",
|
|
|
|
(isInstalled ?
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP) :
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP)));
|
|
|
|
button.addEventListener("command", function() {
|
2017-07-22 14:28:15 +00:00
|
|
|
Zotero.debug(`Install button pressed for ${addon.APP} plugin`);
|
|
|
|
try {
|
|
|
|
var zpi = new ZoteroPluginInstaller(addon, false, true);
|
|
|
|
zpi.showPreferences(document);
|
|
|
|
} catch (e) {
|
|
|
|
Zotero.logError(e);
|
|
|
|
}
|
2017-02-11 20:52:22 +00:00
|
|
|
}, false);
|
|
|
|
hbox.appendChild(button);
|
|
|
|
groupbox.appendChild(hbox);
|
|
|
|
|
|
|
|
var tabpanel = document.getElementById("wordProcessors"),
|
|
|
|
old = document.getElementById(this._addon.EXTENSION_DIR);
|
|
|
|
if(old) {
|
|
|
|
tabpanel.replaceChild(groupbox, old);
|
|
|
|
} else {
|
|
|
|
tabpanel.insertBefore(groupbox, tabpanel.firstChild);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-03-20 17:22:24 +00:00
|
|
|
updateInstallStatus: function(status) {
|
|
|
|
this.prefBranch.setBoolPref("installed", status);
|
|
|
|
if (! this.prefPaneDoc) return;
|
|
|
|
var isInstalled = this.isInstalled();
|
|
|
|
var description = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} description`);
|
|
|
|
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]);
|
|
|
|
var button = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} button`);
|
|
|
|
button.setAttribute("label",
|
|
|
|
(isInstalled ?
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP) :
|
|
|
|
Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP)));
|
|
|
|
},
|
|
|
|
|
2017-03-17 12:27:18 +00:00
|
|
|
_firstRunListener: 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);
|
|
|
|
var me = this;
|
|
|
|
setTimeout(function() {
|
|
|
|
me._progressWindow.focus();
|
|
|
|
setTimeout(function() {
|
|
|
|
me._progressWindow.focus();
|
|
|
|
try {
|
|
|
|
me._addon.install(me);
|
|
|
|
} catch(e) {
|
|
|
|
me.error();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}, 100);
|
|
|
|
},
|
|
|
|
};
|