Bug 861462 - Allow contacts to be preloaded when we create the contacts DB. r=bent, a=tef+
authorGregor Wagner <anygregor@gmail.com>
Thu, 18 Apr 2013 10:39:10 -0400
changeset 119125 42f118fcbcea27c2e8a936cfcbc4ed3dc2ac5b3d
parent 119124 cff28c0455ad0420a245aab3643ab4e0fc72dc79
child 119126 3533e3017f32746a3f0a60cc35a0d247e3d2c987
push id685
push userryanvm@gmail.com
push dateThu, 18 Apr 2013 18:40:39 +0000
reviewersbent, tef
bugs861462
milestone18.0
Bug 861462 - Allow contacts to be preloaded when we create the contacts DB. r=bent, a=tef+
dom/contacts/fallback/ContactDB.jsm
--- a/dom/contacts/fallback/ContactDB.jsm
+++ b/dom/contacts/fallback/ContactDB.jsm
@@ -328,16 +328,67 @@ ContactDB.prototype = {
             objectStore.deleteIndex(names[i]);
           }
         }
       } else if (currVersion == 7) {
         if (DEBUG) debug("Adding object store for cached searches");
         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) {
     let contact = {};
     contact.properties = {
       name:            [],
       honorificPrefix: [],
       givenName:       [],