serviceproviders/jsapi/platformservices/src/framework.js
changeset 27 02682e02e51f
child 44 0b68a1b0c15e
equal deleted inserted replaced
26:5d0ec8b709be 27:02682e02e51f
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Defination of class CCalendarInterface
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 var __device_debug_on__ = true;
       
    20 
       
    21 var event_completed = 2;
       
    22 var event_cancelled = 3;
       
    23 
       
    24 var __Service_Interface_Ver=1.0;
       
    25 
       
    26 
       
    27 var MapErrorCode = {1016:100,1012:101,1010:102,1009:103,1005:104,1000:105,1011:106,1007:107,1003:1,1002:2, 1004:3};
       
    28 
       
    29 function __device_debug(text){
       
    30   //if(__device_debug_on__) alert(text);
       
    31 }
       
    32 
       
    33 function __device_handle_exception(e, text){
       
    34 	__device_debug(text);
       
    35 	throw(e);
       
    36 }
       
    37 
       
    38 function __device_typeof(value)
       
    39 {
       
    40 	// First check to see if the value is undefined.
       
    41 	if (value == undefined) {
       
    42         return "undefined";
       
    43     }
       
    44 	// Check for objects created with the "new" keyword.
       
    45 	if (value instanceof Object) {
       
    46 		// Check whether it's a string object.
       
    47 		if (value instanceof String) {
       
    48 			return "String";
       
    49 		}
       
    50 		// Check whether it's an array object/array literal.
       
    51 		else 
       
    52 			if (value instanceof Array) {
       
    53 				return "Array";
       
    54 			}
       
    55 	}
       
    56 	// dealing with a literal.
       
    57 		if (typeof value) {
       
    58 			if (typeof value == "object") {
       
    59 				if (typeof value == "object" && !value) {
       
    60 					return "null";
       
    61 				}
       
    62 			}
       
    63            // if not null check for other types
       
    64 			
       
    65 				// Check if it's a string literal.
       
    66 			else if (typeof value == "string") {
       
    67 					return "string";
       
    68 				}
       
    69 		}	 
       
    70 }
       
    71 
       
    72 if ((typeof nokia) == 'undefined') {
       
    73 	nokia = {};
       
    74 	nokia.device = {};
       
    75 }
       
    76 else {
       
    77     if ((typeof nokia.device) != 'undefined') {
       
    78 		nokia.device = {};
       
    79     }
       
    80 	else{
       
    81 		throw ("nokia.device already defined");
       
    82 	} 
       
    83 }
       
    84 
       
    85 nokia.device = {
       
    86     load: __device_service_load,
       
    87     listInterfaces: __device_service_interfaces,
       
    88 	getSystemProperties: __device_getSystemProperties,
       
    89 	getLibraryVersion: __device_getLibraryVersion,
       
    90     version: 2.0
       
    91 };
       
    92 
       
    93 function __device_getLibraryVersion(){
       
    94 	return 2.0;
       
    95 }
       
    96 
       
    97 function __device_getSystemProperties(){
       
    98 	return {
       
    99 		uriPath:{
       
   100 			// maximum limitation is 255 charectors
       
   101 			max:255
       
   102 		}
       
   103 	};
       
   104 }
       
   105 // Configure the services offered.
       
   106 
       
   107 var __device_services_inited = false;
       
   108 
       
   109 var __device_services = [
       
   110 
       
   111   // For now, the only service is the base "device"" service
       
   112   {
       
   113     "name":"device",
       
   114     "version": 2.0, 
       
   115     "interfaces": []
       
   116   }
       
   117 ];
       
   118 
       
   119 // Initialize the configured services.
       
   120 
       
   121 function __device_services_init(){
       
   122   if(__device_services_inited){
       
   123     return;
       
   124   }
       
   125   __device_services_inited = true;
       
   126   
       
   127   try {
       
   128   	var so_SrvReg = device.getServiceObject("Service.ServiceRegistry", "IService");
       
   129 	var filter = {ServiceName:"Service.Calendar", InterfaceName:"IDataSource"};  
       
   130 	
       
   131 	var returnVal = so_SrvReg.IService.GetList(filter);
       
   132 		if (returnVal.ErrorCode == 0)	{
       
   133 			var intf_iterator = returnVal.ReturnValue;
       
   134 			if(intf_iterator)	{
       
   135 				var intf = intf_iterator.getNext();
       
   136 				if(intf){
       
   137 					var versionList = intf.VersionList;
       
   138 					var index;
       
   139 					for(index in versionList){
       
   140 						if(versionList[index] > __Service_Interface_Ver){						
       
   141 							__Service_Interface_Ver = versionList[index];
       
   142 						}
       
   143 					}
       
   144 				}
       
   145 			}
       
   146 			intf_iterator.close();
       
   147 		}
       
   148 	so_SrvReg.close();
       
   149   }catch(e){
       
   150   	// service registry not available default version to 1.0
       
   151 //	alert('in catch'+e);
       
   152 	__Service_Interface_Ver = 1.0;
       
   153   }
       
   154 	
       
   155  
       
   156 
       
   157   // Get the service-specific service entries. Note that these
       
   158   // need to be individually wrapped by try/catch blocks so that the
       
   159   // interpreter gracefully handles missing services. 
       
   160 
       
   161   try {
       
   162     __device_services[0].interfaces.push(__device_geolocation_service_entry);
       
   163   }catch (e){
       
   164     __device_debug("Missing library implementation: " + e);
       
   165   }
       
   166   try {
       
   167     __device_services[0].interfaces.push(__device_camera_service_entry);
       
   168   }catch (e1){
       
   169     __device_debug("Missing library implementation: " + e1);
       
   170   }
       
   171   try {
       
   172     __device_services[0].interfaces.push(__device_media_service_entry);
       
   173   }catch (e2){
       
   174 //    __device_debug("Missing library implementation: " + e2);
       
   175   }
       
   176   try {
       
   177     __device_services[0].interfaces.push(__device_contacts_service_entry);
       
   178   }catch (e3){
       
   179 //    __device_debug("Missing library implementation: " + e3);
       
   180   }
       
   181  try {
       
   182     __device_services[0].interfaces.push(__device_messaging_service_entry);
       
   183   }catch (e4){
       
   184       __device_debug("Missing library implementation: " + e4);
       
   185   }
       
   186   try {
       
   187     __device_services[0].interfaces.push(__device_calendar_service_entry);
       
   188   }catch (e5){
       
   189       __device_debug("Missing library implementation: " + e5);
       
   190   }
       
   191   try {
       
   192     __device_services[0].interfaces.push(__device_landmarks_service_entry);
       
   193   }catch (e6){
       
   194       __device_debug("Missing library implementation: " + e6);
       
   195   }
       
   196   try {
       
   197     __device_services[0].interfaces.push(__device_commlog_service_entry);
       
   198   }catch (e7){
       
   199       __device_debug("Missing library implementation: " + e7);
       
   200   }
       
   201   try {
       
   202     __device_services[0].interfaces.push(__device_sysinfo_service_entry);
       
   203   }catch (e8){
       
   204       __device_debug("Missing library implementation: " + e8);
       
   205   }
       
   206   try {
       
   207     __device_services[0].interfaces.push(__device_sensors_service_entry);
       
   208   }catch (e9){
       
   209       __device_debug("Missing library implementation: " + e9);
       
   210   }
       
   211 
       
   212 }
       
   213 
       
   214 function __device_get_implementation(i){
       
   215   //__device_debug("get_implementation: " + i);
       
   216   return  new i.proto(new(i.providers[0].instance));
       
   217 }
       
   218 
       
   219 function __device_get_descriptor(i){
       
   220   //__device_debug("get_descriptor: " + i);
       
   221   return new i.descriptor(new(i.providers[0].descriptor));
       
   222 }
       
   223 
       
   224 function __device_get_interface(s, interfaceName, version){
       
   225 
       
   226     // Find first match of name and version
       
   227     var i = s.interfaces;
       
   228 	for (var d in i){
       
   229 		if(i[d].name == null){
       
   230 		  __device_update_descriptor(i[d]);
       
   231 		}
       
   232 		
       
   233 		if(i[d].name == undefined){
       
   234 		  continue;
       
   235 		}
       
   236 		
       
   237 		if (i[d].name == interfaceName){
       
   238 		// Match version if specified
       
   239 			if (version) {
       
   240 				if (i[d].version >= version) {
       
   241 					return __device_get_implementation(i[d]);
       
   242 				}
       
   243 			}else{
       
   244 				return __device_get_implementation(i[d]);
       
   245 			}
       
   246 	    }
       
   247 	}
       
   248 	return null;
       
   249 }
       
   250 
       
   251 // Implemention of the load method
       
   252 
       
   253 function __device_service_load(interfaceName, version){
       
   254     var error = new DeviceException('dummy',0);
       
   255 	if(interfaceName){
       
   256         if(typeof interfaceName != 'string'){
       
   257             throw new DeviceException(error.INVALID_ARG_ERR, 'nokia.device.load:Invalid type interfaceName');
       
   258         }
       
   259     }else{
       
   260         throw new DeviceException(error.MISSING_ARG_ERR, 'nokia.device.load:interfaceName param expected');
       
   261     }
       
   262     if(version){
       
   263         if(typeof version != 'number'){
       
   264             throw new DeviceException(error.INVALID_ARG_ERR, 'nokia.device.load:Invalid type version');
       
   265         }
       
   266     }
       
   267 
       
   268     __device_services_init();
       
   269   
       
   270 	for(var s in __device_services){
       
   271 	  //__device_debug("service_load: " + s + ":" +  __device_services[s].name + ": " + interfaceName);
       
   272 	  var i = __device_get_interface(__device_services[s], interfaceName, version);
       
   273 	  if (i != null){
       
   274 	    return i;
       
   275 	  }
       
   276 	}
       
   277     return null;
       
   278 }
       
   279 
       
   280 // Lazily fill in the descriptor table
       
   281 
       
   282 function __device_update_descriptor(i){
       
   283   var d = __device_get_descriptor(i);
       
   284   i.name = d.interfaceName;
       
   285   i.version = d.version;  
       
   286 }
       
   287 // Get an array of interface descriptors for a service
       
   288 
       
   289 function __device_interface_list(s){
       
   290   var retval = new Array();
       
   291   for(var i in s.interfaces){
       
   292     if(s.interfaces[i].name == null){
       
   293       __device_update_descriptor(s.interfaces[i]);
       
   294     }
       
   295     if(s.interfaces[i].name == undefined){
       
   296       continue;
       
   297     }
       
   298     retval[i] = new Object();
       
   299     retval[i].name = s.interfaces[i].name;
       
   300     retval[i].version = s.interfaces[i].version;
       
   301   }  
       
   302   return retval;
       
   303 }
       
   304 
       
   305 // Get a service description
       
   306 
       
   307 function __device_service_descriptor(s){
       
   308   this.name = s.name;
       
   309   this.version = s.version;
       
   310   this.interfaces = __device_interface_list(s);
       
   311   this.toString = __device_service_descriptor_to_string;
       
   312 }
       
   313 
       
   314 function __device_service_descriptor_to_string(){
       
   315   var is = "\nInterfaces(s): ";
       
   316 
       
   317   for (i in this.interfaces){
       
   318     is += "\n" + this.interfaces[i].name + " " + this.interfaces[0].version;
       
   319   }
       
   320   return ("Service: " + this.name + is);
       
   321 }
       
   322 
       
   323 // Implement the listInterfaces method
       
   324     
       
   325 function __device_service_interfaces(){
       
   326   __device_services_init();
       
   327 
       
   328   for (var s in __device_services){
       
   329       var intfs = __device_interface_list(__device_services[s]);
       
   330 	  // create interator
       
   331 	  return new __FWIter(intfs);
       
   332   }
       
   333   return null;
       
   334 }
       
   335 
       
   336 
       
   337 function __FWIter(arr)
       
   338 {
       
   339     this._arr = arr;
       
   340 	this._pos = 0;
       
   341 	this._valid = true;
       
   342 }
       
   343 
       
   344 __FWIter.prototype.hasNext = function() {
       
   345     return (this._valid && this._pos < this._arr.length);
       
   346 }
       
   347 
       
   348 __FWIter.prototype.next = function() {
       
   349     if(this._valid && (this._pos < this._arr.length)){
       
   350 		return this._arr[this._pos++];
       
   351 	}
       
   352 	else{
       
   353 		return null;
       
   354 	}
       
   355 }
       
   356 
       
   357 __FWIter.prototype.close = function(){
       
   358     delete this._arr;
       
   359 	this._valid = false;
       
   360 }
       
   361 
       
   362 function modifyObjectBaseProp(obj){
       
   363   for (pro in obj) {
       
   364     if(typeof obj[pro] == "function" )
       
   365       obj[pro] = 0;
       
   366     }
       
   367 }
       
   368 
       
   369 //TBD: Get rid of this global, use closures instead
       
   370 //Functions for  Handling Async API's use this variable
       
   371 var glob_obj = null;
       
   372 var temp_scb = null;
       
   373 var temp_ecb = null;
       
   374  
       
   375 glob_obj = new Object();
       
   376 
       
   377 glob_obj.glob_cb_arr = new Array();
       
   378 glob_obj.addToGlobalArray = addToGlobalArray;
       
   379 glob_obj.removeFromArray = removeFromArray;
       
   380 glob_obj.getFromArray = getFromArray;
       
   381      
       
   382 DeviceAPIError.prototype = new Error(); //inheritance occurs here
       
   383 DeviceAPIError.prototype.constructor = DeviceAPIError; //If this not present then, it uses default constructor of Error
       
   384 
       
   385 //constructor for DeviceAPIError.
       
   386 function DeviceAPIError(code,message) 
       
   387 {
       
   388 	this.toString = concatenate;
       
   389 	this.code = code;
       
   390 	this.name = "DeviceError";//we can even overwrite default name "Error"
       
   391 	this.message=message; 
       
   392 	
       
   393         this.MISSING_ARG_ERR            = 1;
       
   394         this.INVALID_ARG_ERR            = 2;
       
   395         this.NOT_SUPPORTED_ERR          = 3;
       
   396         this.TIMEOUT_ERR                = 100;
       
   397         this.DATA_NOT_FOUND_ERR         = 101;
       
   398         this.DATA_ALREADY_EXISTS_ERR    = 102;
       
   399         this.SERVICE_BUSY_ERR           = 103;
       
   400         this.SERVICE_IN_USE_ERR         = 104;
       
   401         this.DATA_OUT_OF_RANGE_ERR      = 105;
       
   402         this.NOT_ALLOWED_ERR            = 106;
       
   403         this.SIZE_EXCEEDED_ERR          = 107;
       
   404         this.INVALID_URI_ERR            = 108;
       
   405         this.URI_NOT_FOUND_ERR          = 109;
       
   406         this.URI_ALREADY_EXISTS_ERR     = 110;
       
   407         this.NO_MEMORY_ERR              = 111;  
       
   408 }
       
   409 
       
   410 DeviceException.prototype = new Error(); //inheritance occurs here
       
   411 DeviceException.prototype.constructor = DeviceException; //If this not present then, it uses default constructor of Error
       
   412 
       
   413 //constructor for DeviceException.
       
   414 function DeviceException(code,message) 
       
   415 {
       
   416     this.toString = concatenate;
       
   417     this.code = code;
       
   418     this.name = "DeviceException";//we can even overwrite default name "Error"
       
   419     this.message=message; 
       
   420     
       
   421         this.MISSING_ARG_ERR            = 1;
       
   422         this.INVALID_ARG_ERR            = 2;
       
   423         this.NOT_SUPPORTED_ERR          = 3;
       
   424         this.TIMEOUT_ERR                = 100;
       
   425         this.DATA_NOT_FOUND_ERR         = 101;
       
   426         this.DATA_ALREADY_EXISTS_ERR    = 102;
       
   427         this.SERVICE_BUSY_ERR           = 103;
       
   428         this.SERVICE_IN_USE_ERR         = 104;
       
   429         this.DATA_OUT_OF_RANGE_ERR      = 105;
       
   430         this.NOT_ALLOWED_ERR            = 106;
       
   431         this.SIZE_EXCEEDED_ERR          = 107;
       
   432         this.INVALID_URI_ERR            = 108;
       
   433         this.URI_NOT_FOUND_ERR          = 109;
       
   434         this.URI_ALREADY_EXISTS_ERR     = 110;
       
   435         this.NO_MEMORY_ERR              = 111;  
       
   436 }
       
   437 
       
   438 
       
   439 function concatenate()
       
   440 {
       
   441     return (this.name+': errcode: '+this.code+'\nline: '+this.lineNumber+'\nfileName: '+this.fileName+'\nerrmsg: '+this.message);
       
   442 }
       
   443 
       
   444 function splitErrorMessage(errmessage)
       
   445 {
       
   446 	if(errmessage.search(/:/)!=-1)
       
   447 	{
       
   448 		if((errmessage.split(":").length)==2)
       
   449 		{
       
   450 			return errmessage.split(":")[1];
       
   451 		}
       
   452 		if((errmessage.split(":").length)>2)
       
   453 		{
       
   454 			return errmessage.split(":")[2];
       
   455 		}
       
   456 	}
       
   457 	return errmessage;
       
   458 }
       
   459 
       
   460 
       
   461 var __s60_start_and_wait_cb;
       
   462 
       
   463 function __s60_on_app_exit(){
       
   464   widget.onshow = null;
       
   465   if(__s60_start_and_wait_cb !== null){
       
   466     __s60_start_and_wait_cb();
       
   467   }
       
   468 }
       
   469 
       
   470 function __s60_on_app_start(){
       
   471   widget.onhide = null;
       
   472   widget.onshow = __s60_on_app_exit;
       
   473 }
       
   474 
       
   475 // This function cannot actually force JS to wait,
       
   476 // but it does supply a callback the apps can use
       
   477 // to continue processing on return from the app.
       
   478 // Apps should take care not to reinvoke this and
       
   479 // should be careful about any other processing
       
   480 // that might happen while the app is running.
       
   481 
       
   482 function __s60_start_and_wait(id, args, app_exit_cb){
       
   483   __s60_start_and_wait_cb = app_exit_cb;
       
   484   widget.onhide = __s60_on_app_start;
       
   485   widget.openApplication(id, args);
       
   486 }
       
   487 
       
   488 function __s60_api_not_supported(){
       
   489   throw(err_ServiceNotSupported);
       
   490 }
       
   491 
       
   492 function __s60_enumerate_object(object, namespace, func, param){
       
   493    var key;
       
   494    for (key in object) {
       
   495    		var propname;
       
   496    		if (namespace) {
       
   497    			propname = namespace + "." + key;
       
   498    		}
       
   499    		else {
       
   500    			propname = key;
       
   501    		}
       
   502    		var value = object[key];
       
   503    		if (typeof value == "object") {
       
   504    			__s60_enumerate_object(value, propname, func, param);
       
   505    		}
       
   506    		else {
       
   507    			func(propname, value, param);
       
   508    		}
       
   509    }
       
   510 }
       
   511 
       
   512 
       
   513 /*
       
   514  * Handling Async calls
       
   515  */
       
   516 function removeFromArray(transId){
       
   517     for (i = 0; i < (glob_obj.glob_cb_arr.length); i++) {
       
   518         if (glob_obj.glob_cb_arr[i].transactionId == transId) {
       
   519             glob_obj.glob_cb_arr.splice(i, 1);
       
   520         }
       
   521     }
       
   522 }
       
   523 
       
   524 function addToGlobalArray(transId, sCb, errCb){
       
   525 
       
   526     var obj = new Object();
       
   527     
       
   528     obj.success_cb = sCb;
       
   529     obj.transactionId = transId;
       
   530     obj.error_cb = errCb;
       
   531     
       
   532     glob_obj.glob_cb_arr.push(obj);
       
   533     
       
   534 }
       
   535 
       
   536 function getFromArray(tid){
       
   537     var i;
       
   538     
       
   539     for (i = 0; i < (glob_obj.glob_cb_arr.length); i++) {
       
   540         if (glob_obj.glob_cb_arr[i].transactionId == tid) {
       
   541             return glob_obj.glob_cb_arr[i];
       
   542         }
       
   543     }
       
   544 	
       
   545 	if(temp_scb)
       
   546 	{
       
   547 		var obj = new Object();
       
   548     
       
   549 	    obj.success_cb = temp_scb;
       
   550 	    obj.error_cb = temp_ecb;
       
   551 		
       
   552 		temp_ecb = temp_scb = null;
       
   553 		return obj;		
       
   554 	}
       
   555     return null;
       
   556 }