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