snappymail/dev/View/Popup/NewOpenPgpKey.js

114 lines
2.4 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,
sUserID = '',
mKeyPair = null,
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;
}
sUserID = this.email();
if ('' !== this.name())
{
2014-08-21 23:08:34 +08:00
sUserID = this.name() + ' <' + sUserID + '>';
}
2014-08-21 23:08:34 +08:00
this.submitRequest(true);
_.delay(function () {
2015-02-03 07:58:58 +08:00
mKeyPair = PgpStore.openpgp.generateKeyPair({
2014-08-21 23:08:34 +08:00
'userId': sUserID,
'numBits': Utils.pInt(self.keyBitLength()),
'passphrase': Utils.trim(self.password())
});
if (mKeyPair && mKeyPair.privateKeyArmored)
{
oOpenpgpKeyring.privateKeys.importKey(mKeyPair.privateKeyArmored);
oOpenpgpKeyring.publicKeys.importKey(mKeyPair.publicKeyArmored);
oOpenpgpKeyring.store();
2014-10-18 21:43:44 +08:00
require('App/User').reloadOpenPgpKeys();
2014-08-21 23:08:34 +08:00
Utils.delegateRun(self, 'cancelCommand');
}
2014-08-21 23:08:34 +08:00
self.submitRequest(false);
}, 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
}());