2013-06-06 07:34:22 -04:00
|
|
|
/**
|
|
|
|
* The main application controller
|
|
|
|
*/
|
2013-06-10 21:14:57 -04:00
|
|
|
define(['jquery', 'js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/cloudstorage-dao',
|
|
|
|
'js/app-config', 'cordova'
|
|
|
|
], function($, EmailDAO, KeychainDAO, cloudstorage, app) {
|
2013-06-06 07:34:22 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-06-10 11:57:33 -04:00
|
|
|
var self = {};
|
2013-06-06 07:34:22 -04:00
|
|
|
|
2013-06-10 21:14:57 -04:00
|
|
|
var emailDao;
|
|
|
|
|
2013-06-06 07:34:22 -04:00
|
|
|
/**
|
|
|
|
* Initializes modules through dependecy injection
|
|
|
|
*/
|
2013-06-10 11:57:33 -04:00
|
|
|
self.init = function(callback) {
|
2013-06-10 21:14:57 -04:00
|
|
|
var keychain = new KeychainDAO(cloudstorage);
|
|
|
|
emailDao = new EmailDAO(cloudstorage, keychain);
|
2013-06-06 07:34:22 -04:00
|
|
|
callback();
|
|
|
|
};
|
|
|
|
|
2013-06-06 09:00:51 -04:00
|
|
|
/**
|
|
|
|
* Start the application by loading the view templates
|
|
|
|
*/
|
2013-06-10 11:57:33 -04:00
|
|
|
self.start = function(callback) {
|
2013-06-06 09:00:51 -04:00
|
|
|
// the views to load
|
2013-06-10 14:00:14 -04:00
|
|
|
var views = ['login', 'compose', 'folderlist', 'messagelist',
|
|
|
|
'messagelistitem', 'read'
|
2013-06-06 09:00:51 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
// 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) {
|
2013-06-06 09:00:51 -04:00
|
|
|
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);
|
2013-06-06 09:00:51 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-06 07:34:22 -04:00
|
|
|
/**
|
|
|
|
* Executes a number of commands
|
|
|
|
*/
|
2013-06-10 11:57:33 -04:00
|
|
|
self.execute = function(cmd, args, callback) {
|
2013-06-06 07:34:22 -04:00
|
|
|
if (cmd === 'login') {
|
|
|
|
// login user
|
|
|
|
login(args.userId, args.password, function(err) {
|
|
|
|
callback({
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} 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
|
2013-06-06 13:19:37 -04:00
|
|
|
emailDao.listItems(args.folder, args.offset, args.num, function(err, emails) {
|
2013-06-06 07:34:22 -04:00
|
|
|
callback({
|
|
|
|
err: err,
|
2013-06-06 13:19:37 -04:00
|
|
|
emails: emails
|
2013-06-06 07:34:22 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (cmd === 'getEmail') {
|
|
|
|
// list emails from folder
|
|
|
|
var mail = emailDao.getItem(args.folder, args.messageId);
|
|
|
|
callback({
|
|
|
|
err: null,
|
2013-06-06 13:19:37 -04:00
|
|
|
email: mail
|
2013-06-06 07:34:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
} else if (cmd === 'sendEmail') {
|
|
|
|
// list emails from folder
|
2013-06-06 14:41:25 -04:00
|
|
|
emailDao.sendEmail(args.email, function(err) {
|
2013-06-06 07:34:22 -04:00
|
|
|
callback({
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// error: invalid message from sandbox
|
|
|
|
callback({
|
|
|
|
err: {
|
|
|
|
errMsg: 'Invalid message posted from sandbox!'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Helper methods
|
|
|
|
//
|
|
|
|
|
|
|
|
function login(userId, password, callback) {
|
|
|
|
var account = new app.model.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;
|
|
|
|
});
|