Permalink
Browse files

Bug 835345 - Added preloaded contacts in build time r=vingtetun

  • Loading branch information...
1 parent ea8a221 commit 4eb1c502b065ae82af8e8e9473f80ebd564d6f27 @acperez acperez committed Feb 4, 2013
Showing with 123 additions and 3 deletions.
  1. +6 −2 Android.mk
  2. +8 −1 Makefile
  3. +109 −0 build/contacts.js
View
@@ -61,5 +61,9 @@ ifeq ($(CLEAN_PROFILE), 1)
rm -rf $(GAIA_PATH)/profile $(GAIA_PATH)/profile.tar.gz
endif
$(MAKE) -j1 $(GAIA_MAKE_FLAGS) profile
- cd $(GAIA_PATH)/profile && tar cfz $(abspath $@) webapps
-
+ @if [ ! -d $(GAIA_PATH)/profile/indexedDB ]; then \
+ cd $(GAIA_PATH)/profile && tar cfz $(abspath $@) webapps; \
+ else \
+ cd $(GAIA_PATH)/profile && tar cfz $(abspath $@) indexedDB webapps; \
+ rm -rf $(GAIA_PATH)/profile/indexedDB; \
+ fi
View
@@ -180,7 +180,7 @@ TEST_DIRS ?= $(CURDIR)/tests
# Generate profile/
-profile: multilocale applications-data preferences app-makefiles test-agent-config offline extensions install-xulrunner-sdk profile/settings.json
+profile: multilocale applications-data preferences app-makefiles test-agent-config offline contacts extensions install-xulrunner-sdk profile/settings.json
@echo "Profile Ready: please run [b2g|firefox] -profile $(CURDIR)$(SEP)profile"
LANG=POSIX # Avoiding sort order differences between OSes
@@ -263,6 +263,13 @@ offline-cache: webapp-manifests install-xulrunner-sdk
@$(call run-js-command, offline-cache)
@echo "Done"
+# Create contacts DB
+contacts: install-xulrunner-sdk
+ @echo "Generate contacts database"
+ @rm -rf profile/indexedDB
+ @$(call run-js-command, contacts)
+ @echo "Done"
+
# Create webapps
offline: webapp-manifests webapp-optimize webapp-zip optimize-clean
View
@@ -0,0 +1,109 @@
+'use strict';
+
+Cu.import("resource://gre/modules/ContactDB.jsm");
+
+function log(str) {
+ dump('-*- Contacts generator: ' + str + '\n');
+}
+
+let uuidGen = Cc["@mozilla.org/uuid-generator;1"]
+ .getService(Ci.nsIUUIDGenerator);
+
+const CONTACTS_FOLDER = 'data'
+const CONTACTS_FILE = 'contacts-default.json'
+
+let global = this;
+
+function saveContacts(contacts){
+ let idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"]
+ .getService(Ci.nsIIndexedDatabaseManager);
+ idbManager.initWindowless(global);
+
+ let contactDB = new ContactDB(global);
+ contactDB.init(global);
+
+ let counter = 0;
+
+ contacts.forEach(function save(contact) {
+ let data = initContact(contact);
+
+ contactDB.saveContact(data,
+ function onSuccess() {
+ counter++;
+ if (counter == contacts.length - 1){
+ finish = true;
+ }
+ },
+ function onError(msg) {
+ log('Error importing contacts: ' + msg);
+ finish = true;
+ }
+ );
+ });
+}
+
+function initContact(contact) {
+ let newContact = {};
+ newContact.properties = {
+ name: [],
+ honorificPrefix: [],
+ givenName: [],
+ additionalName: [],
+ familyName: [],
+ honorificSuffix: [],
+ nickname: [],
+ email: [],
+ photo: [],
+ url: [],
+ category: [],
+ adr: [],
+ tel: [],
+ org: [],
+ jobTitle: [],
+ bday: null,
+ note: [],
+ impp: [],
+ anniversary: null,
+ sex: null,
+ genderIdentity: null
+ };
+ for (let field in newContact.properties) {
+ newContact.properties[field] = contact[field];
+ }
+
+ if (contact.id == undefined) {
+ contact.id = uuidGen.generateUUID().toString();
+ contact.id = contact.id.replace('-', '', 'g').replace('{', '').replace('}', '');
+ }
+
+ newContact.id = contact.id;
+ newContact.published = contact.published;
+ newContact.updated = contact.updated;
+
+ return newContact;
+}
+
+let finish = true;
+let contactsFile = getFile(GAIA_DIR, CONTACTS_FOLDER, CONTACTS_FILE);
+if (contactsFile.exists()) {
+ try {
+ let contacts = getJSON(contactsFile);
+ log(contacts.length + ' contacts to import');
+
+ if (contacts.length > 0) {
+ finish = false;
+ saveContacts(contacts);
+ }
+ } catch(e) {
+ throw e;
+ }
+} else {
+ log("File with default contacts not available");
+}
+
+let thread = Cc["@mozilla.org/thread-manager;1"]
+ .getService().currentThread;
+
+while (!finish) {
+ thread.processNextEvent(true);
+}

0 comments on commit 4eb1c50

Please sign in to comment.