Wikipedia/preview/script/lib/sapi/AppManager.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     1 /**
       
     2  * AppManager.js
       
     3  * 
       
     4  * Nokia Web Runtime Service API emulation 
       
     5  * WRT v1.1
       
     6  * 
       
     7  * Copyright 2009 Nokia Corporation. All rights reserved.
       
     8 */
       
     9 
       
    10  
       
    11 (function(){
       
    12 	
       
    13 	var provider = 'Service.AppManager',
       
    14 		Interface = 'IAppManager';
       
    15 
       
    16 	/**
       
    17 	 * AppManager service
       
    18 	 */
       
    19 	var AppManagerService = function(){
       
    20 		this.GetList 	= __GetList;
       
    21 		this.LaunchApp	= __LaunchApp;
       
    22 		this.LaunchDoc	= __LaunchDoc;
       
    23 		this.Cancel 	= __Cancel;
       
    24 	}
       
    25 
       
    26 	device.implementation.extend(provider, Interface, new AppManagerService() );
       
    27 
       
    28 
       
    29 	/******************************************************/	
       
    30 	/******************************************************/	
       
    31 	/******************************************************/	
       
    32 
       
    33 	var	context = device.implementation.context,
       
    34 		_t = context._t,
       
    35 		method = '',
       
    36 		result = false,
       
    37 		DBase = null;
       
    38 	
       
    39 	/**
       
    40 	 * AppManager: GetList
       
    41 	 * @param {Object} criteria
       
    42 	 */
       
    43 	function __GetList(criteria){
       
    44 		if ((result = validator.apply('GetList', arguments)) !== false)
       
    45 			return result; 
       
    46 
       
    47 		if (typeof criteria.Type == 'undefined') 
       
    48 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingType);
       
    49 
       
    50 		if (!/^(Application|UserInstalledPackage)$/i.test(criteria.Type)) 
       
    51 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE);
       
    52 		
       
    53 		// check if a callback was provided
       
    54 		if (arguments.length > 1)
       
    55 			return error(device.implementation.ERR_SERVICE_NOT_SUPPORTED, msg.badAsync);
       
    56 		
       
    57 		var returnValue,
       
    58 			filter = criteria.Filter || null;
       
    59  
       
    60 		DBase = context.getData(provider);
       
    61 
       
    62 		if (criteria.Filter)
       
    63 			context.notify(_t('%s:: GetList : filter not implemented in preview').arg(provider));
       
    64 
       
    65 		// Type = UserInstalledPackage
       
    66 		if (!/UserInstalledPackage/i.test(criteria.Type)){
       
    67 
       
    68 			returnValue = context.Iterator( DBase[criteria.Type] || [] );
       
    69 
       
    70 		} else {
       
    71 		// Type = Application
       
    72 			// @todo: apply filter criteria
       
    73 			
       
    74 			returnValue = context.Iterator( DBase[criteria.Type] || [] );
       
    75 		}
       
    76 
       
    77 		return context.Result(returnValue);
       
    78 	}
       
    79 			
       
    80 	/**
       
    81 	 * AppManager: LaunchApp
       
    82 	 * @param {Object} criteria
       
    83 	 * @param {function} callback function for async call (optional)
       
    84 	 */
       
    85 	function __LaunchApp(criteria, callback){
       
    86 
       
    87 		if ((result = validator.apply('LaunchApp', arguments)) !== false)
       
    88 			return result; 
       
    89 		
       
    90 		if (typeof criteria.ApplicationID == 'undefined')
       
    91 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingAppID);
       
    92 
       
    93 		// app id must be in the form "s60uid://<appid>" where <appid> is 
       
    94 		// what is returned by GetList.
       
    95 		var appId = criteria.ApplicationID;
       
    96 		
       
    97 		if (!/^s60uid:\/\/0x/i.test(appId))
       
    98 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE);
       
    99 	
       
   100 		if (criteria.CmdLind)
       
   101 			context.notify(_t('%s:: LaunchApp : CmdLine not implemented in preview').arg(provider));
       
   102 
       
   103 		if (criteria.Options)
       
   104 			context.notify(_t('%s:: LaunchApp : Options not implemented in preview').arg(provider));
       
   105 
       
   106 
       
   107 		if (typeof callback == 'function') {
       
   108 			return context.callAsync(this, arguments.callee, criteria, callback);
       
   109 		}
       
   110 
       
   111 		appId = appId.replace(/^s60uid:\/\//i, '');	
       
   112 		DBase = context.getData(provider);
       
   113 
       
   114 		for(var type in DBase){
       
   115 			for(var i in DBase[type]) {
       
   116 				var item = DBase[type][i];
       
   117 				if (item.Uid == appId) {
       
   118 					// found!
       
   119 					context.notify(_t('%s:: LaunchApp : application found & launched : id=%s').arg(provider, appId));
       
   120 					return context.ErrorResult(device.implementation.ERR_SUCCESS);
       
   121 				}
       
   122 			}
       
   123 		}
       
   124 		// if not found
       
   125 		return error(device.implementation.ERR_BAD_ARGUMENT_TYPE);
       
   126 	}
       
   127 			
       
   128 	/**
       
   129 	 * AppManager: LaunchDoc
       
   130 	 * @param {Object} criteria
       
   131 	 * @param {function} callback function for async call (optional)
       
   132 	 */
       
   133 	function __LaunchDoc(criteria, callback){
       
   134 
       
   135 		if ((result = validator.apply('LaunchDoc', arguments)) !== false)
       
   136 			return result; 
       
   137 
       
   138 		if (typeof criteria.Document == 'undefined' && typeof criteria.MimeType == 'undefined')
       
   139 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingDoc);
       
   140 
       
   141 		if (typeof criteria.Document != 'undefined' && !criteria.Document.DocumentPath)
       
   142 			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE);
       
   143 
       
   144 		if (criteria.Options)
       
   145 			context.notify(_t('%s:: LaunchDoc : Options not implemented in preview').arg(provider));
       
   146 
       
   147 		if (typeof callback == 'function') {
       
   148 			return context.callAsync(this, arguments.callee, criteria, callback);
       
   149 		}
       
   150 
       
   151 		// nothing to launch in emulation, just notify user
       
   152 		context.notify(_t('%s:: LaunchDoc : document launched').arg(provider));
       
   153 		
       
   154 		if (criteria.Document)
       
   155 			// return success
       
   156 			return context.ErrorResult(device.implementation.ERR_SUCCESS);
       
   157 		else
       
   158 			// for mimetype, return value name of document
       
   159 			return context.Result('', device.implementation.ERR_SUCCESS);
       
   160 	}
       
   161 
       
   162 	
       
   163 
       
   164 	/**
       
   165 	 * AppManager: Cancel
       
   166 	 * @param {Object} criteria
       
   167 	 */
       
   168 	function __Cancel(criteria){
       
   169 		method = 'Cancel';
       
   170 		if (!criteria || !criteria.TransactionID)
       
   171 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingTID);
       
   172 		
       
   173 		clearTimeout(criteria.TransactionID);
       
   174 		return context.ErrorResult(device.implementation.ERR_SUCCESS);
       
   175 	}
       
   176 
       
   177 
       
   178 	
       
   179 	/*******************************
       
   180 	 * helper functions
       
   181 	 *******************************/
       
   182 	
       
   183 	function error(code, msg /*, args...*/){
       
   184 
       
   185 		var args = ['AppManager',method].concat([].slice.call(arguments,2));
       
   186 		msg = msg ? _t().arg.apply(msg,args) : undefined;
       
   187 		return context.ErrorResult(code, msg);
       
   188 	}
       
   189 
       
   190 	/**
       
   191 	 * validate common input arguments
       
   192 	 * 'this' is string (object) name of calling function
       
   193 	 * 
       
   194 	 * @param {arguments} arguments of calling function
       
   195 	 * @return {Result} Result object if error, false if no error.
       
   196 	 */
       
   197 	function validator() {
       
   198 		method = ''+this;
       
   199 		var	failed = false,
       
   200 			criteria = arguments[0] || false;
       
   201 			
       
   202 		if (!criteria || typeof criteria != 'object')
       
   203 			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingType);
       
   204 
       
   205 		return failed;
       
   206 	}
       
   207 
       
   208 	/** 
       
   209 	 * error messages
       
   210 	 * order of %s args: Service name, method name, parameter name 
       
   211 	 */
       
   212 	var msg = {
       
   213 		missingType		: '%s:%s:Content Type Missing',
       
   214 		badAsync		: 'AppManger:GetList:Asynchronous version of API is not supported',	// typo on device!
       
   215 		missingAppID	: '%s:%s:Application ID  Missing',	// double space between ID & missing!!
       
   216 		missingDoc		: '%s:%s:Document/MimeType Missing/value more than expected length ',
       
   217 		missingTID		: '%s:Incorrect TransactionID',
       
   218 		is_invalid		: '%s:%s:%s is invalid'
       
   219 	};
       
   220 		
       
   221 
       
   222 }) ()
       
   223