upload tizen1.0 source
[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         JSContextRef context = priveData->getOnInstalled()->getContext();
59         JSValueRef errorObject = NULL;
60         JSValueRef result = NULL;
61
62         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
63                 Try  {
64                         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
65                         switch(event->getEventCode()) {
66                                 case EventInstalledApplicationChanged::APPLICATON_CHANGED_EVENT_CODE_INSTALLED: 
67                                         LogError(">>> installed event is come");
68                                         result = converter->toJSValueRef(event->getApplicationInformation());
69                                         priveData->getOnInstalled()->callOnSuccess(result);
70                                         break;
71                                 case EventInstalledApplicationChanged::APPLICATON_CHANGED_EVENT_CODE_UPDATED:
72                                         LogError(">>> updated event is come");
73                                         result = converter->toJSValueRef(event->getApplicationInformation());
74                                         priveData->getOnUpdated()->callOnSuccess(result);
75                                         break;
76                                 case EventInstalledApplicationChanged::APPLICATON_CHANGED_EVENT_CODE_UNINSTALLED:
77                                         LogError(">>> uninstalled event is come");
78                                         result = converter->toJSValueRef(event->getApplicationInformation()->getAppId());
79                                         priveData->getOnUninstalled()->callOnSuccess(result);
80                                         break;
81                                 default:
82                                         // TODO: if error callback is not exist???
83                                         LogError(">>> unknown event is come");
84                                         errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
85                                         priveData->getOnInstalled()->callOnError(errorObject);
86                                         break;
87                         }
88                 } Catch (WrtDeviceApis::Commons::Exception) {
89                         LogError("error during function executing");
90                         errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
91                         priveData->getOnInstalled()->callOnError(errorObject);
92                 }
93         } else {
94                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
95                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
96                 priveData->getOnInstalled()->callOnError(errorObject);
97         }       
98 }
99
100 }
101 }
102 }
103