Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSCallHistoryEntry.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 #include <cassert>
19 #include <memory>
20 #include <dpl/log/log.h>
21 #include <dpl/shared_ptr.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <API/Call/CallHistoryEntryProperties.h>
25 #include <API/Call/CallHistoryFactory.h>
26 #include <API/Call/ICallHistoryEntry.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include <Tizen/Common/JSTizenExceptionFactory.h>
29 #include <Tizen/Common/SecurityExceptions.h>
30 #include "JSCallHistoryEntry.h"
31 #include "JSRemoteParty.h"
32 #include "Converter.h"
33
34 namespace TizenApis {
35 namespace Tizen1_0 {
36
37 using namespace WrtDeviceApis;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace Api::Call;
41 using namespace TizenApis::Commons;
42
43 #define STR_ENTRY_ID                    "entryId"
44 #define STR_SERVICE_ID                  "serviceId"
45 #define STR_CALL_TYPE                   "callType"
46 #define STR_TAGS                                "tags"
47 #define STR_REMOTE_PARTIES              "remoteParties"
48 #define STR_FORWARDEDFROM               "forwardedFrom"
49 #define STR_START_TIME                  "startTime"
50 #define STR_DURATION                    "duration"
51 #define STR_END_REASON                  "endReason"
52 #define STR_DIRECTION                   "direction"
53 #define STR_RECORDING                   "recording"
54 #define STR_COST                                "cost"
55 #define STR_CURRENCY                    "currency"
56
57 JSClassDefinition JSCallHistoryEntry::m_classInfo = {
58         0,
59         kJSClassAttributeNone,
60         "callhistoryentry",
61         0,
62         m_property,
63         0,
64         initialize,
65         finalize,
66         NULL,     //HasProperty,
67         getProperty,
68         NULL,     //SetProperty,
69         NULL,     //DeleteProperty,
70         NULL,     //GetPropertyNames,
71         NULL,     //CallAsFunction,
72         NULL,     //CallAsConstructor,
73         hasInstance,
74         NULL,     //ConvertToType
75 };
76
77 JSStaticValue JSCallHistoryEntry::m_property[] = {
78         { STR_ENTRY_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
79         { STR_SERVICE_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
80         { STR_CALL_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
81         { STR_TAGS, getProperty, NULL, kJSPropertyAttributeReadOnly },
82         { STR_REMOTE_PARTIES, getProperty, NULL, kJSPropertyAttributeReadOnly },
83         { STR_FORWARDEDFROM, getProperty, NULL, kJSPropertyAttributeReadOnly },
84         { STR_START_TIME, getProperty, NULL, kJSPropertyAttributeReadOnly },
85         { STR_DURATION, getProperty, NULL, kJSPropertyAttributeReadOnly },
86         { STR_END_REASON, getProperty, NULL, kJSPropertyAttributeReadOnly },
87         { STR_DIRECTION, getProperty, setProperty, kJSPropertyAttributeNone },
88         { STR_RECORDING, getProperty, NULL, kJSPropertyAttributeReadOnly },
89         { STR_COST, getProperty, NULL, kJSPropertyAttributeReadOnly },
90         { STR_CURRENCY, getProperty, NULL, kJSPropertyAttributeReadOnly },
91         { 0, 0, 0, 0 }
92 };
93
94 const JSClassRef JSCallHistoryEntry::getClassRef()
95 {
96         if (!m_jsClassRef) {
97                 m_jsClassRef = JSClassCreate(&m_classInfo);
98         }
99         return m_jsClassRef;
100 }
101
102 const JSClassDefinition* JSCallHistoryEntry::getClassInfo()
103 {
104         return &m_classInfo;
105 }
106
107 JSClassRef JSCallHistoryEntry::m_jsClassRef = JSClassCreate(JSCallHistoryEntry::getClassInfo());
108
109 JSObjectRef JSCallHistoryEntry::createJSObject(JSContextRef context,
110         const CallHistoryEntryProperties &entryInfo)
111 {
112         std::auto_ptr<CallHistoryEntryProperties> callHistoryEntryProps(new CallHistoryEntryProperties(entryInfo));
113         JSCallHistoryEntryPriv *priv = new JSCallHistoryEntryPriv(context, callHistoryEntryProps.get());
114         callHistoryEntryProps.release();
115         if (!priv) {
116                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
117         }
118         return JSObjectMake(context, getClassRef(), priv);
119 }
120
121 void JSCallHistoryEntry::initialize(JSContextRef context, JSObjectRef object)
122 {
123 }
124
125 void JSCallHistoryEntry::finalize(JSObjectRef object)
126 {
127         JSCallHistoryEntryPriv* priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
128         JSObjectSetPrivate(object, NULL);
129         delete priv;
130 }
131
132 JSValueRef JSCallHistoryEntry::getProperty(JSContextRef context,
133         JSObjectRef object,
134         JSStringRef propertyName,
135         JSValueRef* exception)
136 {
137         JSCallHistoryEntryPriv *priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
138         assert(priv && "Private object not set.");
139
140         try {
141                 JSContextRef globalContext = priv->getContext();
142                 CallHistoryEntryProperties *entryInfo = priv->getObject();
143                 Converter convert(context);
144
145                 if (JSStringIsEqualToUTF8CString(propertyName, STR_ENTRY_ID)) {
146                         return convert.toJSValueRef(entryInfo->getEntryId());
147                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_SERVICE_ID)) {
148                         return convert.toJSValueRef(entryInfo->getServiceId());
149                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_CALL_TYPE)) {
150                         return convert.toJSValueRef(entryInfo->getCallType());
151                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TAGS)) {
152                         return convert.toJSValueRef(entryInfo->getTags(), context);
153                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_REMOTE_PARTIES)) {
154                         return convert.toJSValueRef(entryInfo->getRemoteParties(), context);
155                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_FORWARDEDFROM)) {
156                         return TizenApis::Tizen1_0::JSRemoteParty::createJSObject(globalContext, (*(entryInfo->getForwardedFrom())));
157                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_START_TIME)) {
158                         return convert.toJSValueRef(entryInfo->getStartTime());
159                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DURATION)) {
160                         return convert.toJSValueRef(entryInfo->getDuration());
161                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_END_REASON)) {
162                         return convert.toJSValueRef(entryInfo->getEndReason());
163                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DIRECTION)) {
164                         return convert.toJSValueRef(entryInfo->getDirection());
165                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DURATION)) {
166                         return convert.toJSValueRef(entryInfo->getDuration());
167                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_RECORDING)) {
168                         return convert.toJSValueRef(entryInfo->getRecording(), context);
169                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_COST)) {
170                         return convert.toJSValueRef(entryInfo->getCost());
171                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_CURRENCY)) {
172                         return convert.toJSValueRef(entryInfo->getCurrency());
173                 }
174         } catch(WrtDeviceApis::Commons::Exception) {
175                 LogWarning("trying to get incorrect value");
176         }
177         return JSValueMakeUndefined(context);
178 }
179
180 bool JSCallHistoryEntry::setProperty(JSContextRef context,
181                 JSObjectRef object,
182                 JSStringRef propertyName,
183                 JSValueRef value,
184                 JSValueRef* exception)
185 {
186         JSCallHistoryEntryPriv *priv = static_cast<JSCallHistoryEntryPriv*>(JSObjectGetPrivate(object));
187         assert(priv && "Private object not set.");
188
189         Converter converter(context);
190         try {
191                 if (JSStringIsEqualToUTF8CString(propertyName, STR_DIRECTION)) {
192                         CallHistoryEntryProperties *entryInfo = priv->getObject();
193                         if (entryInfo != NULL) {
194                                 ICallHistoryEntryPtr callHistoryEntry(CallHistoryFactory::getInstance().getCallHistoryEntryObject());
195                                 std::string direction(converter.toString(value));
196
197                                 if (callHistoryEntry != NULL) {
198                                         if((entryInfo->getDirection().compare("missed-new") == 0) && (direction.compare("missed") == 0)) {
199                                                 callHistoryEntry->setMarkSeen(entryInfo->getEntryId());
200                                                 entryInfo->setDirection("missed");
201                                         } else {
202                                                 Throw(WrtDeviceApis::Commons::UnsupportedException);
203                                         }
204
205                                         return true;
206                                 } else {
207                                         Throw(WrtDeviceApis::Commons::Exception);
208                                 }
209                         } else {
210                                 Throw(WrtDeviceApis::Commons::Exception);
211                         }
212                 } else {
213                         Throw(WrtDeviceApis::Commons::UnsupportedException);
214                 }
215         } catch(const WrtDeviceApis::Commons::UnsupportedException& ex) {
216                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, "Not supported error");
217         } catch (const WrtDeviceApis::Commons::Exception& ex) {
218                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
219         }
220
221         return false;
222 }
223 bool JSCallHistoryEntry::hasInstance(JSContextRef context,
224         JSObjectRef constructor,
225         JSValueRef possibleInstance,
226         JSValueRef* exception)
227 {
228         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
229 }
230 }
231 }
232