Bug 811538 - B2G SMS: Applying PhoneNumberJS to SMS DB. r=bent

This commit is contained in:
Gregor Wagner 2012-12-04 15:40:41 -08:00
parent d94774623e
commit 80023c84f9

View File

@ -8,13 +8,14 @@ const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
const RIL_SMSDATABASESERVICE_CONTRACTID = "@mozilla.org/sms/rilsmsdatabaseservice;1";
const RIL_SMSDATABASESERVICE_CID = Components.ID("{a1fa610c-eb6c-4ac2-878f-b005d5e89249}");
const DEBUG = false;
const DB_NAME = "sms";
const DB_VERSION = 5;
const DB_VERSION = 6;
const STORE_NAME = "sms";
const MOST_RECENT_STORE_NAME = "most-recent";
@ -189,6 +190,10 @@ SmsDatabaseService.prototype = {
if (DEBUG) debug("Upgrade to version 5. Populate quick threads view.")
self.upgradeSchema4(event.target.transaction);
break;
case 5:
if (DEBUG) debug("Upgrade to version 6. Use PhonenumberJS.")
self.upgradeSchema5(event.target.transaction);
break;
default:
event.target.transaction.abort();
callback("Old database version: " + event.oldVersion, null);
@ -357,6 +362,76 @@ SmsDatabaseService.prototype = {
}
},
upgradeSchema5: function upgradeSchema5(transaction) {
let smsStore = transaction.objectStore(STORE_NAME);
let mostRecentStore = transaction.objectStore(MOST_RECENT_STORE_NAME);
smsStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (!cursor) {
return;
}
let needsUpdate = false;
let message = cursor.value;
if (message.receiver) {
if (message.receiver !== "undefined") {
if (DEBUG) debug("upgrade message.receiver from: " + message.receiver + "\n");
let parsedNumber = PhoneNumberUtils.parse(message.receiver.toString());
if (parsedNumber && parsedNumber.internationalNumber) {
message.receiver = parsedNumber.internationalNumber;
needsUpdate = true;
}
if (DEBUG) debug("upgrade message.receiver to: " + message.receiver + "\n");
} else {
message.receiver = null;
needsUpdate = true;
}
}
if (message.sender) {
if (message.sender !== "undefined") {
if (DEBUG) debug("upgrade message.sender from: " + message.sender + "\n");
let parsedNumber = PhoneNumberUtils.parse(message.sender.toString());
if (parsedNumber && parsedNumber.internationalNumber) {
message.sender = parsedNumber.internationalNumber;
needsUpdate = true;
if (DEBUG) debug("upgrade message.sender to: " + message.sender + "\n");
}
} else {
message.sender = null;
needsUpdate = true;
}
}
if (needsUpdate) {
cursor.update(message);
}
cursor.continue();
}
mostRecentStore.openCursor().onsuccess = function(event) {
let cursor = event.target.result;
if (!cursor) {
return;
}
let entry = cursor.value;
if (entry.senderOrReceiver) {
if (DEBUG) debug("upgrade mostRecentStore from: " + entry.senderOrReceiver + "\n");
let parsedNumber = PhoneNumberUtils.parse(entry.senderOrReceiver);
if (parsedNumber && parsedNumber.internationalNumber) {
entry.senderOrReceiver = parsedNumber.internationalNumber;
cursor.update(entry);
if (DEBUG) debug("upgrade mostRecentStore to: " + entry.senderOrReceiver + "\n");
}
}
cursor.continue();
}
},
/**
* Helper function to make the intersection of the partial result arrays
* obtained within createMessageList.
@ -493,7 +568,7 @@ SmsDatabaseService.prototype = {
* nsISmsDatabaseService API
*/
saveReceivedMessage: function saveReceivedMessage(sender, body, messageClass, date) {
saveReceivedMessage: function saveReceivedMessage(aSender, aBody, aMessageClass, aDate) {
let receiver = this.mRIL.rilContext.icc ? this.mRIL.rilContext.icc.msisdn : null;
// Workaround an xpconnect issue with undefined string objects.
@ -502,18 +577,33 @@ SmsDatabaseService.prototype = {
receiver = null;
}
if (receiver) {
let parsedNumber = PhoneNumberUtils.parse(receiver);
receiver = (parsedNumber && parsedNumber.internationalNumber)
? parsedNumber.internationalNumber
: receiver;
}
let sender = aSender;
if (sender) {
let parsedNumber = PhoneNumberUtils.parse(sender);
sender = (parsedNumber && parsedNumber.internationalNumber)
? parsedNumber.internationalNumber
: sender;
}
let message = {delivery: DELIVERY_RECEIVED,
deliveryStatus: DELIVERY_STATUS_SUCCESS,
sender: sender,
receiver: receiver,
body: body,
messageClass: messageClass,
timestamp: date,
body: aBody,
messageClass: aMessageClass,
timestamp: aDate,
read: FILTER_READ_UNREAD};
return this.saveMessage(message);
},
saveSentMessage: function saveSentMessage(receiver, body, date) {
saveSentMessage: function saveSentMessage(aReceiver, aBody, aDate) {
let sender = this.mRIL.rilContext.icc ? this.mRIL.rilContext.icc.msisdn : null;
// Workaround an xpconnect issue with undefined string objects.
@ -522,13 +612,28 @@ SmsDatabaseService.prototype = {
sender = null;
}
let receiver = aReceiver
if (receiver) {
let parsedNumber = PhoneNumberUtils.parse(receiver.toString());
receiver = (parsedNumber && parsedNumber.internationalNumber)
? parsedNumber.internationalNumber
: receiver;
}
if (sender) {
let parsedNumber = PhoneNumberUtils.parse(sender.toString());
sender = (parsedNumber && parsedNumber.internationalNumber)
? parsedNumber.internationalNumber
: sender;
}
let message = {delivery: DELIVERY_SENT,
deliveryStatus: DELIVERY_STATUS_PENDING,
sender: sender,
receiver: receiver,
body: body,
body: aBody,
messageClass: MESSAGE_CLASS_NORMAL,
timestamp: date,
timestamp: aDate,
read: FILTER_READ_READ};
return this.saveMessage(message);
},
@ -625,7 +730,10 @@ SmsDatabaseService.prototype = {
};
txn.onerror = function onerror(event) {
if (DEBUG) debug("Caught error on transaction", event.target.errorCode);
if (DEBUG) {
if (event.target)
debug("Caught error on transaction", event.target.errorCode);
}
//TODO look at event.target.errorCode, pick appropriate error constant
aRequest.notifyGetMessageFailed(Ci.nsISmsRequest.INTERNAL_ERROR);
};