mail/src/js/controller/read.js

158 lines
4.5 KiB
JavaScript
Raw Normal View History

define(function(require) {
'use strict';
var appController = require('js/app-controller'),
download = require('js/util/download'),
angular = require('angular'),
str = require('js/app-config').string,
emailDao, invitationDao, outbox, crypto, keychain;
//
// Controller
//
var ReadCtrl = function($scope) {
emailDao = appController._emailDao;
invitationDao = appController._invitationDao;
outbox = appController._outboxBo;
crypto = appController._crypto;
keychain = appController._keychain;
2013-12-04 05:50:20 -05:00
// set default value so that the popover height is correct on init
$scope.keyId = 'XXXXXXXX';
2013-12-04 05:50:20 -05:00
$scope.state.read = {
open: false,
toggle: function(to) {
this.open = to;
}
};
$scope.lineEmpty = function(line) {
return line.replace(/>/g, '').trim().length === 0;
};
$scope.getKeyId = function(address) {
2014-02-20 17:53:53 -05:00
$scope.keyId = 'unknown user';
keychain.getReceiverPublicKey(address, function(err, pubkey) {
if (err) {
$scope.onError(err);
return;
}
if (!pubkey) {
return;
}
var fpr = crypto.getFingerprint(pubkey.publicKey);
var formatted = fpr.slice(32);
$scope.keyId = formatted;
$scope.$apply();
});
};
2014-02-23 17:20:47 -05:00
$scope.$watch('state.mailList.selected', function(mail) {
2013-12-04 10:42:17 -05:00
if (!mail) {
return;
}
// display sender security status
mail.from.forEach(checkPublicKey);
// display recipient security status
mail.to.forEach(checkPublicKey);
// display recipient security status
Array.isArray(mail.cc) && mail.cc.forEach(checkPublicKey);
});
function checkPublicKey(user) {
user.secure = undefined;
keychain.getReceiverPublicKey(user.address, function(err, pubkey) {
if (err) {
$scope.onError(err);
return;
}
if (pubkey && pubkey.publicKey) {
user.secure = true;
} else {
user.secure = false;
}
$scope.$apply();
});
}
$scope.download = function(attachment) {
// download file to disk if content is available
if (attachment.content) {
saveToDisk(attachment);
return;
}
var folder = $scope.state.nav.currentFolder;
var email = $scope.state.mailList.selected;
emailDao.getAttachment({
path: folder.path,
uid: email.uid,
attachment: attachment
}, function(err) {
if (err) {
$scope.onError(err);
return;
}
saveToDisk(attachment);
});
function saveToDisk(attachment) {
download.createDownload({
content: attachment.content,
filename: attachment.filename,
contentType: attachment.mimeType
}, $scope.onError);
}
};
$scope.inviteUser = function(address) {
invitationDao.invite({
recipient: address,
sender: emailDao._account.emailAddress
}, function(err) {
if (err) {
$scope.onError(err);
return;
}
var invitationMail = {
from: [emailDao._account.emailAddress],
to: [address],
subject: str.invitationSubject,
body: str.invitationMessage
};
// send invitation mail
outbox.put(invitationMail, $scope.onError);
});
};
};
//
// Directives
//
var ngModule = angular.module('read', []);
ngModule.directive('frameLoad', function() {
return function(scope, elm) {
elm.bind('load', function() {
var frame = elm[0];
frame.height = frame.contentWindow.document.body.scrollHeight + 'px';
});
};
});
return ReadCtrl;
});