2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "CalendarResponseDispatcher.h"
19 #include <CommonsJavaScript/JSCallbackManager.h>
20 #include <JSWebAPIErrorFactory.h>
21 #include "CalendarConverter.h"
22 #include "JSCalendarEventId.h"
23 #include "CalendarMultiCallback.h"
27 #include <GlobalContextManager.h>
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace DeviceAPI::Common;
36 CalendarResponseDispatcher& CalendarResponseDispatcher::getInstance()
38 static CalendarResponseDispatcher instance;
42 CalendarResponseDispatcher::CalendarResponseDispatcher() :
43 GetCalendarsEventAnswerReceiver(ThreadEnum::NULL_THREAD),
44 CalendarEventsEventAnswerReceiverAdds(ThreadEnum::NULL_THREAD),
45 CalendarEventsEventAnswerReceiverDeletes(ThreadEnum::NULL_THREAD),
46 CalendarEventsEventAnswerReceiverUpdates(ThreadEnum::NULL_THREAD),
47 CalendarEventsEventAnswerReceiverFind(ThreadEnum::NULL_THREAD),
48 CalendarEventsEventAnswerReceiverExpand(ThreadEnum::NULL_THREAD),
49 OnEventsChangedListener(ThreadEnum::NULL_THREAD)
53 CalendarResponseDispatcher::~CalendarResponseDispatcher()
57 void CalendarResponseDispatcher::OnAnswerReceived(const IEventGetCalendarsPtr &event)
59 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
61 LoggerE("no callback manager");
65 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(cbm->getContext())){
66 LoggerW("Global context invalidated.");
72 if (event->getResult()) {
73 CalendarConverter converter(cbm->getContext());
74 const std::vector<ICalendarPtr> &results = event->getCalendars();
75 LoggerD("found calendars: " << results.size());
76 JSValueRef result = converter.toJSValueRef(results);
77 cbm->callOnSuccess(result);
83 LoggerE("error during processing answer");
85 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
86 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
87 cbm->callOnError(errorObject);
90 void CalendarResponseDispatcher::OnAnswerReceived(const IEventAddEventsPtr &event)
92 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
96 LoggerD("no callback manager");
100 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(cbm->getContext())){
101 LoggerW("Global context invalidated.");
105 if (event->getResult()) {
106 LoggerD("result success");
107 CalendarConverter converter(cbm->getContext());
110 if (CalendarEvent::TASK_TYPE==event->getCalendarType()) {
111 result = converter.toJSValueRefTaskArray(event->getEvents());
113 result = converter.toJSValueRefEventArray(event->getEvents());
116 cbm->callOnSuccess(result);
119 LoggerD("result fail");
121 Catch(ConversionException)
123 LoggerE("cannot convert event");
125 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
126 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
127 cbm->callOnError(errorObject);
130 void CalendarResponseDispatcher::OnAnswerReceived(const IEventUpdateEventsPtr &event)
132 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
134 LoggerD("no callback manager");
138 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(cbm->getContext())){
139 LoggerW("Global context invalidated.");
143 if (event->getResult()) {
144 cbm->callOnSuccess();
146 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
147 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
148 cbm->callOnError(errorObject);
152 void CalendarResponseDispatcher::OnAnswerReceived(const IEventDeleteEventsPtr &event)
154 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
156 LoggerD("no callback manager");
160 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(cbm->getContext())){
161 LoggerW("Global context invalidated.");
165 if (event->getResult()) {
166 cbm->callOnSuccess();
168 JSValueRef errorObject;
169 if (event->getExceptionCode() ==
170 ExceptionCodes::NotFoundException) {
171 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
172 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::NOT_FOUND_ERROR);
174 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
175 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
177 cbm->callOnError(errorObject);
181 void CalendarResponseDispatcher::OnAnswerReceived(const IEventFindEventsPtr &event)
183 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
185 LoggerE("no callback manager");
189 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(cbm->getContext())){
190 LoggerW("Global context invalidated.");
196 if (event->getResult()) {
197 CalendarConverter converter(cbm->getContext());
200 if (CalendarEvent::TASK_TYPE==event->getCalendarType()) {
201 result = converter.toJSValueRefTaskArray(event->getEvents());
203 result = converter.toJSValueRefEventArray(event->getEvents());
205 cbm->callOnSuccess(result);
208 JSValueRef errorObject;
209 if (event->getExceptionCode() == ExceptionCodes::InvalidArgumentException) {
210 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
211 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::INVALID_VALUES_ERROR);
212 } else if (event->getExceptionCode() == ExceptionCodes::NotFoundException) {
213 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
214 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
216 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
217 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
219 cbm->callOnError(errorObject);
225 LoggerE("error during processing answer");
226 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
227 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
228 cbm->callOnError(errorObject);
232 void CalendarResponseDispatcher::OnAnswerReceived(const IEventExpandEventRecurrencePtr &event)
234 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
238 LoggerD("no callback manager");
242 if( !GlobalContextManager::getInstance()->isAliveGlobalContext(cbm->getContext())){
243 LoggerW("Global context invalidated.");
247 if (event->getResult()) {
248 CalendarConverter converter(cbm->getContext());
249 cbm->callOnSuccess(converter.toJSValueRefEventArray(event->getExpandedEventList()));
252 JSValueRef errorObject;
253 if (event->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
254 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
255 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::INVALID_VALUES_ERROR);
256 } else if (event->getExceptionCode()==ExceptionCodes::NotFoundException) {
257 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
258 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::NOT_FOUND_ERROR);
260 errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
261 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
263 cbm->callOnError(errorObject);
269 LoggerE("error during processing answer");
270 JSValueRef errorObject = DeviceAPI::Common::JSWebAPIErrorFactory::makeErrorObject(
271 cbm->getContext(), DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR);
272 cbm->callOnError(errorObject);
276 void CalendarResponseDispatcher::onAnswerReceived(const OnEventsChangedPtr& event)
278 CalendarChangeCallbackPrivateDataPtr multiCallbacks =
279 DPL::StaticPointerCast<CalendarChangeCallbackPrivateData>(event->getPrivateData());
280 JSContextRef context = multiCallbacks->getOnAdded()->getContext();
282 CalendarConverter converter(context);
287 switch (event->getStatus())
289 case OnEventsChanged::ON_ADD:
291 if (CalendarEvent::TASK_TYPE==event->getCalendarType()) {
292 result = converter.toJSValueRefTaskArray(event->getEventList());
294 result = converter.toJSValueRefEventArray(event->getEventList());
297 multiCallbacks->getOnAdded()->callOnSuccess(result);
300 case OnEventsChanged::ON_UPDATE:
302 if (CalendarEvent::TASK_TYPE==event->getCalendarType()) {
303 result = converter.toJSValueRefTaskArray(event->getEventList());
305 result = converter.toJSValueRefEventArray(event->getEventList());
307 multiCallbacks->getOnUpdated()->callOnSuccess(result);
310 case OnEventsChanged::ON_DELETE:
312 CalendarEventListPtr eventList = event->getEventList();
314 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
315 if (NULL == jsResult) {
316 ThrowMsg(ConversionException, "Could not create js array object");
319 for (std::size_t i = 0; i < eventList->size(); i++) {
320 EventIdPtr eventId( new EventId() );
321 eventId->setUId(eventList->at(i)->getUId());
322 std::stringstream ss;
323 std::time_t rid = eventList->at(i)->getRecurrenceId();
325 eventId->setRecurrenceId(ss.str());
326 JSValueRef tmpVal =JSCalendarEventId::createJSCalendarEventId(context, eventId);
327 if (!JSSetArrayElement(context, jsResult, i, tmpVal)) {
328 ThrowMsg(ConversionException, "Could not create js array element");
332 multiCallbacks->getOnDeleted()->callOnSuccess(jsResult);
335 //case OnEventsChanged::ON_CALENDAR_RESET:
338 LoggerD("Invalid status!");
343 Catch(ConversionException)
345 LoggerE("cannot convert change event");
349 LoggerE("exception occurred while handling change noti callback!");