8fa770834441c518d8bad5bb75911cc21ed55cda
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / ApplicationListener.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
18 #include <Commons/Exception.h>
19 #include <Tizen/Common/JSTizenExceptionFactory.h>
20 #include <Tizen/Common/JSTizenException.h>
21 #include <JavaScriptCore/JavaScript.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "JSApplicationInformation.h"
25 #include "JSApplicationEvent.h"
26 #include "ApplicationListener.h"
27
28 namespace TizenApis {
29 namespace Tizen1_0 {
30 namespace Application {
31
32 using namespace std;
33 using namespace TizenApis::Commons;
34 using namespace TizenApis::Api::Application;
35 using namespace WrtDeviceApis;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38
39 ApplicationListener& ApplicationListener::getInstance() 
40 {
41         static ApplicationListener instance;
42         return instance;
43 }
44
45 ApplicationListener::ApplicationListener() :
46         WrtDeviceApis::Commons::EventListener<EventInstalledApplicationChanged>(WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD)
47 {
48 }
49
50 void ApplicationListener::onAnswerReceived(const EventInstalledApplicationChangedPtr& event) 
51 {
52         JSCallbackManagerPtr callbackManager = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
53         JSContextRef context = callbackManager->getContext();
54         Converter converter(context);
55         
56         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
57
58                 Try
59                 {               
60                         JSValueRef result = JSApplicationEvent::createJSObject(context, event->getEventCode(), event->getApplicationInformation());
61                         callbackManager->callOnSuccess(result);
62                 }
63                 Catch(WrtDeviceApis::Commons::Exception)
64                 {
65                         LogError("error during function executing");
66                 }
67         }
68         else {
69                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
70                 JSValueRef errorObject = NULL;
71
72                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
73                 switch(exception){
74                         default:
75                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
76                                 break;
77                 }
78                 callbackManager->callOnError(errorObject);
79         }       
80 }
81
82 }
83 }
84 }
85