1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-11 03:35:01 -05:00
mail/src/js/app-controller.js

185 lines
4.2 KiB
JavaScript
Raw Normal View History

/**
* The main application controller
*/
define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keychain-dao',
'js/dao/cloudstorage-dao', 'js/app-config', 'cordova'
], function($, ImapClient, SmtpClient, EmailDAO, KeychainDAO, cloudstorage, app) {
'use strict';
var self = {},
emailDao;
/**
* Start the application by loading the view templates
*/
2013-06-10 11:57:33 -04:00
self.start = function(callback) {
// the views to load
2013-06-10 14:00:14 -04:00
var views = ['login', 'compose', 'folderlist', 'messagelist',
'messagelistitem', 'read'
];
// are we running in native app or in browser?
2013-06-23 09:30:19 -04:00
if (document.URL.indexOf("http") === 0 || document.URL.indexOf("app") === 0 || document.URL.indexOf("chrome") === 0) {
console.log('Assuming Browser environment...');
onDeviceReady();
} else {
console.log('Assuming Cordova environment...');
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
console.log('Starting app.');
2013-06-10 21:14:57 -04:00
loadTemplates(views, callback);
}
};
/**
* Executes a number of commands
*/
2013-06-10 11:57:33 -04:00
self.execute = function(cmd, args, callback) {
if (cmd === 'login') {
// login user
fetchOAuthToken(args.password, function(err, userId) {
callback({
err: err,
userId: userId
});
});
} else if (cmd === 'syncEmails') {
// list emails from folder
emailDao.syncFromCloud(args.folder, function(err) {
callback({
err: err
});
});
} else if (cmd === 'listEmails') {
// list emails from folder
emailDao.listItems(args.folder, args.offset, args.num, function(err, emails) {
callback({
err: err,
emails: emails
});
});
} else if (cmd === 'getEmail') {
// list emails from folder
var mail = emailDao.getItem(args.folder, args.messageId);
callback({
err: null,
email: mail
});
} else if (cmd === 'sendEmail') {
// list emails from folder
2013-06-06 14:41:25 -04:00
emailDao.sendEmail(args.email, function(err) {
callback({
err: err
});
});
} else {
// error: invalid message from sandbox
callback({
err: {
errMsg: 'Invalid message posted from sandbox!'
}
});
}
};
//
// Helper methods
//
function fetchOAuthToken(password, callback) {
// get OAuth Token from chrome
chrome.identity.getAuthToken({
'interactive': true
},
function(token) {
// fetch gmail user's email address from the Google Authorization Server endpoint
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
type: 'GET',
dataType: 'json',
success: function(info) {
// login using the received email address
login(info.email, password, token, function(err) {
// send email address to sandbox
callback(err, info.email);
});
},
error: function(xhr, textStatus, err) {
callback({
errMsg: xhr.status + ': ' + xhr.statusText,
err: err
});
}
});
}
);
}
function login(userId, password, token, callback) {
var auth, imapOptions, smtpOptions,
keychain, imapClient, smtpClient;
// create mail credentials objects for imap/smtp
auth = {
XOAuth2: {
user: userId,
clientId: '440907777130.apps.googleusercontent.com',
accessToken: token
}
};
imapOptions = {
secure: true,
port: 993,
host: 'imap.gmail.com',
auth: auth
};
smtpOptions = {
secure: true,
port: 465,
host: 'smtp.gmail.com',
auth: auth
};
// init objects and inject dependencies
keychain = new KeychainDAO(cloudstorage);
imapClient = new ImapClient(imapOptions);
smtpClient = new SmtpClient(smtpOptions);
emailDao = new EmailDAO(keychain, imapClient, smtpClient);
// init email dao
var account = {
emailAddress: userId,
symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize,
asymKeySize: app.config.asymKeySize
};
emailDao.init(account, password, callback);
}
2013-06-10 21:14:57 -04:00
function loadTemplates(names, callback) {
var loadTemplate = function(index) {
var name = names[index];
console.log('Loading template: ' + name);
$.get('tpl/' + name + '.html', function(data) {
app.util.tpl.templates[name] = data;
index++;
if (index < names.length) {
loadTemplate(index);
} else {
callback();
}
});
};
loadTemplate(0);
}
2013-06-10 11:57:33 -04:00
return self;
});