2012-02-28 22:01:48 +00: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 18:00:53 +00:00
|
|
|
"use strict";
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
/* static functions */
|
|
|
|
let DEBUG = 0;
|
|
|
|
if (DEBUG)
|
|
|
|
debug = function (s) { dump("-*- ContactManager: " + s + "\n"); }
|
|
|
|
else
|
|
|
|
debug = function (s) {}
|
|
|
|
|
|
|
|
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 21:45:24 +00:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
|
|
|
|
2012-04-02 23:39:57 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(Services, "DOMRequest", function() {
|
2012-03-07 21:45:24 +00:00
|
|
|
return Cc["@mozilla.org/dom/dom-request-service;1"].getService(Ci.nsIDOMRequestService);
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
|
|
|
|
return Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsIFrameMessageManager);
|
|
|
|
});
|
2012-02-28 22:01:48 +00:00
|
|
|
|
2012-06-01 17:07:07 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "mRIL", function () {
|
|
|
|
return Cc["@mozilla.org/telephony/system-worker-manager;1"].getService(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIRadioInterfaceLayer);
|
|
|
|
});
|
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
const nsIClassInfo = Ci.nsIClassInfo;
|
2012-05-11 04:51:48 +00:00
|
|
|
const CONTACTPROPERTIES_CID = Components.ID("{f5181640-89e8-11e1-b0c4-0800200c9a66}");
|
2012-02-28 22:01:48 +00:00
|
|
|
const nsIDOMContactProperties = Ci.nsIDOMContactProperties;
|
|
|
|
|
|
|
|
// ContactProperties is not directly instantiated. It is used as interface.
|
|
|
|
|
2012-05-11 04:51:48 +00:00
|
|
|
function ContactProperties(aProp) { debug("ContactProperties Constructor"); }
|
|
|
|
|
2012-02-28 22:01:48 +00: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-11 04:51:48 +00:00
|
|
|
const CONTACTADDRESS_CID = Components.ID("{eba48030-89e8-11e1-b0c4-0800200c9a66}");
|
2012-02-28 22:01:48 +00:00
|
|
|
const nsIDOMContactAddress = Components.interfaces.nsIDOMContactAddress;
|
|
|
|
|
2012-05-11 04:51:48 +00:00
|
|
|
function ContactAddress(aType, aStreetAddress, aLocality, aRegion, aPostalCode, aCountryName) {
|
|
|
|
this.type = aType || null;
|
2012-02-28 22:01:48 +00:00
|
|
|
this.streetAddress = aStreetAddress || null;
|
|
|
|
this.locality = aLocality || null;
|
|
|
|
this.region = aRegion || null;
|
|
|
|
this.postalCode = aPostalCode || null;
|
|
|
|
this.countryName = aCountryName || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ContactAddress.prototype = {
|
|
|
|
|
|
|
|
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-07-03 18:00:53 +00:00
|
|
|
//ContactEmail
|
|
|
|
|
|
|
|
const CONTACTEMAIL_CONTRACTID = "@mozilla.org/contactEmail;1";
|
|
|
|
const CONTACTEMAIL_CID = Components.ID("{94811520-c11f-11e1-afa7-0800200c9a66}");
|
|
|
|
const nsIDOMContactEmail = Components.interfaces.nsIDOMContactEmail;
|
|
|
|
|
|
|
|
function ContactEmail(aType, aAddress) {
|
|
|
|
this.type = aType || null;
|
|
|
|
this.address = aAddress || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ContactEmail.prototype = {
|
|
|
|
|
|
|
|
classID : CONTACTEMAIL_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTEMAIL_CID,
|
|
|
|
contractID: CONTACTEMAIL_CONTRACTID,
|
|
|
|
classDescription: "ContactEmail",
|
|
|
|
interfaces: [nsIDOMContactEmail],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactEmail])
|
|
|
|
}
|
|
|
|
|
2012-05-11 04:51:48 +00:00
|
|
|
//ContactTelephone
|
|
|
|
|
|
|
|
const CONTACTTELEPHONE_CONTRACTID = "@mozilla.org/contactTelephone;1";
|
|
|
|
const CONTACTTELEPHONE_CID = Components.ID("{82601b20-89e8-11e1-b0c4-0800200c9a66}");
|
|
|
|
const nsIDOMContactTelephone = Components.interfaces.nsIDOMContactTelephone;
|
|
|
|
|
|
|
|
function ContactTelephone(aType, aNumber) {
|
|
|
|
this.type = aType || null;
|
|
|
|
this.number = aNumber || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ContactTelephone.prototype = {
|
|
|
|
|
|
|
|
classID : CONTACTTELEPHONE_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTTELEPHONE_CID,
|
|
|
|
contractID: CONTACTTELEPHONE_CONTRACTID,
|
|
|
|
classDescription: "ContactTelephone",
|
|
|
|
interfaces: [nsIDOMContactTelephone],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactTelephone])
|
|
|
|
}
|
|
|
|
|
2012-02-28 22:01:48 +00: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 23:39:57 +00:00
|
|
|
function ContactFindOptions() { };
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
ContactFindOptions.prototype = {
|
|
|
|
|
|
|
|
classID : CONTACTFINDOPTIONS_CID,
|
|
|
|
classInfo : XPCOMUtils.generateCI({classID: CONTACTFINDOPTIONS_CID,
|
|
|
|
contractID: CONTACTFINDOPTIONS_CONTRACTID,
|
|
|
|
classDescription: "ContactFindOptions",
|
|
|
|
interfaces: [nsIDOMContactFindOptions],
|
|
|
|
flags: nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([nsIDOMContactFindOptions])
|
|
|
|
}
|
|
|
|
|
|
|
|
//Contact
|
|
|
|
|
|
|
|
const CONTACT_CONTRACTID = "@mozilla.org/contact;1";
|
|
|
|
const CONTACT_CID = Components.ID("{da0f7040-388b-11e1-b86c-0800200c9a66}");
|
|
|
|
const nsIDOMContact = Components.interfaces.nsIDOMContact;
|
|
|
|
|
|
|
|
function Contact() { debug("Contact constr: "); };
|
|
|
|
|
|
|
|
Contact.prototype = {
|
|
|
|
|
|
|
|
init: function init(aProp) {
|
|
|
|
// Accept non-array strings for DOMString[] properties and convert them.
|
2012-07-03 18:00:53 +00:00
|
|
|
function _create(aField) {
|
|
|
|
if (Array.isArray(aField)) {
|
|
|
|
for (let i = 0; i < aField.length; i++) {
|
|
|
|
if (typeof aField[i] !== "string")
|
|
|
|
aField[i] = String(aField[i]);
|
|
|
|
}
|
|
|
|
return aField;
|
|
|
|
} else if (aField != null) {
|
|
|
|
return [String(aField)];
|
|
|
|
}
|
2012-02-28 22:01:48 +00: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 18:00:53 +00: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++)
|
|
|
|
this.email.push(new ContactEmail(aProp.email[i].type, aProp.email[i].address));
|
|
|
|
} else {
|
|
|
|
this.email = null;
|
|
|
|
}
|
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
this.photo = _create(aProp.photo) || null;
|
|
|
|
this.url = _create(aProp.url) || null;
|
|
|
|
this.category = _create(aProp.category) || null;
|
|
|
|
|
|
|
|
if (aProp.adr) {
|
|
|
|
// Make sure adr argument is an array. Instanceof doesn't work.
|
2012-05-11 04:51:48 +00:00
|
|
|
aProp.adr = Array.isArray(aProp.adr) ? aProp.adr : [aProp.adr];
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
this.adr = new Array();
|
|
|
|
for (let i = 0; i < aProp.adr.length; i++)
|
2012-05-11 04:51:48 +00: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 22:01:48 +00:00
|
|
|
} else {
|
|
|
|
this.adr = null;
|
|
|
|
}
|
|
|
|
|
2012-05-11 04:51:48 +00: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++)
|
|
|
|
this.tel.push(new ContactTelephone(aProp.tel[i].type, aProp.tel[i].number));
|
|
|
|
} else {
|
|
|
|
this.tel = null;
|
|
|
|
}
|
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
this.org = _create(aProp.org) || null;
|
2012-05-11 04:51:48 +00:00
|
|
|
this.jobTitle = _create(aProp.jobTitle) || null;
|
2012-02-28 22:01:48 +00:00
|
|
|
this.bday = (aProp.bday == "undefined" || aProp.bday == null) ? null : new Date(aProp.bday);
|
|
|
|
this.note = _create(aProp.note) || null;
|
|
|
|
this.impp = _create(aProp.impp) || null;
|
|
|
|
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-11 04:51:48 +00:00
|
|
|
|
2012-02-28 22:01:48 +00: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";
|
2012-06-01 17:07:07 +00:00
|
|
|
const CONTACTMANAGER_CID = Components.ID("{d88af7e0-a45f-11e1-b3dd-0800200c9a66}");
|
2012-02-28 22:01:48 +00:00
|
|
|
const nsIDOMContactManager = Components.interfaces.nsIDOMContactManager;
|
|
|
|
|
|
|
|
function ContactManager()
|
|
|
|
{
|
|
|
|
debug("Constructor");
|
|
|
|
}
|
|
|
|
|
|
|
|
ContactManager.prototype = {
|
2012-03-07 21:45:24 +00:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
2012-05-08 18:42:41 +00:00
|
|
|
_oncontactchange: null,
|
|
|
|
|
|
|
|
set oncontactchange(aCallback) {
|
|
|
|
if (this.hasPrivileges)
|
|
|
|
this._oncontactchange = aCallback;
|
|
|
|
else
|
|
|
|
throw Components.results.NS_ERROR_FAILURE;
|
|
|
|
},
|
|
|
|
|
|
|
|
get oncontactchange() {
|
|
|
|
return this._oncontactchange;
|
|
|
|
},
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
save: function save(aContact) {
|
|
|
|
let request;
|
|
|
|
if (this.hasPrivileges) {
|
|
|
|
debug("save: " + JSON.stringify(aContact) + " :" + aContact.id);
|
|
|
|
let newContact = {};
|
|
|
|
newContact.properties = {
|
|
|
|
name: [],
|
|
|
|
honorificPrefix: [],
|
|
|
|
givenName: [],
|
|
|
|
additionalName: [],
|
|
|
|
familyName: [],
|
|
|
|
honorificSuffix: [],
|
|
|
|
nickname: [],
|
|
|
|
email: [],
|
|
|
|
photo: [],
|
|
|
|
url: [],
|
|
|
|
category: [],
|
|
|
|
adr: [],
|
|
|
|
tel: [],
|
|
|
|
org: [],
|
2012-05-11 04:51:48 +00:00
|
|
|
jobTitle: [],
|
2012-02-28 22:01:48 +00:00
|
|
|
bday: null,
|
|
|
|
note: [],
|
|
|
|
impp: [],
|
|
|
|
anniversary: null,
|
|
|
|
sex: null,
|
|
|
|
genderIdentity: null
|
|
|
|
};
|
|
|
|
for (let field in newContact.properties)
|
|
|
|
newContact.properties[field] = aContact[field];
|
|
|
|
|
2012-05-08 18:42:41 +00:00
|
|
|
let reason;
|
2012-02-28 22:01:48 +00:00
|
|
|
if (aContact.id == "undefined") {
|
2012-03-15 21:30:19 +00:00
|
|
|
// for example {25c00f01-90e5-c545-b4d4-21E2ddbab9e0} becomes
|
|
|
|
// 25c00f0190e5c545b4d421E2ddbab9e0
|
2012-05-08 18:42:41 +00:00
|
|
|
aContact.id = this._getRandomId().replace('-', '', 'g').replace('{', '').replace('}', '');
|
|
|
|
reason = "create";
|
|
|
|
} else {
|
|
|
|
reason = "update";
|
2012-02-28 22:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._setMetaData(newContact, aContact);
|
|
|
|
debug("send: " + JSON.stringify(newContact));
|
2012-03-07 21:45:24 +00:00
|
|
|
request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Contact:Save", {contact: newContact,
|
2012-05-08 18:42:41 +00:00
|
|
|
requestID: this.getRequestId({request: request, reason: reason })});
|
2012-02-28 22:01:48 +00:00
|
|
|
return request;
|
|
|
|
} else {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function removeContact(aRecord) {
|
|
|
|
let request;
|
|
|
|
if (this.hasPrivileges) {
|
2012-03-07 21:45:24 +00:00
|
|
|
request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Contact:Remove", {id: aRecord.id,
|
2012-05-08 18:42:41 +00:00
|
|
|
requestID: this.getRequestId({request: request, reason: "remove"})});
|
2012-02-28 22:01:48 +00:00
|
|
|
return request;
|
|
|
|
} else {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setMetaData: function(aNewContact, aRecord) {
|
|
|
|
aNewContact.id = aRecord.id;
|
|
|
|
aNewContact.published = aRecord.published;
|
|
|
|
aNewContact.updated = aRecord.updated;
|
|
|
|
},
|
|
|
|
|
|
|
|
_convertContactsArray: function(aContacts) {
|
|
|
|
let contacts = new Array();
|
|
|
|
for (let i in aContacts) {
|
|
|
|
let newContact = new Contact();
|
|
|
|
newContact.init(aContacts[i].properties);
|
|
|
|
this._setMetaData(newContact, aContacts[i]);
|
|
|
|
contacts.push(newContact);
|
|
|
|
}
|
|
|
|
return contacts;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
debug("Contactmanager::receiveMessage: " + aMessage.name);
|
|
|
|
let msg = aMessage.json;
|
|
|
|
let contacts = msg.contacts;
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Contacts:Find:Return:OK":
|
|
|
|
let req = this.getRequest(msg.requestID);
|
|
|
|
if (req) {
|
|
|
|
let result = this._convertContactsArray(contacts);
|
|
|
|
debug("result: " + JSON.stringify(result));
|
2012-05-08 18:42:41 +00:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, result);
|
2012-02-28 22:01:48 +00:00
|
|
|
} else {
|
|
|
|
debug("no request stored!" + msg.requestID);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
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 18:42:41 +00:00
|
|
|
Services.DOMRequest.fireSuccess(req.request, null);
|
|
|
|
|
|
|
|
// Fire oncontactchange event
|
|
|
|
if (this._oncontactchange) {
|
2012-06-14 17:13:12 +00:00
|
|
|
let event = new this._window.MozContactChangeEvent("contactchanged", {
|
|
|
|
contactID: msg.contactID,
|
|
|
|
reason: req.reason
|
|
|
|
});
|
2012-05-08 18:42:41 +00:00
|
|
|
this._oncontactchange.handleEvent(event);
|
|
|
|
}
|
2012-02-28 22:01:48 +00:00
|
|
|
break;
|
|
|
|
case "Contacts:Find:Return:KO":
|
|
|
|
case "Contact:Save:Return:KO":
|
|
|
|
case "Contact:Remove:Return:KO":
|
|
|
|
case "Contacts:Clear:Return:KO":
|
|
|
|
req = this.getRequest(msg.requestID);
|
|
|
|
if (req)
|
2012-05-08 18:42:41 +00:00
|
|
|
Services.DOMRequest.fireError(req.request, msg.errorMsg);
|
2012-02-28 22:01:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("Wrong message: " + aMessage.name);
|
|
|
|
}
|
|
|
|
this.removeRequest(msg.requestID);
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function(aOptions) {
|
|
|
|
let request;
|
|
|
|
if (this.hasPrivileges) {
|
2012-03-07 21:45:24 +00:00
|
|
|
request = this.createRequest();
|
|
|
|
cpmm.sendAsyncMessage("Contacts:Find", {findOptions: aOptions,
|
2012-05-08 18:42:41 +00:00
|
|
|
requestID: this.getRequestId({request: request, reason: "find"})});
|
2012-02-28 22:01:48 +00:00
|
|
|
return request;
|
|
|
|
} else {
|
|
|
|
debug("find not allowed");
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
|
|
|
let request;
|
|
|
|
if (this.hasPrivileges) {
|
2012-03-07 21:45:24 +00:00
|
|
|
request = this.createRequest();
|
2012-05-08 18:42:41 +00:00
|
|
|
cpmm.sendAsyncMessage("Contacts:Clear", {requestID: this.getRequestId({request: request, reason: "remove"})});
|
2012-02-28 22:01:48 +00:00
|
|
|
return request;
|
|
|
|
} else {
|
|
|
|
debug("clear not allowed");
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-01 17:07:07 +00:00
|
|
|
getSimContacts: function(aType) {
|
|
|
|
let request;
|
|
|
|
if (this.hasPrivileges) {
|
|
|
|
let callback = function(aType, aContacts) {
|
|
|
|
debug("got SIM contacts: " + aType + " " + JSON.stringify(aContacts));
|
|
|
|
let result = aContacts.map(function(c) { return { name: [c.alphaId], tel: [c.number] } });
|
|
|
|
debug("result: " + JSON.stringify(result));
|
|
|
|
Services.DOMRequest.fireSuccess(request, result);
|
|
|
|
};
|
|
|
|
debug("getSimContacts " + aType);
|
|
|
|
request = this.createRequest();
|
|
|
|
mRIL.getICCContacts(aType, callback);
|
|
|
|
return request;
|
|
|
|
} else {
|
|
|
|
debug("getSimContacts not allowed");
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-02-28 22:01:48 +00:00
|
|
|
init: function(aWindow) {
|
|
|
|
// Set navigator.mozContacts to null.
|
|
|
|
if (!Services.prefs.getBoolPref("dom.mozContacts.enabled"))
|
|
|
|
return null;
|
|
|
|
|
2012-03-07 21:45:24 +00:00
|
|
|
this.initHelper(aWindow, ["Contacts:Find:Return:OK", "Contacts:Find:Return:KO",
|
2012-05-11 04:51:48 +00:00
|
|
|
"Contacts:Clear:Return:OK", "Contacts:Clear:Return:KO",
|
|
|
|
"Contact:Save:Return:OK", "Contact:Save:Return:KO",
|
|
|
|
"Contact:Remove:Return:OK", "Contact:Remove:Return:KO"]);
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
let principal = aWindow.document.nodePrincipal;
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
|
|
|
|
|
2012-07-16 15:44:52 +00:00
|
|
|
let perm = principal == secMan.getSystemPrincipal()
|
|
|
|
? Ci.nsIPermissionManager.ALLOW_ACTION
|
2012-08-01 20:38:31 +00:00
|
|
|
: Services.perms.testExactPermissionFromPrincipal(principal, "contacts");
|
2012-02-28 22:01:48 +00:00
|
|
|
|
|
|
|
//only pages with perm set can use the contacts
|
|
|
|
this.hasPrivileges = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
|
2012-08-01 20:38:31 +00:00
|
|
|
debug("Contacts permission: " + this.hasPrivileges);
|
2012-02-28 22:01:48 +00:00
|
|
|
},
|
|
|
|
|
2012-05-08 18:42:41 +00:00
|
|
|
// Called from DOMRequestIpcHelper
|
|
|
|
uninit: function uninit() {
|
|
|
|
debug("uninit call");
|
|
|
|
if (this._oncontactchange)
|
|
|
|
this._oncontactchange = null;
|
|
|
|
},
|
|
|
|
|
2012-02-28 22:01:48 +00: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-05-11 04:51:48 +00:00
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory(
|
2012-07-03 18:00:53 +00:00
|
|
|
[Contact, ContactManager, ContactProperties, ContactAddress, ContactTelephone, ContactFindOptions, ContactEmail])
|