×
Namespaces

Variants
Actions
Revision as of 15:46, 17 December 2008 by copyeditor (Talk | contribs)

Adding contact in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
S60 5th Edition
Article
Keywords: contacts, widget, device.getServiceObject, Service.IDataSource.GetList, Service.IDataSource.Add
Created: (28 Nov 2008)
Last edited: copyeditor (17 Dec 2008)


Overview

This code snippet shows how to add contacts to the device's phonebook using Contact Platform Service for S60 Web Runtime, introduced in S60 5th Edition.

To obtain access to the service object for the Contact Service API, method device.getServiceObject("Service.Contact", "IDataSource") used.

After setting correct values for the contact type criteria.Type and its parameters criteria.Data, method IDataSource.Add(criteria) was used for adding s new contact.

Method IDataSource.GetList(criteria) was used for retrieving existing contact items.

Source file: phonebook.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="phonebook.js"></script>
<title></title>
</head>
<body onload="init()">
<select id="dbList" onchange="doShow()"></select><br/>
<div id="inputdata">
<!-- input field for search string -->
First Name: <input id="firstname" type="text"></input>
<button onClick="doShow( )">Show item</button><br/>
Last Name: <input id="lastname" type="text"></input><br/>
Phone number: <input id="phonenumber" type="text"></input><br/>
E-Mail: <input id="email" type="text"></input><br/>
<button onClick="doSave( )">Add</button>
</div>
<hr>
<!-- table for showing existing items -->
<table id="maintable" cellspacing=1 cellpadding=1 border=1 cols=4>
</table>
</html>

Source file: phonebook.js

// contacts system service object
var phonebook;
// DOM select object handle
var list;
// system error code
var noItemsErrorCode = 1012;
//Menu command id, all of menu items must have unique id
var CMD_SEARCH = 11;
 
 
/**
* Setting default data on application load
*/

function init() {
// Create main menu items
var searchData = new MenuItem( "Add contact", CMD_SEARCH );
 
// Setting handlers for main menu items
searchData.onSelect = doSave;
 
// Adding items to menu
menu.append(searchData);
 
// Start displaying menu
menu.showSoftkeys();
 
// contacts system service object
try {
phonebook = device.getServiceObject("Service.Contact", "IDataSource");
} catch( err ) {
alert( "Error receiving Contact service object" );
return;
}
 
// getting handler to select item
list = document.getElementById("dbList");
// cleaning select
while(list.options.length > 0) {
list.remove(0);
}
// making a request for accessible databases
var criteria = new Object();
criteria.Type = 'Database';
var contacts = 0;
// making a call to service to receive a list of suitable items
try {
contacts = phonebook.IDataSource.GetList(criteria);
} catch(err) {
alert( "error receiving contacts databases" );
return;
}
// if we received databases -- filling dropdown list with accessible items
if(contacts.ErrorCode != 0) {
// if error code is not "no suitable items" -- show alert otherwise
// leave the function
if(contacts.ErrorCode != noItemsErrorCode) {
alert(contacts.ErrorMessage);
}
alert("No contacts databases accessible");
window.close();
return;
}
 
for(i = 0; (item = contacts.ReturnValue.getNext()) != undefined; ++i) {
var temp = document.createElement('option');
temp.text = item.DBUri;
list.add(temp, null);
}
// make the first item selected
list.selectedIndex = 0;
// refreshing information in table
doShow();
}
 
/**
* function refreshes information in table
*/

function doShow() {
// Criteria object for making a GetList call to receive contacts
// information
var criteria = new Object();
criteria.Type = 'Contact';
var temp = document.getElementById('firstname').value;
criteria.Filter = new Object();
// setting parameter what database to work with
criteria.Filter.DBUri = list.options[list.selectedIndex].text;
if(temp != "") {
// if we have some search string add search parameter to criteria
criteria.Filter.SearchVal = temp;
}
var contacts = 0;
// making a call to service to receive a list of suitable items
try{
contacts = phonebook.IDataSource.GetList(criteria);
} catch(err) {
alert("Error receiving contacts list");
return;
}
// receiving a handler to table to display info
var table = document.getElementById('maintable');
// cleaning table and adding header
while(table.rows[0] != undefined) {
table.deleteRow(0);
}
table.insertRow(0);
table.rows[0].innerHTML = "<th>Firstname</th><th>Lastname</th>" +
"<th>Phone</th><th>Email</th>";
// looking for error code returned by GetList
if(contacts.ErrorCode != 0) {
// if error code is not "no suitable items" -- show alert otherwise
// leave the function
if(contacts.ErrorCode != noItemsErrorCode) {
alert(contacts.ErrorMessage);
}
return;
} else {
// filling table with received data
var table = document.getElementById('maintable');
var i;
var item;
// loading only data that is present
for(i = 0;
(item = contacts.ReturnValue.getNext()) != undefined;
++i) {
table.insertRow(i + 1);
var result = "<td>";
if(item.FirstName != undefined) {
result += item.FirstName.Value;
}
result += "</td><td>";
if(item.LastName != undefined) {
result += item.LastName.Value;
}
result += "</td><td>";
if(item.MobilePhoneGen != undefined) {
result += item.MobilePhoneGen.Value;
}
result += "</td><td>";
if(item.EmailGen != undefined) {
result += item.EmailGen.Value;
}
result += "</td>";
table.rows[i + 1].innerHTML = result;
}
}
}
 
/**
* function adds item to contacts
*/

function doSave() {
// Criteria object for making a GetList call to receive contacts
// information
var criteria = new Object();
criteria.Type = 'Contact';
criteria.Data = new Object();
// demanding first name, but that is not essential
if(document.getElementById('firstname').value == "") {
alert("You should input firstname");
return;
}
criteria.Data.DBUri = list.options[list.selectedIndex].text;
// adding First Name parameter to new item
criteria.Data.FirstName = new Object();
criteria.Data.FirstName.Value =
document.getElementById('firstname').value;
// adding other parameters if present
if(document.getElementById('lastname').value != "") {
criteria.Data.LastName = new Object();
criteria.Data.LastName.Value =
document.getElementById('lastname').value;
}
if(document.getElementById('phonenumber').value != "") {
criteria.Data.MobilePhoneGen = new Object();
criteria.Data.MobilePhoneGen.Value =
document.getElementById('phonenumber').value;
}
if(document.getElementById('email').value != "") {
criteria.Data.EmailGen = new Object();
criteria.Data.EmailGen.Value =
document.getElementById('email').value;
}
// making a request to service to receive a list of suitable items
var contacts = 0;
try {
contacts = phonebook.IDataSource.Add(criteria);
} catch(err) {
alert( "error saving data" );
return;
}
// looking for error code returned by GetList
if(contacts.ErrorCode != 0) {
// show alert
alert(contacts.ErrorMessage);
}
// updating information in table to show that new contact exists
doShow();
}


Postconditions

  • Upon loading shows a table with all contacts and a selection list with available databases.
  • Typing some text in the input field and pressing "Show" will update the information in the ,showing only items that are returned by the standard search (that have common letters in first name or last name).
  • Filling fields and pressing "Add" will add a new item to the contacts and refresh the information in the table, showing the contacts satisfying the FirstName string.

Supplementary material

You can view the source file and executable application in the attached zip archive. The archive is available for download at Adding_contact_in_WRT.zip

See also

177 page views in the last 30 days.
×