Fix left over use of old ev.ModemAdded event object structure
[profile/ivi/sockdrawer.git] / Tizen.Device.js
1 ///////////////////////////////////////////////////////////////////////////////
2 // This is a partial implementation of the tizen.application web specification
3 // which using a websocket server for implementing the native backend
4
5 var __application = function () {
6     var appList;
7     var callback;
8
9     var socket = new WebSocket("ws://localhost:7681", "app-list-protocol");
10     socket.onmessage = function(msg) {
11         appList = JSON.parse(msg.data);
12         if (callback)
13             callback(appList);
14     }
15
16     this.launch = function (id, successCallback, errorCallback, argument) {
17         var cmd = {"cmd": "launch", "index": id};
18         socket.send(JSON.stringify(cmd));
19     }
20     this.getAppInfo = function(id) {
21         return appList[id];
22     }
23     this.addAppInfoEventListener = function(eventCallback, errorCallback) {
24         callback = eventCallback;
25     }
26 }
27
28 var __callmanager = function () {
29     var calls = new Array();
30     var callHandlers = new Array();
31     var socket = new WebSocket("ws://localhost:7681", "dialer-protocol");
32     var services = new Array();
33
34     var listener = { 
35         "onAccountUpdated": function(account) { 
36         }, 
37         "onAccountAdded": function(account) { 
38             services.push(new __callservice(socket, account.id));
39         }, 
40         "onAccountRemoved": function(id) {
41             var tmp = new Array();
42             for (var i in services)
43                 if (services[i].id != id)
44                     tmp.push(id);
45             services = tmp;
46         }
47     }
48     tizen.__account.addAccountListener(listener);
49
50     socket.onmessage = function(msg) {
51         var ev = JSON.parse(msg.data);
52
53         if (ev.type == "ModemAdded") {
54             tizen.__account.addAccount(new tizen.Account(ev.ModemAdded, ev));
55         } else if (ev.type == "ModemRemoved") {
56             tizen.__account.deleteAccount(ev.id);
57         } else if (ev.type == "CallAdded") {
58             calls.push(new __call(this, socket, ev));
59         } else if (ev.type == "CallRemoved") {
60             var tmp = new Array();
61             for (var i in calls)
62                 if (calls[i].id != ev.id)
63                     tmp.push(calls[i]);
64             calls = tmp;
65         } else if (ev.type == "PropertyChanged" && ev.State) {
66             if (ev.State == "incoming") {
67                 for (var index in callHandlers)
68                     for (var i in calls)
69                         if (calls[i].id == ev.id)
70                             callHandlers[index].onIncoming(calls[i]);
71             } else if (ev.State == "dialing") {
72                 for (var index in callHandlers)
73                     for (var i in calls)
74                         if (calls[i].id == ev.id)
75                             callHandlers[index].onDialing(calls[i]);
76             } else if (ev.State == "alerting") {
77                 for (var index in callHandlers)
78                     for (var i in calls)
79                         if (calls[i].id == ev.id)
80                             callHandlers[index].onAlerting(calls[i]);
81             } else if (ev.State == "hold") {
82                 for (var index in callHandlers)
83                     for (var i in calls)
84                         if (calls[i].id == ev.id)
85                             callHandlers[index].onHold(calls[i]);
86             } else if (ev.State == "waiting") {
87                 for (var index in callHandlers)
88                     for (var i in calls)
89                         if (calls[i].id == ev.id)
90                             callHandlers[index].onWaiting(calls[i]);
91             } else if (ev.State == "disconnected") {
92                 for (var index in callHandlers)
93                     for (var i in calls)
94                         if (calls[i].id == ev.id)
95                             callHandlers[index].onDisconnected(calls[i]);
96             } else if (ev.State == "disconnecting") {
97                 for (var index in callHandlers)
98                     for (var i in calls)
99                         if (calls[i].id == ev.id)
100                             callHandlers[index].onDisconnecting(calls[i]);
101             } else if (ev.State == "accepted") {
102                 for (var index in callHandlers)
103                     for (var i in calls)
104                         if (calls[i].id == ev.id)
105                             callHandlers[index].onAccepted(calls[i]);
106             } else if (ev.State == "remotelyheld") {
107                 for (var index in callHandlers)
108                     for (var i in calls)
109                         if (calls[i].id == ev.id)
110                             callHandlers[index].onRemotelyHeld(calls[i]);
111             } else if (ev.State == "active") {
112                 for (var index in callHandlers)
113                     for (var i in calls)
114                         if (calls[i].id == ev.id)
115                             callHandlers[index].onActivated(calls[i]);
116             } else if (ev.State == "initializing") {
117                 for (var index in callHandlers)
118                     for (var i in calls)
119                         if (calls[i].id == ev.id)
120                             callHandlers[index].onInitializing(calls[i]);
121             } else if (ev.State == "initialized") {
122                 for (var index in callHandlers)
123                     for (var i in calls)
124                         if (calls[i].id == ev.id)
125                             callHandlers[index].onInitialized(calls[i]);
126             } else {
127                 console.log("Unhandled state: " + ev.State);
128             }
129         } else {
130             console.log("Unhandled event");
131             console.log(ev);
132         }
133     }
134
135     this.history = null;
136     this.isCallInProgress = function() { 
137         return calls.length > 0; 
138     }
139     this.addCallHandler = function(handler, callServiceType, serviceName) { 
140         callHandlers.push(handler);
141     }
142     this.removeCallHandler = function(handler) { 
143         var tmp = new Array();
144         for (var item in callHandlers)
145             if (item != handler)
146                 tmp.push(item);
147         callHandlers = tmp;
148         
149     }
150     this.getCallService = function(account) { 
151         try {
152             for (var i in services)
153                 if (services[i].id == account.id)
154                     return services[i];
155         } catch (ex) {}
156         
157         return services[0];
158     }
159 }
160
161 var __callservice = function (wsi, serviceid) {
162     var socket = wsi;
163
164     var id = serviceid;
165     this.__defineGetter__("id", function() {
166         return id;
167     });
168
169     this.launchDialer = function(remotePartyId) {
170         socket.send(JSON.stringify({ "cmd": "launchDialer" }));
171     }
172
173     var voicemailNumbers = new Array();
174     this.__defineGetter__("voiceNumbers", function() {
175         return voicemailNumbers;
176     });
177     
178     this.makeCall = function(remotePartyId, handler, extension, localVideoOn) {
179         var c = {
180             "cmd": "makeCall", 
181             "remotePartyId": remotePartyId.toString(), 
182             "extension": extension, 
183             "localVideoOn": localVideoOn
184         };
185         socket.send(JSON.stringify(c));
186     }
187 }
188
189 var __call = function(manager, wsi, data) {
190     var socket = wsi;
191
192     var id = data.id;
193     this.__defineGetter__("id", function() {
194         return id;
195     });
196
197     var callData = data;
198     this.__defineGetter__("callData", function() {
199         return callData;
200     });
201     
202     var streamList = new Array();
203     this.__defineGetter__("streamList", function() {
204         return streamList;
205     });
206
207     this.accept = function() {
208         socket.send(JSON.stringify({"cmd": "accept"}));
209     }
210     this.reject = function() {
211         socket.send(JSON.stringify({"cmd": "reject"}));
212     }
213     this.wait = function() {
214         socket.send(JSON.stringify({"cmd": "wait"}));
215     }
216     this.redirect = function(rid) {
217         socket.send(JSON.stringify({"cmd": "redirect"}));
218     }
219     this.end = function() {
220         socket.send(JSON.stringify({"cmd": "end", "path": id}));
221     }
222     this.hold = function() {
223         socket.send(JSON.stringify({"cmd": "hold"}));
224     }
225     this.activate = function() {
226         socket.send(JSON.stringify({"cmd": "activate"}));
227     }
228     this.migrate = function(callService, callParticipant) {
229         socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
230     }
231     this.merge = function(otherCall) {
232         socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
233     }
234     this.drop = function(participant) {
235         socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
236     }
237     this.split = function(participant) {
238         socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
239     }
240     this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
241     }
242     this.addStream = function(addStream, successCallback, errorCallback) {
243     }
244     this.removeStream = function(addStream, successCallback, errorCallback) {
245     }
246     this.setAudioVolume = function(volume, successCallback, errorCallback) {
247     }
248     this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
249     }
250     this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
251     }
252     this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
253     }
254     this.record = function() {
255     }
256     this.pause = function() {
257     }
258 }
259
260 var __accountmanager = function() {
261     var listeners = new Array();
262     var accounts = new Array();
263
264     this.findServices = function(filter) {
265         /* brain dead matching, patches accepted */
266         var result = new Array();
267         if (accounts.length == 0)
268             return result;
269         try {
270             for (var i in accounts) {
271                 var a = accounts[i];
272                 for (var f in filter)
273                     if (a[f] && a[f] == filter[f]) {
274                         result.push(a);
275                         continue;
276                     }
277             }
278         } catch (err) {
279             console.log(err);
280         }
281
282         if (result.length == 0)
283             result.push(accounts[0]);
284
285         return result;
286     }
287     this.getAccountById = function(accountid) {
288         for (var i in accounts)
289             if (accounts[i].id == accountid)
290                 return accounts[i];
291         return null;
292     }
293     this.deleteAccount = function(accountid) {
294         var l = new Array();
295         for (var i in accounts)
296             if (accountid != accounts[i].id)
297                 l.push(accounts[i]);
298         accounts = l;
299
300         for (var i in listeners)
301             listeners[i].onAccountRemoved(accountid);   
302     }
303     this.addAccount = function(account) {
304         accounts.push(account);
305         for (var i in listeners)
306             listeners[i].onAccountAdded(account);
307     }
308     this.addAccountListener = function(observer, errorCallback) {
309         listeners.push(observer);
310         return listeners.length;
311     }
312     this.removeAccountListener = function(handle) {
313         var l = new Array();
314         for (var i in listeners)
315             if (i != handle)
316                 l.push(listeners[i]);
317         listeners = l;
318     }
319 }
320
321 var __tizen = function() {
322     var application = null;
323     this.__defineGetter__("application", function() {
324         if (application == null)
325             application = new __application();
326         return application;
327     });
328
329     this.Account = function (accountId, accountProperties) {
330         var id = accountId;
331         var properties = accountProperties;
332         this.__defineGetter__("id", function() {
333             return id;
334         });
335         for (var p in properties) {
336             this.__defineGetter__(p, function() {
337                 return properties[p];
338             });
339         }
340     }
341
342     var call = null;
343     this.__defineGetter__("call", function() {
344         if (call == null) {
345             // ensure the account object is created first
346             tizen.account;
347
348             call = new __callmanager();
349         }
350         return call;
351     });
352     var account = null;
353     this.__defineGetter__("account", function() {
354         if (account == null)
355             account = new __accountmanager();
356
357         // ensure the call object is always the first listener
358         // for new accounts, which means we need a backdoor to
359         // access the acount object before the getter has finished
360         // executing for the first time
361         this.__defineGetter__("__account", function() {
362             return account;
363         });
364         if (call == null)
365             call = new __callmanager();        
366         return account;
367     });
368 }
369
370 var tizen = new __tizen();