snappymail/dev/View/Popup/ComposeOpenPgp.js

264 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-03-20 06:39:36 +08:00
2014-09-05 06:49:03 +08:00
(function () {
2014-03-20 06:39:36 +08:00
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'),
key = require('key'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
Enums = require('Common/Enums'),
2014-03-20 06:39:36 +08:00
2014-10-18 21:43:44 +08:00
Data = require('Storage/User/Data'),
2014-03-21 07:47:13 +08:00
EmailModel = require('Model/Email'),
2014-08-25 15:10:51 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
2014-08-25 15:10:51 +08:00
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function ComposeOpenPgpPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.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(
Data.openpgp.signClearMessage([oPrivateKey], self.text())
2014-08-21 23:08:34 +08:00
);
}
else if (oPrivateKey && 0 < aPublicKeys.length)
{
self.resultCallback(
Data.openpgp.signAndEncryptMessage(aPublicKeys, oPrivateKey, self.text())
2014-08-21 23:08:34 +08:00
);
}
else if (!oPrivateKey && 0 < aPublicKeys.length)
{
self.resultCallback(
Data.openpgp.encryptMessage(aPublicKeys, self.text())
2014-08-21 23:08:34 +08:00
);
}
}
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
kn.extendAsViewModel(['View/Popup/ComposeOpenPgp', 'PopupsComposeOpenPgpViewModel'], ComposeOpenPgpPopupView);
_.extend(ComposeOpenPgpPopupView.prototype, AbstractView.prototype);
2014-03-21 07:47:13 +08:00
ComposeOpenPgpPopupView.prototype.clearPopup = function ()
2014-08-21 23:08:34 +08:00
{
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
ComposeOpenPgpPopupView.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
ComposeOpenPgpPopupView.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
ComposeOpenPgpPopupView.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
ComposeOpenPgpPopupView.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 = ComposeOpenPgpPopupView;
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
}());