Update change log and spec for wrt-plugins-tizen_0.4.29
[framework/web/wrt-plugins-tizen.git] / src / Messaging / JSMessageFolder.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
19
20
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"
29
30 #include <Logger.h>
31 #include <SecurityExceptions.h>
32
33 #include "MessagingErrorMsg.h"
34 #include "JSMessageFolder.h"
35 #include "ConverterMessage.h"
36
37 using namespace std;
38 using namespace DPL;
39 using namespace DeviceAPI::Messaging;
40 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43
44 namespace DeviceAPI {
45 namespace Messaging {
46
47 JSClassDefinition JSMessageFolder::m_classInfo =
48 {
49         0,
50         kJSClassAttributeNone,
51         "MessageFolder",
52         NULL,
53         NULL, //m_properties,
54         NULL,
55         initialize,
56         finalize,
57         NULL, //hasProperty,
58         getProperty,
59         setProperty,
60         NULL, //deleteProperty,
61         getPropertyNames,
62         NULL,
63         NULL,
64         hasInstance,
65         NULL
66 };
67
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";
76
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";
80
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    = "";
86
87
88
89 #if 0
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 },
99         {0, 0, 0, 0}
100 };
101 #endif
102
103 const JSClassRef JSMessageFolder::getClassRef() {
104         if (!m_jsClassRef) {
105                 m_jsClassRef = JSClassCreate(&m_classInfo);
106         }
107         return m_jsClassRef;
108 }
109
110 const JSClassDefinition* JSMessageFolder::getClassInfo() {
111         return &m_classInfo;
112 }
113
114 JSClassRef JSMessageFolder::m_jsClassRef = JSClassCreate(JSMessageFolder::getClassInfo());
115
116 void JSMessageFolder::initialize(JSContextRef context, JSObjectRef object)
117 {
118         LoggerD("JSMessageFolder::initialize ");
119         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
120
121         if (priv == NULL)
122         {
123
124         }
125         else
126         {
127                 LoggerD("JSMessageFolder::already exist ");
128         }
129 }
130
131 JSObjectRef JSMessageFolder::createJSObject(JSContextRef context, IMessageFolderPtr object)
132 {
133         JSMessageFolderPriv* priv = new JSMessageFolderPriv(context, object);
134         return JSObjectMake(context, getClassRef(), priv);
135 }
136
137
138 void JSMessageFolder::finalize(JSObjectRef object) {
139         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
140
141         LoggerD("JSMessageFolder::Finalrize");
142
143         if (priv != NULL)
144         {
145                 JSObjectSetPrivate(object, NULL);
146                 delete priv;
147         }
148 }
149
150 bool JSMessageFolder::hasInstance(JSContextRef context, JSObjectRef constructor,
151                 JSValueRef possibleInstance, JSValueRef* exception) {
152         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
153 }
154
155 JSValueRef JSMessageFolder::getProperty(JSContextRef context,
156                                                                                 JSObjectRef object,
157                                                                                 JSStringRef propertyName,
158                                                                                 JSValueRef* exception)
159 {
160         LoggerD("<<<");
161
162         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
163         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
164         try
165         {
166                 if (priv == NULL)
167                 {
168                         throw DeviceAPI::Common::UnknownException("Object is null");
169                 }
170
171                 IMessageFolderPtr MessageFolder(priv->getObject());
172                 if(JSStringIsEqualToUTF8CString(propertyName, FOLDERID))
173                 {
174                         return converter->toJSValueRef(converter->convertIntToString(MessageFolder->getId()));
175                 }
176                 else if(JSStringIsEqualToUTF8CString(propertyName, PARENTID))
177                 {
178                         int tmpInt;
179                         tmpInt = MessageFolder->getParentId();
180                         if(tmpInt < 0)
181                         {
182                                 return JSValueMakeNull(context);
183                         }
184                         return converter->toJSValueRef(converter->convertIntToString(tmpInt));
185                 }
186                 else if(JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID))
187                 {
188                         return converter->toJSValueRef(converter->convertIntToString(MessageFolder->getAccountId()));
189                 }
190                 else if(JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE))
191                 {
192                         return converter->toJSValueRef(convertMessageType(MessageFolder->getContentType()));
193                 }
194                 else if(JSStringIsEqualToUTF8CString(propertyName, NAME))
195                 {
196                         return converter->toJSValueRef(MessageFolder->getName());
197                 }
198                 else if(JSStringIsEqualToUTF8CString(propertyName, PATH))
199                 {
200                         return converter->toJSValueRef(MessageFolder->getPath());
201                 }
202                 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
203                 {
204                         return converter->toJSValueRef(convertFolderType(MessageFolder->getType()));
205                 }
206                 else if(JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE))
207                 {
208                         return converter->toJSValueRef(MessageFolder->getSynchronizable());
209                 }
210
211         }
212         catch (const WrtDeviceApis::Commons::Exception& ex) {
213                 LoggerE("Exception: " << ex.GetMessage());
214                 return JSValueMakeUndefined(context);
215         }
216         return NULL;
217 }
218
219 bool JSMessageFolder::setProperty(JSContextRef context,
220         JSObjectRef object,
221         JSStringRef propertyName,
222         JSValueRef value,
223         JSValueRef* exception)
224 {
225     LoggerD("entered");
226         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
227
228     try
229     {
230                 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
231         if (!priv) {
232                 throw DeviceAPI::Common::UnknownException("Object is null");
233         }
234                 IMessageFolderPtr MessageFolder(priv->getObject());
235
236             if (JSStringIsEqualToUTF8CString(propertyName, FOLDERID)) {
237                 return true;
238             } else if (JSStringIsEqualToUTF8CString(propertyName, PARENTID)) {
239                 return true;
240             } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID)) {
241                 return true;
242             } else if (JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE)) {
243                 return true;
244             } else if (JSStringIsEqualToUTF8CString(propertyName, NAME)) {
245             std::string name = converter.toString(value);
246             MessageFolder->setName(name);
247             return true;
248             } else if (JSStringIsEqualToUTF8CString(propertyName, PATH)) {
249                 return true;
250             } else if (JSStringIsEqualToUTF8CString(propertyName, TYPE)) {
251                 return true;
252         } else if (JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) {
253             bool synchronizable = converter.toBool(value);
254             MessageFolder->setSynchronizable(synchronizable);
255             return true;
256         }
257     }
258     catch(const WrtDeviceApis::Commons::Exception& ex)
259     {
260                 LoggerE("Exception: " << ex.GetMessage());
261                 return true;
262     }
263     return false;
264 }
265
266 void JSMessageFolder::getPropertyNames(JSContextRef context,
267         JSObjectRef object,
268         JSPropertyNameAccumulatorRef propertyNames)
269 {
270     LoggerD("Entered");
271     JSStringRef propertyName = NULL;
272
273     propertyName = JSStringCreateWithUTF8CString(FOLDERID);
274     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
275     JSStringRelease(propertyName);
276
277     propertyName = JSStringCreateWithUTF8CString(PARENTID);
278     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
279     JSStringRelease(propertyName);
280
281     propertyName = JSStringCreateWithUTF8CString(ACCOUNTID);
282     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
283     JSStringRelease(propertyName);
284
285     propertyName = JSStringCreateWithUTF8CString(CONTENTTYPE);
286     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
287     JSStringRelease(propertyName);
288
289     propertyName = JSStringCreateWithUTF8CString(NAME);
290     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
291     JSStringRelease(propertyName);
292
293     propertyName = JSStringCreateWithUTF8CString(PATH);
294     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
295     JSStringRelease(propertyName);
296
297     propertyName = JSStringCreateWithUTF8CString(TYPE);
298     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
299     JSStringRelease(propertyName);
300
301     propertyName = JSStringCreateWithUTF8CString(SYNCHRONIZABLE);
302     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
303     JSStringRelease(propertyName);
304 }
305
306 bool JSMessageFolder::isObjectOfClass(JSContextRef context, JSValueRef value)
307 {
308         return JSValueIsObjectOfClass(context, value, getClassRef());
309 }
310
311 IMessageFolderPtr JSMessageFolder::getMessageFolder(JSContextRef context, JSValueRef value)
312 {
313         JSObjectRef object = JSValueToObject(context, value, NULL);
314         if (!object)
315         {
316                 throw DeviceAPI::Common::TypeMismatchException("Failed to get object");
317         }
318         JSMessageFolderPriv *priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
319
320         if (!priv)
321         {
322                 throw DeviceAPI::Common::TypeMismatchException("Object is null");
323         }
324         return priv->getObject();
325 }
326
327 std::string JSMessageFolder::convertMessageType(int msgType){
328
329         if(msgType == SMS){
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;
335         }else{
336                 return NULL;
337         }
338  }
339
340 std::string JSMessageFolder::convertFolderType(int msgType){
341
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;
352         }else{
353                 return JSMessageFolder::FOLDER_NOTSTANDARD;
354         }
355  }
356
357
358 }
359 }// WrtPlugins
360
361