2014-03-13 06:29:33 +08:00
|
|
|
|
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);
|
2016-07-16 05:29:42 +08:00
|
|
|
this.submitError = ko.observable('');
|
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();
|
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.submitRequest(true);
|
2016-07-16 05:29:42 +08:00
|
|
|
this.submitError('');
|
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
|
|
|
try {
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-10 03:52:30 +08:00
|
|
|
PgpStore.openpgp.generateKey({
|
2016-06-30 08:02:45 +08:00
|
|
|
userIds: [oUserId],
|
|
|
|
numBits: Utils.pInt(self.keyBitLength()),
|
|
|
|
passphrase: Utils.trim(self.password())
|
2016-08-10 03:52:30 +08:00
|
|
|
}).then(function(mKeyPair) {
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
self.submitRequest(false);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
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();
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
require('App/User').default.reloadOpenPgpKeys();
|
|
|
|
Utils.delegateRun(self, 'cancelCommand');
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
}).then(null, function(e) {
|
2015-07-06 02:06:19 +08:00
|
|
|
self.submitRequest(false);
|
2016-07-16 05:29:42 +08:00
|
|
|
self.showError(e);
|
2016-06-30 08:02:45 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
self.submitRequest(false);
|
2016-07-16 05:29:42 +08:00
|
|
|
self.showError(e);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
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-07-16 05:29:42 +08:00
|
|
|
NewOpenPgpKeyPopupView.prototype.showError = function(e)
|
|
|
|
{
|
|
|
|
Utils.log(e);
|
|
|
|
if (e && e.message)
|
|
|
|
{
|
|
|
|
this.submitError(e.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
NewOpenPgpKeyPopupView.prototype.clearPopup = function()
|
|
|
|
{
|
|
|
|
this.name('');
|
|
|
|
this.password('');
|
2014-03-13 06:29:33 +08:00
|
|
|
|
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-07-16 05:29:42 +08:00
|
|
|
|
|
|
|
this.submitError('');
|
2016-06-30 08:02:45 +08:00
|
|
|
};
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
NewOpenPgpKeyPopupView.prototype.onShow = function()
|
|
|
|
{
|
|
|
|
this.clearPopup();
|
|
|
|
};
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
NewOpenPgpKeyPopupView.prototype.onShowWithDelay = function()
|
|
|
|
{
|
|
|
|
this.email.focus(true);
|
|
|
|
};
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
module.exports = NewOpenPgpKeyPopupView;
|