Beta merge 2
[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
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
26 #include <API/Messaging/IMessageFolder.h>
27 #include <API/Messaging/IMessagingTypes.h>
28
29
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/SecurityExceptions.h>
33
34 #include "MessagingErrorMsg.h"
35 #include "JSMessageFolder.h"
36
37 using namespace std;
38 using namespace DPL;
39 using namespace TizenApis::Api::Messaging;
40 using namespace TizenApis::Commons;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43
44 namespace TizenApis {
45 namespace Tizen1_0 {
46
47 JSClassDefinition JSMessageFolder::m_classInfo =
48 {
49         0,
50         kJSClassAttributeNone,
51         "MessageFolder",
52         NULL,
53         m_properties,
54         NULL,
55         initialize,
56         finalize,
57         NULL, //hasProperty,
58         NULL, //getProperty,
59         NULL, //setProperty,
60         NULL, //deleteProperty,
61         NULL, //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 JSStaticValue JSMessageFolder::m_properties[] = {
90         {"id", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
91         {"parentId", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },        
92         {"serviceId", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },       
93         {"contentType", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
94         {"name", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
95         {"path", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
96         {"type", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
97         {"synchronizable", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
98         {0, 0, 0, 0}
99 };
100
101 const JSClassRef JSMessageFolder::getClassRef() {
102         if (!m_jsClassRef) {
103                 m_jsClassRef = JSClassCreate(&m_classInfo);
104         }
105         return m_jsClassRef;
106 }
107
108 const JSClassDefinition* JSMessageFolder::getClassInfo() {
109         return &m_classInfo;
110 }
111
112 JSClassRef JSMessageFolder::m_jsClassRef = JSClassCreate(JSMessageFolder::getClassInfo());
113
114 void JSMessageFolder::initialize(JSContextRef context, JSObjectRef object) 
115 {
116         LogDebug("JSMessageFolder::initialize ");
117         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
118
119         if (priv == NULL)
120         {
121
122         }
123         else
124         {
125                 LogDebug("JSMessageFolder::already exist ");            
126         }
127 }
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         LogDebug("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         LogDebug("<<<");        
161
162         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
163         JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));      
164         try 
165         {
166                 if (priv == NULL)
167                 {
168                         return JSTizenExceptionFactory::postException(context, exception, 
169                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
170                 }
171                 
172                 IMessageFolderPtr MessageFolder(priv->getObject());
173                 if(JSStringIsEqualToUTF8CString(propertyName, FOLDERID)) 
174                 {
175                         return converter.toJSValueRef(MessageFolder->getId());
176                 }
177                 else if(JSStringIsEqualToUTF8CString(propertyName, PARENTID)) 
178                 {
179                         return converter.toJSValueRef(MessageFolder->getParentId());
180                 }
181                 else if(JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID)) 
182                 {
183                         return converter.toJSValueRef(MessageFolder->getAccountId());                   
184                 }
185                 else if(JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE)) 
186                 {
187                         return converter.toJSValueRef(convertMessageType(MessageFolder->getContentType()));     
188                 }
189                 else if(JSStringIsEqualToUTF8CString(propertyName, NAME)) 
190                 {
191                         return converter.toJSValueRef(MessageFolder->getName());        
192                 }
193                 else if(JSStringIsEqualToUTF8CString(propertyName, PATH)) 
194                 {
195                         return converter.toJSValueRef(MessageFolder->getPath());        
196                 }
197                 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE)) 
198                 {
199                         return converter.toJSValueRef(convertFolderType(MessageFolder->getType()));     
200                 }       
201                 else if(JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) 
202                 {
203                         return converter.toJSValueRef(MessageFolder->getSynchronizable());                              
204                 }
205
206         }
207         catch (const WrtDeviceApis::Commons::Exception& ex) {
208                 LogError("Exception: " << ex.GetMessage());
209         }       
210         return JSValueMakeNull(context);
211 }
212
213
214 bool JSMessageFolder::setProperty(JSContextRef context,
215         JSObjectRef object,
216         JSStringRef propertyName,
217         JSValueRef value,
218         JSValueRef* exception)
219 {
220     LogDebug("entered");
221         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
222         
223     Try
224     {
225                 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));      
226         if (!priv) {
227             Throw(WrtDeviceApis::Commons::NullPointerException);
228         }
229                 Api::Messaging::IMessageFolderPtr MessageFolder(priv->getObject());
230
231         if (JSStringIsEqualToUTF8CString(propertyName, NAME)) {
232             std::string name = converter.toString(value);
233             MessageFolder->setName(name);
234             return true;
235         } else if (JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) {
236             bool synchronizable = converter.toBool(value);
237             MessageFolder->setSynchronizable(synchronizable);
238             return true;
239     }
240     }
241     Catch(WrtDeviceApis::Commons::Exception)
242     {
243         LogWarning("trying to set incorrect value");
244            return JSTizenExceptionFactory::postException(context, exception, 
245                          JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
246     }
247
248     return false;
249 }
250
251 bool JSMessageFolder::isObjectOfClass(JSContextRef context, JSValueRef value)
252 {
253         return JSValueIsObjectOfClass(context, value, getClassRef());
254 }
255
256 IMessageFolderPtr JSMessageFolder::getMessageFolder(JSContextRef context, JSValueRef value)
257 {
258         JSObjectRef object = JSValueToObject(context, value, NULL);
259         if (!object) 
260         {
261                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
262         }
263         JSMessageFolderPriv *priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
264         
265         if (!priv) 
266         {
267                 Throw(WrtDeviceApis::Commons::NullPointerException);
268         }
269         return priv->getObject();
270 }
271
272 std::string JSMessageFolder::convertMessageType(int msgType){
273
274         if(msgType == Api::Messaging::SMS){
275                 return JSMessageFolder::TYPE_SMS;               
276         }else if(msgType == Api::Messaging::MMS){
277                 return JSMessageFolder::TYPE_MMS;
278         }else if(msgType == Api::Messaging::EMAIL){
279                 return JSMessageFolder::TYPE_EMAIL;
280         }else{
281                 return NULL;
282         }
283  }
284
285 std::string JSMessageFolder::convertFolderType(int msgType){
286
287         if(msgType == Api::Messaging::INBOX){
288                 return JSMessageFolder::FOLDER_INBOX;           
289         }else if(msgType == Api::Messaging::OUTBOX){
290                 return JSMessageFolder::FOLDER_OUTBOX;
291         }else if(msgType == Api::Messaging::DRAFTBOX){
292                 return JSMessageFolder::FOLDER_DRAFTBOX;
293         }else if(msgType == Api::Messaging::SENTBOX){
294                 return JSMessageFolder::FOLDER_SENTBOX;
295         }else if(msgType == Api::Messaging::USERDEFINED_FOLDER){
296                 return JSMessageFolder::FOLDER_NOTSTANDARD;
297         }else{
298                 return NULL;
299         }
300  }
301
302
303
304 }// WrtPlugins
305
306