×
Namespaces

Variants
Actions
(Difference between revisions)

Adding contact in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
kiran10182 (Talk | contribs)
m
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
 
(19 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Symbian Web Runtime]][[Category:PIM]][[Category:S60 5th Edition]][[Category:Code Examples]]
__NOEDITSECTION__
+
{{Abstract|This code example shows how to add contacts to the device's default phone book in Symbian Web Runtime.}}
{{CodeSnippet
+
|id= 
+
|platform=S60 5th Edition
+
|devices=Nokia 5800 XpressMusic
+
|category=Web Runtime (WRT)
+
|subcategory=Phonebook / Contacts
+
|creationdate=November 28, 2008
+
|keywords=contacts, widget, device.getServiceObject, Service.IDataSource.GetList, Service.IDataSource.Add
+
}}
+
  
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media:Adding contact in WRT.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= Nokia 5800 XpressMusic
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= S60 5th Edition
 +
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= device.getServiceObject(), Service.Contact.Add()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081210
 +
|author= [[User:MiGryz]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001238
 +
}}
  
 
==Overview==
 
==Overview==
This code snippet shows how to use methods of Contact Service API for adding
 
contacts to phone phonebook.
 
  
To obtain access to service object for the Contact Service API, method
+
The example uses the Contact Platform Service, introduced in S60 5th Edition.
<tt>device.getServiceObject("Service.Contact", "IDataSource")</tt> used.
+
  
After setting correct values for contact type <tt>criteria.Type</tt>,
+
To obtain access to the service object for the Contact Service API, the {{Icode|device.getServiceObject("Service.Contact", "IDataSource")}} method is used.
its parameters <tt>criteria.Data</tt>,  
+
method IDataSource.Add(criteria) was used for adding new contact.
+
  
Method IDataSource.GetList(criteria) was used for retrieving existing contacts
+
After setting the correct values for the criteria object ({{Icode|criteria.Type}} and {{Icode|criteria.Data}}), the {{Icode|IDataSource.Add(criteria)}} method is used to add the new contact.
items.  
+
  
==Source file: phonebook.html==
+
==Source: phonebook.html==
  
 
<code xml>
 
<code xml>
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
     <head>
 
     <head>
         <script src="phonebook.js"></script>
+
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 +
         <script type="text/javascript" src="phonebook.js" />
 
         <title></title>
 
         <title></title>
 
     </head>
 
     </head>
 
     <body onload="init()">
 
     <body onload="init()">
        <select id="dbList" onchange="doShow()"></select><br/>
 
 
         <div id="inputdata">  
 
         <div id="inputdata">  
            <!-- input field for search string -->
+
             First Name: <input id="firstname" type="text"></input><br />
             First Name: <input id="firstname" type="text"></input>
+
             Last Name: <input id="lastname" type="text"></input><br />
            <button onClick="doShow( )">Show item</button><br/>
+
             Phone number: <input id="phonenumber" type="text"></input><br />
             Last Name: <input id="lastname" type="text"></input><br/>
+
             E-Mail: <input id="email" type="text"></input><br />
             Phone number: <input id="phonenumber" type="text"></input><br/>
+
             <input type="button" value="Add" onclick="doSave();" />
             E-Mail: <input id="email" type="text"></input><br/>
+
             <button onClick="doSave( )">Add</button>
+
 
         </div>
 
         </div>
        <hr>
+
    </body>
        <!-- table for showing existing items -->
+
        <table id="maintable" cellspacing=1 cellpadding=1 border=1 cols=4>
+
        </table>
+
 
</html>
 
</html>
 
 
</code>
 
</code>
  
==Source file: phonebook.js==
+
==Source: phonebook.js==
  
 
<code javascript>
 
<code javascript>
 
// contacts system service object
 
// contacts system service object
var phonebook;
+
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
 
  * Setting default data on application load
 
  */
 
  */
 
function init() {
 
function init() {
     // Create main menu items
+
     // getting contacts system service object
    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 {
 
     try {
 
         phonebook = device.getServiceObject("Service.Contact", "IDataSource");
 
         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) {
 
     } catch(err) {
         alert( "error receiving contacts databases" );
+
         alert( "No Contact service available" );
 
         return;
 
         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 adds an item to the default phonebook
*/
+
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() {
 
function doSave() {
     // Criteria object for making a GetList call to receive contacts  
+
     // Criteria object for adding contacts
    // information
+
 
     var criteria = new Object();
 
     var criteria = new Object();
 
     criteria.Type = 'Contact';
 
     criteria.Type = 'Contact';
 
     criteria.Data = new Object();
 
     criteria.Data = new Object();
 +
 
     // demanding first name, but that is not essential
 
     // demanding first name, but that is not essential
 
     if(document.getElementById('firstname').value == "") {
 
     if(document.getElementById('firstname').value == "") {
Line 220: Line 92:
 
         return;
 
         return;
 
     }
 
     }
    criteria.Data.DBUri = list.options[list.selectedIndex].text;
+
 
 
     // adding First Name parameter to new item
 
     // adding First Name parameter to new item
 
     criteria.Data.FirstName = new Object();
 
     criteria.Data.FirstName = new Object();
Line 241: Line 113:
 
             document.getElementById('email').value;
 
             document.getElementById('email').value;
 
     }
 
     }
     // making a request to service to receive a list of suitable items
+
      
     var contacts = 0;
+
     var result = 0;
 
     try {
 
     try {
         contacts = phonebook.IDataSource.Add(criteria);
+
         result = phonebook.IDataSource.Add(criteria);
 
     } catch(err) {
 
     } catch(err) {
 
         alert( "error saving data" );
 
         alert( "error saving data" );
 
         return;
 
         return;
 
     }
 
     }
    // looking for error code returned by GetList
+
     if(result.ErrorCode != 0) {
     if(contacts.ErrorCode != 0) {
+
         alert(result.ErrorMessage);
        // show alert
+
         alert(contacts.ErrorMessage);
+
 
     }
 
     }
    // updating information in table to show that new contact exists
 
    doShow();
 
 
}
 
}
 
 
</code>
 
</code>
 
  
 
==Postconditions==
 
==Postconditions==
* Upon loading shows a table with all contacts and select list with available databases.
+
 
* Typing some text in input field and pressing "Show" button will update information in table showing only items that are returned by standard search (that have common letters in first name or last name).
+
* Filling in the fields and selecting "Add" will add a new item to the default phone book.
* Filling fields and pressing Add button will add new item to contacts and refresh information in table, showing the contacts satisfying the FirstName string.
+
  
 
==Supplementary material==
 
==Supplementary material==
You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Adding_contact_in_WRT.zip|Adding_contact_in_WRT.zip]]
+
 
 +
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Adding contact in WRT.zip]].
  
 
==See also==
 
==See also==
*[[Retrieving contacts info in WRT]]
 
  
[[Category:Web Runtime (WRT)]][[Category:Code Examples]]
+
* [[Retrieving contacts info in Symbian Web Runtime]]
 +
* [[Removing contacts in Symbian Web Runtime]]

Latest revision as of 09:59, 5 October 2012

This code example shows how to add contacts to the device's default phone book in Symbian Web Runtime.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
S60 5th Edition
Article
Keywords: device.getServiceObject(), Service.Contact.Add()
Created: MiGryz (10 Dec 2008)
Last edited: hamishwillee (05 Oct 2012)

Contents

[edit] Overview

The example uses the Contact Platform Service, introduced in S60 5th Edition.

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

After setting the correct values for the criteria object (criteria.Type and criteria.Data), the IDataSource.Add(criteria) method is used to add the new contact.

[edit] Source: 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="phonebook.js" />
<title></title>
</head>
<body onload="init()">
<div id="inputdata">
First Name: <input id="firstname" type="text"></input><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 />
<input type="button" value="Add" onclick="doSave();" />
</div>
</body>
</html>

[edit] Source: phonebook.js

// contacts system service object
var phonebook;
 
/**
* Setting default data on application load
*/

function init() {
// getting contacts system service object
try {
phonebook = device.getServiceObject("Service.Contact", "IDataSource");
} catch(err) {
alert( "No Contact service available" );
return;
}
}
 
/**
* function adds an item to the default phonebook
*/

function doSave() {
// Criteria object for adding contacts
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;
}
 
// 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;
}
 
var result = 0;
try {
result = phonebook.IDataSource.Add(criteria);
} catch(err) {
alert( "error saving data" );
return;
}
if(result.ErrorCode != 0) {
alert(result.ErrorMessage);
}
}

[edit] Postconditions

  • Filling in the fields and selecting "Add" will add a new item to the default phone book.

[edit] Supplementary material

You can view the source file and executable application in the attached ZIP archive. The archive is available for download at Media:Adding contact in WRT.zip.

[edit] See also

This page was last modified on 5 October 2012, at 09:59.
137 page views in the last 30 days.