snappymail/dev/View/Popup/NewOpenPgpKey.js

126 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2014-08-25 23:49:01 +08:00
2016-07-06 03:52:52 +08:00
Enums = require('Common/Enums'),
2016-06-30 08:02:45 +08:00
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
PgpStore = require('Stores/User/Pgp'),
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function NewOpenPgpKeyPopupView()
{
AbstractView.call(this, 'Popups', 'PopupsNewOpenPgpKey');
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +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
2016-06-30 08:02:45 +08:00
this.name = ko.observable('');
this.password = ko.observable('');
2016-07-06 03:52:52 +08:00
this.keyBitLength = ko.observable(Enums.Magics.BitLength2048);
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
this.submitRequest = ko.observable(false);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.email.subscribe(function() {
this.email.error(false);
}, this);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.generateOpenPgpKeyCommand = Utils.createCommand(this, function() {
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
var
self = this,
oUserId = {},
oOpenpgpKeyring = PgpStore.openpgpKeyring;
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.email.error('' === Utils.trim(this.email()));
if (!oOpenpgpKeyring || this.email.error())
{
return false;
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
oUserId.email = this.email();
if ('' !== this.name())
{
oUserId.name = this.name();
}
2016-06-30 08:02:45 +08:00
this.submitRequest(true);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
_.delay(function() {
2015-04-07 03:32:19 +08:00
2016-06-30 08:02:45 +08:00
var mPromise = false;
2016-06-30 08:02:45 +08:00
try {
2016-06-30 08:02:45 +08:00
mPromise = PgpStore.openpgp.generateKey({
userIds: [oUserId],
numBits: Utils.pInt(self.keyBitLength()),
passphrase: Utils.trim(self.password())
});
2016-06-30 08:02:45 +08:00
mPromise.then(function(mKeyPair) {
2016-06-30 08:02:45 +08:00
self.submitRequest(false);
2016-06-30 08:02:45 +08:00
if (mKeyPair && mKeyPair.privateKeyArmored)
{
oOpenpgpKeyring.privateKeys.importKey(mKeyPair.privateKeyArmored);
oOpenpgpKeyring.publicKeys.importKey(mKeyPair.publicKeyArmored);
2015-07-06 02:06:19 +08:00
2016-06-30 08:02:45 +08:00
oOpenpgpKeyring.store();
2016-06-30 08:02:45 +08:00
require('App/User').default.reloadOpenPgpKeys();
Utils.delegateRun(self, 'cancelCommand');
}
2016-06-30 08:02:45 +08:00
}).then(null, function() {
2015-07-06 02:06:19 +08:00
self.submitRequest(false);
2016-06-30 08:02:45 +08:00
});
}
catch (e)
{
Utils.log(e);
self.submitRequest(false);
}
2014-08-21 23:08:34 +08:00
2016-07-06 03:52:52 +08:00
}, Enums.Magics.Time100ms);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
return true;
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
kn.constructorEnd(this);
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
kn.extendAsViewModel(['View/Popup/NewOpenPgpKey', 'PopupsNewOpenPgpKeyViewModel'], NewOpenPgpKeyPopupView);
_.extend(NewOpenPgpKeyPopupView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
NewOpenPgpKeyPopupView.prototype.clearPopup = function()
{
this.name('');
this.password('');
2016-06-30 08:02:45 +08:00
this.email('');
this.email.error(false);
2016-07-06 03:52:52 +08:00
this.keyBitLength(Enums.Magics.BitLength2048);
2016-06-30 08:02:45 +08:00
};
2016-06-30 08:02:45 +08:00
NewOpenPgpKeyPopupView.prototype.onShow = function()
{
this.clearPopup();
};
2016-06-30 08:02:45 +08:00
NewOpenPgpKeyPopupView.prototype.onShowWithDelay = function()
{
this.email.focus(true);
};
2016-06-30 08:02:45 +08:00
module.exports = NewOpenPgpKeyPopupView;