Bug 861462 - Allow contacts to be preloaded when we create the contacts DB. r=bent

This commit is contained in:
Gregor Wagner 2013-04-18 10:39:10 -04:00
Родитель 3e1b613ce9
Коммит c042f794c9
1 изменённых файлов: 51 добавлений и 0 удалений

Просмотреть файл

@ -306,6 +306,57 @@ ContactDB.prototype = {
db.createObjectStore(SAVED_GETALL_STORE_NAME); db.createObjectStore(SAVED_GETALL_STORE_NAME);
} }
} }
// Add default contacts
if (aOldVersion == 0) {
let jsm = {};
Cu.import("resource://gre/modules/FileUtils.jsm", jsm);
Cu.import("resource://gre/modules/NetUtil.jsm", jsm);
// Loading resource://app/defaults/contacts.json doesn't work because
// contacts.json is not in the omnijar.
// So we look for the app dir instead and go from here...
let contactsFile = jsm.FileUtils.getFile("DefRt", ["contacts.json"], false);
if (!contactsFile || (contactsFile && !contactsFile.exists())) {
// For b2g desktop
contactsFile = jsm.FileUtils.getFile("ProfD", ["contacts.json"], false);
if (!contactsFile || (contactsFile && !contactsFile.exists())) {
return;
}
}
let chan = jsm.NetUtil.newChannel(contactsFile);
let stream = chan.open();
// Obtain a converter to read from a UTF-8 encoded input stream.
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let rawstr = converter.ConvertToUnicode(jsm.NetUtil.readInputStreamToString(
stream,
stream.available()) || "");
stream.close();
let contacts;
try {
contacts = JSON.parse(rawstr);
} catch(e) {
if (DEBUG) debug("Error parsing " + contactsFile.path + " : " + e);
return;
}
let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
objectStore = aTransaction.objectStore(STORE_NAME);
for (let i = 0; i < contacts.length; i++) {
let contact = {};
contact.properties = contacts[i];
contact.id = idService.generateUUID().toString().replace('-', '', 'g')
.replace('{', '')
.replace('}', '');
contact = this.makeImport(contact);
this.updateRecordMetadata(contact);
if (DEBUG) debug("import: " + JSON.stringify(contact));
objectStore.put(contact);
}
}
}, },
makeImport: function makeImport(aContact) { makeImport: function makeImport(aContact) {