Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactManager.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 /**
18  * @file        JSContactManager.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.3
21  * @brief
22  */
23
24 #include <string>
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"
39
40 #define TIZEN_CONTACT_MANAGER_DEVICE_ADDRESS_BOOK       "DEVICE_ADDRESS_BOOK"
41 #define TIZEN_CONTACT_MANAGER_PHONE_ADDRESS_BOOK        "PHONE_ADDRESS_BOOK"
42
43 namespace TizenApis {
44 namespace Tizen1_0 {
45 namespace Contact {
46
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;
52
53 JSClassDefinition JSContactManager::m_classInfo = {
54         0,
55         kJSClassAttributeNone,
56         "ContactManager",
57         0,
58         m_property,
59         m_function,
60         Initialize,
61         Finalize,
62         NULL, //HasProperty,
63         NULL, //GetProperty,
64         NULL, //SetProperty,
65         NULL, //DeleteProperty,
66         NULL, //GetPropertyNames,
67         NULL, //CallAsFunction,
68         NULL, //CallAsConstructor,
69         NULL, //HasInstance,
70         NULL, //ConvertToType,
71 };
72
73 const JSClassDefinition* JSContactManager::getClassInfo()
74 {
75         return &m_classInfo;
76 }
77
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 },
81         { 0, 0, 0, 0 }
82 };
83
84 JSStaticFunction JSContactManager::m_function[] = {
85         { "getAddressBooks", getAddressBooks, kJSPropertyAttributeNone },
86         { "getDefaultAddressBook", getDefaultAddressBook, kJSPropertyAttributeNone },
87         { 0, 0, 0 }
88 };
89
90 JSClassRef JSContactManager::getClassRef()
91 {
92         if (!m_classRef) {
93                 m_classRef = JSClassCreate(&m_classInfo);
94         }
95         return m_classRef;
96 }
97
98 JSClassRef JSContactManager::m_classRef = JSClassCreate(JSContactManager::getClassInfo());
99
100 void JSContactManager::Initialize(JSContextRef context,
101                 JSObjectRef object)
102 {
103         LogDebug("Entered");
104         IContactManagerPtr contactManager = ContactFactory::getInstance().createContactManager();
105
106         ContactManagerController *priv = new ContactManagerController(context, contactManager);
107
108         JSObjectSetPrivate(object, static_cast<void*>(priv));
109 }
110
111 void JSContactManager::Finalize(JSObjectRef object)
112 {
113         LogDebug("entered");
114         ContactManagerController *priv = static_cast<ContactManagerController*>(JSObjectGetPrivate(object));
115         LogInfo("Delete address book manager");
116         delete priv;
117 }
118
119 JSValueRef JSContactManager::getConstProperty(JSContextRef context,
120                 JSObjectRef object,
121                 JSStringRef jsPropertyName,
122                 JSValueRef* exception)
123 {
124         BasicConverter converter = BasicConverterFactory::getConverter(context);
125
126         Try {
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);
132                 }
133         } Catch(Exception) {
134                 LogError("error during getting property");
135         }
136         return JSValueMakeUndefined(context);
137 }
138
139 JSValueRef JSContactManager::getAddressBooks(JSContextRef context,
140                 JSObjectRef object,
141                 JSObjectRef thisObject,
142                 size_t argumentCount,
143                 const JSValueRef arguments[],
144                 JSValueRef* exception)
145 {
146         IContactManagerPtr contactManager;
147         JSContextRef gContext;
148         ContactManagerController *controller;
149
150         Try     {
151                 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
152                 if (!controller) {
153                         ThrowMsg(InvalidArgumentException, "No private object.");
154                 }
155                 contactManager = controller->getObject();
156                 gContext = controller->getContext();
157         } Catch(Exception) {
158                 LogError("No private object");
159                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
160         }
161
162         AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_GET_ADDRESS_BOOKS);
163         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
164
165         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
166         Try {
167                 if (argumentCount < 1 || argumentCount > 2)
168                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
169
170                 if (!validator->isCallback(arguments[0]))
171                         ThrowMsg(InvalidArgumentException, "1st argument is not function.");
172
173                 if (argumentCount >= 2)
174                 {
175                         if (!validator->isCallback(arguments[1]))
176                                 ThrowMsg(InvalidArgumentException, "2nd argument is not function.");
177                 }
178
179         } Catch(Exception ) {
180                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
181                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
182         }
183
184         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
185
186         callbackManager->setOnSuccess(arguments[0]);
187
188         if(argumentCount >= 2)
189                 callbackManager->setOnError(arguments[1]);
190
191         EventContactManagerGetAddressBooksPtr dplEvent(new EventContactManagerGetAddressBooks());
192
193         // set event handler's data
194         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
195         dplEvent->setForAsynchronousCall(controller);
196
197         //DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
198         //ContactManagerGetAddressBooksPendingOperation *gcPendingOperation = new ContactManagerGetAddressBooksPendingOperation(eventContr);
199
200         Try {
201                 contactManager->getAddressBooks(dplEvent);
202         } Catch(Exception) {
203                 LogError("Error on platform : " << _rethrown_exception.GetMessage());
204                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
205         }
206
207         //return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
208         return JSValueMakeUndefined(context);
209 }
210
211 JSValueRef JSContactManager::getDefaultAddressBook(JSContextRef context,
212                 JSObjectRef object,
213                 JSObjectRef thisObject,
214                 size_t argumentCount,
215                 const JSValueRef arguments[],
216                 JSValueRef* exception)
217 {
218         ContactManagerController *priv =
219                 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
220         if (!priv) {
221                 LogError("private object is null");
222                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
223         }
224
225         AceSecurityStatus status = CONTACT_CHECK_ACCESS(priv->getContext(), CONTACT_FUNCTION_API_GET_DEFAULT_ADDRESS_BOOK);
226         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
227
228         IContactManagerPtr contactManager;
229         JSContextRef gContext;
230         Try {
231                 contactManager = priv->getObject();
232                 gContext = priv->getContext();
233         } Catch(Exception) {
234                 LogError("contact manager or context is null");
235                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
236         }
237
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");
243         }
244
245         IAddressBookPtr defaultAddressBook;
246         Try {
247                 defaultAddressBook = contactManager->getDefaultAddressBook();
248         } Catch(Exception){
249                 LogError("Internal error.");
250                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
251         }
252
253         JSValueRef resultValue;
254         Try {
255                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
256                 resultValue = converter->toJSValueRef(defaultAddressBook);
257         } Catch(Exception){
258                 LogError("Conversion error.");
259                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
260         }
261
262         return resultValue;
263 }
264
265 } // Contact
266 } // Tizen1_0
267 } // TizenApis