Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactEmailAddress.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        JSContactEmailAddress.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactEmailAddress class
22  */
23
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactEmailAddressTypeArray.h"
30 #include "JSContactEmailAddress.h"
31
32 #define CONTACT_CLASS_NAME "EmailAddress"
33
34 #define CONTACT_ATTR_EMAIL "email"
35 #define CONTACT_ATTR_TYPES "types"
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 namespace Contact {
40
41 using namespace TizenApis::Commons;
42 using namespace TizenApis::Api::Contact;
43
44 JSClassRef JSContactEmailAddress::m_classRef = NULL;
45
46 JSClassDefinition JSContactEmailAddress::m_classInfo =
47 {
48         0,
49         kJSClassAttributeNone,
50         CONTACT_CLASS_NAME,
51         NULL,
52         m_property,
53         m_functions,
54         Initialize,
55         Finalize,
56         NULL, //hasProperty,
57         NULL, //GetProperty,
58         NULL, //SetProperty,
59         NULL, //DeleteProperty,
60         NULL, //getPropertyNames,
61         NULL,
62         NULL,
63         NULL,
64         NULL, //ConvertToType,
65 };
66
67 JSStaticValue JSContactEmailAddress::m_property[] = {
68         { CONTACT_ATTR_EMAIL, getEmail, setEmail, kJSPropertyAttributeNone },
69         { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
70         { 0, 0, 0, 0 }
71 };
72
73 JSStaticFunction JSContactEmailAddress::m_functions[] =
74 {
75         { 0, 0, 0 }
76 };
77
78 JSClassRef JSContactEmailAddress::getClassRef() {
79         if (!m_classRef) {
80                 m_classRef = JSClassCreate(&m_classInfo);
81         }
82         return m_classRef;
83 }
84
85 JSValueRef JSContactEmailAddress::createJSObject(JSContextRef context,
86                 const std::string email,
87                 const ContactEmailAddressTypeArrayPtr types)
88 {
89         ContactEmailAddressPtr privateData = ContactEmailAddressPtr(new ContactEmailAddress());
90
91         privateData->setEmail(email);
92         privateData->setTypes(types);
93
94         JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, privateData);
95         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
96         if (NULL == jsValueRef) {
97                 LogError("object creation error");
98                 return JSValueMakeUndefined(context);
99         }
100         return jsValueRef;
101 }
102
103 bool JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
104 {
105         return JSValueIsObjectOfClass(context, value, getClassRef());
106 }
107
108 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(JSContextRef context, JSValueRef value)
109 {
110         if (!isObjectOfClass(context, value)) {
111                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
112         }
113         JSObjectRef object = JSValueToObject(context, value, NULL);
114         if (!object) {
115                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
116         }
117         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
118         if (!priv) {
119                 Throw(WrtDeviceApis::Commons::NullPointerException);
120         }
121         return priv->getObject();
122 }
123
124 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
125 {
126         assert(NULL != JSObjectGetPrivate(object));
127 }
128
129 void JSContactEmailAddress::Finalize(JSObjectRef object)
130 {
131         //delete (JSObjectGetPrivate(object));
132 }
133
134 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
135 {
136         //LogDebug("entered");
137         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
138         if (!priv) {
139                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
140         }
141         ContactEmailAddressPtr result = priv->getObject();
142         if (!result) {
143                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
144         }
145         return result;
146 }
147
148 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
149                 JSObjectRef object,
150                 JSStringRef propertyName,
151                 JSValueRef* exception)
152 {
153         //LogDebug("entered");
154         Try
155         {
156                 ContactConverterFactory::ConverterType converter =
157                                 ContactConverterFactory::getConverter(context);
158                 ContactEmailAddressPtr emailAddress = getPrivData(object);
159                 return converter->toJSValueRef(emailAddress->getEmail());
160         }
161         Catch(WrtDeviceApis::Commons::Exception)
162         {
163                 LogWarning("trying to get incorrect value");
164         }
165         return JSValueMakeUndefined(context);
166 }
167
168 bool JSContactEmailAddress::setEmail(JSContextRef context,
169                 JSObjectRef object,
170                 JSStringRef propertyName,
171                 JSValueRef value,
172                 JSValueRef* exception)
173 {
174         Try
175         {
176                 ContactEmailAddressPtr emailAddress = getPrivData(object);
177                 ContactConverterFactory::ConverterType converter =
178                                 ContactConverterFactory::getConverter(context);
179                 emailAddress->setEmail(converter->toString(value));
180                 return true;
181         }
182         Catch(WrtDeviceApis::Commons::Exception)
183         {
184                 LogWarning("trying to set incorrect value");
185         }
186         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
187         return false;
188 }
189
190
191 JSValueRef JSContactEmailAddress::getTypes(JSContextRef context,
192                 JSObjectRef object,
193                 JSStringRef propertyName,
194                 JSValueRef* exception)
195 {
196         //LogDebug("entered");
197         Try
198         {
199                 ContactConverterFactory::ConverterType converter =
200                                 ContactConverterFactory::getConverter(context);
201                 ContactEmailAddressPtr emailAddress = getPrivData(object);
202                 return JSContactEmailAddressTypeArray::createArray(context, emailAddress->getTypes());
203         }
204         Catch(WrtDeviceApis::Commons::Exception)
205         {
206                 LogWarning("trying to get incorrect value");
207         }
208         return JSValueMakeUndefined(context);
209 }
210
211 bool JSContactEmailAddress::setTypes(JSContextRef context,
212                 JSObjectRef object,
213                 JSStringRef propertyName,
214                 JSValueRef value,
215                 JSValueRef* exception)
216 {
217         Try
218         {
219                 ContactEmailAddressPtr emailAddress = getPrivData(object);
220                 ContactConverterFactory::ConverterType converter =
221                                 ContactConverterFactory::getConverter(context);
222
223                 if(JSContactEmailAddressTypeArray::isObjectOfClass(context, value))
224                         emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value));       // TODO to implement this function on converter
225                 else
226                         emailAddress->setTypes(JSContactEmailAddressTypeArray::getContactEmailAddressTypeArray(context, value));
227
228                 return true;
229         }
230         Catch(WrtDeviceApis::Commons::Exception)
231         {
232                 LogWarning("trying to set incorrect value");
233         }
234         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
235         return false;
236 }
237
238 } // Contact
239 } // Tizen1_0
240 } // TizenApis