serviceproviders/jsapi/platformservices/src/media.js
changeset 27 02682e02e51f
child 34 5dae2c62e9b6
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 function __device_media_descriptor(provider){
       
    19   this.interfaceName = provider.interfaceName;
       
    20   this.version = provider.version;
       
    21 }
       
    22 
       
    23 function __device_media_getList(media_cb, match,order, media_errcbk){
       
    24 //  return this.provider.getList(match,order, media_cb);
       
    25    return this.provider.getList(media_cb, match,order, media_errcbk);
       
    26 }
       
    27 function __device_media_getThumbnail(media_cb, match, media_errcbk){
       
    28 
       
    29 
       
    30    return this.provider.getThumbnail(media_cb, match, media_errcbk);
       
    31 }
       
    32 
       
    33 function __device_media_addStreamUri(uri){
       
    34 
       
    35    return this.provider.addStreamUri(uri);
       
    36 }
       
    37 
       
    38 function __device_media_deleteStreamUri(uri){
       
    39 
       
    40    return this.provider.deleteStreamUri(uri);
       
    41 }
       
    42 
       
    43 function __device_media_cancel(transactionId)
       
    44 {
       
    45 
       
    46  this.provider.cancel(transactionId);
       
    47 }
       
    48 
       
    49 function __device_media_refreshMediaDb(uri)
       
    50 {
       
    51 
       
    52  this.provider.refreshMediaDb(uri); 
       
    53 }
       
    54 
       
    55 // Private location prototype: called from service factory
       
    56 function __device_media(provider){
       
    57   //Private properties
       
    58   this.provider = provider;
       
    59   //Read-only properties
       
    60   this.interfaceName = provider.descriptor.interfaceName;
       
    61   this.version = provider.descriptor.version;
       
    62   //constants
       
    63   this.SORT_ASCENDING = 0;
       
    64   this.SORT_DESCENDING = 1;
       
    65     
       
    66   //Extended methods
       
    67   this.getList = __device_media_getList;
       
    68 
       
    69   this.getThumbnail = __device_media_getThumbnail;
       
    70 
       
    71 
       
    72   this.addStreamUri = __device_media_addStreamUri;
       
    73 
       
    74   this.deleteStreamUri = __device_media_deleteStreamUri;
       
    75   
       
    76   this.refreshMediaDb = __device_media_refreshMediaDb;
       
    77   
       
    78   this.cancel = __device_media_cancel;
       
    79 
       
    80 
       
    81 }
       
    82 
       
    83 // media service interface
       
    84 
       
    85 var __device_media_service_entry = {"name": null, 
       
    86 				      "version": null,
       
    87 				      "proto": __device_media,
       
    88 				      "descriptor": __device_media_descriptor,
       
    89 				      "providers": [{"descriptor": __sp_media_descriptor, "instance": __sp_media_instance}]
       
    90 				     };
       
    91 		   
       
    92 
       
    93 
       
    94 
       
    95 
       
    96 /*
       
    97 Copyright � 2009 Nokia. All rights reserved.
       
    98 Code licensed under the BSD License:
       
    99 Software License Agreement (BSD License) Copyright � 2009 Nokia.
       
   100 All rights reserved.
       
   101 Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
       
   102 
       
   103 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
       
   104 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
       
   105 Neither the name of Nokia Corporation. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Nokia Corporation. 
       
   106 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
   107 
       
   108 version: 1.0
       
   109 */
       
   110 /*
       
   111  * Global object for Async utility functions:
       
   112  * 1)addToGlobalArray
       
   113  * 2)getFromArray
       
   114  * 3)removeFromArray
       
   115  */
       
   116 
       
   117 
       
   118 function __device_media_descriptor(provider){
       
   119   this.interfaceName = provider.interfaceName;
       
   120   this.version = provider.version;
       
   121 }
       
   122 
       
   123 function __device_media_getList(media_cb, match,order, media_errcbk){
       
   124 //  return this.provider.getList(match,order, media_cb);
       
   125    return this.provider.getList(media_cb, match,order, media_errcbk);
       
   126 }
       
   127 function __device_media_getThumbnail(media_cb, match, media_errcbk){
       
   128 
       
   129 
       
   130    return this.provider.getThumbnail(media_cb, match, media_errcbk);
       
   131 }
       
   132 
       
   133 function __device_media_addStreamUri(uri){
       
   134 
       
   135    return this.provider.addStreamUri(uri);
       
   136 }
       
   137 
       
   138 function __device_media_deleteStreamUri(uri){
       
   139 
       
   140    return this.provider.deleteStreamUri(uri);
       
   141 }
       
   142 
       
   143 function __device_media_cancel(transactionId)
       
   144 {
       
   145 
       
   146  this.provider.cancel(transactionId);
       
   147 }
       
   148 
       
   149 function __device_media_refreshMediaDb(uri)
       
   150 {
       
   151 
       
   152  this.provider.refreshMediaDb(uri); 
       
   153 }
       
   154 
       
   155 // Private location prototype: called from service factory
       
   156 function __device_media(provider){
       
   157   //Private properties
       
   158   this.provider = provider;
       
   159   //Read-only properties
       
   160   this.interfaceName = provider.descriptor.interfaceName;
       
   161   this.version = provider.descriptor.version;
       
   162   //constants
       
   163   this.SORT_ASCENDING = 0;
       
   164   this.SORT_DESCENDING = 1;
       
   165     
       
   166   //Extended methods
       
   167   this.getList = __device_media_getList;
       
   168 
       
   169   this.getThumbnail = __device_media_getThumbnail;
       
   170 
       
   171 
       
   172   this.addStreamUri = __device_media_addStreamUri;
       
   173 
       
   174   this.deleteStreamUri = __device_media_deleteStreamUri;
       
   175   
       
   176   this.refreshMediaDb = __device_media_refreshMediaDb;
       
   177   
       
   178   this.cancel = __device_media_cancel;
       
   179 
       
   180 
       
   181 }
       
   182 
       
   183 // media service interface
       
   184 
       
   185 var __device_media_service_entry = {"name": null, 
       
   186 				      "version": null,
       
   187 				      "proto": __device_media,
       
   188 				      "descriptor": __device_media_descriptor,
       
   189 				      "providers": [{"descriptor": __sp_media_descriptor, "instance": __sp_media_instance}]
       
   190 				     };
       
   191 		   
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 
       
   199 /*
       
   200 Copyright © 2009 Nokia. All rights reserved.
       
   201 Code licensed under the BSD License:
       
   202 Software License Agreement (BSD License) Copyright © 2009 Nokia.
       
   203 All rights reserved.
       
   204 Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
       
   205 
       
   206 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
       
   207 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
       
   208 Neither the name of Nokia Corporation. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Nokia Corporation. 
       
   209 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
   210 
       
   211 version: 1.0
       
   212 */
       
   213 
       
   214 // S60 sp-based media provider
       
   215 var FILESCHMLEN = 7;
       
   216 
       
   217 //var MISSING_ARG_ERR = 1003;
       
   218 //var error = DeviceException();
       
   219 
       
   220 /*
       
   221 if(undefined==com.nokia.device.media){
       
   222 com.nokia.device.media={};
       
   223 }
       
   224 
       
   225 com.nokia.device.media.SORT_ASCENDING  = 0;
       
   226 com.nokia.device.media.SORT_DESCENDING  = 1;
       
   227 */
       
   228 function __sp_media_descriptor(){
       
   229   //Read-only properties
       
   230   this.interfaceName = "media";
       
   231   if(window.__Service_Interface_Ver)
       
   232 	this.version =__Service_Interface_Ver;
       
   233   else
       
   234 	this.version =1.0;
       
   235   //Class-static properties 
       
   236 }
       
   237 
       
   238 function __sp_device_media_item_build(sp_item){
       
   239     if (!sp_item) {
       
   240 		return null;
       
   241 	}
       
   242     var media_item = {};
       
   243 	modifyObjectBaseProp(media_item);
       
   244     if (sp_item.FileNameAndPath) {
       
   245         var pathname = sp_item.FileNameAndPath.replace(/\\/g, "/");
       
   246         var fileScheme = "file://";
       
   247         media_item.uri = fileScheme + pathname; 
       
   248     }
       
   249     if(sp_item.MediaType){
       
   250 		var mediaType = sp_item.MediaType;
       
   251 		switch( mediaType ){
       
   252 			case 1:
       
   253 			case 2:	 media_item.type	= "audio";
       
   254 					break;
       
   255 			case 3: media_item.type	= "image";
       
   256 					break;
       
   257 			case 4: media_item.type	= "video";
       
   258 					break;
       
   259 			case 5: media_item.type	= "stream";
       
   260 			        break;
       
   261 			default:break;
       
   262 		}
       
   263 	}
       
   264 if(sp_item.FileName){
       
   265 		if(sp_item.MediaType == 3)
       
   266 		{
       
   267 		media_item.title = 	sp_item.FileName;
       
   268 		}
       
   269 	}
       
   270     if (sp_item.FileDate) {
       
   271 		media_item.date = new Date(Date.parse(sp_item.FileDate));
       
   272 	}
       
   273     if (sp_item.FileSize) {
       
   274 		media_item.size = sp_item.FileSize;
       
   275 	}
       
   276     if (sp_item.SongName) {
       
   277 		media_item.title = sp_item.SongName;
       
   278 	}
       
   279     if (sp_item.Artist) {
       
   280 		media_item.artist = sp_item.Artist;
       
   281 	}
       
   282     if (sp_item.Album) {
       
   283 		media_item.album = sp_item.Album;
       
   284 	}
       
   285     if (sp_item.Genre) {
       
   286 		media_item.genre = sp_item.Genre;
       
   287 	}
       
   288     if (sp_item.TrackNumber) {
       
   289 		media_item.track = sp_item.TrackNumber;
       
   290 	}
       
   291     if (sp_item.Composer) {
       
   292 		media_item.composer = sp_item.Composer;
       
   293 	}
       
   294     return media_item;
       
   295 }
       
   296 
       
   297 // ------ Wrap sp iterator ------- 
       
   298 
       
   299 function __sp_media_iterator_get_next(){
       
   300   //__device_debug("sp_media_iterator_get_next");
       
   301   //alert("Before iterator_get_next");
       
   302   var item = this.iter.getNext();
       
   303   //alert("After iterator_get_next");
       
   304   if(typeof item == "undefined"){
       
   305     return null;
       
   306   }
       
   307   var mediaitem =  __sp_device_media_item_build(item);
       
   308   item.close();
       
   309   return mediaitem;
       
   310 }
       
   311 
       
   312 function __sp_media_iterator(js_iterator){
       
   313   this.iter = js_iterator;
       
   314   this.next = __sp_media_iterator_get_next;
       
   315   this.close = function(){
       
   316         this.iter.close();
       
   317     };
       
   318 }
       
   319 
       
   320 function __sp_media_get_field_name(name, type){
       
   321 	 this.error = new DeviceException(0, "Dummy");
       
   322 	//alert("__sp_media_get_field_name");
       
   323 	  if(typeof name != "string"){
       
   324 	    throw new DeviceException(this.error.INVALID_ARG_ERR ,"Media:getList:field is of invalid type");
       
   325 	  }
       
   326      switch(name.toLowerCase()){
       
   327         case "title":
       
   328 			if (type == "Image" || type == "Video") {
       
   329 				//alert("__sp_media_get_field_name video r image");
       
   330 				return "FileName";
       
   331 			}
       
   332 			else {
       
   333 				//alert("__sp_media_get_field_name in else part");
       
   334 				return "SongName";
       
   335 			}
       
   336         case "artist":
       
   337 			//alert(" In artist");
       
   338             return "Artist";
       
   339         case "album":
       
   340 		//alert(" In album");
       
   341             return "Album";
       
   342         case "genre":
       
   343 			//alert(" In genere");
       
   344             return "Genre";
       
   345         case "track":
       
   346 		//alert(" In track");
       
   347             return "TrackNumber";
       
   348         case "composer":
       
   349             return "Composer";
       
   350         default:				
       
   351 		    return null;
       
   352      }
       
   353 }
       
   354 function __sp_media_get_sortfield_name(name, type){
       
   355 	 this.error = new DeviceException(0, "Dummy");
       
   356 	//alert("neme = "+name);
       
   357 	//alert("In get sort field"+typeof name);
       
   358 	  if(typeof name != "string"){
       
   359 	    throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:sortBy is of invalid type");
       
   360 	  }
       
   361 	  var sortfield = name.toLowerCase();
       
   362 	  if( sortfield == "date"){
       
   363 	  	return "FileDate";
       
   364 	  }
       
   365 	  else{
       
   366 	  	return __sp_media_get_field_name(name, type);
       
   367 	  }
       
   368 }
       
   369 
       
   370 
       
   371 // Converting date object to string
       
   372 function __sp_media_get_date_objct(date)
       
   373 {
       
   374 	//alert("In method __sp_media_get_date_objct");
       
   375 	var dateString = "";
       
   376 	// adding year to dateString
       
   377 	dateString = date.getFullYear().toString();
       
   378 
       
   379 	// adding month to dateString
       
   380 	if(date.getMonth() <10)
       
   381 		{
       
   382 		dateString = dateString + ("0")+(date.getMonth().toString());
       
   383 		}
       
   384 	else
       
   385 		{
       
   386 		dateString = dateString + (date.getMonth().toString());
       
   387 		}
       
   388 
       
   389 	// adding day to dateString
       
   390 	var day = date.getDate() -1;
       
   391 	if(day<10)
       
   392 		{
       
   393 		dateString = dateString + ("0")+(day.toString());
       
   394 		}
       
   395 	else
       
   396 		{
       
   397 		dateString = dateString + (day.toString());	
       
   398 		}
       
   399 		
       
   400 		// Adding : to dateString
       
   401 		dateString = dateString + (":");
       
   402 	
       
   403 	// Adding hours to dateString
       
   404 	if(date.getHours()<10)		
       
   405 		{
       
   406 		dateString = dateString + ("0")+(date.getHours().toString());	
       
   407 		}
       
   408 	else
       
   409 		{
       
   410 		dateString = dateString + (date.getHours().toString());		
       
   411 		}
       
   412 		
       
   413 	// Adding minutes to dateString
       
   414 	if(date.getMinutes()<10)		
       
   415 		{
       
   416 		dateString = dateString + ("0")+(date.getMinutes().toString());	
       
   417 		}
       
   418 	else
       
   419 		{
       
   420 		dateString = dateString + (date.getMinutes().toString());		
       
   421 		}
       
   422 
       
   423 	// Adding seconds to dateString
       
   424 	if(date.getSeconds()<10)		
       
   425 		{
       
   426 		dateString = dateString + ("0")+(date.getSeconds().toString());	
       
   427 		}
       
   428 	else
       
   429 		{
       
   430 		dateString = dateString + (date.getSeconds().toString());		
       
   431 		}
       
   432 	return dateString;
       
   433 }
       
   434 
       
   435 function __sp_media_getList_cb(arg1, arg2, arg3) {
       
   436 	//alert("__sp_media_getList_cb" + arg1);
       
   437 	var media_cb;
       
   438 	var media_errcbk;
       
   439 	 var CbObj;
       
   440     
       
   441     //alert("1111111111");
       
   442     CbObj = glob_obj.getFromArray(arg1);
       
   443 	// alert("222222222222");
       
   444     
       
   445     if (CbObj) {
       
   446 		 //alert("33333333333");
       
   447         media_cb = CbObj.success_cb;
       
   448 		// alert("4444444444444444");
       
   449         media_errcbk = CbObj.error_cb;
       
   450 		// alert("5555555555555555");
       
   451     }
       
   452     
       
   453     else {
       
   454 		//alert("6666666666666666");
       
   455         alert("Media : __sp_media_getList_cb: Callback not found ");
       
   456 		return;
       
   457     }
       
   458     
       
   459 		if(arg3.ErrorCode != 0 && arg3.ErrorCode != 1010)
       
   460 		{
       
   461 			//alert("77777777777777");
       
   462 			media_errcbk(new DeviceException( arg3.ErrorCode,"Media: getList: Operation Failed"));
       
   463 			return;
       
   464 		}
       
   465 		if (arg2 != event_cancelled) {
       
   466 			//alert("888888888");
       
   467 			var iter = null;
       
   468 			if (arg3.ReturnValue) {
       
   469 				//alert("11");
       
   470 				iter = new __sp_media_iterator(arg3.ReturnValue);
       
   471 				//alert("12");
       
   472 			}
       
   473 			//alert( " before sucess call back");
       
   474 			media_cb(iter); // Need to make change later on as case of eventid
       
   475 			//alert( " After sucess call back");
       
   476 		}
       
   477 		
       
   478 		glob_obj.removeFromArray(arg1);
       
   479   }
       
   480 
       
   481 function __sp_media_getList(media_cb, match, order, media_errcbk){	
       
   482 	//alert("In __sp_media_getList wraper ");
       
   483 	if (!media_cb) {		
       
   484 		throw new DeviceException(this.error.MISSING_ARG_ERR,"Media:getList:Callback is missing");
       
   485 	}
       
   486 	else 
       
   487 		if (typeof media_cb != "function") //here we check if the type of callback is function. This case is useful when arbitrary values are passed to callback
       
   488 		{			
       
   489 			throw new DeviceException(this.error.INVALID_ARG_ERR,"Media:getList:Callback is of invalid type");
       
   490 		}
       
   491 	if(media_errcbk)
       
   492 	{ 		
       
   493 		if(typeof media_errcbk != 'function')
       
   494 		{			
       
   495 		   	throw new DeviceException(this.error.INVALID_ARG_ERR, "Media: getList: error callback is invalid");
       
   496 		}
       
   497 	}
       
   498 	
       
   499 	 if((typeof match) != 'object'){
       
   500   			//alert(" match is not object");
       
   501   	      	throw  new DeviceException(this.error.INVALID_ARG_ERR, "Invalid Argument:" +match);	
       
   502 			}
       
   503 	
       
   504 		//alert(" In my match");
       
   505 		if (match) {
       
   506 			if (('sortBy' in match) && (match.sortBy == undefined || match.sortBy == null || match.sortBy == "")) {
       
   507 				//alert(" In condition" + this.error.INVALID_ARG_ERR);
       
   508 				throw new DeviceException(this.error.INVALID_ARG_ERR, "Media: getList: sortby is invalid");
       
   509 				
       
   510 			}
       
   511 		}
       
   512 	//alert(" After match");
       
   513 
       
   514   var criteria = {};  
       
   515   modifyObjectBaseProp(criteria);  
       
   516   criteria.Type = 'FileInfo';  
       
   517   criteria.Filter = {};
       
   518   modifyObjectBaseProp(criteria.Filter);
       
   519   criteria.Sort = {};
       
   520   modifyObjectBaseProp(criteria.Sort);
       
   521   if (match) {  	
       
   522   	  if(match.type){	  	
       
   523 	  	  if(typeof match.type != "string"){		  	
       
   524 		  throw new DeviceException(this.error.INVALID_ARG_ERR,"Media:getList:type is of invalid type");
       
   525 		  }
       
   526 	      switch (match.type.toLowerCase()) {
       
   527 	          case "audio":
       
   528 //	              criteria.Filter.FileType = "Music";
       
   529  	              criteria.Filter.FileType = "audio";
       
   530 	              break;
       
   531 	          case "image":
       
   532 	              criteria.Filter.FileType = "Image";
       
   533 	              break;
       
   534 	          case "video":
       
   535 			  		//alert(" In Video");
       
   536 	              criteria.Filter.FileType = "Video";
       
   537 	              break;
       
   538 	          case "stream":
       
   539 	              criteria.Filter.FileType = "StreamingURL";
       
   540 	              break;
       
   541 			  default:			 
       
   542 			  	  throw new DeviceException(this.error.INVALID_ARG_ERR,"Media:getList: type is invalid");
       
   543 	      }
       
   544 		  
       
   545 		  if( ('field' in match) && ( match.field == undefined || match.field == null) )
       
   546         		   throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:field is invalid");
       
   547 				   
       
   548 				   
       
   549 	      if (match.field != null) {
       
   550 	          criteria.Filter.Key = __sp_media_get_field_name(match.field.name,criteria.Filter.FileType);
       
   551 			  if( criteria.Filter.Key == null ){			  
       
   552 			  throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:key is invalid");	
       
   553 			  }
       
   554 	          if(match.field.value){
       
   555 			  criteria.Filter.StartRange = match.field.value;
       
   556 			  if(criteria.Filter.Key == "TrackNumber"){
       
   557 			  criteria.Filter.EndRange = match.field.value;  
       
   558 			  }
       
   559 	          //Note: date matches not supported currently		  	
       
   560 			  }			  
       
   561 	      }
       
   562 		  else{ // When match is not defined then we will look for daterange
       
   563 		  	if(match.dateRange)
       
   564 			{				
       
   565 				if(typeof match.dateRange != "object")
       
   566 					{						
       
   567 					throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:dateRange is of invalid type");		
       
   568 					}
       
   569 				//alert("got daterange");
       
   570 				criteria.Filter.Key = "FileDate";
       
   571 				if((typeof match.dateRange.start != "undefined") && (typeof match.dateRange.end != "undefined"))
       
   572 				{
       
   573 						//alert(" In range");
       
   574 				if((typeof match.dateRange.start != 'object') || ( match.dateRange.start == "") ||(match.dateRange.start == null)){
       
   575 				throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:dateRange:start is of invalid type");		
       
   576 				}
       
   577 				else{
       
   578 				criteria.Filter.StartRange = __sp_media_get_date_objct(match.dateRange.start);	
       
   579 				}	
       
   580 					
       
   581 				if((typeof match.dateRange.end != 'object') || ( match.dateRange.end == "")||(match.dateRange.end == null)){
       
   582 					//alert(" end is empty");
       
   583 				throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:dateRange:end is of invalid type");		
       
   584 				}
       
   585 				else{
       
   586 					//alert(" ")
       
   587 				criteria.Filter.EndRange   = __sp_media_get_date_objct(match.dateRange.end);	
       
   588 				}	
       
   589 				
       
   590 				}
       
   591 				else{					 
       
   592 					throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:start or end of dateRange is missing"); //  frm missing to invalid for test case 60 & 65
       
   593 				}
       
   594 			}		
       
   595 				 else if( ('dateRange' in match) &&(match.dateRange == 0 || match.dateRange == null || match.dateRange == "")){
       
   596 					//alert(" In else match.dateRange = "+typeof(match.dateRange));
       
   597 					throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList: dateRange is Invalid");
       
   598 				}	
       
   599 		  }
       
   600 	      if (match.sortBy) {
       
   601 	          criteria.Sort.Key = __sp_media_get_sortfield_name(match.sortBy, criteria.Filter.FileType);
       
   602 			  if( criteria.Sort.Key == null ){
       
   603 			  throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:sortBy is invalid");	
       
   604 			  }
       
   605 	      } 
       
   606 		  else {
       
   607 		  criteria.Sort.Key = "FileDate";
       
   608 		  }
       
   609 		  // Adding sort order
       
   610 		  if(order)
       
   611 		  {  	
       
   612 				
       
   613 				if (order == this.SORT_ASCENDING) {
       
   614 								//alert(" Ascending ");
       
   615 								criteria.Sort.Order = "Ascending";
       
   616 							}
       
   617 							// else if(order == com.nokia.device.SORT_DESCENDING || order == com.nokia.device.media.SORT_DESCENDING)
       
   618 				else if (order == this.SORT_DESCENDING) {
       
   619 									criteria.Sort.Order = "Descending";
       
   620 								}				
       
   621 								
       
   622 				else if((order != this.SORT_ASCENDING) || (order != this.SORT_DESCENDING)) {
       
   623 								throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:sortBy is invalid");
       
   624 			}
       
   625 		  }
       
   626 		  else
       
   627 		  {
       
   628 		  criteria.Sort.Order = "Ascending"; 		
       
   629 		  }
       
   630 	      //criteria.Sort.Order = "Descending"; 
       
   631 		  //Note: sort order has been removed because of ambiguous behavior: if FileDate
       
   632 		  // is specified then sapi sorts in ascending order by default, if FileDate is not specified,
       
   633 		  // sorting is performed on FileDate in descending order. 	  	
       
   634 	  }
       
   635 	  else{
       
   636 	  			//alert( " In match.type missing");
       
   637 	  	throw new DeviceException(this.error.MISSING_ARG_ERR, "Media:type is missing");
       
   638 	  }	
       
   639   }
       
   640   else {
       
   641       // Must supply something to sp, so default to music .... Need to change to all
       
   642 //      criteria.Filter.FileType = "Music"; // here need to make all
       
   643         criteria.Filter.FileType = "all"; 
       
   644         criteria.Sort.Key = "FileDate";
       
   645 	  //	  criteria.Sort.Order = "Descending";
       
   646 	  criteria.Sort.Order = "Ascending"; // By default it should be ascending
       
   647   }
       
   648   
       
   649   try {    
       
   650     //__device_debug("Getting media list");
       
   651 	//alert(" before calling sapi");
       
   652 	temp_scb = media_cb;
       
   653 	temp_ecb = media_errcbk;
       
   654     var rval = this.so.IDataSource.GetList(criteria, this.getListMediaCb);
       
   655 	//alert(" errorcode = "+rval.ErrorCode);
       
   656 	//alert(" errorcode = "+rval.TransactionID);
       
   657 	if (rval.TransactionID) {
       
   658 		//alert(" In if of TransactionID");
       
   659         glob_obj.addToGlobalArray(rval.TransactionID, media_cb, media_errcbk);
       
   660     }
       
   661 	//alert("rval.errorcode = "+rval.ErrorCode);
       
   662 	if(criteria.Sort)
       
   663 	{
       
   664 	delete criteria.Sort;	
       
   665 	}	
       
   666 	if(criteria.Filter.StartRange)
       
   667 	{
       
   668 	delete 	criteria.Filter.StartRange;
       
   669 	}
       
   670 
       
   671 	if(criteria.Filter.EndRange)
       
   672 	{
       
   673 	delete 	criteria.Filter.EndRange;
       
   674 	}
       
   675 
       
   676 	if(criteria.Filter)
       
   677 	{
       
   678 	delete 	criteria.Filter;
       
   679 	}
       
   680 
       
   681     if(rval.ErrorCode != 0){
       
   682     	  switch(MapErrorCode[rval.ErrorCode]){
       
   683 				case this.error.MISSING_ARG_ERR :
       
   684 				case this.error.INVALID_ARG_ERR :
       
   685 				case this.error.NOT_SUPPORTED_ERR :
       
   686         		//case err_missing_argument :
       
   687         		//case err_bad_argument :
       
   688 						//case err_InvalidService_Argument :
       
   689 						//case err_ServiceNotSupported :
       
   690 							
       
   691 						if(rval.ErrorMessage){ 
       
   692             			var err_msg = splitErrorMessage(rval.ErrorMessage); 	
       
   693 			            throw new DeviceException(MapErrorCode[rval.ErrorCode],"Media:getList:"+err_msg);						    
       
   694 						}
       
   695 						else{
       
   696 						throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:getList:Operation failed");	
       
   697 						}
       
   698 						break;
       
   699 						default:						
       
   700 						//media_cb(iter) ;
       
   701 						//err_cb(new DeviceException(MapErrorCode[rval.ErrorCode], "Media: getList: Operation Failed"));
       
   702 						media_errcbk(new DeviceException(MapErrorCode[rval.ErrorCode], "Media: getList: Operation Failed"));
       
   703     	  }
       
   704     }
       
   705   return rval.TransactionID;
       
   706   }
       
   707   catch (e) {
       
   708     __device_handle_exception(e, '__sp_media_getList: '+e);
       
   709   }  
       
   710 }
       
   711 
       
   712 function __sp_media_getThumbnail_cb(arg1, arg2, arg3) {
       
   713 	var thumbnail_errcbk;
       
   714 	var thumbnail_cb;
       
   715 	 var CbObj;
       
   716     
       
   717     
       
   718     CbObj = glob_obj.getFromArray(arg1);
       
   719     
       
   720     if (CbObj) {
       
   721         thumbnail_cb = CbObj.success_cb;
       
   722         thumbnail_errcbk = CbObj.error_cb;
       
   723     }
       
   724     
       
   725     else {
       
   726         alert("Media : __sp_media_getThumbnail_cb: Callback not found ");
       
   727 		return;
       
   728     }
       
   729     
       
   730 	if(arg3.ErrorCode != 0 && arg3.ErrorCode != 1010)
       
   731 	{
       
   732 		
       
   733 		thumbnail_errcbk(new DeviceException(arg3.ErrorCode, "Media: getThumbnail: Operation Failed"));
       
   734 		return;
       
   735 	}
       
   736 	if (arg2 != event_cancelled) {
       
   737 		var thumbUrl = null;
       
   738 		if (arg3.ReturnValue) {
       
   739 			thumbUrl = arg3.ReturnValue.replace(/\\/g, "/");
       
   740 			var fileScheme = "file://";
       
   741 			thumbUrl = fileScheme + thumbUrl;
       
   742 		}
       
   743 		thumbnail_cb(thumbUrl);
       
   744 	} 
       
   745 	glob_obj.removeFromArray(arg1);
       
   746 	}
       
   747 	
       
   748 // Code for getthumbnail
       
   749 function __sp_media_getThumbnail(thumbnail_cb,thumbnailInfo, thumbnail_errcbk)
       
   750 {
       
   751 	//alert("In __sp_media_getThumbnail");
       
   752 	// Checking validation for callback method
       
   753 	if(!thumbnail_cb)
       
   754   	{
       
   755   		throw new DeviceException(this.error.MISSING_ARG_ERR, "Media:getThumbnail:Callback is missing");
       
   756   	}
       
   757   	else if(typeof thumbnail_cb != "function")	//here we check if the type of callback is function. This case is useful when arbitrary values are passed to callback
       
   758   	{
       
   759   		throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getList:Callback is invalid");
       
   760   	}
       
   761 	if(thumbnail_errcbk)
       
   762 	{ 
       
   763 		var err_cb = thumbnail_errcbk;
       
   764 		//alert(" In thumbnail_errcbk");
       
   765 		if(typeof thumbnail_errcbk != 'function')
       
   766 		{
       
   767 			//alert(" thumbnail_errcbk != function");
       
   768 		   	throw new DeviceException(this.error.INVALID_ARG_ERR, "Media: getThumbnail: error callback is invalid");
       
   769 			//err_cb(new DeviceException(INVALID_ARG_ERR, "Media: getThumbnail: error callback is invalid"));
       
   770 		}
       
   771 	}
       
   772 
       
   773 
       
   774 	// Creating url and thumbnail
       
   775 	var inputParam = {};
       
   776 	modifyObjectBaseProp(inputParam);
       
   777 	if (thumbnailInfo) {
       
   778 		if(typeof thumbnailInfo != "object" ){			
       
   779 			throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getThumbnail:thumbnailInfo is of invalid type object");
       
   780 			}
       
   781 		if (thumbnailInfo.uri) {			
       
   782 			if (typeof thumbnailInfo.uri != "string") {				
       
   783 				throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getThumbnail:uri is not of type string");
       
   784 				
       
   785 				}
       
   786 			if (thumbnailInfo.uri.search("file://") == 0) {				
       
   787 				url = thumbnailInfo.uri.slice(FILESCHMLEN);
       
   788 				inputParam.Url = url.replace(/\//g, "\\");
       
   789 			}
       
   790 			else {				
       
   791 				throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getThumbnail:uri is not in file protocol");
       
   792 			}
       
   793 		}
       
   794 		else {			
       
   795 			throw new DeviceException(this.error.MISSING_ARG_ERR, "Media:getThumbnail:uri is missing");
       
   796 		}
       
   797 	}
       
   798 	else{		
       
   799 		throw new DeviceException(this.error.MISSING_ARG_ERR, "Media:thumbnailInfo is missing");
       
   800 		}	
       
   801 	//for thumbanilsize
       
   802 
       
   803 	if ( thumbnailInfo.size == null ||  thumbnailInfo.size == "" ){
       
   804 		//alert(" size is null");
       
   805 		throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getThumbnail:size is not of type Object");
       
   806 	}
       
   807 	if (thumbnailInfo.size) {
       
   808 	
       
   809 		// chekcing if sise is object or not
       
   810 		//alert(" In thumbnail size");
       
   811 		if(typeof thumbnailInfo.size != "object" ){
       
   812 				throw new DeviceException(this.error.INVALID_ARG_ERR, "Media:getThumbnail:size is not of type Object");
       
   813 		}
       
   814 		if((typeof thumbnailInfo.size.width == "undefined") && ( typeof thumbnailInfo.size.height == "undefined"))
       
   815 			{
       
   816 			throw new DeviceException(this.error.MISSING_ARG_ERR, "Media:thumbnailInfo should have atleast width or height");	
       
   817 			}
       
   818 		else{
       
   819 			var thumbnailsize = {};
       
   820 			modifyObjectBaseProp(thumbnailsize);
       
   821 			if (typeof thumbnailInfo.size.width != "undefined") // Need to put check on this
       
   822 				{
       
   823 				thumbnailsize.width = thumbnailInfo.size.width;
       
   824 				}
       
   825 			if (typeof thumbnailInfo.size.height != "undefined") 
       
   826 				{
       
   827 				thumbnailsize.height = thumbnailInfo.size.height;
       
   828 				}
       
   829 			inputParam.ThumbnailSize = thumbnailsize;
       
   830 			delete thumbnailsize;
       
   831 			}
       
   832 	
       
   833 		}
       
   834 
       
   835   try {    
       
   836    	temp_scb = thumbnail_cb;
       
   837 	temp_ecb = thumbnail_errcbk;
       
   838     var rval = this.so.IDataSource.GetThumbnail(inputParam, this.getThumbnailCb);
       
   839 	//alert(" errorcode = "+rval.ErrorCode);
       
   840 	if (rval.TransactionID) {
       
   841         glob_obj.addToGlobalArray(rval.TransactionID, thumbnail_cb, thumbnail_errcbk);
       
   842     }
       
   843 	delete inputParam;
       
   844 
       
   845     if(rval.ErrorCode != 0){
       
   846 		//alert(" In if loop = "+ MapErrorCode[rval.ErrorCode]);
       
   847 		//alert(" In not supported= "+ this.error.NOT_SUPPORTED_ERR);
       
   848 		//alert(" in if of rval.errorcode" +MapErrorCode[rval.ErrorCode]);	
       
   849 		//alert(" MISSING_ARG_ERR = " +MapErrorCode[1003]);	
       
   850 		//var errmsg = null;
       
   851 		//errmsg = splitErrorMessage(rval.ErrorMessage); 		
       
   852     	  switch(MapErrorCode[rval.ErrorCode]){		  	        				
       
   853         				case this.error.MISSING_ARG_ERR :
       
   854 						{
       
   855 							//alert(" in switch MISSING_ARG_ERR");
       
   856 							thumbnail_errcbk(new DeviceException(this.error.MISSING_ARG_ERR, "Media: getThumbnail: Operation Failed"));
       
   857 						}
       
   858 						break;	
       
   859 						case this.error.NOT_SUPPORTED_ERR :	{
       
   860 							//alert(" In not supported");
       
   861 							thumbnail_errcbk( new DeviceException(MapErrorCode[rval.ErrorCode], "Media:getThumbnail:Operation failed"));
       
   862 						}						
       
   863 						break;				
       
   864 						case this.error.INVALID_ARG_ERR :												
       
   865 						throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:getThumbnail:Operation failed");							
       
   866 						break;
       
   867 						default:	
       
   868 							//throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:getThumbnail:Operation failed");					
       
   869 							//thumbnail_errcbk
       
   870 							if (thumbnail_errcbk) {
       
   871 							
       
   872 								thumbnail_errcbk(new DeviceException(MapErrorCode[rval.ErrorCode], "Media:getThumbnail:Operation failed"));
       
   873 							}
       
   874 							
       
   875 							else {
       
   876 							
       
   877 								throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:getThumbnail:Operation failed");
       
   878 							}
       
   879 							
       
   880 						
       
   881     	  }		  	
       
   882     }	
       
   883   return rval.TransactionID;
       
   884   }
       
   885   catch (e) {		
       
   886   		//alert(" In exception");
       
   887     __device_handle_exception(e, '__sp_media_getThumbnail: '+e);
       
   888   }  		
       
   889 	
       
   890 }
       
   891 
       
   892 function __sp_media_addStreamUri(uri)
       
   893 	{
       
   894 	throw new DeviceException(MapErrorCode[rval.ErrorCode],"Media:addStreamUri:Not Supported");	
       
   895 	}
       
   896 function __sp_media_deleteStreamUri(uri)
       
   897 	{
       
   898 	throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:deleteStreamUri:Not Supported");	
       
   899 	}
       
   900 
       
   901 function 	__sp_media_refreshMediaDb(uri)
       
   902 {
       
   903 
       
   904 }
       
   905 
       
   906 function __sp_media_cancel(transactionId)
       
   907 {
       
   908 	try{
       
   909 		var rval = this.so.IDataSource.Cancel(transactionId);
       
   910 	
       
   911 	    if(rval.ErrorCode != 0){
       
   912     	  switch(rval.ErrorCode){
       
   913         				//case err_missing_argument :
       
   914         				//case err_bad_argument :
       
   915 						//case err_InvalidService_Argument :
       
   916 						//case err_ServiceNotSupported :
       
   917 							case this.error.MISSING_ARG_ERR :
       
   918 							case this.error.INVALID_ARG_ERR :							
       
   919 							case this.error.NOT_SUPPORTED_ERR :
       
   920 						if(rval.ErrorMessage){ 
       
   921             			var err_msg = splitErrorMessage(rval.ErrorMessage); 	
       
   922 			            throw new DeviceException(MapErrorCode[rval.ErrorCode],"Media:cancel:"+err_msg);						    
       
   923 						}
       
   924 						else{
       
   925 						throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:cancel:Operation failed");	
       
   926 						}
       
   927 						break;
       
   928 						default:
       
   929 						throw new DeviceException(MapErrorCode[rval.ErrorCode], "Media:cancel:Operation failed");
       
   930     	  }
       
   931     	}
       
   932 
       
   933 	}
       
   934 	catch(e){
       
   935 	   __device_handle_exception(e, '__sp_media_refreshMediaDb: '+e);
       
   936 	}
       
   937 }
       
   938 
       
   939 function __sp_media_instance(){
       
   940   
       
   941   //Descriptor
       
   942   this.descriptor = new __sp_media_descriptor();
       
   943   //Extended methods
       
   944   this.SORT_ASCENDING = 0;
       
   945   this.SORT_DESCENDING = 1;
       
   946   this.getList = __sp_media_getList;
       
   947   this.getListMediaCb = __sp_media_getList_cb;
       
   948   //Method for gethumbnail
       
   949 
       
   950   this.getThumbnail = __sp_media_getThumbnail;
       
   951   this.getThumbnailCb = __sp_media_getThumbnail_cb;
       
   952 	// Method for addStreamuri
       
   953   this.addStreamUri = __sp_media_addStreamUri;
       
   954   
       
   955   //Method for deleteStreamUri
       
   956   this.deleteStreamUri = __sp_media_deleteStreamUri;
       
   957   
       
   958   //Method for refreshMediaDb
       
   959   this.refreshMediaDb = __sp_media_refreshMediaDb;
       
   960   
       
   961   //Method for cancel
       
   962   this.cancel = __sp_media_cancel;
       
   963   //Private data
       
   964   this.error = new DeviceException(0, "Dummy");
       
   965   try {
       
   966     //__device_debug("Trying media service");
       
   967       this.so = device.getServiceObject("Service.MediaManagement", "IDataSource");
       
   968     //__device_debug("Got media service object");    
       
   969   }
       
   970   catch (e){
       
   971     this.so = null;
       
   972     __device_handle_exception(e, "media service not available");
       
   973   }
       
   974 }