2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file JSContactManager.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/JSCallbackManager.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <CommonsJavaScript/JSPendingOperation.h>
30 #include <API/Contact/ContactFactory.h>
31 #include <API/Contact/ContactTypes.h>
32 #include <Tizen/Common/JSTizenExceptionFactory.h>
33 #include <Tizen/Common/JSTizenException.h>
34 #include <Tizen/Common/SecurityExceptions.h>
35 #include "plugin_config.h"
36 #include "ContactManagerController.h"
37 #include "ContactConverter.h"
38 #include "JSContactManager.h"
40 #define TIZEN_CONTACT_MANAGER_DEVICE_ADDRESS_BOOK "DEVICE_ADDRESS_BOOK"
41 #define TIZEN_CONTACT_MANAGER_PHONE_ADDRESS_BOOK "PHONE_ADDRESS_BOOK"
47 using namespace TizenApis::Commons;
48 using namespace TizenApis::Commons;
49 using namespace TizenApis::Api::Contact;
50 using namespace WrtDeviceApis::Commons;
51 using namespace WrtDeviceApis::CommonsJavaScript;
53 JSClassDefinition JSContactManager::m_classInfo = {
55 kJSClassAttributeNone,
65 NULL, //DeleteProperty,
66 NULL, //GetPropertyNames,
67 NULL, //CallAsFunction,
68 NULL, //CallAsConstructor,
70 NULL, //ConvertToType,
73 const JSClassDefinition* JSContactManager::getClassInfo()
78 JSStaticValue JSContactManager::m_property[] = {
79 { TIZEN_CONTACT_MANAGER_DEVICE_ADDRESS_BOOK, getConstProperty, NULL, kJSPropertyAttributeReadOnly },
80 { TIZEN_CONTACT_MANAGER_PHONE_ADDRESS_BOOK, getConstProperty, NULL, kJSPropertyAttributeReadOnly },
84 JSStaticFunction JSContactManager::m_function[] = {
85 { "getAddressBooks", getAddressBooks, kJSPropertyAttributeNone },
86 { "getDefaultAddressBook", getDefaultAddressBook, kJSPropertyAttributeNone },
90 JSClassRef JSContactManager::getClassRef()
93 m_classRef = JSClassCreate(&m_classInfo);
98 JSClassRef JSContactManager::m_classRef = JSClassCreate(JSContactManager::getClassInfo());
100 void JSContactManager::Initialize(JSContextRef context,
104 IContactManagerPtr contactManager = ContactFactory::getInstance().createContactManager();
106 ContactManagerController *priv = new ContactManagerController(context, contactManager);
108 JSObjectSetPrivate(object, static_cast<void*>(priv));
111 void JSContactManager::Finalize(JSObjectRef object)
114 ContactManagerController *priv = static_cast<ContactManagerController*>(JSObjectGetPrivate(object));
115 LogInfo("Delete address book manager");
119 JSValueRef JSContactManager::getConstProperty(JSContextRef context,
121 JSStringRef jsPropertyName,
122 JSValueRef* exception)
124 BasicConverter converter = BasicConverterFactory::getConverter(context);
127 std::string propertyName = converter->toString(jsPropertyName);
128 if (propertyName == TIZEN_CONTACT_MANAGER_DEVICE_ADDRESS_BOOK) {
129 return converter->toJSValueRef(DEVICE_ADDRESS_BOOK);
130 } else if (propertyName == TIZEN_CONTACT_MANAGER_PHONE_ADDRESS_BOOK) {
131 return converter->toJSValueRef(PHONE_ADDRESS_BOOK);
134 LogError("error during getting property");
136 return JSValueMakeUndefined(context);
139 JSValueRef JSContactManager::getAddressBooks(JSContextRef context,
141 JSObjectRef thisObject,
142 size_t argumentCount,
143 const JSValueRef arguments[],
144 JSValueRef* exception)
146 IContactManagerPtr contactManager;
147 JSContextRef gContext;
148 ContactManagerController *controller;
151 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
153 ThrowMsg(InvalidArgumentException, "No private object.");
155 contactManager = controller->getObject();
156 gContext = controller->getContext();
158 LogError("No private object");
159 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
162 AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_GET_ADDRESS_BOOKS);
163 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
167 if (argumentCount < 1 || argumentCount > 2)
168 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
170 if (!validator->isCallback(arguments[0]))
171 ThrowMsg(InvalidArgumentException, "1st argument is not function.");
173 if (argumentCount >= 2)
175 if (!validator->isCallback(arguments[1]))
176 ThrowMsg(InvalidArgumentException, "2nd argument is not function.");
179 } Catch(Exception ) {
180 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
181 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
184 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
186 callbackManager->setOnSuccess(arguments[0]);
188 if(argumentCount >= 2)
189 callbackManager->setOnError(arguments[1]);
191 EventContactManagerGetAddressBooksPtr dplEvent(new EventContactManagerGetAddressBooks());
193 // set event handler's data
194 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
195 dplEvent->setForAsynchronousCall(controller);
197 //DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
198 //ContactManagerGetAddressBooksPendingOperation *gcPendingOperation = new ContactManagerGetAddressBooksPendingOperation(eventContr);
201 contactManager->getAddressBooks(dplEvent);
203 LogError("Error on platform : " << _rethrown_exception.GetMessage());
204 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
207 //return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
208 return JSValueMakeUndefined(context);
211 JSValueRef JSContactManager::getDefaultAddressBook(JSContextRef context,
213 JSObjectRef thisObject,
214 size_t argumentCount,
215 const JSValueRef arguments[],
216 JSValueRef* exception)
218 ContactManagerController *priv =
219 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
221 LogError("private object is null");
222 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
225 AceSecurityStatus status = CONTACT_CHECK_ACCESS(priv->getContext(), CONTACT_FUNCTION_API_GET_DEFAULT_ADDRESS_BOOK);
226 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
228 IContactManagerPtr contactManager;
229 JSContextRef gContext;
231 contactManager = priv->getObject();
232 gContext = priv->getContext();
234 LogError("contact manager or context is null");
235 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
238 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
239 if (argumentCount > 0) {
240 /* Argument count must be 0. */
241 LogError("TypeMismatchException!");
242 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
245 IAddressBookPtr defaultAddressBook;
247 defaultAddressBook = contactManager->getDefaultAddressBook();
249 LogError("Internal error.");
250 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
253 JSValueRef resultValue;
255 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
256 resultValue = converter->toJSValueRef(defaultAddressBook);
258 LogError("Conversion error.");
259 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");