snappymail/dev/View/Popup/NewOpenPgpKey.js

116 lines
2.3 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2014-08-25 23:49:01 +08:00
import { pInt } from 'Common/Utils';
2014-08-21 23:08:34 +08:00
import PgpStore from 'Stores/User/Pgp';
2014-08-25 15:10:51 +08:00
2019-07-05 03:19:24 +08:00
import { getApp } from 'Helper/Apps/User';
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
import { popup, command } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/NewOpenPgpKey',
templateID: 'PopupsNewOpenPgpKey'
})
2019-07-05 03:19:24 +08:00
class NewOpenPgpKeyPopupView extends AbstractViewNext {
constructor() {
super();
2014-08-25 15:10:51 +08:00
this.email = ko.observable('');
this.email.focus = ko.observable('');
this.email.error = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.name = ko.observable('');
this.password = ko.observable('');
2020-08-14 04:58:41 +08:00
this.keyBitLength = ko.observable(2048);
2014-08-21 23:08:34 +08:00
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
2014-08-21 23:08:34 +08:00
this.email.subscribe(() => {
this.email.error(false);
});
2016-09-10 06:38:16 +08:00
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
@command()
generateOpenPgpKeyCommand() {
2019-07-05 03:19:24 +08:00
const userId = {},
2016-09-10 06:38:16 +08:00
openpgpKeyring = PgpStore.openpgpKeyring;
this.email.error(!this.email().trim());
2019-07-05 03:19:24 +08:00
if (!openpgpKeyring || this.email.error()) {
2016-09-10 06:38:16 +08:00
return false;
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
userId.email = this.email();
if (this.name()) {
2016-09-10 06:38:16 +08:00
userId.name = this.name();
}
2015-04-07 03:32:19 +08:00
2016-09-10 06:38:16 +08:00
this.submitRequest(true);
this.submitError('');
setTimeout(() => {
2016-09-10 06:38:16 +08:00
try {
2019-07-05 03:19:24 +08:00
PgpStore.openpgp
.generateKey({
userIds: [userId],
numBits: pInt(this.keyBitLength()),
passphrase: this.password().trim()
2019-07-05 03:19:24 +08:00
})
.then((keyPair) => {
this.submitRequest(false);
if (keyPair && keyPair.privateKeyArmored) {
openpgpKeyring.privateKeys.importKey(keyPair.privateKeyArmored);
openpgpKeyring.publicKeys.importKey(keyPair.publicKeyArmored);
openpgpKeyring.store();
getApp().reloadOpenPgpKeys();
this.cancelCommand && this.cancelCommand();
2019-07-05 03:19:24 +08:00
}
})
.catch((e) => {
this.submitRequest(false);
this.showError(e);
});
} catch (e) {
2016-09-10 06:38:16 +08:00
this.submitRequest(false);
this.showError(e);
}
2020-08-14 04:58:41 +08:00
}, 100);
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
return true;
}
2014-08-21 23:08:34 +08:00
showError(e) {
console.log(e);
2019-07-05 03:19:24 +08:00
if (e && e.message) {
this.submitError(e.message);
}
2016-07-16 05:29:42 +08:00
}
clearPopup() {
this.name('');
this.password('');
this.email('');
this.email.error(false);
2020-08-14 04:58:41 +08:00
this.keyBitLength(2048);
2016-07-16 05:29:42 +08:00
this.submitError('');
}
onShow() {
this.clearPopup();
}
onShowWithDelay() {
this.email.focus(true);
}
}
2019-07-05 03:19:24 +08:00
export { NewOpenPgpKeyPopupView, NewOpenPgpKeyPopupView as default };