Fix left over use of old ev.ModemAdded event object structure
[profile/ivi/sockdrawer.git] / Tizen.Device.js
index f2aecdb..dce5707 100644 (file)
@@ -6,7 +6,7 @@ var __application = function () {
     var appList;
     var callback;
 
-    var socket = new WebSocket("ws://" + document.URL.substring(7), "app-list-protocol");
+    var socket = new WebSocket("ws://localhost:7681", "app-list-protocol");
     socket.onmessage = function(msg) {
         appList = JSON.parse(msg.data);
        if (callback)
@@ -25,25 +25,346 @@ var __application = function () {
     }
 }
 
-var __dialer = function () {
-    var callback;
+var __callmanager = function () {
+    var calls = new Array();
+    var callHandlers = new Array();
+    var socket = new WebSocket("ws://localhost:7681", "dialer-protocol");
+    var services = new Array();
+
+    var listener = { 
+       "onAccountUpdated": function(account) { 
+       }, 
+       "onAccountAdded": function(account) { 
+           services.push(new __callservice(socket, account.id));
+       }, 
+       "onAccountRemoved": function(id) {
+           var tmp = new Array();
+           for (var i in services)
+               if (services[i].id != id)
+                   tmp.push(id);
+           services = tmp;
+       }
+    }
+    tizen.__account.addAccountListener(listener);
 
-    var socket = new WebSocket("ws://" + document.URL.substring(7), "dialer-protocol");
     socket.onmessage = function(msg) {
-        appList = JSON.parse(msg.data);
-       if (callback)
-           callback(JSON.parse(msg.data));
+        var ev = JSON.parse(msg.data);
+
+       if (ev.type == "ModemAdded") {
+           tizen.__account.addAccount(new tizen.Account(ev.ModemAdded, ev));
+       } else if (ev.type == "ModemRemoved") {
+           tizen.__account.deleteAccount(ev.id);
+       } else if (ev.type == "CallAdded") {
+           calls.push(new __call(this, socket, ev));
+       } else if (ev.type == "CallRemoved") {
+           var tmp = new Array();
+           for (var i in calls)
+               if (calls[i].id != ev.id)
+                   tmp.push(calls[i]);
+           calls = tmp;
+       } else if (ev.type == "PropertyChanged" && ev.State) {
+            if (ev.State == "incoming") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onIncoming(calls[i]);
+           } else if (ev.State == "dialing") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onDialing(calls[i]);
+           } else if (ev.State == "alerting") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onAlerting(calls[i]);
+            } else if (ev.State == "hold") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onHold(calls[i]);
+            } else if (ev.State == "waiting") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onWaiting(calls[i]);
+            } else if (ev.State == "disconnected") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onDisconnected(calls[i]);
+            } else if (ev.State == "disconnecting") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onDisconnecting(calls[i]);
+            } else if (ev.State == "accepted") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onAccepted(calls[i]);
+           } else if (ev.State == "remotelyheld") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onRemotelyHeld(calls[i]);
+            } else if (ev.State == "active") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onActivated(calls[i]);
+            } else if (ev.State == "initializing") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onInitializing(calls[i]);
+            } else if (ev.State == "initialized") {
+                for (var index in callHandlers)
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onInitialized(calls[i]);
+           } else {
+               console.log("Unhandled state: " + ev.State);
+           }
+       } else {
+           console.log("Unhandled event");
+           console.log(ev);
+       }
     }
 
-    this.addDialerEventListener = function(eventCallback, errorCallback) {
-       callback = eventCallback;
+    this.history = null;
+    this.isCallInProgress = function() { 
+        return calls.length > 0; 
+    }
+    this.addCallHandler = function(handler, callServiceType, serviceName) { 
+        callHandlers.push(handler);
+    }
+    this.removeCallHandler = function(handler) { 
+        var tmp = new Array();
+        for (var item in callHandlers)
+            if (item != handler)
+                tmp.push(item);
+        callHandlers = tmp;
+        
+    }
+    this.getCallService = function(account) { 
+       try {
+           for (var i in services)
+               if (services[i].id == account.id)
+                   return services[i];
+       } catch (ex) {}
+       
+       return services[0];
+    }
+}
+
+var __callservice = function (wsi, serviceid) {
+    var socket = wsi;
+
+    var id = serviceid;
+    this.__defineGetter__("id", function() {
+       return id;
+    });
+
+    this.launchDialer = function(remotePartyId) {
+        socket.send(JSON.stringify({ "cmd": "launchDialer" }));
+    }
+
+    var voicemailNumbers = new Array();
+    this.__defineGetter__("voiceNumbers", function() {
+        return voicemailNumbers;
+    });
+    
+    this.makeCall = function(remotePartyId, handler, extension, localVideoOn) {
+        var c = {
+            "cmd": "makeCall", 
+            "remotePartyId": remotePartyId.toString(), 
+            "extension": extension, 
+            "localVideoOn": localVideoOn
+        };
+        socket.send(JSON.stringify(c));
+    }
+}
+
+var __call = function(manager, wsi, data) {
+    var socket = wsi;
+
+    var id = data.id;
+    this.__defineGetter__("id", function() {
+        return id;
+    });
+
+    var callData = data;
+    this.__defineGetter__("callData", function() {
+        return callData;
+    });
+    
+    var streamList = new Array();
+    this.__defineGetter__("streamList", function() {
+        return streamList;
+    });
+
+    this.accept = function() {
+        socket.send(JSON.stringify({"cmd": "accept"}));
+    }
+    this.reject = function() {
+        socket.send(JSON.stringify({"cmd": "reject"}));
+    }
+    this.wait = function() {
+        socket.send(JSON.stringify({"cmd": "wait"}));
+    }
+    this.redirect = function(rid) {
+        socket.send(JSON.stringify({"cmd": "redirect"}));
+    }
+    this.end = function() {
+        socket.send(JSON.stringify({"cmd": "end", "path": id}));
+    }
+    this.hold = function() {
+        socket.send(JSON.stringify({"cmd": "hold"}));
+    }
+    this.activate = function() {
+        socket.send(JSON.stringify({"cmd": "activate"}));
+    }
+    this.migrate = function(callService, callParticipant) {
+        socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
+    }
+    this.merge = function(otherCall) {
+        socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
+    }
+    this.drop = function(participant) {
+        socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
+    }
+    this.split = function(participant) {
+        socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
+    }
+    this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
+    }
+    this.addStream = function(addStream, successCallback, errorCallback) {
+    }
+    this.removeStream = function(addStream, successCallback, errorCallback) {
+    }
+    this.setAudioVolume = function(volume, successCallback, errorCallback) {
+    }
+    this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
+    }
+    this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
+    }
+    this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
+    }
+    this.record = function() {
+    }
+    this.pause = function() {
+    }
+}
+
+var __accountmanager = function() {
+    var listeners = new Array();
+    var accounts = new Array();
+
+    this.findServices = function(filter) {
+       /* brain dead matching, patches accepted */
+       var result = new Array();
+       if (accounts.length == 0)
+           return result;
+       try {
+           for (var i in accounts) {
+               var a = accounts[i];
+               for (var f in filter)
+                   if (a[f] && a[f] == filter[f]) {
+                       result.push(a);
+                       continue;
+                   }
+           }
+       } catch (err) {
+           console.log(err);
+       }
+
+       if (result.length == 0)
+           result.push(accounts[0]);
+
+       return result;
+    }
+    this.getAccountById = function(accountid) {
+       for (var i in accounts)
+           if (accounts[i].id == accountid)
+               return accounts[i];
+       return null;
+    }
+    this.deleteAccount = function(accountid) {
+       var l = new Array();
+       for (var i in accounts)
+           if (accountid != accounts[i].id)
+               l.push(accounts[i]);
+       accounts = l;
+
+       for (var i in listeners)
+           listeners[i].onAccountRemoved(accountid);   
+    }
+    this.addAccount = function(account) {
+       accounts.push(account);
+       for (var i in listeners)
+           listeners[i].onAccountAdded(account);
+    }
+    this.addAccountListener = function(observer, errorCallback) {
+       listeners.push(observer);
+       return listeners.length;
+    }
+    this.removeAccountListener = function(handle) {
+       var l = new Array();
+       for (var i in listeners)
+           if (i != handle)
+               l.push(listeners[i]);
+       listeners = l;
     }
 }
 
 var __tizen = function() {
-    this.application = new __application();
-    this.dialer = new __dialer();
+    var application = null;
+    this.__defineGetter__("application", function() {
+        if (application == null)
+            application = new __application();
+        return application;
+    });
+
+    this.Account = function (accountId, accountProperties) {
+       var id = accountId;
+       var properties = accountProperties;
+       this.__defineGetter__("id", function() {
+           return id;
+       });
+       for (var p in properties) {
+           this.__defineGetter__(p, function() {
+               return properties[p];
+           });
+       }
+    }
+
+    var call = null;
+    this.__defineGetter__("call", function() {
+        if (call == null) {
+            // ensure the account object is created first
+            tizen.account;
+
+            call = new __callmanager();
+        }
+        return call;
+    });
+    var account = null;
+    this.__defineGetter__("account", function() {
+       if (account == null)
+           account = new __accountmanager();
+
+        // ensure the call object is always the first listener
+        // for new accounts, which means we need a backdoor to
+        // access the acount object before the getter has finished
+        // executing for the first time
+        this.__defineGetter__("__account", function() {
+            return account;
+        });
+        if (call == null)
+            call = new __callmanager();        
+       return account;
+    });
 }
 
 var tizen = new __tizen();
-