Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #13712 from gtorodelvalle/contacts-bug-937040-remo…
Browse files Browse the repository at this point in the history
…ving-and-saving-mozContacts

Bug 937040 - Revise contacts app to adapt to WebIDL changes in a uniform way
  • Loading branch information
gtorodelvalle committed Nov 15, 2013
2 parents c1424c8 + d83339a commit 1555752
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 38 deletions.
8 changes: 5 additions & 3 deletions apps/communications/contacts/js/contacts_cleaner.js
Expand Up @@ -13,6 +13,10 @@ window.ContactsCleaner = function(contacts) {
var holded = false;
var mustFinish = false;

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

this.start = function() {
mustHold = holded = mustFinish = false;

Expand Down Expand Up @@ -45,9 +49,7 @@ window.ContactsCleaner = function(contacts) {
};

this.performClean = function(contact, number, cbs) {
var theContact = (contact instanceof mozContact) ?
contact : new mozContact(contact);
var req = navigator.mozContacts.remove(theContact);
var req = navigator.mozContacts.remove(getContact(contact));
req.number = number;
req.onsuccess = cbs.success;
req.onerror = function(e) {
Expand Down
9 changes: 6 additions & 3 deletions apps/communications/contacts/js/contacts_importer.js
Expand Up @@ -24,6 +24,11 @@
window.addEventListener('online', onLineChanged);
window.addEventListener('offline', onLineChanged);

function getContact(contact) {
return (contact instanceof mozContact) ?
contact : new mozContact(contact);
}

function onLineChanged() {
isOnLine = navigator.onLine;
}
Expand All @@ -48,9 +53,7 @@
}

function saveMozContact(deviceContact, successCb, errorCb) {
var mzContact = new mozContact(deviceContact);

var req = navigator.mozContacts.save(mzContact);
var req = navigator.mozContacts.save(getContact(deviceContact));

req.onsuccess = successCb;
req.onerror = errorCb;
Expand Down
10 changes: 5 additions & 5 deletions apps/communications/contacts/js/contacts_merger.js
Expand Up @@ -7,6 +7,9 @@ contacts.Merger = (function() {
var DEFAULT_TEL_TYPE = 'another';
var DEFAULT_EMAIL_TYPE = 'personal';

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

// Performs the merge passing the master contact and matching contacts
// The master contact will be the one that will contain all the merged info
Expand Down Expand Up @@ -203,18 +206,15 @@ contacts.Merger = (function() {
}

// Updating the master contact
var req = navigator.mozContacts.save(masterContact);
var req = navigator.mozContacts.save(getContact(masterContact));

req.onsuccess = function() {
// Now for all the matchingContacts they have to be removed
matchingContacts.forEach(function(aMatchingContact) {
// Only remove those contacts which are already in the DB
if (aMatchingContact.matchingContact.id) {
var contact = aMatchingContact.matchingContact;
if (!(contact instanceof mozContact)) {
contact = new mozContact(contact);
}
navigator.mozContacts.remove(contact);
navigator.mozContacts.remove(getContact(contact));
}
});

Expand Down
31 changes: 14 additions & 17 deletions apps/communications/contacts/js/fb/fb_contact.js
Expand Up @@ -8,6 +8,10 @@ fb.Contact = function(deviceContact, cid) {
var devContact = deviceContact;
var contactid = cid;

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

function doGetFacebookUid(data) {
return fb.getFriendUid(data);
}
Expand Down Expand Up @@ -132,12 +136,10 @@ fb.Contact = function(deviceContact, cid) {

doSetFacebookUid(contactInfo, contactData.uid);

var contactObj = new mozContact(contactInfo);

var fbReq = persistToFbCache(contactData);

fbReq.onsuccess = function() {
var mozContactsReq = navigator.mozContacts.save(contactObj);
var mozContactsReq = navigator.mozContacts.save(getContact(contactInfo));
mozContactsReq.onsuccess = function(e) {
outReq.done(fbReq.result);
};
Expand Down Expand Up @@ -253,7 +255,7 @@ fb.Contact = function(deviceContact, cid) {

var auxReq = new fb.utils.Request();
auxReq.onsuccess = function() {
var mozContactsReq = navigator.mozContacts.save(devContact);
var mozContactsReq = navigator.mozContacts.save(getContact(devContact));
mozContactsReq.onsuccess = function(e) {
outReq.done();
};
Expand Down Expand Up @@ -470,15 +472,13 @@ fb.Contact = function(deviceContact, cid) {
}

propagateNames(fbFriend.mozContact, contactdata);
var mozContactsReq = navigator.mozContacts.save(contactdata);
var mozContactsReq = navigator.mozContacts.save(getContact(contactdata));

mozContactsReq.onsuccess = function(e) {
// The FB contact on mozContacts needs to be removed
if (fbFriend.mozContact && !fb.isFbLinked(fbFriend.mozContact)) {
var theContact = (fbFriend.mozContact instanceof mozContact) ?
fbFriend.mozContact :
new mozContact(fbFriend.mozContact);
var deleteReq = navigator.mozContacts.remove(theContact);
var deleteReq = navigator.mozContacts.remove(
getContact(fbFriend.mozContact));

deleteReq.onsuccess = function(e) {
out.done(e.target.result);
Expand Down Expand Up @@ -551,7 +551,7 @@ fb.Contact = function(deviceContact, cid) {

resetNames(dContact);
fb.markAsUnlinked(dContact);
var req = navigator.mozContacts.save(dContact);
var req = navigator.mozContacts.save(getContact(dContact));

req.onsuccess = function(e) {
if (theType !== 'hard') {
Expand All @@ -574,10 +574,8 @@ fb.Contact = function(deviceContact, cid) {
data.url = imported.url;
doSetFacebookUid(data, uid);

var mcontact = new mozContact(data);

// The FB contact is restored
var reqRestore = navigator.mozContacts.save(mcontact);
var reqRestore = navigator.mozContacts.save(getContact(data));

reqRestore.onsuccess = function(e) {
out.done(mcontact.id);
Expand Down Expand Up @@ -633,8 +631,8 @@ fb.Contact = function(deviceContact, cid) {
// Then corresponding FB Data is removed otherwise only
// the device contact is removed
if (fbNumReq.result === 1) {
var theContact = new mozContact(devContact);
var removeReq = navigator.mozContacts.remove(theContact);
var removeReq = navigator.mozContacts.remove(
getContact(devContact));
removeReq.onsuccess = function(e) {
var fbReq = fb.contacts.remove(uid, forceFlush);
fbReq.onsuccess = function() {
Expand All @@ -650,8 +648,7 @@ fb.Contact = function(deviceContact, cid) {
};
}
else {
var theContact = new mozContact(devContact);
var removeReq = navigator.mozContacts.remove(theContact);
var removeReq = navigator.mozContacts.remove(getContact(devContact));
removeReq.onsuccess = function(e) {
out.done();
};
Expand Down
9 changes: 6 additions & 3 deletions apps/communications/contacts/js/fb/fb_utils.js
Expand Up @@ -14,6 +14,11 @@ window.fb = fb;
var LAST_UPDATED_KEY = Utils.LAST_UPDATED_KEY = 'lastUpdatedTime';
Utils.ALARM_ID_KEY = 'nextAlarmId';

function getContact(contact) {
return (contact instanceof mozContact) ?
contact : new mozContact(contact);
}

var REDIRECT_LOGOUT_URI = window.oauthflow ?
oauthflow.params.facebook['redirectLogout'] : '';
var STORAGE_KEY = Utils.TOKEN_DATA_KEY = 'tokenData';
Expand Down Expand Up @@ -441,9 +446,7 @@ window.fb = fb;
req = fbContact.remove();
}
else {
var theContact = (contact instanceof mozContact) ?
contact : new mozContact(contact);
var req = navigator.mozContacts.remove(theContact);
var req = navigator.mozContacts.remove(getContact(contact));
}
}
req.number = number;
Expand Down
Expand Up @@ -21,6 +21,10 @@ function SimContactsImporter() {
var mustFinish = false;
var loadedMatch = false;

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

function notifyFinish() {
if (typeof self.onfinish === 'function') {
window.setTimeout(self.onfinish, 0);
Expand Down Expand Up @@ -183,7 +187,7 @@ function SimContactsImporter() {


function saveContact(contact) {
var req = window.navigator.mozContacts.save(contact);
var req = window.navigator.mozContacts.save(getContact(contact));
req.onsuccess = function saveSuccess() {
continueCb();
};
Expand Down
6 changes: 5 additions & 1 deletion apps/communications/contacts/js/utilities/vcard_parser.js
Expand Up @@ -5,6 +5,10 @@ var VCFReader = (function _VCFReader() {
var ReBasic = /^([^:]+):(.+)$/;
var ReTuple = /([a-zA-Z]+)=(.+)/;

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

function _parseTuple(p) {
var match = p.match(ReTuple);
return match ? [match[1].toLowerCase(), match[2]] : ['type', p];
Expand Down Expand Up @@ -456,7 +460,7 @@ var VCFReader = (function _VCFReader() {
* @param {Function} cb Callback.
*/
VCFReader._save = function(item, cb) {
var req = navigator.mozContacts.save(item);
var req = navigator.mozContacts.save(getContact(item));
req.onsuccess = cb;
req.onerror = cb;
};
Expand Down
6 changes: 5 additions & 1 deletion apps/communications/contacts/js/views/details.js
Expand Up @@ -36,6 +36,10 @@ contacts.Details = (function() {
'#msg_button'
];

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

var init = function cd_init(currentDom) {
_ = navigator.mozL10n.get;
dom = currentDom || document;
Expand Down Expand Up @@ -245,7 +249,7 @@ contacts.Details = (function() {
// Disabling button while saving the contact
favoriteMessage.style.pointerEvents = 'none';

var request = navigator.mozContacts.save(contact);
var request = navigator.mozContacts.save(getContact(contact));
request.onsuccess = function onsuccess() {
var cList = contacts.List;
/*
Expand Down
10 changes: 6 additions & 4 deletions apps/communications/contacts/js/views/form.js
Expand Up @@ -48,6 +48,10 @@ contacts.Form = (function() {

var touchstart = 'ontouchstart' in window ? 'touchstart' : 'mousedown';

function getContact(contact) {
return (contact instanceof mozContact) ? contact : new mozContact(contact);
}

var textFieldsCache = {
_textFields: null,

Expand Down Expand Up @@ -447,9 +451,7 @@ contacts.Form = (function() {
request = fbContact.remove(true);
request.onsuccess = deleteSuccess;
} else {
var theContact = (contact instanceof mozContact) ?
contact : new mozContact(contact);
request = navigator.mozContacts.remove(theContact);
request = navigator.mozContacts.remove(getContact(contact));
request.onsuccess = deleteSuccess;
}

Expand Down Expand Up @@ -741,7 +743,7 @@ contacts.Form = (function() {
};

var doSave = function doSave(contact, noTransition) {
var request = navigator.mozContacts.save(contact);
var request = navigator.mozContacts.save(getContact(contact));

request.onsuccess = function onsuccess() {
hideThrobber();
Expand Down

0 comments on commit 1555752

Please sign in to comment.