snappymail/dev/View/Popup/NewOpenPgpKey.js

130 lines
2.5 KiB
JavaScript
Raw Normal View History

import _ from '_';
import ko from 'ko';
2014-08-25 23:49:01 +08:00
import {Magics} from 'Common/Enums';
import {trim, log, createCommand, 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
import {getApp} from 'Helper/Apps/User';
2014-08-21 23:08:34 +08:00
import {view, ViewType} from 'Knoin/Knoin';
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
2014-08-21 23:08:34 +08:00
@view({
name: 'View/Popup/NewOpenPgpKey',
type: ViewType.Popup,
templateID: 'PopupsNewOpenPgpKey'
})
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);
});
2014-08-21 23:08:34 +08:00
this.generateOpenPgpKeyCommand = createCommand(() => {
2014-08-21 23:08:34 +08:00
const
userId = {},
openpgpKeyring = PgpStore.openpgpKeyring;
this.email.error('' === trim(this.email()));
if (!openpgpKeyring || this.email.error())
{
return false;
}
2014-08-21 23:08:34 +08:00
userId.email = this.email();
if ('' !== this.name())
{
userId.name = this.name();
}
2015-04-07 03:32:19 +08:00
this.submitRequest(true);
this.submitError('');
_.delay(() => {
try {
PgpStore.openpgp.generateKey({
userIds: [userId],
numBits: pInt(this.keyBitLength()),
passphrase: trim(this.password())
}).then((keyPair) => {
2015-07-06 02:06:19 +08:00
this.submitRequest(false);
if (keyPair && keyPair.privateKeyArmored)
{
openpgpKeyring.privateKeys.importKey(keyPair.privateKeyArmored);
openpgpKeyring.publicKeys.importKey(keyPair.publicKeyArmored);
openpgpKeyring.store();
2014-08-21 23:08:34 +08:00
getApp().reloadOpenPgpKeys();
delegateRun(this, 'cancelCommand');
}
2014-08-21 23:08:34 +08:00
}).catch((e) => {
this.submitRequest(false);
this.showError(e);
});
}
catch (e)
{
this.submitRequest(false);
this.showError(e);
}
2014-08-21 23:08:34 +08:00
}, Magics.Time100ms);
2014-08-21 23:08:34 +08:00
return true;
});
}
2014-08-21 23:08:34 +08:00
showError(e) {
log(e);
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);
}
}
2016-06-30 08:02:45 +08:00
module.exports = NewOpenPgpKeyPopupView;