2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 using namespace DeviceAPI::Common;
30 using namespace WrtDeviceApis::CommonsJavaScript;
32 #define TIZEN_BOOKMARKFOLDER_PARENT "parent"
33 #define TIZEN_BOOKMARKFOLDER_TITLE "title"
38 JSClassRef JSBookmarkFolder::m_jsClassRef = NULL;
40 JSClassDefinition JSBookmarkFolder::m_jsClassInfo = {
41 0, // current (and only) version is 0
42 kJSClassAttributeNone, //attributes
43 "BookmarkFolder", //class name
45 JSBookmarkFolder::m_property, //static values
46 NULL, // static functions
47 JSBookmarkFolder::initialize, // initialize
48 JSBookmarkFolder::finalize, //finalize
52 NULL, //deleteProperty
53 NULL, //getPropertyNames
54 NULL, // callAsFunction
61 JSStaticValue JSBookmarkFolder::m_property[] =
63 {TIZEN_BOOKMARKFOLDER_PARENT, getProperty, NULL, kJSPropertyAttributeReadOnly},
64 {TIZEN_BOOKMARKFOLDER_TITLE, getProperty, NULL, kJSPropertyAttributeReadOnly},
68 const JSClassRef JSBookmarkFolder::getClassRef()
71 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
76 const JSClassDefinition* JSBookmarkFolder::getClassInfo()
78 return &m_jsClassInfo;
81 void JSBookmarkFolder::initialize(JSContextRef ctx, JSObjectRef object)
83 LoggerD("JSBookmarkFolder::initialize()\n");
86 void JSBookmarkFolder::finalize(JSObjectRef object)
88 LoggerD("JSBookmarkFolder::finalize()\n");
89 BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
91 JSObjectSetPrivate(object, NULL);
96 JSObjectRef JSBookmarkFolder::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
99 ArgumentValidator validator(ctx, argumentCount, arguments);
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;
110 throw TypeMismatchException("Can't create BookmarkFolder");
111 } catch(const BasePlatformException& err){
112 JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, err);
118 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title) {
119 return createJSObject(context, title, UNDEFINED_ID, UNDEFINED_ID);
122 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title, const int id, const int parentId) {
125 throw InvalidValuesException("Title is empty");
126 BookmarkData *priv = new BookmarkData(BOOKMARKFOLDER_TYPE, title, "", id, parentId);
127 JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
132 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, void *priv) {
133 JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
137 JSValueRef JSBookmarkFolder::getParent(JSContextRef context, JSObjectRef object) {
139 BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
142 throw TypeMismatchException("Private data is null");
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);
150 if (priv->m_parentId == BookmarkManager::getInstance()->getRootFolderId())
151 return JSValueMakeNull(context);
153 return createJSObject(context, (parentSearchData->m_parent)->m_title, (parentSearchData->m_parent)->m_id, (parentSearchData->m_parent)->m_parentId);
155 priv->m_parentId = UNDEFINED_ID;
157 return JSValueMakeUndefined(context);
160 JSValueRef JSBookmarkFolder::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
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));
171 throw TypeMismatchException("Private data is null");
172 return convert.toJSValueRef(priv->m_title);
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");
179 return JSValueMakeUndefined(context);