snappymail/dev/ViewModels/PopupsComposeOpenPgpViewModel.js

203 lines
4 KiB
JavaScript
Raw Normal View History

2014-03-20 06:39:36 +08:00
/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function PopupsComposeOpenPgpViewModel()
{
KnoinAbstractViewModel.call(this, 'Popups', 'PopupsComposeOpenPgp');
this.notification = ko.observable('');
this.sign = ko.observable(true);
this.encrypt = ko.observable(true);
this.password = ko.observable('');
this.password.focus = ko.observable(true);
2014-03-21 07:47:13 +08:00
this.from = ko.observable('');
this.to = ko.observableArray([]);
this.text = ko.observable('');
this.resultCallback = null;
this.submitRequest = ko.observable(false);
2014-03-20 06:39:36 +08:00
// commands
this.doCommand = Utils.createCommand(this, function () {
2014-03-21 07:47:13 +08:00
var
self = this,
bResult = true,
2014-04-02 02:03:37 +08:00
oData = RL.data(),
2014-03-21 07:47:13 +08:00
oPrivateKey = null,
2014-04-02 02:03:37 +08:00
aPublicKeys = []
2014-03-21 07:47:13 +08:00
;
this.submitRequest(true);
if (bResult && this.sign() && '' === this.from())
{
2014-04-02 02:03:37 +08:00
// TODO i18n
this.notification('Please specify FROM email address');
2014-03-21 07:47:13 +08:00
bResult = false;
}
if (bResult && this.sign())
{
2014-04-02 02:03:37 +08:00
oPrivateKey = oData.findPrivateKeyByEmail(this.from(), this.password());
2014-03-21 07:47:13 +08:00
if (!oPrivateKey)
{
2014-04-02 02:03:37 +08:00
// TODO i18n
2014-03-21 07:47:13 +08:00
this.notification('No private key found for "' + this.from() + '" email');
bResult = false;
}
}
if (bResult && this.encrypt() && 0 === this.to().length)
{
2014-04-02 02:03:37 +08:00
// TODO i18n
2014-03-21 07:47:13 +08:00
this.notification('Please specify at least one recipient');
bResult = false;
}
if (bResult && this.encrypt())
{
aPublicKeys = [];
_.each(this.to(), function (sEmail) {
2014-04-02 02:03:37 +08:00
var aKeys = oData.findPublicKeysByEmail(sEmail);
if (0 === aKeys.length && bResult)
2014-03-21 07:47:13 +08:00
{
2014-04-02 02:03:37 +08:00
// TODO i18n
2014-03-21 07:47:13 +08:00
self.notification('No public key found for "' + sEmail + '" email');
bResult = false;
}
aPublicKeys = aPublicKeys.concat(aKeys);
});
2014-04-02 02:03:37 +08:00
if (bResult && (0 === aPublicKeys.length || this.to().length !== aPublicKeys.length))
2014-03-21 07:47:13 +08:00
{
bResult = false;
}
}
_.delay(function () {
if (self.resultCallback && bResult)
{
try {
if (oPrivateKey && 0 === aPublicKeys.length)
{
self.resultCallback(
window.openpgp.signClearMessage([oPrivateKey], self.text())
);
}
else if (oPrivateKey && 0 < aPublicKeys.length)
{
self.resultCallback(
window.openpgp.signAndEncryptMessage(aPublicKeys, oPrivateKey, self.text())
);
}
else if (!oPrivateKey && 0 < aPublicKeys.length)
{
self.resultCallback(
window.openpgp.encryptMessage(aPublicKeys, self.text())
);
}
}
catch (e)
{
2014-04-02 02:03:37 +08:00
// TODO i18n
2014-03-21 07:47:13 +08:00
self.notification('OpenPGP error: ' + e);
bResult = false;
}
}
if (bResult)
{
self.cancelCommand();
}
self.submitRequest(false);
}, 10);
2014-03-20 06:39:36 +08:00
}, function () {
2014-03-21 07:47:13 +08:00
return !this.submitRequest() && (this.sign() || this.encrypt());
2014-03-20 06:39:36 +08:00
});
Knoin.constructorEnd(this);
}
Utils.extendAsViewModel('PopupsComposeOpenPgpViewModel', PopupsComposeOpenPgpViewModel);
PopupsComposeOpenPgpViewModel.prototype.clearPopup = function ()
{
this.notification('');
this.password('');
this.password.focus(false);
2014-03-21 07:47:13 +08:00
this.from('');
this.to([]);
this.text('');
this.submitRequest(false);
this.resultCallback = null;
2014-03-20 06:39:36 +08:00
};
PopupsComposeOpenPgpViewModel.prototype.onHide = function ()
{
this.clearPopup();
};
PopupsComposeOpenPgpViewModel.prototype.onShow = function (fCallback, sText, sFromEmail, sTo, sCc, sBcc)
{
this.clearPopup();
2014-03-21 00:05:35 +08:00
var
oEmail = new EmailModel(),
sResultFromEmail = '',
aRec = []
;
2014-03-21 07:47:13 +08:00
this.resultCallback = fCallback;
2014-03-21 00:05:35 +08:00
oEmail.clear();
oEmail.mailsoParse(sFromEmail);
2014-03-21 07:47:13 +08:00
if ('' !== oEmail.email)
2014-03-21 00:05:35 +08:00
{
sResultFromEmail = oEmail.email;
2014-03-20 06:39:36 +08:00
}
2014-03-21 00:05:35 +08:00
if ('' !== sTo)
{
aRec.push(sTo);
}
if ('' !== sCc)
{
aRec.push(sCc);
}
if ('' !== sBcc)
{
aRec.push(sBcc);
}
aRec = aRec.join(', ').split(',');
aRec = _.compact(_.map(aRec, function (sValue) {
oEmail.clear();
oEmail.mailsoParse(Utils.trim(sValue));
return '' === oEmail.email ? false : oEmail.email;
}));
2014-03-21 07:47:13 +08:00
this.from(sResultFromEmail);
this.to(aRec);
this.text(sText);
2014-03-20 06:39:36 +08:00
};