Update change log and spec for wrt-plugins-tizen_0.4.29
[framework/web/wrt-plugins-tizen.git] / src / Callhistory / JSCallHistoryEntry.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <cassert>
19 #include <memory>
20 #include <dpl/shared_ptr.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include "CallHistoryEntryProperties.h"
24 #include "CallHistoryFactory.h"
25 #include "ICallHistoryEntry.h"
26 #include "CallHistoryDefine.h"
27 #include <JSWebAPIErrorFactory.h>
28 #include <SecurityExceptions.h>
29 #include "JSCallHistoryEntry.h"
30 #include "JSRemoteParty.h"
31 #include "Converter.h"
32 #include <Logger.h>
33
34 using namespace WrtDeviceApis;
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37 using namespace DeviceAPI::Common;
38
39 namespace DeviceAPI {
40 namespace CallHistory {
41
42 JSClassDefinition JSCallHistoryEntry::m_classInfo = {
43         0,
44         kJSClassAttributeNone,
45         "callhistoryentry",
46         0,
47         m_property,
48         0,
49         initialize,
50         finalize,
51         NULL,     //HasProperty,
52         getProperty,
53         setProperty, // NULL,     //SetProperty,
54         NULL,     //DeleteProperty,
55         NULL,     //GetPropertyNames,
56         NULL,     //CallAsFunction,
57         NULL,     //CallAsConstructor,
58         hasInstance,
59         NULL,     //ConvertToType
60 };
61
62 JSStaticValue JSCallHistoryEntry::m_property[] = {
63         { STR_ENTRY_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
64         { STR_CALL_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
65         { STR_TAGS, getProperty, NULL, kJSPropertyAttributeReadOnly },
66         { STR_REMOTE_PARTIES, getProperty, NULL, kJSPropertyAttributeReadOnly },
67         { STR_START_TIME, getProperty, NULL, kJSPropertyAttributeReadOnly },
68         { STR_DURATION, getProperty, NULL, kJSPropertyAttributeReadOnly },
69         { STR_DIRECTION, getProperty, setProperty, kJSPropertyAttributeNone },
70         { 0, 0, 0, 0 }
71 };
72
73 const JSClassRef JSCallHistoryEntry::getClassRef()
74 {
75         if (!m_jsClassRef) {
76                 m_jsClassRef = JSClassCreate(&m_classInfo);
77         }
78         return m_jsClassRef;
79 }
80
81 const JSClassDefinition* JSCallHistoryEntry::getClassInfo()
82 {
83         return &m_classInfo;
84 }
85
86 JSClassRef JSCallHistoryEntry::m_jsClassRef = JSClassCreate(JSCallHistoryEntry::getClassInfo());
87
88 JSObjectRef JSCallHistoryEntry::createJSObject(JSContextRef context,
89         const CallHistoryEntryProperties &entryInfo)
90 {
91         CallHistoryEntryPropertiesPtr callHistoryEntryProps(new CallHistoryEntryProperties(entryInfo));
92         JSCallHistoryEntryPriv *priv = new JSCallHistoryEntryPriv(context, CallHistoryEntryPropertiesPtr(callHistoryEntryProps));
93         if (!priv) {
94                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
95         }
96         return JSObjectMake(context, getClassRef(), priv);
97 }
98
99 CallHistoryEntryPropertiesPtr JSCallHistoryEntry::getCallHistoryEntry(JSContextRef context, JSValueRef value)
100 {
101         if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
102                 Throw(WrtDeviceApis::Commons::ConversionException);
103         }
104         JSObjectRef object = JSValueToObject(context, value, NULL);
105         if (!object) {
106                 Throw(WrtDeviceApis::Commons::ConversionException);
107         }
108         JSCallHistoryEntryPriv *priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
109         if (!priv) {
110                 Throw(WrtDeviceApis::Commons::ConversionException);
111         }
112         return priv->getObject();
113 }
114
115 void JSCallHistoryEntry::initialize(JSContextRef context, JSObjectRef object)
116 {
117 }
118
119 void JSCallHistoryEntry::finalize(JSObjectRef object)
120 {
121         JSCallHistoryEntryPriv* priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
122         if (priv != NULL) {
123                 JSObjectSetPrivate(object, NULL);
124                 delete priv;
125         }
126 }
127
128 JSValueRef JSCallHistoryEntry::getProperty(JSContextRef context,
129         JSObjectRef object,
130         JSStringRef propertyName,
131         JSValueRef* exception)
132 {
133         JSCallHistoryEntryPriv *priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
134         if (!priv) {
135                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type missmatch error : Invalid private pointer");
136         }
137
138         try {
139                 CallHistoryEntryPropertiesPtr entryInfo = priv->getObject();
140                 Converter convert(context);
141
142                 if (JSStringIsEqualToUTF8CString(propertyName, STR_ENTRY_ID)) {
143                         std::ostringstream stream;
144                         stream << entryInfo->getEntryId();
145                         std::string idStr = stream.str();
146                         return convert.toJSValueRef(idStr);
147                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_CALL_TYPE)) {
148                         return convert.toJSValueRef(entryInfo->getCallType());
149                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TAGS)) {
150                         return convert.toJSValueRef(entryInfo->getTags(), context);
151                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_REMOTE_PARTIES)) {
152                         return convert.toJSValueRef(entryInfo->getRemoteParties(), context);
153                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_START_TIME)) {
154                         return convert.toJSValueRef(entryInfo->getStartTime());
155                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DURATION)) {
156                         return convert.toJSValueRef(entryInfo->getDuration());
157                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DIRECTION)) {
158                         return convert.toJSValueRef(entryInfo->getDirection());
159                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DURATION)) {
160                         return convert.toJSValueRef(entryInfo->getDuration());
161                 }
162         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
163                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, ex.GetMessage());
164         } catch(const WrtDeviceApis::Commons::Exception& ex) {
165                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, ex.GetMessage());
166         }
167         return NULL;
168 }
169
170 bool JSCallHistoryEntry::setProperty(JSContextRef context,
171                 JSObjectRef object,
172                 JSStringRef propertyName,
173                 JSValueRef value,
174                 JSValueRef* exception)
175 {
176         JSCallHistoryEntryPriv *priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
177         if (!priv) {
178                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type missmatch error : Invalid private pointer");
179         }
180
181         Converter converter(context);
182         try {
183                 if (JSStringIsEqualToUTF8CString(propertyName, STR_DIRECTION)) {
184                         CallHistoryEntryPropertiesPtr entryInfo = priv->getObject();
185                         if (entryInfo != NULL) {
186                                 ICallHistoryEntryPtr callHistoryEntry(CallHistoryFactory::getInstance().getCallHistoryEntryObject());
187                                 std::string direction(converter.toString(value));
188
189                                 if (callHistoryEntry != NULL) {
190                                         if((entryInfo->getDirection().compare(STR_MISSED_NEW) == 0) && (direction.compare(STR_MISSED) == 0)) {
191                                                 callHistoryEntry->setMarkSeen(entryInfo->getEntryId());
192                                                 entryInfo->setDirection(STR_MISSED);
193                                         }
194                                         return true;
195                                 }
196                         }
197                 } 
198         } catch (const WrtDeviceApis::Commons::Exception& ex) {
199                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
200         }
201
202         return false;
203 }
204 bool JSCallHistoryEntry::hasInstance(JSContextRef context,
205         JSObjectRef constructor,
206         JSValueRef possibleInstance,
207         JSValueRef* exception)
208 {
209         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
210 }
211 }
212 }
213