Bug 815833 - [contacts] Add PhoneNumberJS to ContactsDB. r=bent

This commit is contained in:
Gregor Wagner 2012-11-29 14:50:19 -08:00
parent 3e4ab34c8b
commit 2e5034912c
6 changed files with 317 additions and 31 deletions

View File

@ -15,9 +15,10 @@ const Ci = Components.interfaces;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
const DB_NAME = "contacts";
const DB_VERSION = 4;
const DB_VERSION = 5;
const STORE_NAME = "contacts";
this.ContactDB = function ContactDB(aGlobal) {
@ -109,12 +110,14 @@ ContactDB.prototype = {
// Upgrade existing email field in the DB.
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor && cursor.value.properties.email) {
if (DEBUG) debug("upgrade email1: " + JSON.stringify(cursor.value));
cursor.value.properties.email =
cursor.value.properties.email.map(function(address) { return { address: address }; });
cursor.update(cursor.value);
if (DEBUG) debug("upgrade email2: " + JSON.stringify(cursor.value));
if (cursor) {
if (cursor.value.properties.email) {
if (DEBUG) debug("upgrade email1: " + JSON.stringify(cursor.value));
cursor.value.properties.email =
cursor.value.properties.email.map(function(address) { return { address: address }; });
cursor.update(cursor.value);
if (DEBUG) debug("upgrade email2: " + JSON.stringify(cursor.value));
}
cursor.continue();
}
};
@ -131,24 +134,61 @@ ContactDB.prototype = {
// Upgrade existing impp field in the DB.
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor && cursor.value.properties.impp) {
if (DEBUG) debug("upgrade impp1: " + JSON.stringify(cursor.value));
cursor.value.properties.impp =
cursor.value.properties.impp.map(function(value) { return { value: value }; });
cursor.update(cursor.value);
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
if (cursor) {
if (cursor.value.properties.impp) {
if (DEBUG) debug("upgrade impp1: " + JSON.stringify(cursor.value));
cursor.value.properties.impp =
cursor.value.properties.impp.map(function(value) { return { value: value }; });
cursor.update(cursor.value);
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
}
cursor.continue();
}
};
// Upgrade existing url field in the DB.
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor && cursor.value.properties.url) {
if (DEBUG) debug("upgrade url1: " + JSON.stringify(cursor.value));
cursor.value.properties.url =
cursor.value.properties.url.map(function(value) { return { value: value }; });
cursor.update(cursor.value);
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
if (cursor) {
if (cursor.value.properties.url) {
if (DEBUG) debug("upgrade url1: " + JSON.stringify(cursor.value));
cursor.value.properties.url =
cursor.value.properties.url.map(function(value) { return { value: value }; });
cursor.update(cursor.value);
if (DEBUG) debug("upgrade impp2: " + JSON.stringify(cursor.value));
}
cursor.continue();
}
};
} else if (currVersion == 4) {
if (DEBUG) debug("Add international phone numbers upgrade");
if (!objectStore) {
objectStore = aTransaction.objectStore(STORE_NAME);
}
objectStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (cursor) {
if (cursor.value.properties.tel) {
if (DEBUG) debug("upgrade : " + JSON.stringify(cursor.value));
cursor.value.properties.tel.forEach(
function(duple) {
let parsedNumber = PhoneNumberUtils.parse(duple.value.toString());
if (parsedNumber) {
debug("InternationalFormat: " + parsedNumber.internationalFormat);
debug("InternationalNumber: " + parsedNumber.internationalNumber);
debug("NationalNumber: " + parsedNumber.nationalNumber);
debug("NationalFormat: " + parsedNumber.nationalFormat);
if (duple.value.toString() !== parsedNumber.internationalNumber) {
cursor.value.search.tel.push(parsedNumber.internationalNumber);
}
} else {
dump("Warning: No international number found for " + duple.value + "\n");
}
}
)
cursor.update(cursor.value);
}
if (DEBUG) debug("upgrade2 : " + JSON.stringify(cursor.value));
cursor.continue();
}
};
@ -213,17 +253,29 @@ ContactDB.prototype = {
let number = aContact.properties[field][i].value;
if (number) {
for (let i = 0; i < number.length; i++) {
contact.search[field].push(number.substring(i, number.length));
contact.search[field].push(number.substring(0, number.length - i));
}
// Store +1-234-567 as ["1234567", "234567"...]
let digits = number.match(/\d/g);
if (digits && number.length != digits.length) {
digits = digits.join('');
for(let i = 0; i < digits.length; i++) {
contact.search[field].push(digits.substring(i, digits.length));
contact.search[field].push(digits.substring(0, digits.length - i));
}
}
if (DEBUG) debug("lookup: " + JSON.stringify(contact.search[field]));
if (DEBUG) debug("lookup: " + JSON.stringify(contact.search[field]));
let parsedNumber = PhoneNumberUtils.parse(number.toString());
if (parsedNumber) {
debug("InternationalFormat: " + parsedNumber.internationalFormat);
debug("InternationalNumber: " + parsedNumber.internationalNumber);
debug("NationalNumber: " + parsedNumber.nationalNumber);
debug("NationalFormat: " + parsedNumber.nationalFormat);
if (number.toString() !== parsedNumber.internationalNumber) {
contact.search[field].push(parsedNumber.internationalNumber);
}
} else {
dump("Warning: No international number found for " + number + "\n");
}
}
} else if (field == "email") {
let address = aContact.properties[field][i].value;

View File

@ -18,6 +18,7 @@ MOCHITEST_FILES = \
test_contacts_basics.html \
test_contacts_events.html \
test_contacts_blobs.html \
test_contacts_international.html \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -98,7 +98,7 @@ var properties2 = {
honorificSuffix: "dummyHonorificSuffix",
additionalName: "dummyadditionalName",
nickname: "dummyNickname",
tel: [{type: ["test"], value: "123456789", carrier: "myCarrier"},{type: ["home", "custom"], value: "234567890"}],
tel: [{type: ["test"], value: "7932012345", carrier: "myCarrier"},{type: ["home", "custom"], value: "7932012346"}],
email: [{type: ["test"], value: "a@b.c"}, {value: "b@c.d"}],
adr: [adr1, adr2],
impp: [{type: ["aim"], value:"im1"}, {value: "im2"}],

View File

@ -0,0 +1,210 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id={815833}
-->
<head>
<title>Test for Bug {815833} WebContacts</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={815833}">Mozilla Bug {815833}</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
"use strict";
// this shouldn't be necessary when bug 792594 is fixed
if (!SpecialPowers.getBoolPref("dom.mozContacts.enabled")) {
var comp = SpecialPowers.wrap(SpecialPowers.Components);
comp.utils.import("resource://gre/modules/ContactService.jsm");
comp.utils.import("resource://gre/modules/PermissionPromptHelper.jsm");
SpecialPowers.setBoolPref("dom.mozContacts.enabled", true);
}
SpecialPowers.addPermission("contacts-write", true, document);
SpecialPowers.addPermission("contacts-read", true, document);
SpecialPowers.addPermission("contacts-create", true, document);
function onFailure() {
ok(false, "in on Failure!");
}
var number1 = {
local: "7932012345",
international: "+557932012345"
};
var number2 = {
local: "7932012346",
international: "+557932012346"
};
var properties1 = {
name: "Testname1",
tel: [{type: ["work"], value: number1.local, carrier: "testCarrier"} , {type: ["home", "fax"], value: number2.local}],
};
var req;
var index = 0;
var createResult1;
var findResult1;
var sample_id1;
var mozContacts = window.navigator.mozContacts;
var steps = [
function () {
ok(true, "Deleting database");
req = mozContacts.clear();
req.onsuccess = function () {
ok(true, "Deleted the database");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Adding a new contact1");
createResult1 = new mozContact();
createResult1.init(properties1);
req = navigator.mozContacts.save(createResult1);
req.onsuccess = function () {
ok(createResult1.id, "The contact now has an ID.");
sample_id1 = createResult1.id;
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: number1.local};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for international number");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: number1.international};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
next();
};
req.onerror = onFailure;
},
function() {
ok(true, "Modifying number");
findResult1.tel[0].value = number2.local;
req = mozContacts.save(findResult1);
req.onsuccess = function () {
next();
};
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: number1.local};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "Found exactly 0 contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: number1.international};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 0, "Found exactly 0 contact.");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: number2.local};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "Searching for local number");
var options = {filterBy: ["tel"],
filterOp: "contains",
filterValue: number2.international};
req = mozContacts.find(options);
req.onsuccess = function () {
ok(req.result.length == 1, "Found exactly 1 contact.");
findResult1 = req.result[0];
ok(findResult1.id == sample_id1, "Same ID");
next();
};
req.onerror = onFailure;
},
function () {
ok(true, "all done!\n");
SimpleTest.finish();
}
];
function next() {
ok(true, "Begin!");
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
index += 1;
}
function permissionTest() {
if (gContactsEnabled) {
next();
} else {
is(mozContacts, null, "mozContacts is null when not enabled.");
SimpleTest.finish();
}
}
var gContactsEnabled = SpecialPowers.getBoolPref("dom.mozContacts.enabled");
SimpleTest.waitForExplicitFinish();
addLoadEvent(permissionTest);
ok(true, "test passed");
</script>
</pre>
</body>
</html>

View File

@ -13,6 +13,9 @@ EXTRA_JS_MODULES = \
PhoneNumber.jsm \
PhoneNumberMetaData.jsm \
mcc_iso3166_table.jsm \
$(NULL)
EXTRA_PP_JS_MODULES = \
PhoneNumberUtils.jsm \
$(NULL)

View File

@ -5,33 +5,46 @@
this.EXPORTED_SYMBOLS = ["PhoneNumberUtils"];
const DEBUG = true;
function debug(s) { dump("-*- PhoneNumberutils: " + s + "\n"); }
const DEBUG = false;
function debug(s) { if(DEBUG) dump("-*- PhoneNumberutils: " + s + "\n"); }
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import("resource://gre/modules/PhoneNumber.jsm");
Cu.import("resource://gre/modules/mcc_iso3166_table.jsm");
#ifdef MOZ_B2G_RIL
XPCOMUtils.defineLazyServiceGetter(this, "ril",
"@mozilla.org/ril/content-helper;1",
"nsIRILContentHelper");
#endif
this.PhoneNumberUtils = {
// 1. See whether we have a network mcc
// 2. If we don't have that, look for the simcard mcc
// 3. TODO: If we don't have that or its 0 (not activated), pick up the last used mcc
// 4. If we don't have, default to some mcc
// mcc for Brasil
_mcc: '724',
_getCountryName: function() {
let mcc;
let countryName;
#ifdef MOZ_B2G_RIL
// Get network mcc.
if (ril.voiceConnectionInfo && ril.voiceConnectionInfo.network)
mcc = ril.voiceConnectionInfo.network.mcc;
// Get SIM mcc or set it to mcc for Brasil
if (!mcc)
mcc = ril.iccInfo.mcc || '724';
mcc = ril.iccInfo.mcc || this._mcc;
#else
mcc = this._mcc;
#endif
countryName = MCC_ISO3166_TABLE[mcc];
debug("MCC: " + mcc + "countryName: " + countryName);
@ -39,11 +52,18 @@ this.PhoneNumberUtils = {
},
parse: function(aNumber) {
debug("call parse: " + aNumber);
let result = PhoneNumber.Parse(aNumber, this._getCountryName());
debug("InternationalFormat: " + result.internationalFormat);
debug("InternationalNumber: " + result.internationalNumber);
debug("NationalNumber: " + result.nationalNumber);
debug("NationalFormat: " + result.nationalFormat);
if (DEBUG) {
if (result) {
debug("InternationalFormat: " + result.internationalFormat);
debug("InternationalNumber: " + result.internationalNumber);
debug("NationalNumber: " + result.nationalNumber);
debug("NationalFormat: " + result.nationalFormat);
} else {
debug("No result!\n");
}
}
return result;
},
@ -51,5 +71,5 @@ this.PhoneNumberUtils = {
let countryName = MCC_ISO3166_TABLE[aMCC];
debug("found country name: " + countryName);
return PhoneNumber.Parse(aNumber, countryName);
},
}
};