×
Namespaces

Variants
Actions
(Difference between revisions)

Exporting and importing calendar events in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
copyeditor (Talk | contribs)
m (language check)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Symbian Web Runtime]][[Category:Code Examples]][[Category:PIM]][[Category:S60 5th Edition]]
__NOEDITSECTION__
+
{{Abstract|This code example demonstrates how to export and import calendar entries in Symbian Web Runtime, using the Calendar Platform Service (introduced in S60 5th Edition).}}
{{CodeSnippet
+
 
|id= 
+
{{ArticleMetaData <!-- v1.2 -->
|platform=S60 5th Edition
+
|sourcecode= [[Media:Listing calendars and events in WRT.zip]]
|devices=Nokia 5800 XpressMusic
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|category=Web Runtime (WRT)
+
|devices= Nokia 5800 XpressMusic
|subcategory=Calendar
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|creationdate=November 24, 2008
+
|platform= S60 5th Edition
|keywords=widget.setNavigationEnabled(), device.getServiceObject(), Service.IDataSource.GetList(),
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
Service.IDataSource.Import(), Service.IDataSource.Export(), Service.IDataSource.Cancel()
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= device.getServiceObject(), Service.Calendar.Import(), Service.Calendar.Export()
 +
|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:Dekuykin]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001237
 
}}
 
}}
 
  
 
==Overview==
 
==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.
+
The {{Icode|device.getServiceObject("Service.Calendar", "IDataSource")}} method is used to obtain access to the service object for the Calendar Service API.
  
To obtain access to the service object for the Calendar Service API, the  
+
The "Export" button handler uses the {{Icode|IDataSource.Export(criteria, callback)}} method from the Calendar Service API to export the selected calendar entries to the file.
<tt>getServiceObject("Service.Calendar", "IDataSource")</tt> method is used.
+
  
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.
+
The "Import" button handler uses the {{Icode|IDataSource.Import(criteria, callback)}} method from the Calendar Service API to import calendar events from the file.
  
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.
+
The methods used for exporting and importing calendar entries are asynchronous.
<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 "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 parameter:
+
To interrupt the execution of the asynchronous exporting or importing process, the method {{Icode|IDataSource.Cancel(criteria)}} is used.
* List of events to be exported to file (for <tt>IDataSource.Export()</tt> method only)
+
Its {{Icode|criteria}} parameter specifies the transaction ID of the asynchronous method to be canceled.
* File name and format
+
* Calendar name for the entries to be imported into (for <tt>IDataSource.Import()</tt> method only)
+
  
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.
+
==Source: Relevant HTML components==
The callback function used has the following form:
+
<tt>function callback(transId, eventCode, result)<tt>, where:
+
<tt>transId</tt> - number representing the transaction that calls the callback handler
+
<tt>eventCode</tt> - number representing the callback return status
+
<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.
+
Its parameter <tt>criteria</tt> specifies a transaction ID of the asynchronous method to be canceled.
+
 
+
==Source file: index.html==
+
  
 
<code xml>
 
<code xml>
<!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
 
<input type="radio" name="eventType" id="meeting" checked
     onClick="showEvents();">
+
     onclick="showEntries();" />
    Meetings
+
<label for="meeting">Meetings</label><br />
 +
 
 
<input type="radio" name="eventType" id="anniversary"
 
<input type="radio" name="eventType" id="anniversary"
     onClick="showEvents();">
+
     onclick="showEntries();" />
    Anniversary<br>
+
<label for="anniversary">Anniversaries</label><br />
<input type="radio" name="eventType" id="dayEvent"  
+
 
     onClick="showEvents();">
+
<input type="radio" name="eventType" id="dayEvent"
    DayEvent
+
     onclick="showEntries();" />
 +
<label for="dayEvent">Day Events</label><br />
 +
 
 
<input type="radio" name="eventType" id="reminder"
 
<input type="radio" name="eventType" id="reminder"
     onClick="showEvents();">
+
     onclick="showEntries();" />
    Reminder<br>
+
<label for="reminder">Reminders</label><br />
 +
 
 
<input type="radio" name="eventType" id="toDo"
 
<input type="radio" name="eventType" id="toDo"
     onClick="showEvents();">
+
     onclick="showEntries();" />
    ToDo<br>
+
<label for="toDo">ToDos</label><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>
+
</code>
+
  
==Source file: ExportImport.css==
+
<label for="entriesList">Entries:</label><br />
 +
<select size="2" id="entriesList"></select><br />
  
<code css>
+
<label for="fileSelection">File:</label><br />
h2 {
+
<p id="fileSelection">
     font-weight:bold;
+
     C:\Data\Others\<input type="text" size="20" id="file" />
    text-align:center;
+
</p>
}
+
input {
+
    font-weight:bold;
+
}
+
  
.left {
+
<p>File Type:</p>
    text-align: left;
+
<input type="radio" name="fileType" id="iCal" checked />
}
+
<label for="iCal">ICal</label><br />
 +
<input type="radio" name="fileType" id="vCal">
 +
<label for="vCal">VCal</label><br />
 +
 
 +
<input type="button" id="export" onclick="exportEntry();" value="Export">
 +
<input type="button" id="import" onclick="importEntry();" value="Import">
 +
<input type="button" id="cancel" onclick="cancelAction();" value="Cancel">
 +
<br />
 +
<p id="state"></p><br />
 
</code>
 
</code>
  
==Source file: ExportImport.js==
+
==Source: JavaScript file==
  
 
<code javascript>
 
<code javascript>
// Object is available through a Calendar Service API.
+
var serviceObj = null;
// Declare service object that is used to access the calendar services.
+
 
var calendarServiceObject = null;
+
// Contains current export or import transaction id value  
// Global variable that captures current export or import transaction id value  
+
 
var currentTransactionId = 0;
 
var currentTransactionId = 0;
  
/**
+
window.onload = init;
* Java Script function uses object which is available
+
 
* through the Calendar Service API.
+
// Initializes the widget
* Function creates options in html <select> which are used to show available
+
function init() {
* calendars.
+
     // Obtain the service object
* @param iterator List of calendars
+
     try {
*/
+
         serviceObj = device.getServiceObject("Service.Calendar",
function showCalendars(iterator) {
+
            "IDataSource");
    var calendarList = document.getElementById("calendarList");
+
    } catch (ex) {
    // Iterator is the list of the calendars
+
         alert("Service object cannot be found.");
    // Reset iterator to set pointer to the first element
+
         return;
    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";
+
function showEntries() {
            try {
+
    // Showing entries is omitted here for brevity. Refer to the See also
                // Get list of calendar events
+
    // section of the code snippet for more information.
                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.
+
* Imports all entries from the specified file to the calendar.
* 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() {
 
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();
 
     var criteria = new Object();
    //Set the type of the entry
 
 
     criteria.Type = "CalendarEntry";
 
     criteria.Type = "CalendarEntry";
 
     criteria.Data = new Object();
 
     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;
 
  
 +
    // Name of the file from which calendar events will be imported
 +
    var fileName = "C:\\Data\\Others" + document.getElementById("file").value;
 
     criteria.Data.FileName = fileName;
 
     criteria.Data.FileName = fileName;
 
     // Get format of the file.
 
     // Get format of the file.
     if (document.getElementById('iCal').checked) {
+
     if (document.getElementById("iCal").checked) {
 
         criteria.Data.Format = "ICal";
 
         criteria.Data.Format = "ICal";
 
     } else {
 
     } else {
 
         criteria.Data.Format = "VCal";
 
         criteria.Data.Format = "VCal";
 
     }
 
     }
 
+
 +
    // Import the data
 
     try {
 
     try {
        // Import the data.
+
         var result = serviceObj.IDataSource.Import(criteria, importCallback);
         var result = calendarServiceObject.IDataSource.Import(criteria,
+
            importCallback);
+
 
         // Store current transaction id to global variable
 
         // Store current transaction id to global variable
 
         currentTransactionId = result.TransactionID;
 
         currentTransactionId = result.TransactionID;
         document.getElementById('state').innerHTML = "Importing...";
+
         document.getElementById("state").innerHTML = "Importing...";
        // Set state of all buttons.
+
     } catch (ex) {
        blockCancel(false);
+
         alert("Error in importing entries: " + ex);
        blockInputs(true);
+
     } catch (e) {
+
         alert ("Importing error: " + e);
+
 
     }
 
     }
 
}
 
}
 +
 
/**
 
/**
* Java Script function, uses object available through a Service API.
+
* Callback function for import asynchronous call.
* Exports selected entries from calendar to file.
+
* @param transId A number representing the transaction that called the
*/
+
* callback handler.
function exportEntry() {
+
* @param eventCode A number representing the callback return status.
    var calendarList = document.getElementById("calendarList");
+
* @param result An object for holding the callback return value
    var calName = "";
+
*/
     // Get name of the selected calendar.
+
function importCallback(transId, eventCode, result) {
     for (var i = 0; i < calendarList.length; i++) {
+
     // Check result of importing
         if (calendarList.options[i].selected) {
+
     if (result.ErrorCode != 0) {
            calName = calendarList.options[i].value;
+
         alert("Error in importing: " + result.ErrorMessage);
        }
+
    } else {
 +
        alert("Data was imported successfully!");
 
     }
 
     }
     // Get name of the selected event in selected calendar.
+
     var eventList = document.getElementById("eventList");
+
    document.getElementById("state").innerHTML = "";
    // Array containing the calendar entries
+
    showEntries();
 +
}
 +
 
 +
/**
 +
* Exports the selected entries from calendar to file.
 +
*/
 +
function exportEntry() {
 +
     // Get the name of the selected entry
 +
     var entriesList = document.getElementById("entriesList");
 
     var entryList = new Array();
 
     var entryList = new Array();
     for (var i = 0; i < eventList.length; i++) {
+
     for (var i = 0; i < entriesList.length; i++) {
         if (eventList.options[i].selected) {
+
         if (entriesList.options[i].selected) {
             entryList[i] = eventList.options[i].value;
+
             entryList[i] = entriesList.options[i].value;
 
             break;
 
             break;
 
         }
 
         }
 
     }
 
     }
 
+
     // Criteria indicating data to be exported and the destination file.
+
     // Criteria indicating the data to be exported and the destination file
 
     var criteria = new Object();
 
     var criteria = new Object();
 
     criteria.Type = "CalendarEntry";
 
     criteria.Type = "CalendarEntry";
 
     criteria.Data = new Object();
 
     criteria.Data = new Object();
    criteria.Data.CalendarName = calName;
 
 
     criteria.Data.LocalIdList = entryList;
 
     criteria.Data.LocalIdList = entryList;
 
     // Destination file name
 
     // Destination file name
     var fileLocalName = document.getElementById('file').value;
+
     var fileLocalName = document.getElementById("file").value;
 
     // Check the file name correctness
 
     // Check the file name correctness
 
     var regPattern = new RegExp("^[a-zA-Z]+\.?[a-zA-Z]+$");
 
     var regPattern = new RegExp("^[a-zA-Z]+\.?[a-zA-Z]+$");
Line 378: Line 188:
 
     }
 
     }
 
     // Create full destination file name
 
     // Create full destination file name
     var fileName = "C:\\Data\\" + fileLocalName;
+
     var fileName = "C:\\Data\\Others\\" + fileLocalName;
 
     criteria.Data.FileName = fileName;
 
     criteria.Data.FileName = fileName;
     // Set format of destination file.
+
     // Set the format of the destination file.
     if (document.getElementById('iCal').checked) {
+
     if (document.getElementById("iCal").checked) {
 
         criteria.Data.Format = "ICal";
 
         criteria.Data.Format = "ICal";
 
     } else {
 
     } else {
 
         criteria.Data.Format = "VCal";
 
         criteria.Data.Format = "VCal";
 
     }
 
     }
 
+
 
     try {
 
     try {
         // Export the entries which are selected.
+
         // Export the selected entries
         alert("Export begin");
+
         alert("Export begins");
         var result = calendarServiceObject.IDataSource.Export(criteria,
+
         var result = serviceObj.IDataSource.Export(criteria, exportCallback);
                exportCallback);
+
 
         currentTransactionId = result.TransactionID;
 
         currentTransactionId = result.TransactionID;
         document.getElementById('state').innerHTML = "Exporting...";
+
         document.getElementById("state").innerHTML = "Exporting...";
        blockCancel(false);
+
     } catch (ex) {
        blockInputs(true);
+
         alert ("Error in exporting entries: " + ex);
     } catch (e) {
+
         alert ("Exporting error: " + e);
+
 
     }
 
     }
 
}
 
}
 +
 
/**
 
/**
  * Function cancels current action (exporting or importing).
+
  * Callback function for export asynchronous call.
 +
* @param transId A number representing the transaction that called the
 +
* callback handler.
 +
* @param eventCode A number representing the callback return status.
 +
* @param result An object for holding the callback return value
 +
*/
 +
function exportCallback(transId, eventCode, result) {
 +
    // Check result of exporting
 +
    if (result.ErrorCode != 0) {
 +
        alert("Error in exporting: " + result.ErrorMessage);
 +
    } else {
 +
        alert("Data was exported successfully!");
 +
    }
 +
 
 +
    document.getElementById("state").innerHTML = "";
 +
    showEntries();
 +
}
 +
 
 +
/**
 +
* Cancels the current action (exporting or importing).
 
  */
 
  */
 
function cancelAction() {
 
function cancelAction() {
     // Criteria determining transaction which to be canceled.
+
     // Criteria determining the transaction which is to be canceled
 
     var criteria = new Object();
 
     var criteria = new Object();
     // Get transaction id for transaction which to be canceled.
+
     // Get transaction id for the transaction which is to be canceled
 
     var transactionId = currentTransactionId;
 
     var transactionId = currentTransactionId;
 
     if (transactionId == undefined) {
 
     if (transactionId == undefined) {
    return;
+
        return;
 
     }
 
     }
 
     criteria.TransactionID = transactionId;
 
     criteria.TransactionID = transactionId;
     // Cancel current action.
+
     // Cancel the current action
 
     var result = calendarServiceObject.Cancel(criteria);
 
     var result = calendarServiceObject.Cancel(criteria);
 
     if (result.ErrorCode !== 0) {
 
     if (result.ErrorCode !== 0) {
 
         alert("Cancel error: " + result.ErrorMessage);
 
         alert("Cancel error: " + result.ErrorMessage);
 
     }
 
     }
     // Set buttons state.
+
     showEntries();
    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;
+
 
}
 
}
 
</code>
 
</code>
 
  
 
==Postconditions==
 
==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).
 
  
 +
The selected calendar entries are exported into or imported from the file specified by the user.
  
 
==Supplementary material==
 
==Supplementary material==
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]]
+
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Listing calendars and events in WRT.zip]].
 +
 
 +
==See also==
 +
 
 +
[[Listing calendars in Symbian Web Runtime]]

Latest revision as of 09:22, 5 October 2012

This code example demonstrates how to export and import calendar entries in Symbian Web Runtime, using the Calendar Platform Service (introduced in S60 5th Edition).

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

Contents

[edit] Overview

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

The "Export" button handler uses the IDataSource.Export(criteria, callback) method from the Calendar Service API to export the selected calendar entries to the file.

The "Import" button handler uses the IDataSource.Import(criteria, callback) method from the Calendar Service API to import calendar events from the file.

The methods used for exporting and importing calendar entries are asynchronous.

To interrupt the execution of the asynchronous exporting or importing process, the method IDataSource.Cancel(criteria) is used. Its criteria parameter specifies the transaction ID of the asynchronous method to be canceled.

[edit] Source: Relevant HTML components

<input type="radio" name="eventType" id="meeting" checked
onclick="showEntries();" />
<label for="meeting">Meetings</label><br />
 
<input type="radio" name="eventType" id="anniversary"
onclick="showEntries();" />
<label for="anniversary">Anniversaries</label><br />
 
<input type="radio" name="eventType" id="dayEvent"
onclick="showEntries();" />
<label for="dayEvent">Day Events</label><br />
 
<input type="radio" name="eventType" id="reminder"
onclick="showEntries();" />
<label for="reminder">Reminders</label><br />
 
<input type="radio" name="eventType" id="toDo"
onclick="showEntries();" />
<label for="toDo">ToDos</label><br />
 
<label for="entriesList">Entries:</label><br />
<select size="2" id="entriesList"></select><br />
 
<label for="fileSelection">File:</label><br />
<p id="fileSelection">
C:\Data\Others\<input type="text" size="20" id="file" />
</p>
 
<p>File Type:</p>
<input type="radio" name="fileType" id="iCal" checked />
<label for="iCal">ICal</label><br />
<input type="radio" name="fileType" id="vCal">
<label for="vCal">VCal</label><br />
 
<input type="button" id="export" onclick="exportEntry();" value="Export">
<input type="button" id="import" onclick="importEntry();" value="Import">
<input type="button" id="cancel" onclick="cancelAction();" value="Cancel">
<br />
<p id="state"></p><br />

[edit] Source: JavaScript file

var serviceObj = null;
 
// Contains current export or import transaction id value
var currentTransactionId = 0;
 
window.onload = init;
 
// Initializes the widget
function init() {
// Obtain the service object
try {
serviceObj = device.getServiceObject("Service.Calendar",
"IDataSource");
} catch (ex) {
alert("Service object cannot be found.");
return;
}
}
 
function showEntries() {
// Showing entries is omitted here for brevity. Refer to the See also
// section of the code snippet for more information.
// ...
}
 
/**
* Imports all entries from the specified file to the calendar.
*/

function importEntry() {
var criteria = new Object();
criteria.Type = "CalendarEntry";
criteria.Data = new Object();
 
// Name of the file from which calendar events will be imported
var fileName = "C:\\Data\\Others" + 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";
}
 
// Import the data
try {
var result = serviceObj.IDataSource.Import(criteria, importCallback);
// Store current transaction id to global variable
currentTransactionId = result.TransactionID;
document.getElementById("state").innerHTML = "Importing...";
} catch (ex) {
alert("Error in importing entries: " + ex);
}
}
 
/**
* Callback function for import asynchronous call.
* @param transId A number representing the transaction that called the
* callback handler.
* @param eventCode A number representing the callback return status.
* @param result An object for holding the callback return value
*/

function importCallback(transId, eventCode, result) {
// Check result of importing
if (result.ErrorCode != 0) {
alert("Error in importing: " + result.ErrorMessage);
} else {
alert("Data was imported successfully!");
}
 
document.getElementById("state").innerHTML = "";
showEntries();
}
 
/**
* Exports the selected entries from calendar to file.
*/

function exportEntry() {
// Get the name of the selected entry
var entriesList = document.getElementById("entriesList");
var entryList = new Array();
for (var i = 0; i < entriesList.length; i++) {
if (entriesList.options[i].selected) {
entryList[i] = entriesList.options[i].value;
break;
}
}
 
// Criteria indicating the data to be exported and the destination file
var criteria = new Object();
criteria.Type = "CalendarEntry";
criteria.Data = new Object();
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\\Others\\" + fileLocalName;
criteria.Data.FileName = fileName;
// Set the format of the destination file.
if (document.getElementById("iCal").checked) {
criteria.Data.Format = "ICal";
} else {
criteria.Data.Format = "VCal";
}
 
try {
// Export the selected entries
alert("Export begins");
var result = serviceObj.IDataSource.Export(criteria, exportCallback);
currentTransactionId = result.TransactionID;
document.getElementById("state").innerHTML = "Exporting...";
} catch (ex) {
alert ("Error in exporting entries: " + ex);
}
}
 
/**
* Callback function for export asynchronous call.
* @param transId A number representing the transaction that called the
* callback handler.
* @param eventCode A number representing the callback return status.
* @param result An object for holding the callback return value
*/

function exportCallback(transId, eventCode, result) {
// Check result of exporting
if (result.ErrorCode != 0) {
alert("Error in exporting: " + result.ErrorMessage);
} else {
alert("Data was exported successfully!");
}
 
document.getElementById("state").innerHTML = "";
showEntries();
}
 
/**
* Cancels the current action (exporting or importing).
*/

function cancelAction() {
// Criteria determining the transaction which is to be canceled
var criteria = new Object();
// Get transaction id for the transaction which is to be canceled
var transactionId = currentTransactionId;
if (transactionId == undefined) {
return;
}
criteria.TransactionID = transactionId;
// Cancel the current action
var result = calendarServiceObject.Cancel(criteria);
if (result.ErrorCode !== 0) {
alert("Cancel error: " + result.ErrorMessage);
}
showEntries();
}

[edit] Postconditions

The selected calendar entries are exported into or imported from the file specified by the user.

[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:Listing calendars and events in WRT.zip.

[edit] See also

Listing calendars in Symbian Web Runtime

This page was last modified on 5 October 2012, at 09:22.
120 page views in the last 30 days.
×