Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / CallStaticController.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16  
17 #include <JavaScriptCore/JavaScript.h>
18 #include <dpl/log/log.h>
19 #include <dpl/shared_ptr.h>
20 #include <Commons/IEvent.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include "CallStaticController.h"
25 #include "Converter.h"
26
27 using namespace TizenApis::Api::Call;
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30
31 namespace TizenApis {
32 namespace Tizen1_0 {
33 CallStaticController& CallStaticController::getInstance()
34 {
35     static CallStaticController controller;
36     return controller;
37 }
38
39 CallStaticController::CallStaticController() :
40         WrtDeviceApis::Commons::EventListener<EventCallHistoryListener>(ThreadEnum::NULL_THREAD)
41 {
42 }
43
44 void CallStaticController::onAnswerReceived(const EventCallHistoryListenerPtr& event)
45 {
46         EventCallHistoryListenerPrivateDataPtr multiCallbacks = 
47                 DPL::DynamicPointerCast<EventCallHistoryListenerPrivateData>(event->getPrivateData());
48
49         assert(multiCallbacks != NULL);
50         assert(multiCallbacks->getOnAdded() != NULL);
51         assert(multiCallbacks->getOnChanged() != NULL);
52
53         JSCallbackManagerPtr defaultCbm = multiCallbacks->getOnAdded(); 
54         JSContextRef context = defaultCbm->getContext();
55         Converter converter(context);
56
57         try {
58                 JSValueRef result = converter.toJSValueRef(event->getResult(), context);
59                 
60                 switch (event->getResultState()) {
61                 case EventCallHistoryListener::ADDED :
62                         multiCallbacks->getOnAdded()->callOnSuccess(result);
63                         break;
64                 case EventCallHistoryListener::CHANGED :
65                         multiCallbacks->getOnChanged()->callOnSuccess(result);
66                         break;                  
67                 }
68         } catch(ConversionException) {
69                 LogError(       "Conversion exception while processing EventCallHistoryListener");
70         }
71 }
72
73 }
74 }