|
1 |
|
2 <!DOCTYPE html |
|
3 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="copyright" content="(C) Copyright 2009"/><meta name="DC.rights.owner" content="(C) Copyright 2009"/><meta name="DC.Type" content="mobileconcept"/><meta name="DC.Title" content="Contacts Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-8722B5DB-5EA6-446D-8C1D-7A0B3D8781EF"/><meta name="DC.Relation" scheme="URI" content="GUID-7C69DDA4-16F1-4A8F-BDB2-4CB0015B4E81"/><meta name="DC.Relation" scheme="URI" content="GUID-65AAF569-D347-462B-B59A-9D7CA184AB9C"/><meta name="DC.Relation" scheme="URI" content="GUID-A5853293-7B83-4CCE-9C29-B2B8F0CD8A18"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-B2BCD2C0-DA4D-4830-9BF3-5DA50879A3A1"/><title>Contacts Service API example </title><script type="text/javascript"> |
|
5 function initPage() {} |
|
6 </script><link href="../PRODUCT_PLUGIN/book.css" rel="stylesheet" type="text/css"/><link href="css/s60/style.css" rel="stylesheet" type="text/css" media="all"/></head><body onload="initPage();"><div class="body"><div class="contentLeft prTxt"><h1 class="pageHeading" id="GUID-B2BCD2C0-DA4D-4830-9BF3-5DA50879A3A1">Contacts Service API example</h1><div> |
|
7 <p/> |
|
8 <p>This section presents the full source code of a working sample widget |
|
9 for the <a href="GUID-662F2E2E-9538-42E5-9B5A-5664F1C11A9E.html#GUID-662F2E2E-9538-42E5-9B5A-5664F1C11A9E">Contacts Service</a>. |
|
10 You can download the <code>wgz</code> package for this widget from |
|
11 section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p> |
|
12 <p>For general information about creating widgets, see section <a href="GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A.html#GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A">Widget component files</a>.</p> |
|
13 <p>For widget development and debugging purposes, this example writes its |
|
14 output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>. |
|
15 For instructions on how to enable logging in the Web browser for S60, see |
|
16 section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p> |
|
17 <div><h3>Info.plist</h3> |
|
18 <pre class="codeblock" id="GUID-F38B000F-B0A5-40F2-92F4-52B48314B147"><?xml version="1.0" encoding="UTF-8"?> |
|
19 <!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"> |
|
20 <plist version="1.0"> |
|
21 <dict> |
|
22 <key>DisplayName</key> |
|
23 <string>ContactSample</string> |
|
24 <key>Identifier</key> |
|
25 <string>com.nokia.widget.sapi.contact.sample</string> |
|
26 <key>Version</key> |
|
27 <string>1.0</string> |
|
28 <key>MainHTML</key> |
|
29 <string>contact-sample.html</string> |
|
30 </dict> |
|
31 </plist></pre> |
|
32 </div> |
|
33 <div><h3>contact-sample.html</h3> |
|
34 <pre class="codeblock" id="GUID-8E2E2DE4-7C29-4325-888F-5526F42B372C"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|
35 <html> |
|
36 <head> |
|
37 <script type="text/javascript" src="js/common.js"></script> |
|
38 <script type="text/javascript" src="js/contact-sample.js"></script> |
|
39 </head> |
|
40 <body id='docBody' bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;> |
|
41 <form name="frm"> |
|
42 <h3>Contact API Sample Widget</h3> |
|
43 Async<input type="checkbox" id="async_check" onClick="check_async()"> |
|
44 Cancel<input type="checkbox" id="cancel_check" onClick="check_cancel()"><br/> |
|
45 <hr> |
|
46 <input type="button" onclick="addContact('img1')" value="AddContact"><img id="img1" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
47 <input type="button" onclick="delContact('img2')" value="DeleteContact"><img id="img2" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
48 <input type="button" onclick="getContactList('img3')" value="GetContactList"><img id="img3" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
49 <input type="button" onclick="importContact('img4')" value="ImportContact"><img id="img4" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
50 <input type="button" onclick="exportContact('img5')" value="ExportContact"><img id="img5" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
51 <hr> |
|
52 <input type="button" onclick="addGroup('img6')" value="AddGroup"><img id="img6" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
53 <input type="button" onclick="delGroup('img7')" value="DeleteGroup"><img id="img7" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
54 <input type="button" onclick="getGroupList('img8')" value="GetGroupList"><img id="img8" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
55 <input type="button" onclick="organiseGroup('img9')" value="OrganiseGroup"><img id="img9" src="pic/blank.png" width="25" height="25" align="center"><br> |
|
56 <hr> |
|
57 <div class='contactsample' id='contactsample' bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto></div> |
|
58 </form> |
|
59 </body> |
|
60 </html> |
|
61 </pre> |
|
62 </div> |
|
63 <div><h3>common.js</h3> |
|
64 <pre class="codeblock" id="GUID-4A563AFF-3E2C-4289-B256-1AE1B30E0027">// common.js |
|
65 // |
|
66 // This file contains some utility functions |
|
67 |
|
68 // Check the error code and show the information to users |
|
69 function checkError(message, resultList, divId, imgId) |
|
70 { |
|
71 var errCode = resultList.ErrorCode; |
|
72 var msg = ""; |
|
73 |
|
74 if (errCode) { |
|
75 msg = message + "<BR>" + "Failed Error: " + errCode + "<BR>"; |
|
76 if(resultList.ErrorMessage != undefined) |
|
77 msg += "Error Message: " + resultList.ErrorMessage; |
|
78 showIMG(imgId,"no"); |
|
79 } else { |
|
80 showIMG(imgId,"yes"); |
|
81 } |
|
82 |
|
83 //print error message |
|
84 if(divId != null && divId != undefined) |
|
85 document.getElementById(divId).innerHTML = msg; |
|
86 console.info(msg); |
|
87 |
|
88 return errCode; |
|
89 } |
|
90 |
|
91 // Build the message by reading a iteratorable list in a recursive manner |
|
92 function showIterableList(iterator) |
|
93 { |
|
94 var msg = ""; |
|
95 try |
|
96 { |
|
97 iterator.reset(); |
|
98 var item; |
|
99 while (( item = iterator.getNext()) != undefined ){ |
|
100 msg += showObject( item ); |
|
101 } |
|
102 } |
|
103 catch(e) |
|
104 { |
|
105 alert('<showIterableList> ' + e); |
|
106 } |
|
107 return msg; |
|
108 } |
|
109 |
|
110 // Build the message by reading a JS object in a recursive manner |
|
111 function showObject( obj ) |
|
112 { |
|
113 var txt = ""; |
|
114 try { |
|
115 if ( typeof obj != 'object' ) |
|
116 return "" + obj + '<BR/>'; |
|
117 else { |
|
118 for(var key in obj) { |
|
119 txt += key + ":"; |
|
120 txt += showObject( obj[key] ); |
|
121 txt += '<BR/>'; |
|
122 } |
|
123 txt += '<BR/>'; |
|
124 } |
|
125 } |
|
126 catch (e) |
|
127 { |
|
128 alert("showObject: " + e); |
|
129 } |
|
130 return txt; |
|
131 } |
|
132 |
|
133 // Show the image to indicate the test result |
|
134 function showIMG(imgId, isOK) |
|
135 { |
|
136 if(imgId == null || imgId == undefined) |
|
137 return; |
|
138 |
|
139 if(isOK == "yes") |
|
140 document.getElementById(imgId).src = "pic/yes.png"; |
|
141 else if(isOK == "no") |
|
142 document.getElementById(imgId).src = "pic/no.png"; |
|
143 else |
|
144 document.getElementById(imgId).src = "pic/blank.png"; |
|
145 } |
|
146 |
|
147 // Show elements in object by using 'alert' |
|
148 function testObject(obj) |
|
149 { |
|
150 var msg = ""; |
|
151 for(var key in obj) { |
|
152 msg = msg + ":" + key + "=" + obj[key]; |
|
153 } |
|
154 alert(msg); |
|
155 } |
|
156 |
|
157 // Test whether the input is numeric |
|
158 function IsNumeric(sText) |
|
159 { |
|
160 var ValidChars = "0123456789."; |
|
161 var IsNumber=true; |
|
162 var Char; |
|
163 |
|
164 for (i = 0; i < sText.length && IsNumber == true; i++) |
|
165 { |
|
166 Char = sText.charAt(i); |
|
167 if (ValidChars.indexOf(Char) == -1) |
|
168 { |
|
169 IsNumber = false; |
|
170 } |
|
171 } |
|
172 return IsNumber; |
|
173 }</pre> |
|
174 </div> |
|
175 <div><h3>contact-sample.js</h3> |
|
176 <pre class="codeblock" id="GUID-B088017C-63A8-4E47-99F8-E2FE21112ED4">// contact-sample.js |
|
177 // |
|
178 // In this sample Contact and Group will be added, deleted, and listed from |
|
179 // default database; Contact will be imported and exported; Group will be |
|
180 // organised; Also, async operation will be canceled |
|
181 // |
|
182 |
|
183 //SAPI Error Codes |
|
184 // 0 - Success |
|
185 // 1000 - InvalidServiceArgument |
|
186 // 1001 - UnknownArgumentName |
|
187 // 1002 - BadArgumentType |
|
188 // 1003 - MissingArgument |
|
189 // 1004 - ServiceNotSupported |
|
190 // 1005 - ServiceInUse |
|
191 // 1006 - ServiceNotReady |
|
192 // 1007 - NoMemory |
|
193 // 1008 - HardwareNotAvailable |
|
194 // 1009 - ServerBusy |
|
195 // 1010 - EntryExists |
|
196 // 1011 - AccessDenied |
|
197 // 1012 - NotFound |
|
198 // 1013 - UnknownFormat |
|
199 // 1014 - GeneralError |
|
200 // 1015 - CancelSuccess |
|
201 // 1016 - ServiceTimedOut |
|
202 // 1017 - PathNotFound |
|
203 |
|
204 // declare the service object |
|
205 var so; |
|
206 |
|
207 // async Test Mode |
|
208 var asyncMode; |
|
209 |
|
210 // cancel Test Mode |
|
211 var cancelMode; |
|
212 |
|
213 // imgid for callback1 function |
|
214 var imgid_callback1; |
|
215 |
|
216 // imgid for callback2 function |
|
217 var imgid_callback2; |
|
218 |
|
219 // imgid for callback3 function |
|
220 var imgid_callback3; |
|
221 |
|
222 // imgid for callback4 function |
|
223 var imgid_callback4; |
|
224 |
|
225 // imgid for callback5 function |
|
226 var imgid_callback5; |
|
227 |
|
228 // imgid for callback6 function |
|
229 var imgid_callback6; |
|
230 |
|
231 // imgid for callback7 function |
|
232 var imgid_callback7; |
|
233 |
|
234 // id of the div used to display information |
|
235 const DIV_ID = 'contactsample'; |
|
236 |
|
237 // Called from onload() |
|
238 function setup() |
|
239 { |
|
240 try { |
|
241 so = device.getServiceObject("Service.Contact", "IDataSource"); |
|
242 console.info("setup: so: %s", so); |
|
243 } |
|
244 catch (e) { |
|
245 alert('<setup> ' +e); |
|
246 } |
|
247 } |
|
248 |
|
249 // Async checkbox handler |
|
250 function check_async() { |
|
251 var asyncChk = document.getElementById("async_check"); |
|
252 asyncMode = asyncChk.checked; |
|
253 if (!asyncMode) { |
|
254 var cancelChk = document.getElementById("cancel_check"); |
|
255 cancelChk.checked = false; |
|
256 cancelChk = false; |
|
257 } |
|
258 } |
|
259 |
|
260 // cancel checkbox handler |
|
261 function check_cancel() { |
|
262 var cancelChk = document.getElementById("cancel_check"); |
|
263 cancelMode = cancelChk.checked; |
|
264 |
|
265 // if cancel checkbox is selected, the async checkbox should be |
|
266 // selected automatically |
|
267 if(cancelMode) { |
|
268 var asyncChk = document.getElementById("async_check"); |
|
269 asyncChk.checked = true; |
|
270 asyncMode = true; |
|
271 } |
|
272 } |
|
273 |
|
274 // Add Contact |
|
275 function addContact(imgId) { |
|
276 |
|
277 var last = new Object(); |
|
278 last.Label = "Last name"; |
|
279 last.Value = "Shen"; |
|
280 |
|
281 var first = new Object(); |
|
282 first.Label = "First name"; |
|
283 first.Value = "Yi"; |
|
284 |
|
285 var contactdata = new Object(); |
|
286 contactdata.LastName = last; |
|
287 contactdata.FirstName = first; |
|
288 |
|
289 var criteria = new Object(); |
|
290 criteria.Type = "Contact"; |
|
291 criteria.Data = contactdata; |
|
292 |
|
293 try { |
|
294 var result; |
|
295 if (!asyncMode && !cancelMode) { // sync |
|
296 result = so.IDataSource.Add(criteria); |
|
297 checkError("IDataSource::addContact",result,DIV_ID,imgId); |
|
298 } else if (asyncMode && !cancelMode) {// async |
|
299 // set the image id for callback1 function |
|
300 imgid_callback1 = imgId; |
|
301 result = so.IDataSource.Add(criteria, callback1); |
|
302 if(!checkError("IDataSource::addContactAsync",result,DIV_ID,imgId)) { |
|
303 showIMG(imgId,""); |
|
304 } |
|
305 } else if (cancelMode){ // cancel |
|
306 // set the image id for callback1 function |
|
307 imgid_callback1 = imgId; |
|
308 result = so.IDataSource.Add(criteria, callback1); |
|
309 if(!checkError("IDataSource::cancelAddContactAsync",result,DIV_ID,imgId)) { |
|
310 showIMG(imgId,""); |
|
311 var criteria2 = new Object(); |
|
312 criteria2.TransactionID = result.TransactionID; |
|
313 var result2 =so.IDataSource.Cancel(criteria2); |
|
314 checkError("IDataSource::cancelAddContactAsync",result2,DIV_ID,imgId); |
|
315 } |
|
316 } |
|
317 } |
|
318 catch (e) { |
|
319 showIMG(imgId,"no"); |
|
320 alert ("addContact: " + e); |
|
321 } |
|
322 } |
|
323 |
|
324 // Delete Contact |
|
325 function delContact(imgId) { |
|
326 |
|
327 // using the getFirstId to get the Contact Id |
|
328 // it is impossible to ask user to type the Contact Id because |
|
329 // the id is in binary and hard to be displayed by javascritp |
|
330 |
|
331 var contactId = getFirstId("Contact"); |
|
332 if(contactId == null) { |
|
333 alert("No contact exists!"); |
|
334 return; |
|
335 } |
|
336 |
|
337 var contactdata = new Object(); |
|
338 contactdata.IdList = new Array(); |
|
339 contactdata.IdList[0] = contactId; |
|
340 |
|
341 var criteria = new Object(); |
|
342 criteria.Type = "Contact"; |
|
343 criteria.Data = contactdata; |
|
344 |
|
345 try { |
|
346 var result; |
|
347 if (!asyncMode && !cancelMode) { // sync |
|
348 result = so.IDataSource.Delete(criteria); |
|
349 checkError("IDataSource::delContact",result,DIV_ID,imgId); |
|
350 } else if (asyncMode && !cancelMode) {// async |
|
351 // set the image id for callback2 function |
|
352 imgid_callback2 = imgId; |
|
353 result = so.IDataSource.Delete(criteria, callback2); |
|
354 if(!checkError("IDataSource::delContactAsync",result,DIV_ID,imgId)) { |
|
355 showIMG(imgId,""); |
|
356 } |
|
357 } else if (cancelMode){ // cancel |
|
358 // set the image id for callback2 function |
|
359 imgid_callback2 = imgId; |
|
360 result = so.IDataSource.Delete(criteria, callback2); |
|
361 if(!checkError("IDataSource::cancelDelContactAsync",result,DIV_ID,imgId)) { |
|
362 showIMG(imgId,""); |
|
363 var criteria2 = new Object(); |
|
364 criteria2.TransactionID = result.TransactionID; |
|
365 var result2 =so.IDataSource.Cancel(criteria2); |
|
366 checkError("IDataSource::cancelDelContactAsync",result2,DIV_ID,imgId); |
|
367 } |
|
368 } |
|
369 } |
|
370 catch (e) { |
|
371 showIMG(imgId,"no"); |
|
372 alert ("delContact: " + e); |
|
373 } |
|
374 } |
|
375 |
|
376 // Show Contact List |
|
377 // find the property names in AddContact() |
|
378 // Note, the contact id cannot be display correctly by javascript because |
|
379 // it is a binary number |
|
380 function showContactList(iterator) { |
|
381 var msg = ""; |
|
382 try |
|
383 { |
|
384 iterator.reset(); |
|
385 var item; |
|
386 while (( item = iterator.getNext()) != undefined ){ |
|
387 var lastName = item['LastName']; |
|
388 var firstName = item['FirstName']; |
|
389 msg += lastName['Label'] + ":" + lastName['Value'] + "<br>"; |
|
390 msg += firstName['Label'] + ":" + firstName['Value'] + "<br>"; |
|
391 msg += "<br>"; |
|
392 } |
|
393 } |
|
394 catch(e) |
|
395 { |
|
396 alert('<showContacList> ' + e); |
|
397 } |
|
398 return msg; |
|
399 } |
|
400 |
|
401 // Get the first Id |
|
402 function getFirstId(type) { |
|
403 |
|
404 var criteria = new Object(); |
|
405 criteria.Type = type; |
|
406 |
|
407 var result; |
|
408 var firstId = null; |
|
409 result = so.IDataSource.GetList(criteria); |
|
410 if(!result.ErrorCode) { |
|
411 var iterator = result.ReturnValue; |
|
412 var item = iterator.getNext(); |
|
413 firstId = item['id']; |
|
414 } |
|
415 |
|
416 return firstId; |
|
417 } |
|
418 |
|
419 // Get Contact List |
|
420 function getContactList(imgId) { |
|
421 |
|
422 var criteria = new Object(); |
|
423 criteria.Type = "Contact"; |
|
424 |
|
425 try { |
|
426 var result; |
|
427 if (!asyncMode && !cancelMode) { // sync |
|
428 result = so.IDataSource.GetList(criteria); |
|
429 if(!checkError("IDataSource::getContactList",result,DIV_ID,imgId)) { |
|
430 document.getElementById(DIV_ID).innerHTML = showContactList(result.ReturnValue); |
|
431 } |
|
432 } else if (asyncMode && !cancelMode) {// async |
|
433 // set the image id for callback7 function |
|
434 imgid_callback7 = imgId; |
|
435 result = so.IDataSource.GetList(criteria, callback7); |
|
436 if(!checkError("IDataSource::getContactListAsync",result,DIV_ID,imgId)) { |
|
437 showIMG(imgId,""); |
|
438 } |
|
439 } else if (cancelMode){ // cancel |
|
440 // set the image id for callback7 function |
|
441 imgid_callback7 = imgId; |
|
442 result = so.IDataSource.GetList(criteria, callback7); |
|
443 if(!checkError("IDataSource::cancelGetContactListAsync",result,DIV_ID,imgId)) { |
|
444 showIMG(imgId,""); |
|
445 var criteria2 = new Object(); |
|
446 criteria2.TransactionID = result.TransactionID; |
|
447 var result2 =so.IDataSource.Cancel(criteria2); |
|
448 checkError("IDataSource::cancelGetContactListAsync",result2,DIV_ID,imgId); |
|
449 } |
|
450 } |
|
451 } |
|
452 catch (e) { |
|
453 showIMG(imgId,"no"); |
|
454 alert ("getContactList: " + e); |
|
455 } |
|
456 } |
|
457 |
|
458 // Import Contact |
|
459 function importContact(imgId) { |
|
460 |
|
461 var impFileName = prompt("Please enter the file name", "contact-export.txt"); |
|
462 if (impFileName == "") |
|
463 return; |
|
464 |
|
465 var contactdata = new Object(); |
|
466 contactdata.SourceFile = impFileName; |
|
467 |
|
468 var criteria = new Object(); |
|
469 criteria.Type = "Contact"; |
|
470 criteria.Data = contactdata; |
|
471 |
|
472 try { |
|
473 var result; |
|
474 if (!asyncMode && !cancelMode) { // sync |
|
475 result = so.IDataSource.Import(criteria); |
|
476 checkError("IDataSource::Import",result,DIV_ID,imgId); |
|
477 } else if (asyncMode && !cancelMode) {// async |
|
478 // set the image id for callback4 function |
|
479 imgid_callback4 = imgId; |
|
480 result = so.IDataSource.Import(criteria, callback4); |
|
481 if(!checkError("IDataSource::ImportAsync",result,DIV_ID,imgId)) { |
|
482 showIMG(imgId,""); |
|
483 } |
|
484 } else if (cancelMode){ // cancel |
|
485 // set the image id for callback4 function |
|
486 imgid_callback4 = imgId; |
|
487 result = so.IDataSource.Import(criteria, callback4); |
|
488 if(!checkError("IDataSource::cancelImportAsync",result,DIV_ID,imgId)) { |
|
489 showIMG(imgId,""); |
|
490 var criteria2 = new Object(); |
|
491 criteria2.TransactionID = result.TransactionID; |
|
492 var result2 =so.IDataSource.Cancel(criteria2); |
|
493 checkError("IDataSource::cancelImportAsync",result2,DIV_ID,imgId); |
|
494 } |
|
495 } |
|
496 } |
|
497 catch (e) { |
|
498 showIMG(imgId,"no"); |
|
499 alert ("importContact: " + e); |
|
500 } |
|
501 } |
|
502 |
|
503 // Export Contact |
|
504 function exportContact(imgId) { |
|
505 |
|
506 // using the getFirstId to get the Contact Id |
|
507 // it is impossible to ask user to type the Contact Id because |
|
508 // the id is in binary and hard to be displayed by javascritp |
|
509 |
|
510 var contactId = getFirstId("Contact"); |
|
511 if(contactId == null) { |
|
512 alert("No contact exists!"); |
|
513 return; |
|
514 } |
|
515 |
|
516 var expFileName = prompt("Please enter the file name", "contact-export.txt"); |
|
517 if (expFileName == "" || expFileName == null) |
|
518 return; |
|
519 |
|
520 var contactdata = new Object(); |
|
521 contactdata.id = contactId; |
|
522 contactdata.DestinationFile = expFileName; |
|
523 |
|
524 var criteria = new Object(); |
|
525 criteria.Type = "Contact"; |
|
526 criteria.Data = contactdata; |
|
527 |
|
528 try { |
|
529 var result; |
|
530 if (!asyncMode && !cancelMode) { // sync |
|
531 result = so.IDataSource.Export(criteria); |
|
532 checkError("IDataSource::Export",result,DIV_ID,imgId); |
|
533 } else if (asyncMode && !cancelMode) {// async |
|
534 // set the image id for callback5 function |
|
535 imgid_callback5 = imgId; |
|
536 result = so.IDataSource.Export(criteria, callback5); |
|
537 if(!checkError("IDataSource::ExportAsync",result,DIV_ID,imgId)) { |
|
538 showIMG(imgId,""); |
|
539 } |
|
540 } else if (cancelMode){ // cancel |
|
541 // set the image id for callback5 function |
|
542 imgid_callback5 = imgId; |
|
543 result = so.IDataSource.Export(criteria, callback5); |
|
544 if(!checkError("IDataSource::cancelExportAsync",result,DIV_ID,imgId)) { |
|
545 showIMG(imgId,""); |
|
546 var criteria2 = new Object(); |
|
547 criteria2.TransactionID = result.TransactionID; |
|
548 var result2 =so.IDataSource.Cancel(criteria2); |
|
549 checkError("IDataSource::cancelExportAsync",result2,DIV_ID,imgId); |
|
550 } |
|
551 } |
|
552 } |
|
553 catch (e) { |
|
554 showIMG(imgId,"no"); |
|
555 alert ("exportContact: " + e); |
|
556 } |
|
557 } |
|
558 |
|
559 // Add Group |
|
560 function addGroup(imgId) { |
|
561 |
|
562 var groupdata = new Object(); |
|
563 groupdata.GroupLabel = "TestGroupName" |
|
564 |
|
565 var criteria = new Object(); |
|
566 criteria.Type = "Group"; |
|
567 criteria.Data = groupdata; |
|
568 |
|
569 try { |
|
570 var result; |
|
571 if (!asyncMode && !cancelMode) { // sync |
|
572 result = so.IDataSource.Add(criteria); |
|
573 checkError("IDataSource::addGroup",result,DIV_ID,imgId); |
|
574 } else if (asyncMode && !cancelMode) {// async |
|
575 // set the image id for callback1 function |
|
576 imgid_callback1 = imgId; |
|
577 result = so.IDataSource.Add(criteria, callback1); |
|
578 if(!checkError("IDataSource::addGroupAsync",result,DIV_ID,imgId)) { |
|
579 showIMG(imgId,""); |
|
580 } |
|
581 } else if (cancelMode){ // cancel |
|
582 // set the image id for callback1 function |
|
583 imgid_callback1 = imgId; |
|
584 result = so.IDataSource.Add(criteria, callback1); |
|
585 if(!checkError("IDataSource::cancelAddGroupAsync",result,DIV_ID,imgId)) { |
|
586 showIMG(imgId,""); |
|
587 var criteria2 = new Object(); |
|
588 criteria2.TransactionID = result.TransactionID; |
|
589 var result2 =so.IDataSource.Cancel(criteria2); |
|
590 checkError("IDataSource::cancelAddGroupAsync",result2,DIV_ID,imgId); |
|
591 } |
|
592 } |
|
593 } |
|
594 catch (e) { |
|
595 showIMG(imgId,"no"); |
|
596 alert ("addGroup: " + e); |
|
597 } |
|
598 } |
|
599 |
|
600 // Delete Group |
|
601 function delGroup(imgId) { |
|
602 |
|
603 // using the getFirstId to get the Group Id |
|
604 // it is impossible to ask user to type the Group Id because |
|
605 // the id is in binary and hard to be displayed by javascritp |
|
606 |
|
607 var groupId = getFirstId("Group"); |
|
608 if(groupId == null) { |
|
609 alert("No Group exists!"); |
|
610 return; |
|
611 } |
|
612 |
|
613 var groupdata = new Object(); |
|
614 groupdata.IdList = new Array(); |
|
615 groupdata.IdList[0] = groupId; |
|
616 |
|
617 var criteria = new Object(); |
|
618 criteria.Type = "Group"; |
|
619 criteria.Data = groupdata; |
|
620 |
|
621 try { |
|
622 var result; |
|
623 if (!asyncMode && !cancelMode) { // sync |
|
624 result = so.IDataSource.Delete(criteria); |
|
625 checkError("IDataSource::delGroup",result,DIV_ID,imgId); |
|
626 } else if (asyncMode && !cancelMode) {// async |
|
627 // set the image id for callback2 function |
|
628 imgid_callback2 = imgId; |
|
629 result = so.IDataSource.Delete(criteria, callback2); |
|
630 if(!checkError("IDataSource::delGroupAsync",result,DIV_ID,imgId)) { |
|
631 showIMG(imgId,""); |
|
632 } |
|
633 } else if (cancelMode){ // cancel |
|
634 // set the image id for callback2 function |
|
635 imgid_callback2 = imgId; |
|
636 result = so.IDataSource.Delete(criteria, callback2); |
|
637 if(!checkError("IDataSource::cancelDelGroupAsync",result,DIV_ID,imgId)) { |
|
638 showIMG(imgId,""); |
|
639 var criteria2 = new Object(); |
|
640 criteria2.TransactionID = result.TransactionID; |
|
641 var result2 =so.IDataSource.Cancel(criteria2); |
|
642 checkError("IDataSource::cancelDelGroupAsync",result2,DIV_ID,imgId); |
|
643 } |
|
644 } |
|
645 } |
|
646 catch (e) { |
|
647 showIMG(imgId,"no"); |
|
648 alert ("delGroup: " + e); |
|
649 } |
|
650 } |
|
651 |
|
652 // Show Group List |
|
653 // find the property names in AddGroup() |
|
654 // Note, the group id cannot be display correctly by javascript because |
|
655 // it is a binary number |
|
656 function showGroupList(iterator) { |
|
657 var msg = ""; |
|
658 try { |
|
659 iterator.reset(); |
|
660 var item; |
|
661 while (( item = iterator.getNext()) != undefined ){ |
|
662 msg += item['GroupLabel'] + "<br>"; |
|
663 var groupList = item['Contents']; |
|
664 if(groupList == null || groupList == undefined) |
|
665 msg += "No group member.<br>"; |
|
666 else { |
|
667 var i = 0; |
|
668 for (var key in groupList) |
|
669 i++; |
|
670 msg += "There are " + i +" contacts in this group.<br>"; |
|
671 } |
|
672 msg += "<br>"; |
|
673 } |
|
674 } |
|
675 catch(e) |
|
676 { |
|
677 alert('<showGroupList> ' + e); |
|
678 } |
|
679 return msg; |
|
680 } |
|
681 |
|
682 // Get Group |
|
683 function getGroupList(imgId) { |
|
684 |
|
685 var criteria = new Object(); |
|
686 criteria.Type = "Group"; |
|
687 |
|
688 try { |
|
689 var result; |
|
690 if (!asyncMode && !cancelMode) { // sync |
|
691 result = so.IDataSource.GetList(criteria); |
|
692 if(!checkError("IDataSource::getGroupList",result,DIV_ID,imgId)) { |
|
693 document.getElementById(DIV_ID).innerHTML = showGroupList(result.ReturnValue); |
|
694 } |
|
695 } else if (asyncMode && !cancelMode) {// async |
|
696 // set the image id for callback3 function |
|
697 imgid_callback3 = imgId; |
|
698 result = so.IDataSource.GetList(criteria, callback3); |
|
699 if(!checkError("IDataSource::getGroupListAsync",result,DIV_ID,imgId)) { |
|
700 showIMG(imgId,""); |
|
701 } |
|
702 } else if (cancelMode){ // cancel |
|
703 // set the image id for callback3 function |
|
704 imgid_callback3 = imgId; |
|
705 result = so.IDataSource.GetList(criteria, callback3); |
|
706 if(!checkError("IDataSource::cancelGetGroupListAsync",result,DIV_ID,imgId)) { |
|
707 showIMG(imgId,""); |
|
708 var criteria2 = new Object(); |
|
709 criteria2.TransactionID = result.TransactionID; |
|
710 var result2 =so.IDataSource.Cancel(criteria2); |
|
711 checkError("IDataSource::cancelGetGroupListAsync",result2,DIV_ID,imgId); |
|
712 } |
|
713 } |
|
714 } |
|
715 catch (e) { |
|
716 showIMG(imgId,"no"); |
|
717 alert ("getGroupList: " + e); |
|
718 } |
|
719 } |
|
720 |
|
721 // Organise Group |
|
722 function organiseGroup(imgId) { |
|
723 |
|
724 // using the getFirstId to get the Group Id |
|
725 // it is impossible to ask user to type the Group Id because |
|
726 // the id is in binary and hard to be displayed by javascritp |
|
727 |
|
728 var groupId = getFirstId("Group"); |
|
729 if(groupId == null) { |
|
730 alert("No Group exists!"); |
|
731 return; |
|
732 } |
|
733 |
|
734 // using the getFirstId to get the Contact Id |
|
735 // it is impossible to ask user to type the Contact Id because |
|
736 // the id is in binary and hard to be displayed by javascritp |
|
737 |
|
738 var contactId = getFirstId("Contact"); |
|
739 if(contactId == null) { |
|
740 alert("No contact exists!"); |
|
741 return; |
|
742 } |
|
743 |
|
744 //"Associate" |
|
745 //"Disassociate" |
|
746 var msg = "Please enter the operation type ['Associate' or 'Disassociate']"; |
|
747 var optype = prompt(msg, "Associate"); |
|
748 if (optype == "" || optype == null) |
|
749 return; |
|
750 |
|
751 var groupdata = new Object(); |
|
752 groupdata.id = groupId; |
|
753 groupdata.IdList = new Array(); |
|
754 groupdata.IdList[0] = contactId; |
|
755 |
|
756 var criteria = new Object(); |
|
757 criteria.Type = "Group"; |
|
758 criteria.Data = groupdata; |
|
759 criteria.OperationType = optype; |
|
760 |
|
761 try { |
|
762 var result; |
|
763 if (!asyncMode && !cancelMode) { // sync |
|
764 result = so.IDataSource.Organise(criteria); |
|
765 checkError("IDataSource::organiseGroup",result,DIV_ID,imgId); |
|
766 } else if (asyncMode && !cancelMode) {// async |
|
767 // set the image id for callback6 function |
|
768 imgid_callback6 = imgId; |
|
769 result = so.IDataSource.Organise(criteria, callback6); |
|
770 if(!checkError("IDataSource::organiseGroupAsync",result,DIV_ID,imgId)) { |
|
771 showIMG(imgId,""); |
|
772 } |
|
773 } else if (cancelMode){ // cancel |
|
774 // set the image id for callback6 function |
|
775 imgid_callback6 = imgId; |
|
776 result = so.IDataSource.Organise(criteria, callback6); |
|
777 if(!checkError("IDataSource::cancelOrganiseGroupAsync",result,DIV_ID,imgId)) { |
|
778 showIMG(imgId,""); |
|
779 var criteria2 = new Object(); |
|
780 criteria2.TransactionID = result.TransactionID; |
|
781 var result2 =so.IDataSource.Cancel(criteria2); |
|
782 checkError("IDataSource::cancelOrganiseGroupAsync",result2,DIV_ID,imgId); |
|
783 } |
|
784 } |
|
785 } |
|
786 catch (e) { |
|
787 showIMG(imgId,"no"); |
|
788 alert ("organiseGroup: " + e); |
|
789 } |
|
790 } |
|
791 |
|
792 // This is the asynchronous callback handler |
|
793 function callback1(transId, eventCode, result) |
|
794 { |
|
795 console.info("addAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
796 checkError("IDataSource::addAsync",result,DIV_ID,imgid_callback1); |
|
797 } |
|
798 |
|
799 // This is the asynchronous callback handler |
|
800 function callback2(transId, eventCode, result) |
|
801 { |
|
802 console.info("delAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
803 checkError("IDataSource::delAsync",result,DIV_ID,imgid_callback2); |
|
804 } |
|
805 |
|
806 // This is the asynchronous callback handler |
|
807 function callback3(transId, eventCode, result) |
|
808 { |
|
809 console.info("getListAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
810 if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgid_callback3)) { |
|
811 document.getElementById(DIV_ID).innerHTML = showGroupList(result.ReturnValue); |
|
812 } |
|
813 } |
|
814 |
|
815 // This is the asynchronous callback handler |
|
816 function callback4(transId, eventCode, result) |
|
817 { |
|
818 console.info("importAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
819 checkError("IDataSource::importAsync",result,DIV_ID,imgid_callback4); |
|
820 } |
|
821 |
|
822 // This is the asynchronous callback handler |
|
823 function callback5(transId, eventCode, result) |
|
824 { |
|
825 console.info("exportAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
826 checkError("IDataSource::exportAsync",result,DIV_ID,imgid_callback5); |
|
827 } |
|
828 |
|
829 // This is the asynchronous callback handler |
|
830 function callback6(transId, eventCode, result) |
|
831 { |
|
832 console.info("organiseAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
833 checkError("IDataSource::organiseAsync",result,DIV_ID,imgid_callback6); |
|
834 } |
|
835 |
|
836 // This is the asynchronous callback handler |
|
837 function callback7(transId, eventCode, result) |
|
838 { |
|
839 console.info("getListAsync: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode); |
|
840 if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgid_callback7)) { |
|
841 document.getElementById(DIV_ID).innerHTML = showContactList(result.ReturnValue); |
|
842 } |
|
843 }</pre> |
|
844 </div> |
|
845 </div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html> |