snappymail/dev/View/Popup/MessageOpenPgp.js

130 lines
2.8 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
import { pString } from 'Common/Utils';
2020-08-14 04:58:41 +08:00
import { KeyState } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { popup, command } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/MessageOpenPgp',
templateID: 'PopupsMessageOpenPgp'
})
2019-07-05 03:19:24 +08:00
class MessageOpenPgpPopupView extends AbstractViewNext {
constructor() {
super();
this.notification = ko.observable('');
this.selectedKey = ko.observable(null);
this.privateKeys = ko.observableArray([]);
this.password = ko.observable('');
this.resultCallback = null;
this.submitRequest = ko.observable(false);
2016-09-10 06:38:16 +08:00
this.sDefaultKeyScope = KeyState.PopupMessageOpenPGP;
}
@command((self) => !self.submitRequest())
doCommand() {
this.submitRequest(true);
setTimeout(() => {
2016-09-10 06:38:16 +08:00
let privateKey = null;
2019-07-05 03:19:24 +08:00
try {
if (this.resultCallback && this.selectedKey()) {
2016-09-10 06:38:16 +08:00
const privateKeys = this.selectedKey().getNativeKeys();
privateKey = privateKeys && privateKeys[0] ? privateKeys[0] : null;
2019-07-05 03:19:24 +08:00
if (privateKey) {
try {
if (!privateKey.decrypt(pString(this.password()))) {
console.log('Error: Private key cannot be decrypted');
privateKey = null;
}
2019-07-05 03:19:24 +08:00
} catch (e) {
console.log(e);
2016-09-10 06:38:16 +08:00
privateKey = null;
}
2019-07-05 03:19:24 +08:00
} else {
console.log('Error: Private key cannot be found');
2016-09-10 06:38:16 +08:00
}
}
2019-07-05 03:19:24 +08:00
} catch (e) {
console.log(e);
2016-09-10 06:38:16 +08:00
privateKey = null;
}
2016-09-10 06:38:16 +08:00
this.submitRequest(false);
2016-09-10 06:38:16 +08:00
this.cancelCommand();
this.resultCallback(privateKey);
2020-08-14 04:58:41 +08:00
}, 100);
}
clearPopup() {
this.notification('');
this.password('');
this.selectedKey(false);
this.submitRequest(false);
this.resultCallback = null;
this.privateKeys([]);
}
onBuild(oDom) {
key('tab,shift+tab', KeyState.PopupMessageOpenPGP, () => {
let btn = this.querySelector('.inputPassword');
if (btn.matches(':focus')) {
btn = this.querySelector('.buttonDo');
}
btn.focus();
return false;
});
const self = this;
2019-07-05 03:19:24 +08:00
oDom.on('click', '.key-list__item', function() {
// eslint-disable-line prefer-arrow-callback
2019-07-05 03:19:24 +08:00
oDom
.find('.key-list__item .key-list__item__radio')
.addClass('icon-radio-unchecked')
.removeClass('icon-radio-checked');
jQuery(this)
2019-07-05 03:19:24 +08:00
.find('.key-list__item__radio') // eslint-disable-line no-invalid-this
.removeClass('icon-radio-unchecked')
.addClass('icon-radio-checked');
2019-07-05 03:19:24 +08:00
self.selectedKey(ko.dataFor(this)); // eslint-disable-line no-invalid-this
// this.querySelector('.inputPassword').focus();
2019-07-05 03:19:24 +08:00
});
}
onHideWithDelay() {
this.clearPopup();
}
onShow(fCallback, privateKeys) {
this.clearPopup();
this.resultCallback = fCallback;
this.privateKeys(privateKeys);
2019-07-05 03:19:24 +08:00
if (this.viewModelDom) {
2020-08-27 21:45:47 +08:00
const el = this.viewModelDom.querySelector('.key-list__item');
el && el.click();
}
2016-06-30 08:02:45 +08:00
}
}
2019-07-05 03:19:24 +08:00
export { MessageOpenPgpPopupView, MessageOpenPgpPopupView as default };