Beta merge 2
[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 "ApplicationConverter.h"
25 #include "JSApplicationInformation.h"
26 #include "JSApplicationEvent.h"
27 #include "ApplicationListener.h"
28 #include "API/Application/ApplicationInformation.h"
29 #include "ApplicationInformationEventPrivateData.h"
30
31
32 namespace TizenApis {
33 namespace Tizen1_0 {
34 namespace Application {
35
36 using namespace std;
37 using namespace TizenApis::Commons;
38 using namespace TizenApis::Api::Application;
39 using namespace WrtDeviceApis;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42
43 ApplicationListener& ApplicationListener::getInstance() 
44 {
45         static ApplicationListener instance;
46         return instance;
47 }
48
49 ApplicationListener::ApplicationListener() :
50         WrtDeviceApis::Commons::EventListener<EventInstalledApplicationChanged>(WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD)
51 {
52 }
53
54 void ApplicationListener::onAnswerReceived(const EventInstalledApplicationChangedPtr& event) 
55 {
56         LogError(">>> onAnswerReceived for EventInstalledApplicationChanged");
57         ApplicationInformationEventPrivateDataPtr priveData = DPL::DynamicPointerCast<ApplicationInformationEventPrivateData>(event->getPrivateData());
58
59         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None)
60         {
61                 Try 
62                 {
63                         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(priveData->getOnInstalled()->getContext());
64                         // TODO: why send event code to string. enum value is enough!!
65                         if (event->getEventCode() == APPLICATON_EVENT_CODE_INSTALLED) 
66                         {
67                                 LogError(">>> installed event is come");
68                                 JSValueRef result = converter->toJSValueRef(event->getApplicationInformation());
69                                 priveData->getOnInstalled()->callOnSuccess(result);
70                         } 
71                         else if (event->getEventCode() == APPLICATON_EVENT_CODE_UPDATED) 
72                         {
73                                 LogError(">>> updated event is come");
74                                 JSValueRef result = converter->toJSValueRef(event->getApplicationInformation());
75                                 priveData->getOnUpdated()->callOnSuccess(result);
76                         }
77                         else if (event->getEventCode() == APPLICATON_EVENT_CODE_UNINSTALLED) 
78                         {
79                                 LogError(">>> uninstalled event is come");
80                                 JSValueRef result = converter->toJSValueRef(event->getApplicationInformation()->getAppId());
81                                 priveData->getOnUninstalled()->callOnSuccess(result);
82                         }
83                 }
84                 Catch (WrtDeviceApis::Commons::Exception)
85                 {
86                         LogError("error during function executing");
87                 }
88         }
89         else 
90         {
91                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
92                 JSValueRef errorObject = NULL;
93
94                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
95                 switch(exception){
96                         default:
97                                 errorObject = JSTizenExceptionFactory::makeErrorObject(priveData->getOnInstalled()->getContext(),JSTizenException::UNKNOWN_ERROR,"unknown error");
98                                 break;
99                 }
100                 priveData->getOnInstalled()->callOnError(errorObject);
101         }       
102 }
103
104 }
105 }
106 }
107