2012-02-28 14:01:48 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-07-03 11:00:53 -07:00
|
|
|
"use strict";
|
2012-02-28 14:01:48 -08:00
|
|
|
|
2012-08-22 14:34:57 -07:00
|
|
|
const DEBUG = false;
|
|
|
|
function debug(s) { dump("-*- ContactManager: " + s + "\n"); }
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-03-07 13:45:24 -08:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
|
|
|
|
2012-04-02 16:39:57 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(Services, "DOMRequest", function() {
|
2012-03-07 13:45:24 -08:00
|
|
|
return Cc["@mozilla.org/dom/dom-request-service;1"].getService(Ci.nsIDOMRequestService);
|
|
|
|
});
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
const nsIClassInfo = Ci.nsIClassInfo;
|
2012-05-10 21:51:48 -07:00
|
|
|
const CONTACTPROPERTIES_CID = Components.ID("{f5181640-89e8-11e1-b0c4-0800200c9a66}");
|
2012-02-28 14:01:48 -08:00
|
|
|
const nsIDOMContactProperties = Ci.nsIDOMContactProperties;
|
|
|
|
|
|
|
|
// ContactProperties is not directly instantiated. It is used as interface.
|
|
|
|
|
2012-08-22 14:34:57 -07:00
|
|
|
function ContactProperties(aProp) { if (DEBUG) debug("ContactProperties Constructor"); }
|
2012-05-10 21:51:48 -07:00
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
ContactProperties.prototype = {
|
|
|
|
|
|
|
|
classID : CONTACTPROPERTIES_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTPROPERTIES_CID,
|
|
|
|
contractID:"@mozilla.org/contactProperties;1",
|
|
|
|
classDescription: "ContactProperties",
|
|
|
|
interfaces: [nsIDOMContactProperties],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactProperties])
|
|
|
|
}
|
|
|
|
|
|
|
|
//ContactAddress
|
|
|
|
|
|
|
|
const CONTACTADDRESS_CONTRACTID = "@mozilla.org/contactAddress;1";
|
2012-05-10 21:51:48 -07:00
|
|
|
const CONTACTADDRESS_CID = Components.ID("{eba48030-89e8-11e1-b0c4-0800200c9a66}");
|
2012-02-28 14:01:48 -08:00
|
|
|
const nsIDOMContactAddress = Components.interfaces.nsIDOMContactAddress;
|
|
|
|
|
2012-05-10 21:51:48 -07:00
|
|
|
function ContactAddress(aType, aStreetAddress, aLocality, aRegion, aPostalCode, aCountryName) {
|
|
|
|
this.type = aType || null;
|
2012-02-28 14:01:48 -08:00
|
|
|
this.streetAddress = aStreetAddress || null;
|
|
|
|
this.locality = aLocality || null;
|
|
|
|
this.region = aRegion || null;
|
|
|
|
this.postalCode = aPostalCode || null;
|
|
|
|
this.countryName = aCountryName || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ContactAddress.prototype = {
|
2012-08-22 10:56:25 -07:00
|
|
|
__exposedProps__: {
|
|
|
|
type: 'rw',
|
|
|
|
streetAddress: 'rw',
|
|
|
|
locality: 'rw',
|
|
|
|
region: 'rw',
|
|
|
|
postalCode: 'rw',
|
|
|
|
countryName: 'rw'
|
|
|
|
},
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
classID : CONTACTADDRESS_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTADDRESS_CID,
|
|
|
|
contractID: CONTACTADDRESS_CONTRACTID,
|
|
|
|
classDescription: "ContactAddress",
|
|
|
|
interfaces: [nsIDOMContactAddress],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactAddress])
|
|
|
|
}
|
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
//ContactField
|
2012-07-03 11:00:53 -07:00
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
const CONTACTFIELD_CONTRACTID = "@mozilla.org/contactField;1";
|
|
|
|
const CONTACTFIELD_CID = Components.ID("{e2cb19c0-e4aa-11e1-9b23-0800200c9a66}");
|
|
|
|
const nsIDOMContactField = Components.interfaces.nsIDOMContactField;
|
2012-07-03 11:00:53 -07:00
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
function ContactField(aType, aValue) {
|
2012-07-03 11:00:53 -07:00
|
|
|
this.type = aType || null;
|
2012-08-15 16:42:08 -07:00
|
|
|
this.value = aValue || null;
|
2012-07-03 11:00:53 -07:00
|
|
|
};
|
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
ContactField.prototype = {
|
2012-08-22 10:56:25 -07:00
|
|
|
__exposedProps__: {
|
|
|
|
type: 'rw',
|
|
|
|
value: 'rw'
|
|
|
|
},
|
2012-07-03 11:00:53 -07:00
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
classID : CONTACTFIELD_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTFIELD_CID,
|
|
|
|
contractID: CONTACTFIELD_CONTRACTID,
|
|
|
|
classDescription: "ContactField",
|
|
|
|
interfaces: [nsIDOMContactField],
|
2012-07-03 11:00:53 -07:00
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactField])
|
2012-07-03 11:00:53 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
//ContactTelField
|
2012-05-10 21:51:48 -07:00
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
const CONTACTTELFIELD_CONTRACTID = "@mozilla.org/contactTelField;1";
|
|
|
|
const CONTACTTELFIELD_CID = Components.ID("{ed0ab260-e4aa-11e1-9b23-0800200c9a66}");
|
|
|
|
const nsIDOMContactTelField = Components.interfaces.nsIDOMContactTelField;
|
2012-05-10 21:51:48 -07:00
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
function ContactTelField(aType, aValue, aCarrier) {
|
2012-05-10 21:51:48 -07:00
|
|
|
this.type = aType || null;
|
2012-08-15 16:42:08 -07:00
|
|
|
this.value = aValue || null;
|
|
|
|
this.carrier = aCarrier || null;
|
2012-05-10 21:51:48 -07:00
|
|
|
};
|
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
ContactTelField.prototype = {
|
2012-08-22 10:56:25 -07:00
|
|
|
__exposedProps__: {
|
|
|
|
type: 'rw',
|
|
|
|
value: 'rw',
|
|
|
|
carrier: 'rw'
|
|
|
|
},
|
2012-05-10 21:51:48 -07:00
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
classID : CONTACTTELFIELD_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTTELFIELD_CID,
|
|
|
|
contractID: CONTACTTELFIELD_CONTRACTID,
|
|
|
|
classDescription: "ContactTelField",
|
|
|
|
interfaces: [nsIDOMContactTelField],
|
2012-05-10 21:51:48 -07:00
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
2012-08-15 16:42:08 -07:00
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactTelField])
|
2012-05-10 21:51:48 -07:00
|
|
|
}
|
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
//ContactFindSortOptions
|
|
|
|
|
|
|
|
const CONTACTFINDSORTOPTIONS_CONTRACTID = "@mozilla.org/contactFindSortOptions;1"
|
|
|
|
const CONTACTFINDSORTOPTIONS_CID = Components.ID("{cb008c06-3bf8-495c-8865-f9ca1673a1e1}");
|
|
|
|
const nsIDOMContactFindSortOptions = Ci.nsIDOMContactFindSortOptions;
|
|
|
|
|
|
|
|
function ContactFindSortOptions () { }
|
|
|
|
|
|
|
|
ContactFindSortOptions.prototype = {
|
|
|
|
classID: CONTACTFINDSORTOPTIONS_CID,
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: CONTACTFINDSORTOPTIONS_CID,
|
|
|
|
contractID: CONTACTFINDSORTOPTIONS_CONTRACTID,
|
|
|
|
classDescription: "ContactFindSortOptions",
|
|
|
|
interfaces: [nsIDOMContactFindSortOptions],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([nsIDOMContactFindSortOptions])
|
|
|
|
};
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
//ContactFindOptions
|
|
|
|
|
|
|
|
const CONTACTFINDOPTIONS_CONTRACTID = "@mozilla.org/contactFindOptions;1";
|
|
|
|
const CONTACTFINDOPTIONS_CID = Components.ID("{e31daea0-0cb6-11e1-be50-0800200c9a66}");
|
|
|
|
const nsIDOMContactFindOptions = Components.interfaces.nsIDOMContactFindOptions;
|
|
|
|
|
2012-04-02 16:39:57 -07:00
|
|
|
function ContactFindOptions() { };
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
ContactFindOptions.prototype = {
|
|
|
|
|
|
|
|
classID : CONTACTFINDOPTIONS_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTFINDOPTIONS_CID,
|
|
|
|
contractID: CONTACTFINDOPTIONS_CONTRACTID,
|
|
|
|
classDescription: "ContactFindOptions",
|
2013-02-03 04:05:51 -08:00
|
|
|
interfaces: [nsIDOMContactFindSortOptions,
|
|
|
|
nsIDOMContactFindOptions],
|
2012-02-28 14:01:48 -08:00
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
2013-02-03 04:05:51 -08:00
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactFindSortOptions,
|
|
|
|
nsIDOMContactFindOptions])
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Contact
|
|
|
|
|
|
|
|
const CONTACT_CONTRACTID = "@mozilla.org/contact;1";
|
|
|
|
const CONTACT_CID = Components.ID("{da0f7040-388b-11e1-b86c-0800200c9a66}");
|
|
|
|
const nsIDOMContact = Components.interfaces.nsIDOMContact;
|
|
|
|
|
2013-02-01 12:44:55 -08:00
|
|
|
function Contact() { };
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
Contact.prototype = {
|
2012-08-22 10:56:25 -07:00
|
|
|
__exposedProps__: {
|
2012-09-12 21:02:30 -07:00
|
|
|
id: 'rw',
|
|
|
|
updated: 'rw',
|
|
|
|
published: 'rw',
|
2012-08-22 10:56:25 -07:00
|
|
|
name: 'rw',
|
|
|
|
honorificPrefix: 'rw',
|
|
|
|
givenName: 'rw',
|
|
|
|
additionalName: 'rw',
|
|
|
|
familyName: 'rw',
|
|
|
|
honorificSuffix: 'rw',
|
|
|
|
nickname: 'rw',
|
|
|
|
email: 'rw',
|
|
|
|
photo: 'rw',
|
|
|
|
url: 'rw',
|
|
|
|
category: 'rw',
|
|
|
|
adr: 'rw',
|
|
|
|
tel: 'rw',
|
|
|
|
org: 'rw',
|
|
|
|
jobTitle: 'rw',
|
|
|
|
bday: 'rw',
|
|
|
|
note: 'rw',
|
|
|
|
impp: 'rw',
|
|
|
|
anniversary: 'rw',
|
|
|
|
sex: 'rw',
|
|
|
|
genderIdentity: 'rw'
|
|
|
|
},
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
init: function init(aProp) {
|
|
|
|
// Accept non-array strings for DOMString[] properties and convert them.
|
2013-02-03 04:05:51 -08:00
|
|
|
function _create(aField) {
|
2012-07-03 11:00:53 -07:00
|
|
|
if (Array.isArray(aField)) {
|
|
|
|
for (let i = 0; i < aField.length; i++) {
|
2013-02-03 04:05:51 -08:00
|
|
|
if (typeof aField[i] != "string")
|
2012-07-03 11:00:53 -07:00
|
|
|
aField[i] = String(aField[i]);
|
|
|
|
}
|
|
|
|
return aField;
|
|
|
|
} else if (aField != null) {
|
|
|
|
return [String(aField)];
|
|
|
|
}
|
2012-02-28 14:01:48 -08:00
|
|
|
};
|
|
|
|
|
2012-08-07 15:02:37 -07:00
|
|
|
function _checkBlobArray(aBlob) {
|
|
|
|
if (Array.isArray(aBlob)) {
|
|
|
|
for (let i = 0; i < aBlob.length; i++) {
|
|
|
|
if (typeof aBlob != 'object') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!(aBlob[i] instanceof Components.interfaces.nsIDOMBlob)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aBlob;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
this.name = _create(aProp.name) || null;
|
|
|
|
this.honorificPrefix = _create(aProp.honorificPrefix) || null;
|
|
|
|
this.givenName = _create(aProp.givenName) || null;
|
|
|
|
this.additionalName = _create(aProp.additionalName) || null;
|
|
|
|
this.familyName = _create(aProp.familyName) || null;
|
|
|
|
this.honorificSuffix = _create(aProp.honorificSuffix) || null;
|
|
|
|
this.nickname = _create(aProp.nickname) || null;
|
2012-07-03 11:00:53 -07:00
|
|
|
|
|
|
|
if (aProp.email) {
|
|
|
|
aProp.email = Array.isArray(aProp.email) ? aProp.email : [aProp.email];
|
|
|
|
this.email = new Array();
|
|
|
|
for (let i = 0; i < aProp.email.length; i++)
|
2012-08-15 16:42:08 -07:00
|
|
|
this.email.push(new ContactField(aProp.email[i].type, aProp.email[i].value));
|
2012-07-03 11:00:53 -07:00
|
|
|
} else {
|
|
|
|
this.email = null;
|
|
|
|
}
|
|
|
|
|
2012-08-07 15:02:37 -07:00
|
|
|
this.photo = _checkBlobArray(aProp.photo) || null;
|
2012-02-28 14:01:48 -08:00
|
|
|
this.category = _create(aProp.category) || null;
|
|
|
|
|
|
|
|
if (aProp.adr) {
|
|
|
|
// Make sure adr argument is an array. Instanceof doesn't work.
|
2012-05-10 21:51:48 -07:00
|
|
|
aProp.adr = Array.isArray(aProp.adr) ? aProp.adr : [aProp.adr];
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
this.adr = new Array();
|
|
|
|
for (let i = 0; i < aProp.adr.length; i++)
|
2012-05-10 21:51:48 -07:00
|
|
|
this.adr.push(new ContactAddress(aProp.adr[i].type, aProp.adr[i].streetAddress, aProp.adr[i].locality,
|
|
|
|
aProp.adr[i].region, aProp.adr[i].postalCode, aProp.adr[i].countryName));
|
2012-02-28 14:01:48 -08:00
|
|
|
} else {
|
|
|
|
this.adr = null;
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:51:48 -07:00
|
|
|
if (aProp.tel) {
|
|
|
|
aProp.tel = Array.isArray(aProp.tel) ? aProp.tel : [aProp.tel];
|
|
|
|
this.tel = new Array();
|
|
|
|
for (let i = 0; i < aProp.tel.length; i++)
|
2012-08-15 16:42:08 -07:00
|
|
|
this.tel.push(new ContactTelField(aProp.tel[i].type, aProp.tel[i].value, aProp.tel[i].carrier));
|
2012-05-10 21:51:48 -07:00
|
|
|
} else {
|
|
|
|
this.tel = null;
|
|
|
|
}
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
this.org = _create(aProp.org) || null;
|
2012-05-10 21:51:48 -07:00
|
|
|
this.jobTitle = _create(aProp.jobTitle) || null;
|
2012-02-28 14:01:48 -08:00
|
|
|
this.bday = (aProp.bday == "undefined" || aProp.bday == null) ? null : new Date(aProp.bday);
|
|
|
|
this.note = _create(aProp.note) || null;
|
2012-08-15 16:42:08 -07:00
|
|
|
|
|
|
|
if (aProp.impp) {
|
|
|
|
aProp.impp = Array.isArray(aProp.impp) ? aProp.impp : [aProp.impp];
|
|
|
|
this.impp = new Array();
|
|
|
|
for (let i = 0; i < aProp.impp.length; i++)
|
|
|
|
this.impp.push(new ContactField(aProp.impp[i].type, aProp.impp[i].value));
|
|
|
|
} else {
|
|
|
|
this.impp = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aProp.url) {
|
|
|
|
aProp.url = Array.isArray(aProp.url) ? aProp.url : [aProp.url];
|
|
|
|
this.url = new Array();
|
|
|
|
for (let i = 0; i < aProp.url.length; i++)
|
|
|
|
this.url.push(new ContactField(aProp.url[i].type, aProp.url[i].value));
|
|
|
|
} else {
|
|
|
|
this.url = null;
|
|
|
|
}
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
this.anniversary = (aProp.anniversary == "undefined" || aProp.anniversary == null) ? null : new Date(aProp.anniversary);
|
|
|
|
this.sex = (aProp.sex != "undefined") ? aProp.sex : null;
|
|
|
|
this.genderIdentity = (aProp.genderIdentity != "undefined") ? aProp.genderIdentity : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
get published () {
|
|
|
|
return this._published;
|
|
|
|
},
|
|
|
|
|
|
|
|
set published(aPublished) {
|
|
|
|
this._published = aPublished;
|
|
|
|
},
|
|
|
|
|
|
|
|
get updated () {
|
|
|
|
return this._updated;
|
|
|
|
},
|
2012-05-10 21:51:48 -07:00
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
set updated(aUpdated) {
|
|
|
|
this._updated = aUpdated;
|
|
|
|
},
|
|
|
|
|
|
|
|
classID : CONTACT_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACT_CID,
|
|
|
|
contractID: CONTACT_CONTRACTID,
|
|
|
|
classDescription: "Contact",
|
|
|
|
interfaces: [nsIDOMContact, nsIDOMContactProperties],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContact, nsIDOMContactProperties])
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContactManager
|
|
|
|
|
|
|
|
const CONTACTMANAGER_CONTRACTID = "@mozilla.org/contactManager;1";
|
2013-02-03 04:05:51 -08:00
|
|
|
const CONTACTMANAGER_CID = Components.ID("{1d70322b-f11b-4f19-9586-7bf291f212aa}");
|
2012-02-28 14:01:48 -08:00
|
|
|
const nsIDOMContactManager = Components.interfaces.nsIDOMContactManager;
|
|
|
|
|
|
|
|
function ContactManager()
|
|
|
|
{
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("Constructor");
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ContactManager.prototype = {
|
2012-03-07 13:45:24 -08:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2012-05-08 11:42:41 -07:00
|
|
|
_oncontactchange: null,
|
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
_cursorData: {},
|
|
|
|
|
2012-05-08 11:42:41 -07:00
|
|
|
set oncontactchange(aCallback) {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("set oncontactchange");
|
2012-08-09 11:34:57 -07:00
|
|
|
let allowCallback = function() {
|
2013-02-01 12:44:55 -08:00
|
|
|
if (!this._oncontactchange) {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:RegisterForMessages");
|
|
|
|
}
|
2012-05-08 11:42:41 -07:00
|
|
|
this._oncontactchange = aCallback;
|
2012-08-09 11:34:57 -07:00
|
|
|
}.bind(this);
|
|
|
|
let cancelCallback = function() {
|
2012-05-08 11:42:41 -07:00
|
|
|
throw Components.results.NS_ERROR_FAILURE;
|
2012-08-09 11:34:57 -07:00
|
|
|
}
|
|
|
|
this.askPermission("listen", null, allowCallback, cancelCallback);
|
2012-05-08 11:42:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get oncontactchange() {
|
|
|
|
return this._oncontactchange;
|
|
|
|
},
|
2012-02-28 14:01:48 -08:00
|
|
|
|
|
|
|
_setMetaData: function(aNewContact, aRecord) {
|
|
|
|
aNewContact.id = aRecord.id;
|
|
|
|
aNewContact.published = aRecord.published;
|
|
|
|
aNewContact.updated = aRecord.updated;
|
|
|
|
},
|
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
_convertContact: function CM_convertContact(aContact) {
|
|
|
|
let newContact = new Contact();
|
|
|
|
newContact.init(aContact.properties);
|
|
|
|
this._setMetaData(newContact, aContact);
|
|
|
|
return newContact;
|
|
|
|
},
|
|
|
|
|
|
|
|
_convertContacts: function(aContacts) {
|
|
|
|
let contacts = [];
|
2012-02-28 14:01:48 -08:00
|
|
|
for (let i in aContacts) {
|
2013-02-03 04:05:51 -08:00
|
|
|
contacts.push(this._convertContact(aContacts[i]));
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
return contacts;
|
|
|
|
},
|
|
|
|
|
2013-02-24 19:53:55 -08:00
|
|
|
_fireSuccessOrDone: function(aCursor, aResult) {
|
|
|
|
if (aResult == null) {
|
|
|
|
Services.DOMRequest.fireDone(aCursor);
|
|
|
|
} else {
|
|
|
|
Services.DOMRequest.fireSuccess(aCursor, aResult);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-02-23 03:41:41 -08:00
|
|
|
_pushArray: function(aArr1, aArr2) {
|
|
|
|
aArr1.push.apply(aArr1, aArr2);
|
|
|
|
},
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
receiveMessage: function(aMessage) {
|
2013-02-03 04:05:51 -08:00
|
|
|
if (DEBUG) debug("receiveMessage: " + aMessage.name);
|
2012-02-28 14:01:48 -08:00
|
|
|
let msg = aMessage.json;
|
|
|
|
let contacts = msg.contacts;
|
|
|
|
|
2012-08-31 06:54:48 -07:00
|
|
|
let req;
|
2012-02-28 14:01:48 -08:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Contacts:Find:Return:OK":
|
2012-08-31 06:54:48 -07:00
|
|
|
req = this.getRequest(msg.requestID);
|
2012-02-28 14:01:48 -08:00
|
|
|
if (req) {
|
2013-02-03 04:05:51 -08:00
|
|
|
let result = this._convertContacts(contacts);
|
2012-05-08 11:42:41 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, result);
|
2012-02-28 14:01:48 -08:00
|
|
|
} else {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("no request stored!" + msg.requestID);
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
break;
|
2013-02-03 04:05:51 -08:00
|
|
|
case "Contacts:GetAll:Next":
|
2013-02-24 19:53:55 -08:00
|
|
|
let data = this._cursorData[msg.cursorId];
|
2013-02-23 03:41:41 -08:00
|
|
|
let result = contacts ? this._convertContacts(contacts) : [null];
|
2013-02-24 19:53:55 -08:00
|
|
|
if (data.waitingForNext) {
|
|
|
|
if (DEBUG) debug("cursor waiting for contact, sending");
|
|
|
|
data.waitingForNext = false;
|
2013-02-23 03:41:41 -08:00
|
|
|
let contact = result.shift();
|
|
|
|
this._pushArray(data.cachedContacts, result);
|
2013-02-24 19:53:55 -08:00
|
|
|
this._fireSuccessOrDone(data.cursor, contact);
|
2013-02-03 04:05:51 -08:00
|
|
|
} else {
|
2013-02-24 19:53:55 -08:00
|
|
|
if (DEBUG) debug("cursor not waiting, saving");
|
2013-02-23 03:41:41 -08:00
|
|
|
this._pushArray(data.cachedContacts, result);
|
2013-02-03 04:05:51 -08:00
|
|
|
}
|
|
|
|
break;
|
2012-08-31 06:54:48 -07:00
|
|
|
case "Contacts:GetSimContacts:Return:OK":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
|
|
|
let result = contacts.map(function(c) {
|
|
|
|
let contact = new Contact();
|
2012-12-24 17:47:05 -08:00
|
|
|
let prop = {name: [c.alphaId], tel: [ { value: c.number } ]};
|
|
|
|
|
|
|
|
if (c.email) {
|
|
|
|
prop.email = [{value: c.email}];
|
|
|
|
}
|
|
|
|
|
2013-02-22 00:16:46 -08:00
|
|
|
// ANR - Additional Number
|
|
|
|
if (c.anr) {
|
|
|
|
for (let i = 0; i < c.anr.length; i++) {
|
|
|
|
prop.tel.push({value: c.anr[i]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 17:47:05 -08:00
|
|
|
contact.init(prop);
|
2012-08-31 06:54:48 -07:00
|
|
|
return contact;
|
|
|
|
});
|
|
|
|
if (DEBUG) debug("result: " + JSON.stringify(result));
|
|
|
|
Services.DOMRequest.fireSuccess(req.request, result);
|
|
|
|
} else {
|
|
|
|
if (DEBUG) debug("no request stored!" + msg.requestID);
|
|
|
|
}
|
|
|
|
break;
|
2012-02-28 14:01:48 -08:00
|
|
|
case "Contact:Save:Return:OK":
|
|
|
|
case "Contacts:Clear:Return:OK":
|
|
|
|
case "Contact:Remove:Return:OK":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req)
|
2012-05-08 11:42:41 -07:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, null);
|
2012-02-28 14:01:48 -08:00
|
|
|
break;
|
|
|
|
case "Contacts:Find:Return:KO":
|
|
|
|
case "Contact:Save:Return:KO":
|
|
|
|
case "Contact:Remove:Return:KO":
|
|
|
|
case "Contacts:Clear:Return:KO":
|
2012-09-06 21:31:20 -07:00
|
|
|
case "Contacts:GetSimContacts:Return:KO":
|
2012-02-28 14:01:48 -08:00
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req)
|
2012-05-08 11:42:41 -07:00
|
|
|
Services.DOMRequest.fireError(req.request, msg.errorMsg);
|
2012-02-28 14:01:48 -08:00
|
|
|
break;
|
2012-08-09 11:34:57 -07:00
|
|
|
case "PermissionPromptHelper:AskPermission:OK":
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("id: " + msg.requestID);
|
2012-08-09 11:34:57 -07:00
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (!req) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
req.allow();
|
|
|
|
} else {
|
|
|
|
req.cancel();
|
|
|
|
}
|
|
|
|
break;
|
2013-02-01 12:44:55 -08:00
|
|
|
case "Contact:Changed":
|
|
|
|
// Fire oncontactchange event
|
|
|
|
if (DEBUG) debug("Contacts:ContactChanged: " + msg.contactID + ", " + msg.reason);
|
|
|
|
if (this._oncontactchange) {
|
|
|
|
let event = new this._window.MozContactChangeEvent("contactchanged", {
|
|
|
|
contactID: msg.contactID,
|
|
|
|
reason: msg.reason
|
|
|
|
});
|
|
|
|
this._oncontactchange.handleEvent(event);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("Wrong message: " + aMessage.name);
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
|
|
|
this.removeRequest(msg.requestID);
|
|
|
|
},
|
|
|
|
|
2012-10-13 11:57:59 -07:00
|
|
|
askPermission: function (aAccess, aRequest, aAllowCallback, aCancelCallback) {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("askPermission for contacts");
|
2012-10-31 17:26:05 -07:00
|
|
|
let access;
|
|
|
|
switch(aAccess) {
|
|
|
|
case "create":
|
|
|
|
access = "create";
|
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
case "remove":
|
|
|
|
access = "write";
|
|
|
|
break;
|
|
|
|
case "find":
|
|
|
|
case "getSimContacts":
|
|
|
|
case "listen":
|
|
|
|
access = "read";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
access = "unknown";
|
|
|
|
}
|
2013-02-03 04:05:51 -08:00
|
|
|
|
2012-08-09 11:34:57 -07:00
|
|
|
let requestID = this.getRequestId({
|
2012-10-13 11:57:59 -07:00
|
|
|
request: aRequest,
|
2012-08-09 11:34:57 -07:00
|
|
|
allow: function() {
|
|
|
|
aAllowCallback();
|
|
|
|
}.bind(this),
|
|
|
|
cancel : function() {
|
|
|
|
if (aCancelCallback) {
|
|
|
|
aCancelCallback()
|
2012-10-13 11:57:59 -07:00
|
|
|
} else if (aRequest) {
|
|
|
|
Services.DOMRequest.fireError(aRequest, "Not Allowed");
|
2012-08-09 11:34:57 -07:00
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
});
|
|
|
|
|
|
|
|
let principal = this._window.document.nodePrincipal;
|
|
|
|
cpmm.sendAsyncMessage("PermissionPromptHelper:AskPermission", {
|
|
|
|
type: "contacts",
|
2012-10-31 17:26:05 -07:00
|
|
|
access: access,
|
2012-08-09 11:34:57 -07:00
|
|
|
requestID: requestID,
|
|
|
|
origin: principal.origin,
|
|
|
|
appID: principal.appId,
|
|
|
|
browserFlag: principal.isInBrowserElement
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
save: function save(aContact) {
|
2012-02-28 14:01:48 -08:00
|
|
|
let request;
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("save: " + JSON.stringify(aContact) + " :" + aContact.id);
|
2012-08-09 11:34:57 -07:00
|
|
|
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] = aContact[field];
|
|
|
|
}
|
|
|
|
|
|
|
|
let reason;
|
|
|
|
if (aContact.id == "undefined") {
|
|
|
|
// for example {25c00f01-90e5-c545-b4d4-21E2ddbab9e0} becomes
|
|
|
|
// 25c00f0190e5c545b4d421E2ddbab9e0
|
|
|
|
aContact.id = this._getRandomId().replace('-', '', 'g').replace('{', '').replace('}', '');
|
|
|
|
reason = "create";
|
2012-02-28 14:01:48 -08:00
|
|
|
} else {
|
2012-08-09 11:34:57 -07:00
|
|
|
reason = "update";
|
2012-02-28 14:01:48 -08:00
|
|
|
}
|
2012-08-09 11:34:57 -07:00
|
|
|
|
|
|
|
this._setMetaData(newContact, aContact);
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("send: " + JSON.stringify(newContact));
|
2012-08-09 11:34:57 -07:00
|
|
|
request = this.createRequest();
|
2012-10-31 17:26:05 -07:00
|
|
|
let options = { contact: newContact, reason: reason };
|
2012-08-09 11:34:57 -07:00
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contact:Save", {requestID: this.getRequestId({request: request, reason: reason}), options: options});
|
|
|
|
}.bind(this)
|
|
|
|
this.askPermission(reason, request, allowCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(aOptions) {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("find! " + JSON.stringify(aOptions));
|
2013-02-03 04:05:51 -08:00
|
|
|
let request = this.createRequest();
|
2012-08-09 11:34:57 -07:00
|
|
|
let options = { findOptions: aOptions };
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:Find", {requestID: this.getRequestId({request: request, reason: "find"}), options: options});
|
|
|
|
}.bind(this)
|
|
|
|
this.askPermission("find", request, allowCallback);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2013-02-03 04:05:51 -08:00
|
|
|
createCursor: function CM_createCursor(aRequest) {
|
|
|
|
let id = this._getRandomId();
|
2013-02-24 19:53:55 -08:00
|
|
|
let data = {
|
|
|
|
cursor: Services.DOMRequest.createCursor(this._window, function() {
|
|
|
|
this.handleContinue(id);
|
|
|
|
}.bind(this)),
|
|
|
|
cachedContacts: [],
|
|
|
|
waitingForNext: true,
|
|
|
|
};
|
2013-02-03 04:05:51 -08:00
|
|
|
if (DEBUG) debug("saved cursor id: " + id);
|
2013-02-24 19:53:55 -08:00
|
|
|
this._cursorData[id] = data;
|
|
|
|
return [id, data.cursor];
|
2013-02-03 04:05:51 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
getAll: function CM_getAll(aOptions) {
|
|
|
|
if (DEBUG) debug("getAll: " + JSON.stringify(aOptions));
|
|
|
|
let [cursorId, cursor] = this.createCursor();
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:GetAll", {
|
|
|
|
cursorId: cursorId, findOptions: aOptions});
|
|
|
|
}.bind(this);
|
|
|
|
this.askPermission("find", cursor, allowCallback);
|
|
|
|
return cursor;
|
|
|
|
},
|
|
|
|
|
|
|
|
handleContinue: function CM_handleContinue(aCursorId) {
|
|
|
|
if (DEBUG) debug("handleContinue: " + aCursorId);
|
2013-02-24 19:53:55 -08:00
|
|
|
let data = this._cursorData[aCursorId];
|
|
|
|
if (data.cachedContacts.length > 0) {
|
|
|
|
if (DEBUG) debug("contact in cache");
|
2013-02-23 03:41:41 -08:00
|
|
|
let contact = data.cachedContacts.shift();
|
|
|
|
this._fireSuccessOrDone(data.cursor, contact);
|
2013-02-24 19:53:55 -08:00
|
|
|
} else {
|
|
|
|
if (DEBUG) debug("waiting for contact");
|
|
|
|
data.waitingForNext = true;
|
|
|
|
}
|
2013-02-03 04:05:51 -08:00
|
|
|
},
|
|
|
|
|
2012-08-09 11:34:57 -07:00
|
|
|
remove: function removeContact(aRecord) {
|
|
|
|
let request;
|
|
|
|
request = this.createRequest();
|
|
|
|
let options = { id: aRecord.id };
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contact:Remove", {requestID: this.getRequestId({request: request, reason: "remove"}), options: options});
|
|
|
|
}.bind(this)
|
|
|
|
this.askPermission("remove", request, allowCallback);
|
|
|
|
return request;
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("clear");
|
2012-02-28 14:01:48 -08:00
|
|
|
let request;
|
2012-08-09 11:34:57 -07:00
|
|
|
request = this.createRequest();
|
|
|
|
let options = {};
|
|
|
|
let allowCallback = function() {
|
|
|
|
cpmm.sendAsyncMessage("Contacts:Clear", {requestID: this.getRequestId({request: request, reason: "remove"}), options: options});
|
|
|
|
}.bind(this)
|
|
|
|
this.askPermission("remove", request, allowCallback);
|
|
|
|
return request;
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
2012-09-07 00:57:22 -07:00
|
|
|
getSimContacts: function(aContactType) {
|
2012-06-01 10:07:07 -07:00
|
|
|
let request;
|
2012-08-09 11:34:57 -07:00
|
|
|
request = this.createRequest();
|
2012-09-07 00:57:22 -07:00
|
|
|
let options = {contactType: aContactType};
|
2012-08-09 11:34:57 -07:00
|
|
|
|
|
|
|
let allowCallback = function() {
|
2012-09-07 00:57:22 -07:00
|
|
|
if (DEBUG) debug("getSimContacts " + aContactType);
|
2012-08-31 06:54:48 -07:00
|
|
|
cpmm.sendAsyncMessage("Contacts:GetSimContacts",
|
|
|
|
{requestID: this.getRequestId({request: request, reason: "getSimContacts"}),
|
|
|
|
options: options});
|
2012-08-09 11:34:57 -07:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
let cancelCallback = function() {
|
2012-06-01 10:07:07 -07:00
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2012-08-09 11:34:57 -07:00
|
|
|
this.askPermission("getSimContacts", request, allowCallback, cancelCallback);
|
|
|
|
return request;
|
2012-06-01 10:07:07 -07:00
|
|
|
},
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
init: function(aWindow) {
|
|
|
|
// Set navigator.mozContacts to null.
|
|
|
|
if (!Services.prefs.getBoolPref("dom.mozContacts.enabled"))
|
|
|
|
return null;
|
|
|
|
|
2012-03-07 13:45:24 -08:00
|
|
|
this.initHelper(aWindow, ["Contacts:Find:Return:OK", "Contacts:Find:Return:KO",
|
2012-05-10 21:51:48 -07:00
|
|
|
"Contacts:Clear:Return:OK", "Contacts:Clear:Return:KO",
|
|
|
|
"Contact:Save:Return:OK", "Contact:Save:Return:KO",
|
2012-08-09 11:34:57 -07:00
|
|
|
"Contact:Remove:Return:OK", "Contact:Remove:Return:KO",
|
2012-08-31 06:54:48 -07:00
|
|
|
"Contacts:GetSimContacts:Return:OK",
|
2012-09-06 21:31:20 -07:00
|
|
|
"Contacts:GetSimContacts:Return:KO",
|
2013-02-01 12:44:55 -08:00
|
|
|
"Contact:Changed",
|
2013-02-03 04:05:51 -08:00
|
|
|
"PermissionPromptHelper:AskPermission:OK",
|
|
|
|
"Contacts:GetAll:Next"]);
|
2012-02-28 14:01:48 -08:00
|
|
|
},
|
|
|
|
|
2012-05-08 11:42:41 -07:00
|
|
|
// Called from DOMRequestIpcHelper
|
|
|
|
uninit: function uninit() {
|
2012-08-22 14:34:57 -07:00
|
|
|
if (DEBUG) debug("uninit call");
|
2012-05-08 11:42:41 -07:00
|
|
|
if (this._oncontactchange)
|
|
|
|
this._oncontactchange = null;
|
|
|
|
},
|
|
|
|
|
2012-02-28 14:01:48 -08:00
|
|
|
classID : CONTACTMANAGER_CID,
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactManager, Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTMANAGER_CID,
|
|
|
|
contractID: CONTACTMANAGER_CONTRACTID,
|
|
|
|
classDescription: "ContactManager",
|
|
|
|
interfaces: [nsIDOMContactManager],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT})
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(
|
2013-02-03 04:05:51 -08:00
|
|
|
[Contact, ContactManager, ContactProperties, ContactAddress, ContactField, ContactTelField, ContactFindSortOptions, ContactFindOptions])
|