mypackage_wrt/preview/script/lib/sapi/MediaManagement.js
author nithyar
Mon, 20 Sep 2010 13:47:38 +0100
changeset 61 3c5e0637d4d3
parent 42 20be4dd42b12
permissions -rwxr-xr-x
Integration plan updates, week37
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     1
/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     2
 * MediaManagement.js
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     3
 * 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     4
 * Nokia Web Runtime Service API emulation 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     5
 * WRT v1.1
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     6
 * 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     7
 * Copyright 2009 Nokia Corporation. All rights reserved.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     8
*/
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     9
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    10
 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    11
(function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    12
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    13
	var provider = 'Service.MediaManagement' ,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    14
		Interface = 'IDataSource';
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    15
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    16
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    17
	 * MediaManagement service
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    18
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    19
	var MediaManagementService = function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    20
		this.GetList 	= __GetList;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    21
		this.Cancel 	= __Cancel;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    22
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    23
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    24
	device.implementation.extend(provider, Interface, new MediaManagementService() );
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    25
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    26
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    27
	/******************************************************/	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    28
	/******************************************************/	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    29
	/******************************************************/	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    30
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    31
	var	context = device.implementation.context,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    32
		_t = context._t,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    33
		method = '',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    34
		result = false,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    35
		DBase = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    36
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    37
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    38
	 * MediaManagement: GetList
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    39
	 * @param {Object} criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    40
	 * @param {Function} callback function for async call (mandatory)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    41
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    42
	function __GetList(criteria, callback, _flag){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    43
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    44
		if ((result = validator.apply('GetList', arguments)) !== false)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    45
			return result; 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    46
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    47
		// _flag=true indicates re-called state
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    48
		_flag = _flag || false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    49
		if (!_flag) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    50
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    51
			// callback is mandatory
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    52
			if (typeof callback != 'function')
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    53
				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.badAsync);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    54
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    55
			// continue validation after callback check		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    56
			if (!criteria.Filter) 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    57
				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.is_missing, 'Filter');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    58
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    59
			if (!criteria.Filter.FileType) 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    60
				return error(device.implementation.ERR_MISSING_ARGUMENT, msg.is_missing, 'FileType');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    61
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    62
			if (!/^(Music|Sound|Image|Video|StreamingURL)$/i.test(criteria.Filter.FileType)) 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    63
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    64
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    65
			// process callback
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    66
			_flag = true;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    67
			return context.callAsync(this, arguments.callee, criteria, callback, _flag);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    68
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    69
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    70
		var returnValue = [], 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    71
			match = null,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    72
			fileType = criteria.Filter.FileType,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    73
			filter = criteria.Filter;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    74
 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    75
 		// normalize filetype
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    76
		fileType = fileType[0].toUpperCase() + fileType.substr(1).toLowerCase();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    77
		fileType = fileType.replace(/url/i, 'URL');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    78
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    79
		DBase = context.getData(provider);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    80
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    81
		// unsupported filters 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    82
		if (filter 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    83
			&& (match = context.keys(filter).join().match(/Key|StartRange|EndRange/ig)) ) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    84
			context.notify(_t('%s:: GetList : filter %s not implemented in preview').arg(provider, match.join()));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    85
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    86
		// unsupported sort			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    87
		if (criteria.Sort) { 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    88
			context.notify(_t('%s:: GetList : sort not implemented in preview').arg(provider));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    89
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    90
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    91
		returnValue = DBase[fileType];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    92
		return context.Result(context.Iterator(returnValue));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    93
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    94
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    95
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    96
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    97
	 * MediaManagement: Cancel
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    98
	 * @param {Object} criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    99
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   100
	function __Cancel(criteria){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   101
		method = 'Cancel';
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   102
		if (!criteria || !criteria.TransactionID)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   103
			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.missingTID);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   104
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   105
		clearTimeout(criteria.TransactionID);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   106
		return context.ErrorResult(device.implementation.ERR_SUCCESS);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   107
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   108
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   109
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   110
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   111
	/*******************************
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   112
	 * helper functions
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   113
	 *******************************/
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   114
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   115
	function error(code, msg /*, args...*/){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   116
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   117
		var args = ['MediaMgmt',method].concat([].slice.call(arguments,2));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   118
		msg = msg ? _t().arg.apply(msg,args) : undefined;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   119
		return context.ErrorResult(code, msg);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   120
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   121
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   122
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   123
	 * validate common input arguments
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   124
	 * 'this' is string (object) name of calling function
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   125
	 * 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   126
	 * @param {arguments} arguments of calling function
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   127
	 * @return {Result} Result object if error, false if no error.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   128
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   129
	function validator() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   130
		method = ''+this;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   131
		var	failed = false,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   132
			criteria = arguments[0] || false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   133
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   134
		if (!criteria || typeof criteria != 'object' || typeof criteria.Type == 'undefined')
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   135
			return error(device.implementation.ERR_MISSING_ARGUMENT, msg.is_missing, 'Type');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   136
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   137
		if (!/^FileInfo$/i.test(criteria.Type)) 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   138
			return error(device.implementation.ERR_SERVICE_NOT_SUPPORTED, msg.badType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   139
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   140
		return failed;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   141
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   142
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   143
	/** 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   144
	 * error messages
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   145
	 * order of %s args: Service name, method name, parameter name 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   146
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   147
	var msg = {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   148
		badType			: '%s : %s : Type not supported',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   149
		missingTID 		: '%s : %s : TransactionID is missing',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   150
		badAsync		: '%s : %s : Insufficient arguments for async request',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   151
		is_missing		: '%s : %s : %s is missing',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   152
		is_invalid		: '%s : %s : %s is invalid'
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   153
	};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   154
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   155
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   156
}) ()
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   157