×
Namespaces

Variants
Actions
(Difference between revisions)

Exporting and importing calendar events in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
copyeditor (Talk | contribs)
m (language check)
Line 17: Line 17:
 
This code snippet shows how to export and import calendar events  using Calendar Platform Service for S60 Web Runtime introduced in S60 5th Edition.
 
This code snippet shows how to export and import calendar events  using Calendar Platform Service for S60 Web Runtime introduced in S60 5th Edition.
  
To obtain access to service object for the Calendar Service API method
+
To obtain access to the service object for the Calendar Service API, the
<tt>getServiceObject("Service.Calendar", "IDataSource")</tt> is used.
+
<tt>getServiceObject("Service.Calendar", "IDataSource")</tt> method is used.
  
After setting correct values for calendar type <tt>criteria.Type</tt> and filter <tt>criteria.Filter</tt> method <tt>IDataSource.GetList(criteria)</tt> is used for getting list of calendars.
+
After setting the correct values for the calendar type <tt>criteria.Type</tt> and filter <tt>criteria.Filter</tt>, method <tt>IDataSource.GetList(criteria)</tt> is used for getting a list of calendars.
After choosing the calendar <tt>filter.Type</tt> property used to setup type of events to get and <tt>criteria1.Filter</tt> used to setup filter.
+
 
<tt>IDataSource.GetList(criteria)</tt> is used to get list of events for selected calendar.
+
After choosing the calendar, the  <tt>filter.Type</tt> property is used to set up the type of events to get, and <tt>criteria1.Filter</tt> to set up filter.
 +
<tt>IDataSource.GetList(criteria)</tt> is used to get a list of events for the selected calendar.
 
The "Export" button handler uses method <tt>IDataSource.Export(criteria, callback)</tt> from Calendar Service API to export selected calendar events to file.
 
The "Export" button handler uses method <tt>IDataSource.Export(criteria, callback)</tt> from Calendar Service API to export selected calendar events to file.
 
The "Import" button handler uses method <tt>IDataSource.Import(criteria, callback)</tt> from Calendar Service API to import calendar events from file.
 
The "Import" button handler uses method <tt>IDataSource.Import(criteria, callback)</tt> from Calendar Service API to import calendar events from file.
The <tt>criteria</tt> parameter is used for specifying this parameters:
+
 
  * List of events to be exported to file (for <tt>IDataSource.Export()</tt> method only);
+
The <tt>criteria</tt> parameter is used for specifying this parameter:
  * File name and format;
+
  * List of events to be exported to file (for <tt>IDataSource.Export()</tt> method only)
  * Calendar name which the entries to be imported into (for <tt>IDataSource.Import()</tt> method only).
+
  * File name and format
Methods are used for exporting and importing of calendar entries are both asynchronous and parameter <tt>callback</tt> is used for specifying callback function for this methods.
+
  * Calendar name for the entries to be imported into (for <tt>IDataSource.Import()</tt> method only)
The callback function is used has following form:
+
 
 +
Methods that are used for exporting and importing of calendar entries are both asynchronous and parameter <tt>callback</tt> is used for specifying the callback function for this methods.
 +
The callback function used has the following form:
 
<tt>function callback(transId, eventCode, result)<tt>, where:
 
<tt>function callback(transId, eventCode, result)<tt>, where:
<tt>transId</tt> - number representing the transaction that calls the callback handler;
+
<tt>transId</tt> - number representing the transaction that calls the callback handler
<tt>eventCode</tt> - number representing the callback return status;
+
<tt>eventCode</tt> - number representing the callback return status
<tt>result</tt> - object for holding the callback return value.
+
<tt>result</tt> - object for holding the callback return value
To interrupt evaluating of asynchronous exporting or importing process method <tt>IDataSource.Cancel(criteria)</tt> is used.
+
To interrupt evaluating of asynchronous exporting or importing process, method <tt>IDataSource.Cancel(criteria)</tt> is used.
It's parameter <tt>criteria</tt> specifies a transaction id of asynchronous method which to be canceled.  
+
Its parameter <tt>criteria</tt> specifies a transaction ID of the asynchronous method to be canceled.  
  
 
==Source file: index.html==
 
==Source file: index.html==
Line 485: Line 488:
  
 
==Postconditions==
 
==Postconditions==
* After starting snippet loads the calendar list. User can choose calendar in list and check which events to show(meetings, todo, etc.).
+
* After starting, the  snippet loads the calendar list. User can choose a calendar in the list and check which events to show (meetings, todo, and so on).
* The name of file where events will be exported or from which events will be imported are specified in "File" input area.
+
* The name of the file where the events will be exported or from which events will be imported are specified in the "File" input area.
 
* User can specify file name only in C:\Data\Other directory.
 
* User can specify file name only in C:\Data\Other directory.
* Button "Export" exports the selected events to file specified in "File" input area.
+
* Button "Export" exports the selected events to a file specified in the "File" input area.
* Button "Import" imports calendar entries from specified file to calendar which is selected.
+
* Button "Import" imports calendar entries from a specified file to the selected calendar.
* Button "Cancel" is used to cancel current asynchronous process (importing or exporting).
+
* Button "Cancel" is used to cancel the current asynchronous process (importing or exporting).
  
  
 
==Supplementary material==
 
==Supplementary material==
You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Exporting_and_importing_calendar_events_in_WRT.zip|Exporting_and_importing_calendar_events_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:Exporting_and_importing_calendar_events_in_WRT.zip|Exporting_and_importing_calendar_events_in_WRT.zip]]
  
 
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:PIM]][[Category:S60 5th Edition]]
 
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:PIM]][[Category:S60 5th Edition]]

Revision as of 15:43, 17 December 2008


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


Overview

This code snippet shows how to export and import calendar events using Calendar Platform Service for S60 Web Runtime introduced in S60 5th Edition.

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

After setting the correct values for the calendar type criteria.Type and filter criteria.Filter, method IDataSource.GetList(criteria) is used for getting a list of calendars.

After choosing the calendar, the filter.Type property is used to set up the type of events to get, and criteria1.Filter to set up filter. IDataSource.GetList(criteria) is used to get a list of events for the selected calendar. The "Export" button handler uses method IDataSource.Export(criteria, callback) from Calendar Service API to export selected calendar events to file. The "Import" button handler uses method IDataSource.Import(criteria, callback) from Calendar Service API to import calendar events from file.

The criteria parameter is used for specifying this parameter:

* List of events to be exported to file (for IDataSource.Export() method only)
* File name and format
* Calendar name for the entries to be imported into (for IDataSource.Import() method only)

Methods that are used for exporting and importing of calendar entries are both asynchronous and parameter callback is used for specifying the callback function for this methods. The callback function used has the following form: function callback(transId, eventCode, result)<tt>, where: <tt>transId - number representing the transaction that calls the callback handler eventCode - number representing the callback return status result - object for holding the callback return value To interrupt evaluating of asynchronous exporting or importing process, method IDataSource.Cancel(criteria) is used. Its parameter criteria specifies a transaction ID of the asynchronous method to be canceled.

Source file: index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
Export And Import Calendar Events
</title>
<style type="text/css">
@import "ExportImport.css";
</style>
<script type="text/javascript" src="ExportImport.js"></script>
</head>
<body onLoad="initialize();">
<h2>Calendar Entry Export And Import</h2>
<div>
Calendars:<br>
<select size="2" id="calendarList" onClick="showEvents();">
<option> </option>
</select><br>
<input type="radio" name="eventType" id="meeting" checked
onClick="showEvents();">
Meetings
<input type="radio" name="eventType" id="anniversary"
onClick="showEvents();">
Anniversary<br>
<input type="radio" name="eventType" id="dayEvent"
onClick="showEvents();">
DayEvent
<input type="radio" name="eventType" id="reminder"
onClick="showEvents();">
Reminder<br>
<input type="radio" name="eventType" id="toDo"
onClick="showEvents();">
ToDo<br>
Entries:<br>
<select size="5" id="eventList">
<option> </option>
</select><br>
<div class="left">
File<br>
C:\Data\Others\<input type="text" size="64" id="file">
</div>
File Type
<input type="radio" name="fileType" id="iCal" checked>ICal
<input type="radio" name="fileType" id="vCal">VCal
<br>
<input type="button" id="export"
onClick="exportEntry();" value="Export"><br>
<input type="button" id="import"
onClick="importEntry();" value="Import"><br>
<input id="cancelButton"type="button" value="Cancel"
onClick="cancelAction();">
<br>
<p id="state">
</p>
<br>
<input type="hidden" id="transactionID" value="0">
</div>
</body>
</html>

Source file: ExportImport.css

h2 {
font-weight:bold;
text-align:center;
}
input {
font-weight:bold;
}
 
.left {
text-align: left;
}

Source file: ExportImport.js

// Object is available through a Calendar Service API.
// Declare service object that is used to access the calendar services.
var calendarServiceObject = null;
// Global variable that captures current export or import transaction id value
var currentTransactionId = 0;
 
/**
* Java Script function uses object which is available
* through the Calendar Service API.
* Function creates options in html <select> which are used to show available
* calendars.
* @param iterator List of calendars
*/

function showCalendars(iterator) {
var calendarList = document.getElementById("calendarList");
// Iterator is the list of the calendars
// Reset iterator to set pointer to the first element
iterator.reset();
// Clear current calendar list
while (calendarList.length != 0) {
calendarList.remove(0);
}
var item;
// Fetch values from iterator and add it to calendarList.options.
// Value of each option contains the calendar name.
while ((item = iterator.getNext()) != undefined) {
var node = document.createElement("option");
node.value = item;
node.appendChild(document.createTextNode(item));
calendarList.appendChild(node);
}
if (calendarList.length > 0) {
calendarList.options[0].selected = true;
}
}
/**
* Java Script function uses object is available through a Calendar Service API.
* Function creates options in html <select>
* which represents calendar entries.
* @param iterator List of entries from the selected calendar.
*/

function displayEntries(iterator) {
var eventList = document.getElementById("eventList");
// Clean event list
while (eventList.length != 0) {
eventList.remove(0);
}
// Set event list is not visible.
document.getElementById("eventList").style.display = "none";
//Set iterator to the first element
iterator.reset();
var item;
// Fill the list of calendar events with events from the current calendar
while ((item = iterator.getNext()) != undefined) {
var msg = "";
msg = msg + "Type: " + item["Type"];
msg = msg + "Summary: " + item["Summary"];
var node = document.createElement("option");
node.value = item["LocalId"];
node.appendChild(document.createTextNode(msg));
eventList.appendChild(node);
}
// Set event list is visible
document.getElementById("eventList").style.display = "block";
if (eventList.length > 0) {
eventList.options[0].selected = true;
}
}
/**
* Java Script function uses object available through Calendar Service API.
* Shows all events by type is checked in "Type" checkbox.
*/

function showEvents() {
var calendarList = document.getElementById("calendarList");
// Obtain type of calendar event which to be used
for (var i = 0; i < calendarList.length; i++) {
if (calendarList.options[i].selected) {
var filter = new Object();
var criteria = new Object();
if (document.getElementById("meeting").checked) {
filter.Type = "Meeting";
criteria.Filter = filter;
}
if (document.getElementById("anniversary").checked) {
filter.Type = "Anniversary";
criteria.Filter = filter;
}
if (document.getElementById("dayEvent").checked) {
filter.Type = "DayEvent";
criteria.Filter = filter;
}
if (document.getElementById("reminder").checked) {
filter.Type = "Reminder";
criteria.Filter = filter;
}
if (document.getElementById("toDo").checked) {
filter.Type = "ToDo";
criteria.Filter = filter;
}
 
criteria.Type = "CalendarEntry";
try {
// Get list of calendar events
var result =
calendarServiceObject.IDataSource.GetList(criteria);
if (result.ErrorCode == 0) {
// Shows the list of events which have obtained
displayEntries(result.ReturnValue);
} else {
alert("Exception in getting calendar meetings: " +
result.ErrorMessage);
}
} catch (exception) {
alert("Exception in getting calendar meetings: " +
exception);
}
return;
}
}
}
/**
* Java Script function.
* Closes window.
*/

function rightSoftkeyFunction() {
window.close();
}
 
/**
* Java Script function uses object available through Calendar Service API.
* Initializes main controls and objects.
*/

function initialize() {
// Block "Cancel" button
blockCancel(true);
// Unblock "Export" and "Import" buttons
blockInputs(false);
// Initialize right softkey
try {
// Set label and handler for right softkey
menu.setRightSoftkeyLabel("Exit", rightSoftkeyFunction);
} catch (e) {
}
 
try {
// Get service object for calendar
calendarServiceObject = device.getServiceObject("Service.Calendar",
"IDataSource");
} catch (exception) {
alert("Initialize error: " + exception);
}
// Define criteria for retriving list of calendars
var filter = new Object();
filter.DefaultCalendar = false;
var criteria = new Object();
criteria.Type = "Calendar";
criteria.Filter = filter;
// Get the list of calendars
result = calendarServiceObject.IDataSource.GetList(criteria);
if (result.ErrorCode == 0) {
// Show the list of calendars
showCalendars(result.ReturnValue);
showEvents();
} else {
alert("Error in getting calendars list: " +
result.ErrorMessage + "!");
}
}
/**
* Java Script function uses object available through Calendar Service API.
* Imports all entries from specified file to the calendar.
*/

function importEntry() {
// Get access to list of available calendars
var calendarList = document.getElementById("calendarList");
var calendarName = "";
//Get the name of the selected calendar.
for (var i = 0; i < calendarList.length; i++) {
if (calendarList.options[i].selected) {
calendarName = calendarList.options[i].value;
}
}
 
var criteria = new Object();
//Set the type of the entry
criteria.Type = "CalendarEntry";
criteria.Data = new Object();
criteria.Data.CalendarName = calendarName;
// Name of file which calendar events must be imported from.
var fileName = "C:\\Data\\" +
document.getElementById('file').value;
 
criteria.Data.FileName = fileName;
// Get format of the file.
if (document.getElementById('iCal').checked) {
criteria.Data.Format = "ICal";
} else {
criteria.Data.Format = "VCal";
}
 
try {
// Import the data.
var result = calendarServiceObject.IDataSource.Import(criteria,
importCallback);
// Store current transaction id to global variable
currentTransactionId = result.TransactionID;
document.getElementById('state').innerHTML = "Importing...";
// Set state of all buttons.
blockCancel(false);
blockInputs(true);
} catch (e) {
alert ("Importing error: " + e);
}
}
/**
* Java Script function, uses object available through a Service API.
* Exports selected entries from calendar to file.
*/

function exportEntry() {
var calendarList = document.getElementById("calendarList");
var calName = "";
// Get name of the selected calendar.
for (var i = 0; i < calendarList.length; i++) {
if (calendarList.options[i].selected) {
calName = calendarList.options[i].value;
}
}
// Get name of the selected event in selected calendar.
var eventList = document.getElementById("eventList");
// Array containing the calendar entries
var entryList = new Array();
for (var i = 0; i < eventList.length; i++) {
if (eventList.options[i].selected) {
entryList[i] = eventList.options[i].value;
break;
}
}
 
// Criteria indicating data to be exported and the destination file.
var criteria = new Object();
criteria.Type = "CalendarEntry";
criteria.Data = new Object();
criteria.Data.CalendarName = calName;
criteria.Data.LocalIdList = entryList;
// Destination file name
var fileLocalName = document.getElementById('file').value;
// Check the file name correctness
var regPattern = new RegExp("^[a-zA-Z]+\.?[a-zA-Z]+$");
if (!(regPattern.test(fileLocalName))) {
alert("Incorrect file name: " + fileLocalName);
return;
}
// Create full destination file name
var fileName = "C:\\Data\\" + fileLocalName;
criteria.Data.FileName = fileName;
// Set format of destination file.
if (document.getElementById('iCal').checked) {
criteria.Data.Format = "ICal";
} else {
criteria.Data.Format = "VCal";
}
 
try {
// Export the entries which are selected.
alert("Export begin");
var result = calendarServiceObject.IDataSource.Export(criteria,
exportCallback);
currentTransactionId = result.TransactionID;
document.getElementById('state').innerHTML = "Exporting...";
blockCancel(false);
blockInputs(true);
} catch (e) {
alert ("Exporting error: " + e);
}
}
/**
* Function cancels current action (exporting or importing).
*/

function cancelAction() {
// Criteria determining transaction which to be canceled.
var criteria = new Object();
// Get transaction id for transaction which to be canceled.
var transactionId = currentTransactionId;
if (transactionId == undefined) {
return;
}
criteria.TransactionID = transactionId;
// Cancel current action.
var result = calendarServiceObject.Cancel(criteria);
if (result.ErrorCode !== 0) {
alert("Cancel error: " + result.ErrorMessage);
}
// Set buttons state.
blockCancel(true);
blockInputs(false);
document.getElementById('state').innerHTML = "";
showEvents();
}
/**
* Callback function for import asynchronous call.
* @param transId - number representing the transaction
* that called the callback handler.
* @param eventCode - number representing the callback return status.
* @param result - object for holding the callback return value
*/

function importCallback(transId, eventCode, result) {
// Check result of importing
if (result.ErrorCode != 0) {
alert("Error importing: "+result.ErrorMessage);
} else {
alert("Data was imported successfully!");
}
 
// Set buttons state
blockCancel(true);
blockInputs(false);
document.getElementById('state').innerHTML = "";
document.getElementById('transactionID').value = "0";
// Reload the event list
showEvents();
}
/**
* Callback function for export asynchronous call.
* @param transId - number representing the transaction
* that called the callback handler.
* @param eventCode - number representing the callback return status.
* @param result - object for holding the callback return value
*/

function exportCallback(transId, eventCode, result) {
// Check result of exporting
if (result.ErrorCode != 0) {
alert("Error exporting: "+result.ErrorMessage);
} else {
alert("Success exporting!");
}
// Set state of all buttons
blockCancel(true);
blockInputs(false);
document.getElementById('state').innerHTML = "";
document.getElementById('transactionID').value = "0";
//Reload the event list.
showEvents();
}
/**
* Function sets state of "Export" and "Import" buttons
* according to parameter's value.
* @param isBlock determines the required state.
*/

function blockInputs(isBlock) {
document.getElementById('import').disabled = isBlock;
document.getElementById('export').disabled = isBlock;
}
/**
* Function sets state of button "Cancel".
* @param isBlock determines the required state.
*/

function blockCancel(isBlock) {
document.getElementById('cancelButton').disabled = isBlock;
}


Postconditions

  • After starting, the snippet loads the calendar list. User can choose a calendar in the list and check which events to show (meetings, todo, and so on).
  • The name of the file where the events will be exported or from which events will be imported are specified in the "File" input area.
  • User can specify file name only in C:\Data\Other directory.
  • Button "Export" exports the selected events to a file specified in the "File" input area.
  • Button "Import" imports calendar entries from a specified file to the selected calendar.
  • Button "Cancel" is used to cancel the current asynchronous process (importing or exporting).


Supplementary material

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

141 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×