88a2f5383d199997d0235eb3e61c18f0a2a6fa52
[framework/web/wrt-plugins-tizen.git] / src / Bookmark / JSBookmarkFolder.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 #include <JSTizenException.h>
19 #include <JSTizenExceptionFactory.h>
20 #include <ArgumentValidator.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <PlatformException.h>
23 #include <JSWebAPIException.h>
24 #include "JSBookmarkFolder.h"
25 #include "BookmarkManager.h"
26 #include "BookmarkData.h"
27 #include <Logger.h>
28
29 using namespace DeviceAPI::Common;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31
32 #define TIZEN_BOOKMARKFOLDER_PARENT "parent"
33 #define TIZEN_BOOKMARKFOLDER_TITLE "title"
34
35 namespace DeviceAPI {
36 namespace Bookmark {
37
38 JSClassRef JSBookmarkFolder::m_jsClassRef = NULL;
39
40 JSClassDefinition JSBookmarkFolder::m_jsClassInfo = {
41     0, // current (and only) version is 0
42     kJSClassAttributeNone, //attributes
43     "BookmarkFolder", //class name
44     NULL, // parent class
45     JSBookmarkFolder::m_property, //static values
46     NULL, // static functions
47     JSBookmarkFolder::initialize, // initialize
48     JSBookmarkFolder::finalize, //finalize
49     NULL, //hasProperty
50     NULL, //getProperty
51     NULL, //setProperty
52     NULL, //deleteProperty
53     NULL, //getPropertyNames
54     NULL, // callAsFunction
55     NULL, // constructor
56     NULL,
57     NULL // convertToType
58 };
59
60
61 JSStaticValue JSBookmarkFolder::m_property[] =
62 {
63         {TIZEN_BOOKMARKFOLDER_PARENT,  getProperty, NULL, kJSPropertyAttributeReadOnly},
64         {TIZEN_BOOKMARKFOLDER_TITLE,  getProperty, NULL, kJSPropertyAttributeReadOnly},
65         { 0, 0, 0, 0 }
66 };
67
68 const JSClassRef JSBookmarkFolder::getClassRef()
69 {
70     if (!m_jsClassRef) {
71         m_jsClassRef = JSClassCreate(&m_jsClassInfo);
72     }
73     return m_jsClassRef;
74 }
75
76 const JSClassDefinition* JSBookmarkFolder::getClassInfo()
77 {
78     return &m_jsClassInfo;
79 }
80
81 void JSBookmarkFolder::initialize(JSContextRef ctx, JSObjectRef object)
82 {
83     LoggerD("JSBookmarkFolder::initialize()\n");
84 }
85
86 void JSBookmarkFolder::finalize(JSObjectRef object)
87 {
88     LoggerD("JSBookmarkFolder::finalize()\n");
89     BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
90     if( priv ){
91         JSObjectSetPrivate(object, NULL);
92         delete priv;
93     }
94 }
95
96 JSObjectRef JSBookmarkFolder::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
97     LoggerD("Enter");
98         try{
99                 ArgumentValidator validator(ctx, argumentCount, arguments);
100
101                 std::string title = validator.toString(0);
102             JSObjectRef bookmarkFolder = createJSObject(ctx, title);
103                 if (bookmarkFolder) {
104                         JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
105                     JSObjectSetProperty(ctx, bookmarkFolder, ctorName, constructor,
106                         kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
107                 JSStringRelease(ctorName);
108                         return bookmarkFolder;
109                 } else
110                         throw TypeMismatchException("Can't create BookmarkFolder");
111     } catch(const BasePlatformException& err){
112         JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, err);
113         *exception = error;
114         return error;
115     }
116 }
117
118 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title) {
119         return createJSObject(context, title, UNDEFINED_ID, UNDEFINED_ID);
120 }
121
122 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title, const int id, const int parentId) {
123         LoggerD("Enter");
124         if (title == "")
125                 throw InvalidValuesException("Title is empty");
126         BookmarkData *priv = new BookmarkData(BOOKMARKFOLDER_TYPE, title, "", id, parentId);
127         JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
128     return obj;
129 }
130
131 #if 0
132 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, void *priv) {
133         JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
134     return obj;
135 }
136 #endif
137 JSValueRef JSBookmarkFolder::getParent(JSContextRef context, JSObjectRef object) {
138         LoggerD("Enter");;
139         BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
140
141         if (!priv)
142                 throw TypeMismatchException("Private data is null");
143
144         LoggerD("bookmark id:" << priv->m_id);
145         LoggerD("bookmark parent id:" << priv->m_parentId);
146         if (priv->m_id != UNDEFINED_ID) {
147                 BookmarkParentSearchDataPtr parentSearchData(new BookmarkParentSearchData(priv->m_id, priv->m_parentId));
148                 bool isAlive = BookmarkManager::getInstance()->findParent(parentSearchData);
149                 if (isAlive) {
150                         if (priv->m_parentId == BookmarkManager::getInstance()->getRootFolderId())
151                                 return JSValueMakeNull(context);
152                         else
153                                 return createJSObject(context, (parentSearchData->m_parent)->m_title, (parentSearchData->m_parent)->m_id, (parentSearchData->m_parent)->m_parentId);
154                 }
155                 priv->m_parentId = UNDEFINED_ID;
156         }
157         return JSValueMakeUndefined(context);
158 }
159
160 JSValueRef JSBookmarkFolder::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
161 {
162         LoggerD("Enter");
163
164         try     {
165                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_BOOKMARKFOLDER_PARENT)) {
166                         return getParent(context, object);
167                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_BOOKMARKFOLDER_TITLE)) {
168                         Converter convert(context);
169                         BookmarkData *priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
170                         if (!priv)
171                                 throw TypeMismatchException("Private data is null");
172                         return convert.toJSValueRef(priv->m_title);
173                 }
174     }catch(const BasePlatformException& err){
175         return JSWebAPIException::throwException(context, exception, err);
176     }catch(const WrtDeviceApis::Commons::ConversionException& err){
177         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "converting fail");    
178     }
179         return JSValueMakeUndefined(context);
180 }
181
182 } // Bookmark
183 } // TizenApis
184