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.
19 * @file ContactManagerController.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <JSTizenExceptionFactory.h>
27 #include <JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "ContactManagerController.h"
30 #include "ContactAsyncCallbackManager.h"
31 #include "JSContactManagerChangeCallbackManager.h"
32 #include "ContactQueue.h"
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
42 ContactManagerController::ContactManagerController(JSContextRef context, ContactManagerPtr contactManager) :
43 EventContactManagerGetAddressBooksAnswerReceiver(ThreadEnum::NULL_THREAD),
44 EventContactManagerUpdateBatchAnswerReceiver(ThreadEnum::NULL_THREAD),
45 EventContactManagerRemoveBatchAnswerReceiver(ThreadEnum::NULL_THREAD),
46 EventContactManagerFindAnswerReceiver(ThreadEnum::NULL_THREAD),
47 EventContactManagerChangeListenerListener(ThreadEnum::NULL_THREAD),
48 ContactManagerPrivObject(context, contactManager)
50 LoggerD("ContactManagerController constructed : " << this);
53 ContactManagerController::~ContactManagerController()
55 LoggerD("ContactManagerController destructed : " << this);
58 void ContactManagerAsyncAnswerHandler::getAddressBooks(const EventContactManagerGetAddressBooksPtr &event)
61 JSCallbackManagerPtr callbackManager =
62 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
63 if (!callbackManager) {
64 LoggerE("no callback manager");
68 ContactAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
70 if(event->checkCancelled()) {
71 LoggerD("Operation cancelled");
75 JSContextRef gContext = callbackManager->getContext();
77 JSValueRef error = NULL;
78 if (ExceptionCodes::None != event->getExceptionCode())
80 switch (event->getExceptionCode())
82 case ExceptionCodes::PlatformException:
83 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
85 case ExceptionCodes::InvalidArgumentException:
86 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Invalid value error");
88 case ExceptionCodes::ConversionException:
89 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Wrong argument");
92 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
96 callbackManager->callOnError(error);
101 if (!event->getResult())
102 ThrowMsg(PlatformException, "No result.");
104 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
105 AddressBookArrayPtr addressBooks = event->getAddressBooks();
107 JSValueRef result = converter->toJSValueRef(addressBooks);
109 callbackManager->callOnSuccess(result);
112 LoggerE("error during processing answer" << _rethrown_exception.GetMessage());
113 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
115 callbackManager->callOnError(error);
120 void ContactManagerAsyncAnswerHandler::updateBatch(const EventContactManagerUpdateBatchPtr &event)
124 ContactQueueManagerSingleton::Instance().decreaseQueueList();
125 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
127 ContactQueueManagerSingleton::Instance().pop();
130 JSCallbackManagerPtr callbackManager =
131 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
132 if (!callbackManager) {
133 LoggerE("no callback manager");
137 ContactAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
139 if(event->checkCancelled()) {
140 LoggerD("Operation cancelled");
144 JSContextRef gContext = callbackManager->getContext();
146 JSValueRef error = NULL;
147 if (ExceptionCodes::None != event->getExceptionCode())
149 switch (event->getExceptionCode())
151 case ExceptionCodes::PlatformException:
152 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
154 case ExceptionCodes::NotFoundException:
155 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::NOT_FOUND_ERROR, "record not found");
157 case ExceptionCodes::InvalidArgumentException:
158 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::TYPE_MISMATCH_ERROR, "Invalid value error");
160 case ExceptionCodes::ConversionException:
161 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong argument");
164 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
168 callbackManager->callOnError(error);
172 callbackManager->callOnSuccess();
175 void ContactManagerAsyncAnswerHandler::removeBatch(const EventContactManagerRemoveBatchPtr &event)
178 ContactQueueManagerSingleton::Instance().decreaseQueueList();
179 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
181 ContactQueueManagerSingleton::Instance().pop();
184 JSCallbackManagerPtr callbackManager =
185 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
186 if (!callbackManager) {
187 LoggerE("no callback manager");
191 ContactAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
193 if(event->checkCancelled()) {
194 LoggerD("Operation cancelled");
198 JSContextRef gContext = callbackManager->getContext();
200 JSValueRef error = NULL;
201 if (ExceptionCodes::None != event->getExceptionCode())
203 switch (event->getExceptionCode())
205 case ExceptionCodes::PlatformException:
206 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
208 case ExceptionCodes::NotFoundException:
209 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::NOT_FOUND_ERROR, "record not found");
211 case ExceptionCodes::InvalidArgumentException:
212 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::TYPE_MISMATCH_ERROR, "Invalid value error");
214 case ExceptionCodes::ConversionException:
215 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong argument");
218 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
221 callbackManager->callOnError(error);
225 callbackManager->callOnSuccess();
228 void ContactManagerAsyncAnswerHandler::find(const EventContactManagerFindPtr &event)
231 JSCallbackManagerPtr callbackManager =
232 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
233 if (!callbackManager)
235 LoggerE("no callback manager");
239 ContactAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(callbackManager);
241 if(event->checkCancelled()) {
242 LoggerD("Operation cancelled");
246 JSContextRef gContext = callbackManager->getContext();
248 JSValueRef error = NULL;
249 if (ExceptionCodes::None != event->getExceptionCode())
251 switch (event->getExceptionCode())
253 case ExceptionCodes::PlatformException:
254 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::NOT_SUPPORTED_ERROR, "Not support such a filter");
256 case ExceptionCodes::InvalidArgumentException:
257 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::TYPE_MISMATCH_ERROR, "Invalid value error");
259 case ExceptionCodes::ConversionException:
260 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong argument");
263 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
267 callbackManager->callOnError(error);
272 if (!event->getResult())
273 ThrowMsg(PlatformException, "No result.");
275 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
276 PersonArrayPtr contacts = event->getPersons();
278 JSValueRef result = converter->toJSValueRef(contacts);
280 callbackManager->callOnSuccess(result);
283 LoggerE("error during processing answer" << _rethrown_exception.GetMessage());
284 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
286 callbackManager->callOnError(error);
291 void ContactManagerAsyncAnswerHandler::changeListenerError(const EventContactManagerChangeListenerPtr &event)
294 JSValueRef errorObject;
296 JSCallbackManagerPtr cbm = DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
299 LoggerE("no callback manager");
303 JSContextRef gContext = cbm->getContext();
305 if (event->getExceptionCode() == ExceptionCodes::NotFoundException)
306 errorObject = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::NOT_FOUND_ERROR, "Not found error");
308 errorObject = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
310 cbm->callOnError(errorObject);
313 void ContactManagerAsyncAnswerHandler::changeListenerPersonAdded(const EventContactManagerChangeListenerPtr &event)
316 JSContactManagerChangeCallbackManagerPtr cbm =
317 DPL::StaticPointerCast<JSContactManagerChangeCallbackManager>(event->getPrivateData());
320 LoggerE("no callback manager");
324 EventInfoContactManagerChangePtr _eventInfo = event->getEventInfo();
325 if(_eventInfo->getCallbackType() != EventInfoContactManagerChange::OnPersonsAdded)
327 LoggerE("wrong type");
331 EventInfoContactManagerChangeAddedPtr eventInfo = DPL::DynamicPointerCast<EventInfoContactManagerChangeAdded>(_eventInfo);
333 if (!eventInfo->getPersonsIsSet())
335 LoggerE("no data set");
339 ContactConverterFactory::ConverterType converter =
340 ContactConverterFactory::getConverter(cbm->getContext());
344 contacts = converter->toJSValueRef(eventInfo->getPersons());
345 } Catch (Exception) {
346 LoggerE("Conversion error");
350 cbm->callOnPersonsAdded(contacts);
353 void ContactManagerAsyncAnswerHandler::changeListenerPersonUpdated(const EventContactManagerChangeListenerPtr &event)
356 JSContactManagerChangeCallbackManagerPtr cbm =
357 DPL::StaticPointerCast<JSContactManagerChangeCallbackManager>(event->getPrivateData());
360 LoggerE("no callback manager");
364 EventInfoContactManagerChangePtr _eventInfo = event->getEventInfo();
365 if(_eventInfo->getCallbackType() != EventInfoContactManagerChange::OnPersonsUpdated)
367 LoggerE("wrong type");
371 EventInfoContactManagerChangeUpdatedPtr eventInfo = DPL::DynamicPointerCast<EventInfoContactManagerChangeUpdated>(_eventInfo);
373 if (!eventInfo->getPersonsIsSet())
375 LoggerE("no data set");
379 ContactConverterFactory::ConverterType converter =
380 ContactConverterFactory::getConverter(cbm->getContext());
384 contacts = converter->toJSValueRef(eventInfo->getPersons());
385 } Catch (Exception) {
386 LoggerE("Conversion error");
390 cbm->callOnPersonsUpdated(contacts);
393 void ContactManagerAsyncAnswerHandler::changeListenerPersonRemoved(const EventContactManagerChangeListenerPtr &event)
396 JSContactManagerChangeCallbackManagerPtr cbm =
397 DPL::StaticPointerCast<JSContactManagerChangeCallbackManager>(event->getPrivateData());
400 LoggerE("no callback manager");
404 EventInfoContactManagerChangePtr _eventInfo = event->getEventInfo();
405 if(_eventInfo->getCallbackType() != EventInfoContactManagerChange::OnPersonsRemoved)
407 LoggerE("wrong type");
411 EventInfoContactManagerChangeRemovedPtr eventInfo = DPL::DynamicPointerCast<EventInfoContactManagerChangeRemoved>(_eventInfo);
412 if (!eventInfo->getPersonIdsIsSet())
414 LoggerE("no data set");
418 ContactConverterFactory::ConverterType converter =
419 ContactConverterFactory::getConverter(cbm->getContext());
421 JSValueRef contactIds;
423 contactIds = converter->toJSValueRef(eventInfo->getPersonIds());
424 } Catch (Exception) {
425 LoggerE("Conversion error");
429 cbm->callOnPersonsDeleted(contactIds);
432 void ContactManagerController::OnAnswerReceived(const EventContactManagerGetAddressBooksPtr &event)
434 ContactManagerAsyncAnswerHandler::getAddressBooks(event);
437 void ContactManagerController::OnAnswerReceived(const EventContactManagerUpdateBatchPtr &event)
439 ContactManagerAsyncAnswerHandler::updateBatch(event);
442 void ContactManagerController::OnAnswerReceived(const EventContactManagerRemoveBatchPtr &event)
444 ContactManagerAsyncAnswerHandler::removeBatch(event);
447 void ContactManagerController::OnAnswerReceived(const EventContactManagerFindPtr &event)
449 ContactManagerAsyncAnswerHandler::find(event);
452 void ContactManagerController::onAnswerReceived(const EventContactManagerChangeListenerPtr &event)
454 EventInfoContactManagerChangePtr eventInfo = event->getEventInfo();
455 if(eventInfo == NULL)
457 LoggerE("wrong type");
461 EventInfoContactManagerChange::CallbackType type = eventInfo->getCallbackType();
462 if(type == EventInfoContactManagerChange::OnPersonsAdded)
463 ContactManagerAsyncAnswerHandler::changeListenerPersonAdded(event);
464 else if(type == EventInfoContactManagerChange::OnPersonsUpdated)
465 ContactManagerAsyncAnswerHandler::changeListenerPersonUpdated(event);
466 else if(type == EventInfoContactManagerChange::OnPersonsRemoved)
467 ContactManagerAsyncAnswerHandler::changeListenerPersonRemoved(event);
468 else if(type == EventInfoContactManagerChange::OnError)
469 ContactManagerAsyncAnswerHandler::changeListenerError(event);