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 <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"
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
56 JSBookmarkFolder::hasInstance,
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 LogDebug("JSBookmarkFolder::initialize()\n");
86 void JSBookmarkFolder::finalize(JSObjectRef object)
88 LogDebug("JSBookmarkFolder::finalize()\n");
89 BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
91 JSObjectSetPrivate(object, NULL);
96 bool JSBookmarkFolder::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
97 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
100 JSObjectRef JSBookmarkFolder::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception){
103 ArgumentValidator validator(ctx, argumentCount, arguments);
105 std::string title = validator.toString(0);
106 return createJSObject(ctx, title);
107 } catch(const BasePlatformException& err){
108 JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, err);
114 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title) {
115 return createJSObject(context, title, UNDEFINED_ID, UNDEFINED_ID);
118 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, const std::string &title, const int id, const int parentId) {
121 throw InvalidValuesException("Title is empty");
122 BookmarkData *priv = new BookmarkData(BOOKMARKFOLDER_TYPE, title, "", id, parentId);
123 JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
128 JSObjectRef JSBookmarkFolder::createJSObject(JSContextRef context, void *priv) {
129 JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
133 JSValueRef JSBookmarkFolder::getParent(JSContextRef context, JSObjectRef object) {
135 BookmarkData * priv = static_cast<BookmarkData *>(JSObjectGetPrivate(object));
138 throw TypeMismatchException("Private data is null");
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);
146 if (priv->m_parentId == BookmarkManager::getInstance()->getRootFolderId())
147 return JSValueMakeNull(context);
149 return createJSObject(context, (parentSearchData->m_parent)->m_title, (parentSearchData->m_parent)->m_id, (parentSearchData->m_parent)->m_parentId);
151 priv->m_parentId = UNDEFINED_ID;
153 return JSValueMakeUndefined(context);
156 JSValueRef JSBookmarkFolder::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
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));
167 throw TypeMismatchException("Private data is null");
168 return convert.toJSValueRef(priv->m_title);
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");
175 return JSValueMakeUndefined(context);