Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessageFolder.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 /**
19 * @file          JSMessageFolder.cpp
20 * @author        Oy Kwon (sirot.kwon@samsung.com)
21 * @version       0.1
22 */
23
24
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <CommonsJavaScript/JSUtils.h>
28 #include <CommonsJavaScript/JSCallbackManager.h>
29 #include <CommonsJavaScript/Utils.h>
30 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
31 #include <API/Messaging/IMessageFolder.h>
32 #include <API/Messaging/IMessagingTypes.h>
33
34
35 #include <Tizen/Common/JSTizenExceptionFactory.h>
36 #include <Tizen/Common/JSTizenException.h>
37 #include <Tizen/Common/SecurityExceptions.h>
38
39 #include "MessagingErrorMsg.h"
40 #include "JSMessageFolder.h"
41
42 using namespace std;
43 using namespace DPL;
44 using namespace TizenApis::Api::Messaging;
45 using namespace TizenApis::Commons;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48
49 namespace TizenApis {
50 namespace Tizen1_0 {
51
52 JSClassDefinition JSMessageFolder::m_classInfo =
53 {
54         0,
55         kJSClassAttributeNone,
56         "MessageFolder",
57         NULL,
58         m_properties,
59         NULL,
60         initialize,
61         finalize,
62         NULL, //hasProperty,
63         NULL, //getProperty,
64         NULL, //setProperty,
65         NULL, //deleteProperty,
66         NULL, //getPropertyNames,
67         NULL,
68         NULL,
69         hasInstance,
70         NULL
71 };
72
73 const char* JSMessageFolder::FOLDERID = "id";
74 const char* JSMessageFolder::PARENTID = "parentid";
75 const char* JSMessageFolder::ACCOUNTID = "accountid";
76 const char* JSMessageFolder::CONTENTTYPE = "contentType";
77 const char* JSMessageFolder::NAME = "name";
78 const char* JSMessageFolder::PATH = "path";
79 const char* JSMessageFolder::TYPE = "type";
80 const char* JSMessageFolder::SYNCHRONIZABLE = "synchronizable";
81
82 const std::string JSMessageFolder::TYPE_SMS              = "SMS";
83 const std::string JSMessageFolder::TYPE_MMS              = "MMS";
84 const std::string JSMessageFolder::TYPE_EMAIL    = "EMAIL";
85
86 const std::string JSMessageFolder::FOLDER_INBOX          = "INBOX";
87 const std::string JSMessageFolder::FOLDER_OUTBOX         = "OUTBOX";
88 const std::string JSMessageFolder::FOLDER_DRAFTBOX       = "DRAFTS";
89 const std::string JSMessageFolder::FOLDER_SENTBOX        = "SENTBOX";
90 const std::string JSMessageFolder::FOLDER_NOTSTANDARD    = "";
91
92
93
94 JSStaticValue JSMessageFolder::m_properties[] = {
95         {"id", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
96         {"parentid", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },        
97         {"accountid", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },       
98         {"contentType", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
99         {"name", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
100         {"path", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
101         {"type", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
102         {"synchronizable", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
103         {0, 0, 0, 0}
104 };
105
106 const JSClassRef JSMessageFolder::getClassRef() {
107         if (!m_jsClassRef) {
108                 m_jsClassRef = JSClassCreate(&m_classInfo);
109         }
110         return m_jsClassRef;
111 }
112
113 const JSClassDefinition* JSMessageFolder::getClassInfo() {
114         return &m_classInfo;
115 }
116
117 JSClassRef JSMessageFolder::m_jsClassRef = JSClassCreate(JSMessageFolder::getClassInfo());
118
119 void JSMessageFolder::initialize(JSContextRef context, JSObjectRef object) 
120 {
121         LogDebug("JSMessageFolder::initialize ");
122         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
123
124         if (priv == NULL)
125         {
126
127         }
128         else
129         {
130                 LogDebug("JSMessageFolder::already exist ");            
131         }
132 }
133
134
135
136 JSObjectRef JSMessageFolder::createJSObject(JSContextRef context, IMessageFolderPtr object)
137 {
138         JSMessageFolderPriv* priv = new JSMessageFolderPriv(context, object);
139         return JSObjectMake(context, getClassRef(), priv);
140 }
141
142
143 void JSMessageFolder::finalize(JSObjectRef object) {
144         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
145
146         LogDebug("JSMessageFolder::Finalrize");
147
148         if (priv != NULL) 
149         {
150                 JSObjectSetPrivate(object, NULL);
151                 delete priv;
152         }
153 }
154
155 bool JSMessageFolder::hasInstance(JSContextRef context, JSObjectRef constructor,
156                 JSValueRef possibleInstance, JSValueRef* exception) {
157         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
158 }
159
160 JSValueRef JSMessageFolder::getProperty(JSContextRef context,
161                                                                                 JSObjectRef object,
162                                                                                 JSStringRef propertyName,
163                                                                                 JSValueRef* exception)
164 {
165         LogDebug("<<<");        
166
167         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
168         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));      
169         try 
170         {
171                 if (priv == NULL)
172                 {
173                         return JSTizenExceptionFactory::postException(context, exception, 
174                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
175                 }
176                 
177                 IMessageFolderPtr MessageFolder(priv->getObject());
178                 if(JSStringIsEqualToUTF8CString(propertyName, FOLDERID)) 
179                 {
180                         return converter.toJSValueRef(MessageFolder->getId());
181                 }
182                 else if(JSStringIsEqualToUTF8CString(propertyName, PARENTID)) 
183                 {
184                         return converter.toJSValueRef(MessageFolder->getParentId());
185                 }
186                 else if(JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID)) 
187                 {
188                         return converter.toJSValueRef(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                 LogError("Exception: " << ex.GetMessage());
214         }       
215         return JSValueMakeNull(context);
216 }
217
218
219 bool JSMessageFolder::setProperty(JSContextRef context,
220         JSObjectRef object,
221         JSStringRef propertyName,
222         JSValueRef value,
223         JSValueRef* exception)
224 {
225     LogDebug("entered");
226         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
227         
228     Try
229     {
230                 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));      
231         if (!priv) {
232             Throw(WrtDeviceApis::Commons::NullPointerException);
233         }
234                 Api::Messaging::IMessageFolderPtr MessageFolder(priv->getObject());
235
236         if (JSStringIsEqualToUTF8CString(propertyName, NAME)) {
237             std::string name = converter.toString(value);
238             MessageFolder->setName(name);
239             return true;
240         } else if (JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) {
241             bool synchronizable = converter.toBool(value);
242             MessageFolder->setSynchronizable(synchronizable);
243             return true;
244     }
245     }
246     Catch(WrtDeviceApis::Commons::Exception)
247     {
248         LogWarning("trying to set incorrect value");
249            return JSTizenExceptionFactory::postException(context, exception, 
250                          JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
251     }
252
253     return false;
254 }
255
256 bool JSMessageFolder::isObjectOfClass(JSContextRef context, JSValueRef value)
257 {
258         return JSValueIsObjectOfClass(context, value, getClassRef());
259 }
260
261 IMessageFolderPtr JSMessageFolder::getMessageFolder(JSContextRef context, JSValueRef value)
262 {
263         JSObjectRef object = JSValueToObject(context, value, NULL);
264         if (!object) 
265         {
266                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
267         }
268         JSMessageFolderPriv *priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
269         
270         if (!priv) 
271         {
272                 Throw(WrtDeviceApis::Commons::NullPointerException);
273         }
274         return priv->getObject();
275 }
276
277 std::string JSMessageFolder::convertMessageType(int msgType){
278
279         if(msgType == Api::Messaging::SMS){
280                 return JSMessageFolder::TYPE_SMS;               
281         }else if(msgType == Api::Messaging::MMS){
282                 return JSMessageFolder::TYPE_MMS;
283         }else if(msgType == Api::Messaging::EMAIL){
284                 return JSMessageFolder::TYPE_EMAIL;
285         }else{
286                 return NULL;
287         }
288  }
289
290 std::string JSMessageFolder::convertFolderType(int msgType){
291
292         if(msgType == Api::Messaging::INBOX){
293                 return JSMessageFolder::FOLDER_INBOX;           
294         }else if(msgType == Api::Messaging::OUTBOX){
295                 return JSMessageFolder::FOLDER_OUTBOX;
296         }else if(msgType == Api::Messaging::DRAFTBOX){
297                 return JSMessageFolder::FOLDER_DRAFTBOX;
298         }else if(msgType == Api::Messaging::SENTBOX){
299                 return JSMessageFolder::FOLDER_SENTBOX;
300         }else if(msgType == Api::Messaging::USERDEFINED_FOLDER){
301                 return JSMessageFolder::FOLDER_NOTSTANDARD;
302         }else{
303                 return NULL;
304         }
305  }
306
307
308
309 }// WrtPlugins
310
311