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