snappymail/dev/View/Popup/NewOpenPgpKey.js

132 lines
2.6 KiB
JavaScript
Raw Normal View History

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