snappymail/dev/ViewModels/Popups/PopupsNewOpenPgpKeyViewModel.js

116 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-08-25 23:49:01 +08:00
(function (module, require) {
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
window = require('window'),
_ = require('_'),
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-08-25 23:49:01 +08:00
Utils = require('Utils'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
Data = require('Storage:RainLoop:Data'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
kn = require('App:Knoin'),
KnoinAbstractViewModel = require('Knoin:AbstractViewModel')
2014-08-21 23:08:34 +08:00
;
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
2014-08-27 23:59:44 +08:00
function PopupsNewOpenPgpKeyViewModel()
2014-08-21 23:08:34 +08:00
{
2014-08-27 23:59:44 +08:00
KnoinAbstractViewModel.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,
oOpenpgpKeyring = Data.openpgpKeyring
;
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 () {
// mKeyPair = window.openpgp.generateKeyPair(1, Utils.pInt(self.keyBitLength()), sUserID, Utils.trim(self.password()));
mKeyPair = window.openpgp.generateKeyPair({
'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-08-27 23:59:44 +08:00
require('App:RainLoop').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'], PopupsNewOpenPgpKeyViewModel);
_.extend(PopupsNewOpenPgpKeyViewModel.prototype, KnoinAbstractViewModel.prototype);
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
PopupsNewOpenPgpKeyViewModel.prototype.clearPopup = function ()
2014-08-21 23:08:34 +08:00
{
this.name('');
this.password('');
this.email('');
this.email.error(false);
this.keyBitLength(2048);
};
2014-08-27 23:59:44 +08:00
PopupsNewOpenPgpKeyViewModel.prototype.onShow = function ()
2014-08-21 23:08:34 +08:00
{
this.clearPopup();
};
2014-08-27 23:59:44 +08:00
PopupsNewOpenPgpKeyViewModel.prototype.onFocus = function ()
2014-08-21 23:08:34 +08:00
{
this.email.focus(true);
};
2014-08-27 23:59:44 +08:00
module.exports = PopupsNewOpenPgpKeyViewModel;
2014-08-25 23:49:01 +08:00
}(module, require));