snappymail/dev/View/Popup/ComposeOpenPgp.js

268 lines
5.4 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'),
Translator = require('Common/Translator'),
2014-03-20 06:39:36 +08:00
2015-02-04 01:18:08 +08:00
PgpStore = require('Stores/User/Pgp'),
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
{
this.notification(Translator.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())
{
2015-02-22 06:00:51 +08:00
oPrivateKey = PgpStore.findPrivateKeyByEmail(this.from(), this.password());
2014-08-21 23:08:34 +08:00
if (!oPrivateKey)
2014-03-21 07:47:13 +08:00
{
this.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND_FOR', {
2014-08-21 23:08:34 +08:00
'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
{
this.notification(Translator.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) {
2015-02-22 06:00:51 +08:00
var aKeys = PgpStore.findPublicKeysByEmail(sEmail);
2014-08-21 23:08:34 +08:00
if (0 === aKeys.length && bResult)
2014-03-21 07:47:13 +08:00
{
self.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND_FOR', {
2014-08-21 23:08:34 +08:00
'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)
{
var oPromise = null;
try
{
2014-08-21 23:08:34 +08:00
if (oPrivateKey && 0 === aPublicKeys.length)
{
oPromise = PgpStore.openpgp.signClearMessage([oPrivateKey], self.text());
2014-08-21 23:08:34 +08:00
}
else if (oPrivateKey && 0 < aPublicKeys.length)
{
oPromise = PgpStore.openpgp.signAndEncryptMessage(aPublicKeys, oPrivateKey, self.text());
2014-08-21 23:08:34 +08:00
}
else if (!oPrivateKey && 0 < aPublicKeys.length)
{
oPromise = PgpStore.openpgp.encryptMessage(aPublicKeys, self.text());
2014-08-21 23:08:34 +08:00
}
}
catch (e)
{
self.notification(Translator.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
2014-08-21 23:08:34 +08:00
'ERROR': '' + e
}));
}
2014-03-20 06:39:36 +08:00
if (oPromise)
{
try
{
oPromise.then(function (mData) {
self.resultCallback(mData);
self.cancelCommand();
})['catch'](function (e) {
self.notification(Translator.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
});
}
catch (e)
{
self.notification(Translator.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
}
}
2014-08-21 23:08:34 +08:00
}
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
2015-02-16 05:55:59 +08:00
ComposeOpenPgpPopupView.prototype.onHideWithDelay = 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
2015-02-16 05:55:59 +08:00
ComposeOpenPgpPopupView.prototype.onShowWithDelay = 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, oIdentity, sTo, sCc, sBcc)
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
var
aRec = [],
oEmail = new EmailModel()
2014-08-21 23:08:34 +08:00
;
this.resultCallback = fCallback;
2014-03-21 00:05:35 +08:00
2014-08-21 23:08:34 +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;
}));
this.from(oIdentity ? oIdentity.email() : '');
2014-08-21 23:08:34 +08:00
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
}());