Beta merge 2
[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 "JSApplicationContext.h"
23 #include "ApplicationAnswerReceiver.h"
24 #include "ApplicationConverter.h"
25
26 namespace TizenApis {
27 namespace Tizen1_0 {
28 namespace Application {
29
30 using namespace std;
31 using namespace TizenApis::Api::Application;
32 using namespace WrtDeviceApis;
33 using namespace TizenApis::Commons;
34
35 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const CommonsJavaScript::JSCallbackManagerPtr &callbackManager) :
36         EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
37         EventAnswerReceiver<EventManageApplication> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
38         EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),      
39         m_callbackManager(callbackManager) {
40 }
41
42 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const LaunchServicePrivateDataPtr &launchServiceCallbackManager) :
43         EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
44         EventAnswerReceiver<EventManageApplication> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
45         EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
46         m_privateData(launchServiceCallbackManager) {
47         LogDebug("<<<");
48 }
49         
50 ApplicationAnswerReceiver::~ApplicationAnswerReceiver() {
51 }
52
53 void ApplicationAnswerReceiver::OnAnswerReceived(const EventListInstalledApplicationsPtr &event) 
54 {
55         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
56                 if (event->getEventType() == EventListInstalledApplications::APPMANAGER_LIST_INSTALLED_APPLICATIONS) {
57                         Try
58                         {
59                                 ApplicationInformationArrayPtr appinfoArray = event->getApplicationInformationArray();
60                                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(m_callbackManager->getContext());
61                                 JSValueRef result = converter->toJSValueRef(appinfoArray);
62                                 m_callbackManager->callOnSuccess(result);
63                         }
64                         Catch(WrtDeviceApis::Commons::Exception)
65                         {
66                                 LogError("error during function executing");
67                                 JSContextRef context = m_callbackManager->getContext();
68                                 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
69                                 m_callbackManager->callOnError(errorObject);
70                         }
71                 } else if (event->getEventType() == EventListInstalledApplications::APPMANAGER_LIST_RUNNING_APPLICATIONS) {
72                         Try
73                         {
74                                 ApplicationContextArrayPtr appcontextArray = event->getApplicationContextArray();
75                                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(m_callbackManager->getContext());
76                                 JSValueRef result = converter->toJSValueRef(appcontextArray);
77                                 
78                                 m_callbackManager->callOnSuccess(result);
79                         }
80                         Catch(WrtDeviceApis::Commons::Exception)
81                         {
82                                 LogError("error during function executing");
83                                 JSContextRef context = m_callbackManager->getContext();
84                                 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
85                                 m_callbackManager->callOnError(errorObject);
86                         }
87                 } else {
88                         LogError("unknow event type");
89                         JSContextRef context = m_callbackManager->getContext();
90                         JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
91                         m_callbackManager->callOnError(errorObject);
92                 }
93         }
94         else {
95                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
96                 JSContextRef context = m_callbackManager->getContext();
97                 JSValueRef errorObject = NULL;
98
99                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
100                 switch(exception){
101                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
102                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
103                                 break;
104                         case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
105                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
106                                 break;
107                         default:
108                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
109                                 break;
110                 }
111                 m_callbackManager->callOnError(errorObject);
112         }       
113 }
114
115 void ApplicationAnswerReceiver::OnAnswerReceived(const EventManageApplicationPtr &event) 
116 {
117         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
118                 if (event->getEventType() == EventManageApplication::APP_MANAGER_KILL_APPLICATION ||
119                   event->getEventType() == EventManageApplication::APP_MANAGER_HIDE_APPLICATION ||
120                   event->getEventType() == EventManageApplication::APP_MANAGER_LAUNCH_APPLICATION) {
121                         LogDebug("Event Type: " << event->getEventType());
122                         m_callbackManager->callOnSuccess();
123                 } else {
124                         LogError("unknow event type");
125                         JSContextRef context = m_callbackManager->getContext();
126                         JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
127                         m_callbackManager->callOnError(errorObject);
128                 }
129         }
130         else {
131                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
132                 JSContextRef context = m_callbackManager->getContext();
133                 JSValueRef errorObject = NULL;
134
135                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
136                 switch(exception){
137                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
138                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
139                                 break;
140                         case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
141                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
142                                 break;
143                         default:
144                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
145                                 break;
146                 }
147                 m_callbackManager->callOnError(errorObject);
148         }       
149 }
150
151
152 void ApplicationAnswerReceiver::OnAnswerReceived(const EventLaunchServicePtr &event) 
153 {
154         LogDebug("<<<");
155         LaunchServicePrivateDataPtr privateData =
156                 DPL::StaticPointerCast<LaunchServicePrivateData>(event->getPrivateData());      
157         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) 
158         {
159                 LogDebug("OnAnswerReceived");
160                 Try
161                 {
162                         if( event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_CALLBACK)
163                         {
164                                 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
165                                 callbackManager->callOnSuccess();               
166
167                                 LogDebug("after callOnSuccess");
168
169                         }
170                         else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_SUCCESS_CALLBACK)
171                         {
172                                 
173                                 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
174                                 if(replyCallbackManager)
175                                 {
176                                         LogDebug("check ApplicationServiceReply");
177                                         std::vector<ApplicationServiceDataPtr> reply = event->getService()->getServiceDataArray();
178
179                                         ApplicationConverterFactory::ConverterType converter = 
180                                                 ApplicationConverterFactory::getConverter(replyCallbackManager->getContext());
181                                         JSValueRef jsObj = converter->toJSValueRef(reply);
182
183                                         replyCallbackManager->callOnSuccess(jsObj);
184                                         LogDebug("after replyCallbackManager onsuccess");
185                                 }
186                         }
187                         else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_FAIL_CALLBACK)
188                         {
189                                 
190                                 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
191                                 if(replyCallbackManager)
192                                 {
193                                         replyCallbackManager->callOnError();
194                                         LogDebug("after replyCallbackManager onfail");
195                                 }
196                         }
197                 }
198                 Catch(WrtDeviceApis::Commons::Exception)
199                 {
200                         LogError("error during function executing");
201                 }
202         }
203         else 
204         {
205                 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
206
207                 JSValueRef errorObject = NULL;
208                 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
209                 if(callbackManager == NULL )
210                 {
211                         LogWarning("callbackManager is null");
212                 }
213                 JSContextRef context = callbackManager->getContext();
214
215                 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
216                 switch(exception){
217                         case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
218                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
219                                 break;
220                         case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
221                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
222                                 break;
223                         default:
224                                 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
225                                 break;
226                 }
227                 callbackManager->callOnError(errorObject);
228         }       
229         LogDebug(">>>");        
230 }
231
232 }
233 }
234 }