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