4ed6095a2ff4e43dd97cf6e5b185ba3086ef5817
[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 <dpl/log/log.h>
19 #include <JSTizenException.h>
20 #include <JSTizenExceptionFactory.h>
21 #include <ArgumentValidator.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <PlatformException.h>
24 #include <JSWebAPIException.h>
25 #include "JSBookmarkFolder.h"
26 #include "BookmarkManager.h"
27 #include "BookmarkData.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     JSBookmarkFolder::hasInstance,
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     LogDebug("JSBookmarkFolder::initialize()\n");
84 }
85
86 void JSBookmarkFolder::finalize(JSObjectRef object)
87 {
88     LogDebug("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 bool JSBookmarkFolder::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
97     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
98 }
99
100 JSObjectRef JSBookmarkFolder::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
101     LogDebug("Enter");
102         try{
103                 ArgumentValidator validator(ctx, argumentCount, arguments);
104
105                 std::string title = validator.toString(0);
106             return createJSObject(ctx, title);
107     } catch(const BasePlatformException& err){
108         JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, err);
109         *exception = error;
110         return error;
111     }
112 }
113
114 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title) {
115         return createJSObject(context, title, UNDEFINED_ID, UNDEFINED_ID);
116 }
117
118 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title, const int id, const int parentId) {
119         LogDebug("Enter");
120         if (title == "")
121                 throw InvalidValuesException("Title is empty");
122         BookmarkData *priv = new BookmarkData(BOOKMARKFOLDER_TYPE, title, "", id, parentId);
123         JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
124     return obj;
125 }
126
127 #if 0
128 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, void *priv) {
129         JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
130     return obj;
131 }
132 #endif
133 JSValueRef JSBookmarkFolder::getParent(JSContextRef context, JSObjectRef object) {
134         LogDebug("Enter");;
135         BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
136
137         if (!priv)
138                 throw TypeMismatchException("Private data is null");
139
140         LogDebug("bookmark id:" << priv->m_id);
141         LogDebug("bookmark parent id:" << priv->m_parentId);
142         if (priv->m_id != UNDEFINED_ID) {
143                 BookmarkParentSearchDataPtr parentSearchData(new BookmarkParentSearchData(priv->m_id, priv->m_parentId));
144                 bool isAlive = BookmarkManager::getInstance()->findParent(parentSearchData);
145                 if (isAlive) {
146                         if (priv->m_parentId == BookmarkManager::getInstance()->getRootFolderId())
147                                 return JSValueMakeNull(context);
148                         else
149                                 return createJSObject(context, (parentSearchData->m_parent)->m_title, (parentSearchData->m_parent)->m_id, (parentSearchData->m_parent)->m_parentId);
150                 }
151                 priv->m_parentId = UNDEFINED_ID;
152         }
153         return JSValueMakeUndefined(context);
154 }
155
156 JSValueRef JSBookmarkFolder::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
157 {
158         LogDebug("Enter");
159
160         try     {
161                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_BOOKMARKFOLDER_PARENT)) {
162                         return getParent(context, object);
163                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_BOOKMARKFOLDER_TITLE)) {
164                         Converter convert(context);
165                         BookmarkData *priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
166                         if (!priv)
167                                 throw TypeMismatchException("Private data is null");
168                         return convert.toJSValueRef(priv->m_title);
169                 }
170     }catch(const BasePlatformException& err){
171         return JSWebAPIException::throwException(context, exception, err);
172     }catch(const WrtDeviceApis::Commons::ConversionException& err){
173         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "converting fail");    
174     }
175         return JSValueMakeUndefined(context);
176 }
177
178 } // Bookmark
179 } // TizenApis
180