snappymail/dev/ViewModels/Popups/PopupsComposeOpenPgpViewModel.js

262 lines
5.3 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 */
2014-08-21 23:08:34 +08:00
(function (module) {
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
'use strict';
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
var
window = require('../../External/window.js'),
ko = require('../../External/ko.js'),
key = require('../../External/key.js'),
Utils = require('../../Common/Utils.js'),
Enums = require('../../Common/Enums.js'),
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
Data = require('../../Storages/WebMailDataStorage.js'),
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
kn = require('../../Knoin/Knoin.js'),
KnoinAbstractViewModel = require('../../Knoin/KnoinAbstractViewModel.js')
;
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function PopupsComposeOpenPgpViewModel()
{
KnoinAbstractViewModel.call(this, 'Popups', 'PopupsComposeOpenPgp');
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
this.notification = ko.observable('');
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
this.sign = ko.observable(true);
this.encrypt = ko.observable(true);
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
this.password = ko.observable('');
this.password.focus = ko.observable(false);
this.buttonFocus = ko.observable(false);
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
this.from = ko.observable('');
this.to = ko.observableArray([]);
this.text = ko.observable('');
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
this.resultCallback = null;
this.submitRequest = ko.observable(false);
// commands
this.doCommand = Utils.createCommand(this, function () {
var
self = this,
bResult = true,
oPrivateKey = null,
aPublicKeys = []
;
this.submitRequest(true);
if (bResult && this.sign() && '' === this.from())
2014-03-21 07:47:13 +08:00
{
2014-08-21 23:08:34 +08:00
this.notification(Utils.i18n('PGP_NOTIFICATIONS/SPECIFY_FROM_EMAIL'));
2014-03-21 07:47:13 +08:00
bResult = false;
}
2014-08-21 23:08:34 +08:00
if (bResult && this.sign())
{
oPrivateKey = Data.findPrivateKeyByEmail(this.from(), this.password());
if (!oPrivateKey)
2014-03-21 07:47:13 +08:00
{
2014-08-21 23:08:34 +08:00
this.notification(Utils.i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND_FOR', {
'EMAIL': this.from()
2014-04-18 06:42:30 +08:00
}));
2014-08-21 23:08:34 +08:00
2014-03-21 07:47:13 +08:00
bResult = false;
}
2014-08-21 23:08:34 +08:00
}
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
if (bResult && this.encrypt() && 0 === this.to().length)
2014-03-21 07:47:13 +08:00
{
2014-08-21 23:08:34 +08:00
this.notification(Utils.i18n('PGP_NOTIFICATIONS/SPECIFY_AT_LEAST_ONE_RECIPIENT'));
2014-03-21 07:47:13 +08:00
bResult = false;
}
2014-08-21 23:08:34 +08:00
if (bResult && this.encrypt())
2014-03-21 07:47:13 +08:00
{
2014-08-21 23:08:34 +08:00
aPublicKeys = [];
_.each(this.to(), function (sEmail) {
var aKeys = Data.findPublicKeysByEmail(sEmail);
if (0 === aKeys.length && bResult)
2014-03-21 07:47:13 +08:00
{
2014-08-21 23:08:34 +08:00
self.notification(Utils.i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND_FOR', {
'EMAIL': sEmail
}));
bResult = false;
2014-03-21 07:47:13 +08:00
}
2014-04-18 06:42:30 +08:00
2014-08-21 23:08:34 +08:00
aPublicKeys = aPublicKeys.concat(aKeys);
});
if (bResult && (0 === aPublicKeys.length || this.to().length !== aPublicKeys.length))
{
2014-03-21 07:47:13 +08:00
bResult = false;
}
}
2014-08-21 23:08:34 +08:00
_.delay(function () {
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
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)
{
self.notification(Utils.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
bResult = false;
}
}
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
if (bResult)
{
self.cancelCommand();
}
2014-08-21 23:08:34 +08:00
self.submitRequest(false);
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
}, 10);
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest() && (this.sign() || this.encrypt());
});
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
this.sDefaultKeyScope = Enums.KeyState.PopupComposeOpenPGP;
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
kn.extendAsViewModel('PopupsComposeOpenPgpViewModel', PopupsComposeOpenPgpViewModel);
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
PopupsComposeOpenPgpViewModel.prototype.clearPopup = function ()
{
this.notification('');
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
this.password('');
this.password.focus(false);
this.buttonFocus(false);
2014-08-21 23:08:34 +08:00
this.from('');
this.to([]);
this.text('');
2014-08-21 23:08:34 +08:00
this.submitRequest(false);
2014-08-21 23:08:34 +08:00
this.resultCallback = null;
};
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
PopupsComposeOpenPgpViewModel.prototype.onBuild = function ()
{
2014-08-21 23:08:34 +08:00
key('tab,shift+tab', Enums.KeyState.PopupComposeOpenPGP, _.bind(function () {
2014-08-21 23:08:34 +08:00
switch (true)
{
case this.password.focus():
this.buttonFocus(true);
break;
case this.buttonFocus():
this.password.focus(true);
break;
}
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
return false;
2014-03-21 00:05:35 +08:00
2014-08-21 23:08:34 +08:00
}, this));
};
2014-03-21 00:05:35 +08:00
2014-08-21 23:08:34 +08:00
PopupsComposeOpenPgpViewModel.prototype.onHide = function ()
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
};
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
PopupsComposeOpenPgpViewModel.prototype.onFocus = function ()
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
if (this.sign())
{
this.password.focus(true);
}
else
{
this.buttonFocus(true);
}
};
2014-03-21 00:05:35 +08:00
2014-08-21 23:08:34 +08:00
PopupsComposeOpenPgpViewModel.prototype.onShow = function (fCallback, sText, sFromEmail, sTo, sCc, sBcc)
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
var
oEmail = new EmailModel(),
sResultFromEmail = '',
aRec = []
;
this.resultCallback = fCallback;
2014-03-21 00:05:35 +08:00
oEmail.clear();
2014-08-21 23:08:34 +08:00
oEmail.mailsoParse(sFromEmail);
if ('' !== oEmail.email)
{
sResultFromEmail = oEmail.email;
}
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;
}));
this.from(sResultFromEmail);
this.to(aRec);
this.text(sText);
};
module.exports = new PopupsComposeOpenPgpViewModel();
}(module));