js/contacts.js
changeset 0 54063d8b0412
equal deleted inserted replaced
-1:000000000000 0:54063d8b0412
       
     1 /**
       
     2  * This class provides access to the device contacts.
       
     3  * @constructor
       
     4  */
       
     5 
       
     6 function Contacts() {
       
     7 	
       
     8 }
       
     9 
       
    10 function Contact() {
       
    11 	this.id = null;
       
    12 	this.name = { 
       
    13 		formatted: "",
       
    14 		givenName: "",
       
    15 		familyName: ""
       
    16 	};
       
    17     this.phones = [];
       
    18     this.emails = [];
       
    19 }
       
    20 
       
    21 Contact.prototype.displayName = function()
       
    22 {
       
    23     // TODO: can be tuned according to prefs
       
    24 	return this.givenName + " " + this.familyName;
       
    25 };
       
    26 
       
    27 /*
       
    28  * @param {ContactsFilter} filter Object with filter properties. filter.name only for now.
       
    29  * @param {function} successCallback Callback function on success
       
    30  * @param {function} errorCallback Callback function on failure
       
    31  * @param {object} options Object with properties .page and .limit for paging
       
    32  */
       
    33 
       
    34 Contacts.prototype.find = function(filter, successCallback, errorCallback, options) {
       
    35 	try {
       
    36 		this.contactsService = device.getServiceObject("Service.Contact", "IDataSource");
       
    37 		if (typeof options == 'object')
       
    38 			this.options = options;
       
    39 		else
       
    40 			this.options = {};
       
    41 		
       
    42 		var criteria = new Object();
       
    43 		criteria.Type = "Contact";
       
    44 		if (filter && filter.name) {
       
    45 			var searchTerm = '';
       
    46 			if (filter.name.givenName && filter.name.givenName.length > 0) {
       
    47 				searchTerm += filter.name.givenName;
       
    48 			}
       
    49 			if (filter.name.familyName && filter.name.familyName.length > 0) {
       
    50 				searchTerm += searchTerm.length > 0 ? ' ' + filter.name.familyName : filter.name.familyName;
       
    51 			}
       
    52 			if (!filter.name.familyName && !filter.name.givenName && filter.name.formatted) {
       
    53 				searchTerm = filter.name.formatted;
       
    54 			}
       
    55 			criteria.Filter = { SearchVal: searchTerm };
       
    56 		}
       
    57 		
       
    58 		if (typeof(successCallback) != 'function') 
       
    59 			successCallback = function(){};
       
    60 		if (typeof(errorCallback) != 'function') 
       
    61 			errorCallback = function(){};
       
    62 		if (isNaN(this.options.limit))
       
    63 			this.options.limit = 200;
       
    64 		if (isNaN(this.options.page))
       
    65 			this.options.page = 1;
       
    66 		
       
    67 		//need a closure here to bind this method to this instance of the Contacts object
       
    68 		this.global_success = successCallback;
       
    69 		var obj = this;
       
    70 		
       
    71 		//WRT: result.ReturnValue is an iterator of contacts
       
    72 		this.contactsService.IDataSource.GetList(criteria, function(transId, eventCode, result){
       
    73 			obj.success_callback(result.ReturnValue);
       
    74 		});
       
    75 	} 
       
    76 	catch (ex) {
       
    77 		alert(ex.name + ": " + ex.message);
       
    78 		errorCallback(ex);
       
    79 	}
       
    80 };
       
    81 
       
    82 Contacts.prototype.success_callback = function(contacts_iterator) {
       
    83 	try {
       
    84 	var gapContacts = new Array();
       
    85 	contacts_iterator.reset();
       
    86     var contact;
       
    87 	var i = 0;
       
    88 	var end = this.options.page * this.options.limit;
       
    89 	var start = end - this.options.limit;
       
    90 	while ((contact = contacts_iterator.getNext()) != undefined && i < end) {
       
    91 		try {
       
    92 			if (i >= start) {
       
    93 				var gapContact = new Contact();
       
    94 				gapContact.name.givenName = Contacts.GetValue(contact, "FirstName");
       
    95 				gapContact.name.familyName = Contacts.GetValue(contact, "LastName");
       
    96 				gapContact.name.formatted = gapContact.name.givenName + " " + gapContact.name.familyName;
       
    97 				gapContact.emails = Contacts.getEmailsList(contact);
       
    98 				gapContact.phones = Contacts.getPhonesList(contact);
       
    99 				gapContact.address = Contacts.getAddress(contact);
       
   100 				gapContact.id = Contacts.GetValue(contact, "id");
       
   101 				gapContacts.push(gapContact);
       
   102 			}
       
   103 			i++;
       
   104 		} catch (e) {
       
   105 			alert("ContactsError (" + e.name + ": " + e.message + ")");
       
   106 		}
       
   107 	}
       
   108 	this.contacts = gapContacts;
       
   109 	this.global_success(gapContacts);
       
   110 	} catch (ex) { alert(ex.name + ": " + ex.message); }
       
   111 };
       
   112 
       
   113 Contacts.getEmailsList = function(contact) {
       
   114 	var emails = new Array();
       
   115 	try {
       
   116 			emails[0] = { type:"General", address: Contacts.GetValue(contact, "EmailGen") };
       
   117 			emails[1] = { type:"Work", address: Contacts.GetValue(contact, "EmailWork") };		
       
   118 			emails[2] = { type:"Home", address: Contacts.GetValue(contact, "EmailHome") };
       
   119 	} catch (e) {
       
   120 		emails = [];
       
   121 	}
       
   122 	return emails;
       
   123 };
       
   124 
       
   125 Contacts.getPhonesList = function(contact) {
       
   126 	var phones = new Array();
       
   127 	try {
       
   128 			phones[0] = { type:"Mobile", number: Contacts.GetValue(contact, "MobilePhoneGen") };
       
   129 			phones[1] = { type:"Home", number: Contacts.GetValue(contact, "LandPhoneGen") };
       
   130 			phones[2] = { type:"Fax", number: Contacts.GetValue(contact, "FaxNumberGen") };
       
   131 			phones[3] = { type:"Work", number: Contacts.GetValue(contact, "LandPhoneWork") };
       
   132 			phones[4] = { type:"WorkMobile", number: Contacts.GetValue(contact, "MobilePhoneWork") };
       
   133 	} catch (e) {
       
   134 		phones = [];
       
   135 	}
       
   136 	return phones;
       
   137 };
       
   138 
       
   139 Contacts.getAddress = function(contact) {
       
   140 	var address = "";
       
   141 	try {
       
   142 		address = Contacts.GetValue(contact, "AddrLabelHome") + ", " + Contacts.GetValue(contact, "AddrStreetHome") + ", " +
       
   143 				Contacts.GetValue(contact, "AddrLocalHome") + ", " + Contacts.GetValue(contact, "AddrRegionHome") + ", " + 
       
   144 				Contacts.GetValue(contact, "AddrPostCodeHome") + ", " + Contacts.GetValue(contact, "AddrCountryHome");
       
   145 	} catch (e) {
       
   146 		address = "";
       
   147 	}
       
   148 	return address;
       
   149 };
       
   150 
       
   151 Contacts.GetValue = function(contactObj, key) {
       
   152 	try {
       
   153 		return contactObj[key]["Value"];
       
   154 	} catch (e) {
       
   155 		return "";
       
   156 	}
       
   157 };
       
   158 
       
   159 if (typeof navigator.contacts == "undefined") navigator.contacts = new Contacts();