2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/Validator.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <CommonsJavaScript/JSCallbackManager.h>
25 #include <CommonsJavaScript/Utils.h>
26 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
27 #include "IMessageFolder.h"
28 #include "IMessagingTypes.h"
31 #include <SecurityExceptions.h>
33 #include "MessagingErrorMsg.h"
34 #include "JSMessageFolder.h"
35 #include "ConverterMessage.h"
39 using namespace DeviceAPI::Messaging;
40 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
47 JSClassDefinition JSMessageFolder::m_classInfo =
50 kJSClassAttributeNone,
60 NULL, //deleteProperty,
68 const char* JSMessageFolder::FOLDERID = "id";
69 const char* JSMessageFolder::PARENTID = "parentId";
70 const char* JSMessageFolder::ACCOUNTID = "serviceId";
71 const char* JSMessageFolder::CONTENTTYPE = "contentType";
72 const char* JSMessageFolder::NAME = "name";
73 const char* JSMessageFolder::PATH = "path";
74 const char* JSMessageFolder::TYPE = "type";
75 const char* JSMessageFolder::SYNCHRONIZABLE = "synchronizable";
77 const std::string JSMessageFolder::TYPE_SMS = "messaging.sms";
78 const std::string JSMessageFolder::TYPE_MMS = "messaging.mms";
79 const std::string JSMessageFolder::TYPE_EMAIL = "messaging.email";
81 const std::string JSMessageFolder::FOLDER_INBOX = "INBOX";
82 const std::string JSMessageFolder::FOLDER_OUTBOX = "OUTBOX";
83 const std::string JSMessageFolder::FOLDER_DRAFTBOX = "DRAFTS";
84 const std::string JSMessageFolder::FOLDER_SENTBOX = "SENTBOX";
85 const std::string JSMessageFolder::FOLDER_NOTSTANDARD = "";
90 JSStaticValue JSMessageFolder::m_properties[] = {
91 {"id", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
92 {"parentId", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
93 {"serviceId", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
94 {"contentType", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
95 {"name", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
96 {"path", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
97 {"type", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
98 {"synchronizable", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
103 const JSClassRef JSMessageFolder::getClassRef() {
105 m_jsClassRef = JSClassCreate(&m_classInfo);
110 const JSClassDefinition* JSMessageFolder::getClassInfo() {
114 JSClassRef JSMessageFolder::m_jsClassRef = JSClassCreate(JSMessageFolder::getClassInfo());
116 void JSMessageFolder::initialize(JSContextRef context, JSObjectRef object)
118 LoggerD("JSMessageFolder::initialize ");
119 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
127 LoggerD("JSMessageFolder::already exist ");
131 JSObjectRef JSMessageFolder::createJSObject(JSContextRef context, IMessageFolderPtr object)
133 JSMessageFolderPriv* priv = new JSMessageFolderPriv(context, object);
134 return JSObjectMake(context, getClassRef(), priv);
138 void JSMessageFolder::finalize(JSObjectRef object) {
139 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
141 LoggerD("JSMessageFolder::Finalrize");
145 JSObjectSetPrivate(object, NULL);
150 bool JSMessageFolder::hasInstance(JSContextRef context, JSObjectRef constructor,
151 JSValueRef possibleInstance, JSValueRef* exception) {
152 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
155 JSValueRef JSMessageFolder::getProperty(JSContextRef context,
157 JSStringRef propertyName,
158 JSValueRef* exception)
162 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
163 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
168 throw DeviceAPI::Common::UnknownException("Object is null");
171 IMessageFolderPtr MessageFolder(priv->getObject());
172 if(JSStringIsEqualToUTF8CString(propertyName, FOLDERID))
174 return converter->toJSValueRef(converter->convertIntToString(MessageFolder->getId()));
176 else if(JSStringIsEqualToUTF8CString(propertyName, PARENTID))
179 tmpInt = MessageFolder->getParentId();
182 return JSValueMakeNull(context);
184 return converter->toJSValueRef(converter->convertIntToString(tmpInt));
186 else if(JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID))
188 return converter->toJSValueRef(converter->convertIntToString(MessageFolder->getAccountId()));
190 else if(JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE))
192 return converter->toJSValueRef(convertMessageType(MessageFolder->getContentType()));
194 else if(JSStringIsEqualToUTF8CString(propertyName, NAME))
196 return converter->toJSValueRef(MessageFolder->getName());
198 else if(JSStringIsEqualToUTF8CString(propertyName, PATH))
200 return converter->toJSValueRef(MessageFolder->getPath());
202 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
204 return converter->toJSValueRef(convertFolderType(MessageFolder->getType()));
206 else if(JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE))
208 return converter->toJSValueRef(MessageFolder->getSynchronizable());
212 catch (const WrtDeviceApis::Commons::Exception& ex) {
213 LoggerE("Exception: " << ex.GetMessage());
214 return JSValueMakeUndefined(context);
219 bool JSMessageFolder::setProperty(JSContextRef context,
221 JSStringRef propertyName,
223 JSValueRef* exception)
226 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
230 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
232 throw DeviceAPI::Common::UnknownException("Object is null");
234 IMessageFolderPtr MessageFolder(priv->getObject());
236 if (JSStringIsEqualToUTF8CString(propertyName, FOLDERID)) {
238 } else if (JSStringIsEqualToUTF8CString(propertyName, PARENTID)) {
240 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID)) {
242 } else if (JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE)) {
244 } else if (JSStringIsEqualToUTF8CString(propertyName, NAME)) {
245 std::string name = converter.toString(value);
246 MessageFolder->setName(name);
248 } else if (JSStringIsEqualToUTF8CString(propertyName, PATH)) {
250 } else if (JSStringIsEqualToUTF8CString(propertyName, TYPE)) {
252 } else if (JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) {
253 bool synchronizable = converter.toBool(value);
254 MessageFolder->setSynchronizable(synchronizable);
258 catch(const WrtDeviceApis::Commons::Exception& ex)
260 LoggerE("Exception: " << ex.GetMessage());
266 void JSMessageFolder::getPropertyNames(JSContextRef context,
268 JSPropertyNameAccumulatorRef propertyNames)
271 JSStringRef propertyName = NULL;
273 propertyName = JSStringCreateWithUTF8CString(FOLDERID);
274 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
275 JSStringRelease(propertyName);
277 propertyName = JSStringCreateWithUTF8CString(PARENTID);
278 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
279 JSStringRelease(propertyName);
281 propertyName = JSStringCreateWithUTF8CString(ACCOUNTID);
282 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
283 JSStringRelease(propertyName);
285 propertyName = JSStringCreateWithUTF8CString(CONTENTTYPE);
286 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
287 JSStringRelease(propertyName);
289 propertyName = JSStringCreateWithUTF8CString(NAME);
290 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
291 JSStringRelease(propertyName);
293 propertyName = JSStringCreateWithUTF8CString(PATH);
294 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
295 JSStringRelease(propertyName);
297 propertyName = JSStringCreateWithUTF8CString(TYPE);
298 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
299 JSStringRelease(propertyName);
301 propertyName = JSStringCreateWithUTF8CString(SYNCHRONIZABLE);
302 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
303 JSStringRelease(propertyName);
306 bool JSMessageFolder::isObjectOfClass(JSContextRef context, JSValueRef value)
308 return JSValueIsObjectOfClass(context, value, getClassRef());
311 IMessageFolderPtr JSMessageFolder::getMessageFolder(JSContextRef context, JSValueRef value)
313 JSObjectRef object = JSValueToObject(context, value, NULL);
316 throw DeviceAPI::Common::TypeMismatchException("Failed to get object");
318 JSMessageFolderPriv *priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
322 throw DeviceAPI::Common::TypeMismatchException("Object is null");
324 return priv->getObject();
327 std::string JSMessageFolder::convertMessageType(int msgType){
330 return JSMessageFolder::TYPE_SMS;
331 }else if(msgType == MMS){
332 return JSMessageFolder::TYPE_MMS;
333 }else if(msgType == EMAIL){
334 return JSMessageFolder::TYPE_EMAIL;
340 std::string JSMessageFolder::convertFolderType(int msgType){
342 if(msgType == INBOX){
343 return JSMessageFolder::FOLDER_INBOX;
344 }else if(msgType == OUTBOX){
345 return JSMessageFolder::FOLDER_OUTBOX;
346 }else if(msgType == DRAFTBOX){
347 return JSMessageFolder::FOLDER_DRAFTBOX;
348 }else if(msgType == SENTBOX){
349 return JSMessageFolder::FOLDER_SENTBOX;
350 }else if(msgType == USERDEFINED_FOLDER){
351 return JSMessageFolder::FOLDER_NOTSTANDARD;
353 return JSMessageFolder::FOLDER_NOTSTANDARD;