snappymail/dev/View/Popup/NewOpenPgpKey.js

117 lines
2.4 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2014-08-25 23:49:01 +08:00
2019-07-05 03:19:24 +08:00
import { Magics } from 'Common/Enums';
import { trim, log, delegateRun, 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('');
this.keyBitLength = ko.observable(Magics.BitLength2048);
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;
2016-09-10 06:38:16 +08:00
this.email.error('' === trim(this.email()));
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();
2019-07-05 03:19:24 +08:00
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: trim(this.password())
})
.then((keyPair) => {
this.submitRequest(false);
if (keyPair && keyPair.privateKeyArmored) {
openpgpKeyring.privateKeys.importKey(keyPair.privateKeyArmored);
openpgpKeyring.publicKeys.importKey(keyPair.publicKeyArmored);
openpgpKeyring.store();
getApp().reloadOpenPgpKeys();
delegateRun(this, 'cancelCommand');
}
})
.catch((e) => {
this.submitRequest(false);
this.showError(e);
});
} catch (e) {
2016-09-10 06:38:16 +08:00
this.submitRequest(false);
this.showError(e);
}
}, Magics.Time100ms);
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) {
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);
this.keyBitLength(Magics.BitLength2048);
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 };