b43cee86180682fa9d67effec897b2ae96475b63
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / ApplicationAnswerReceiver.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 <Commons/Exception.h>
18 #include <Tizen/Common/JSTizenExceptionFactory.h>
19 #include <Tizen/Common/JSTizenException.h>
20 #include <JavaScriptCore/JavaScript.h>
21 #include "JSApplicationInformation.h"
22 #include "ApplicationAnswerReceiver.h"
23 #include "ApplicationConverter.h"
24
25 namespace TizenApis {
26 namespace Tizen1_0 {
27 namespace Application {
28
29 using namespace std;
30 using namespace TizenApis::Api::Application;
31 using namespace WrtDeviceApis;
32 using namespace TizenApis::Commons;
33
34 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const CommonsJavaScript::JSCallbackManagerPtr &callbackManager) :
35         EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
36         EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),      
37         m_callbackManager(callbackManager) {
38 }
39
40 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const LaunchServicePrivateDataPtr &launchServiceCallbackManager) :
41         EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
42         EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
43         m_privateData(launchServiceCallbackManager) {
44         LogDebug("<<<");
45 }
46         
47 ApplicationAnswerReceiver::~ApplicationAnswerReceiver() {
48 }
49
50 void ApplicationAnswerReceiver::OnAnswerReceived(const EventListInstalledApplicationsPtr &event) 
51 {
52         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
53                 Try
54                 {
55                         ApplicationInformationArrayPtr appinfoArray = event->getApplicationInformationArray();
56                         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(m_callbackManager->getContext());
57                         JSValueRef result = converter->toJSValueRef(appinfoArray);
58                         
59                         m_callbackManager->callOnSuccess(result);
60                 }
61                 Catch(WrtDeviceApis::Commons::Exception)
62                 {
63                         LogError("error during function executing");
64                         JSContextRef context = m_callbackManager->getContext();
65                         JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
66                         m_callbackManager->callOnError(errorObject);
67                 }
68         }
69         else {
70                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
71                 JSContextRef context = m_callbackManager->getContext();
72                 JSValueRef errorObject = NULL;
73
74                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
75                 switch(exception){
76                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
77                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
78                                 break;
79                         case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
80                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
81                                 break;
82                         default:
83                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
84                                 break;
85                 }
86                 m_callbackManager->callOnError(errorObject);
87         }       
88 }
89
90 void ApplicationAnswerReceiver::OnAnswerReceived(const EventLaunchServicePtr &event) 
91 {
92         LogDebug("<<<");
93         LaunchServicePrivateDataPtr privateData =
94                 DPL::StaticPointerCast<LaunchServicePrivateData>(event->getPrivateData());      
95         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
96                 LogDebug("OnAnswerReceived");
97                 Try
98                 {
99                         if( event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_CALLBACK)
100                         {
101                                 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
102                                 callbackManager->callOnSuccess();               
103
104                                 LogDebug("after callOnSuccess");
105
106                         }
107                         else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_SUCCESS_CALLBACK)
108                         {
109                                 
110                                 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
111                                 if(replyCallbackManager)
112                                 {
113                                         LogDebug("check ApplicationServiceReply");
114                                         ApplicationServiceReplyPtr reply = event->getServiceReply();
115
116                                         ApplicationConverterFactory::ConverterType converter = 
117                                                 ApplicationConverterFactory::getConverter(replyCallbackManager->getContext());
118                                         JSValueRef jsObj = converter->toJSValueRef(reply);
119
120                                         replyCallbackManager->callOnSuccess(jsObj);
121                                         LogDebug("after replyCallbackManager onsuccess");
122                                 }
123                         }
124                         else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_FAIL_CALLBACK)
125                         {
126                                 
127                                 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
128                                 if(replyCallbackManager)
129                                 {
130                                         
131                                         LogDebug("check ApplicationServiceReply");
132                                         ApplicationServiceReplyPtr reply = event->getServiceReply();
133
134                                         ApplicationConverterFactory::ConverterType converter = 
135                                                 ApplicationConverterFactory::getConverter(replyCallbackManager->getContext());
136                                         JSValueRef jsObj = converter->toJSValueRef(reply);
137
138                                         replyCallbackManager->callOnError(jsObj);
139                                         LogDebug("after replyCallbackManager onfail");
140                                 }
141                         }
142                         else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_CANCEL_CALLBACK)
143                         {
144                                 CommonsJavaScript::JSCallbackManagerPtr replyCancelCallbackManager = privateData->getReplyCancelCallbackManager();
145                                 if(replyCancelCallbackManager)
146                                 {                       
147                                         LogDebug("replyCancelCallbackManager");
148                                         replyCancelCallbackManager->callOnSuccess();
149                                         LogDebug("after replyCallback oncancel");
150                                 }
151                         }
152                 }
153                 Catch(WrtDeviceApis::Commons::Exception)
154                 {
155                         LogError("error during function executing");
156                 }
157         }
158         else {
159                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
160
161                 JSValueRef errorObject = NULL;
162                 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
163                 if(callbackManager == NULL )
164                 {
165                         LogWarning("callbackManager is null");
166                 }
167                 JSContextRef context = callbackManager->getContext();
168
169                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
170                 switch(exception){
171                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
172                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
173                                 break;
174                         case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
175                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
176                                 break;
177                         default:
178                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
179                                 break;
180                 }
181                 callbackManager->callOnError(errorObject);
182         }       
183         LogDebug(">>>");        
184 }
185
186 }
187 }
188 }