2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* High level storage api that handles all persistence on the device. If
|
|
|
|
* SQLcipher/SQLite is available, all data is securely persisted there,
|
|
|
|
* through transparent encryption. If not, the crypto API is
|
|
|
|
* used to encrypt data on the fly before persisting via a JSON store.
|
|
|
|
*/
|
|
|
|
app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
|
2013-04-01 18:12:15 -04:00
|
|
|
'use strict';
|
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
/**
|
2013-04-02 09:02:57 -04:00
|
|
|
* Stores a list of encrypted items in the object store
|
|
|
|
* @param list [Array] The list of items to be persisted
|
|
|
|
* @param type [String] The type of item to be persisted e.g. 'email'
|
2013-03-13 11:58:46 -04:00
|
|
|
*/
|
2013-04-02 09:02:57 -04:00
|
|
|
this.storeEcryptedList = function(list, type, callback) {
|
|
|
|
var i, date, key, items = [];
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-31 19:45:38 -04:00
|
|
|
// nothing to store
|
|
|
|
if (list.length === 0) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
// format items for batch storing in dao
|
2013-05-31 09:51:34 -04:00
|
|
|
list.forEach(function(i) {
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
// put date in key if available... for easy querying
|
2013-05-31 09:51:34 -04:00
|
|
|
if (i.sentDate) {
|
|
|
|
date = util.parseDate(i.sentDate);
|
|
|
|
key = crypto.emailAddress + '_' + type + '_' + date.getTime() + '_' + i.id;
|
2013-04-02 09:02:57 -04:00
|
|
|
} else {
|
2013-05-31 09:51:34 -04:00
|
|
|
key = crypto.emailAddress + '_' + type + '_' + i.id;
|
2013-04-01 18:12:15 -04:00
|
|
|
}
|
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
items.push({
|
|
|
|
key: key,
|
2013-05-31 09:51:34 -04:00
|
|
|
object: i
|
2013-04-01 18:12:15 -04:00
|
|
|
});
|
2013-05-31 09:51:34 -04:00
|
|
|
|
|
|
|
});
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
jsonDao.batch(items, function() {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-05-31 09:51:34 -04:00
|
|
|
* List stored items of a given type
|
2013-04-02 09:02:57 -04:00
|
|
|
* @param type [String] The type of item e.g. 'email'
|
|
|
|
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
|
|
|
|
* @param num [Number] The number of items to fetch (null means fetch all)
|
|
|
|
*/
|
2013-05-31 09:51:34 -04:00
|
|
|
this.listEncryptedItems = function(type, offset, num, callback) {
|
2013-04-02 09:02:57 -04:00
|
|
|
// fetch all items of a certain type from the data-store
|
|
|
|
jsonDao.list(crypto.emailAddress + '_' + type, offset, num, function(encryptedList) {
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-31 09:51:34 -04:00
|
|
|
callback(null, encryptedList);
|
2013-04-02 09:02:57 -04:00
|
|
|
});
|
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* Clear the whole device data-store
|
|
|
|
*/
|
|
|
|
this.clear = function(callback) {
|
|
|
|
jsonDao.clear(callback);
|
2013-03-13 11:58:46 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
};
|