wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Systeminfo / JSLocaleInfo.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 <memory>
19 #include "JSLocaleInfo.h"
20 #include <Logger.h>
21
22 namespace DeviceAPI {
23 namespace Systeminfo {
24 using namespace WrtDeviceApis::CommonsJavaScript;
25 using namespace WrtDeviceApis::Commons;
26
27 namespace {
28 const char* LOCALE_LANGUAGE_PROPERTY = "language";
29 const char* LOCALE_COUNTRY_PROPERTY = "country";
30 }
31
32 JSClassRef JSLocaleInfo::m_classRef = NULL;
33
34 JSClassDefinition JSLocaleInfo::m_classInfo = {
35     0,
36     kJSClassAttributeNone,
37     "localeinfo",
38     0,
39     m_properties,
40     NULL,
41     Initialize,
42     Finalize,
43     hasProperty,
44     getProperty,
45     NULL,
46     NULL,
47     NULL,
48     NULL,
49     NULL,
50     NULL,
51     NULL
52 };
53
54 JSStaticValue JSLocaleInfo::m_properties[] = {
55     { LOCALE_LANGUAGE_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
56     { LOCALE_COUNTRY_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
57     { 0, 0, 0, 0 }
58 };
59
60 const JSClassRef JSLocaleInfo::getClassRef()
61 {
62     if (!m_classRef) {
63         m_classRef = JSClassCreate(&m_classInfo);
64     }
65     return m_classRef;
66 }
67
68 const JSClassDefinition* JSLocaleInfo::getClassInfo()
69 {
70     return &m_classInfo;
71 }
72
73 void JSLocaleInfo::Initialize(JSContextRef context, JSObjectRef object)
74 {
75 }
76
77 void JSLocaleInfo::Finalize(JSObjectRef object)
78 {
79     LoggerD("Entered");
80     JSLocalePriv* priv = static_cast<JSLocalePriv*>(JSObjectGetPrivate(object));
81     JSObjectSetPrivate(object, NULL);
82     LoggerD("Deleting localeInfo object");
83     delete priv;
84 }
85
86 bool JSLocaleInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
87 {
88     return JSUtils::hasProperty(m_properties, propertyName);
89 }
90
91 JSObjectRef JSLocaleInfo::createJSObject(JSContextRef context, const LocalePropertiesPtr localeInfo)
92 {
93     JSLocalePriv *priv = new JSLocalePriv(context, localeInfo);
94     return JSObjectMake(context, getClassRef(), priv);
95 }
96
97 JSValueRef JSLocaleInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
98 {
99     LoggerD("Enter");
100     JSLocalePriv *priv = static_cast<JSLocalePriv*>(JSObjectGetPrivate(object));
101     if (NULL == priv) {
102         LoggerE("Private object not set.");
103         return JSValueMakeUndefined(context);
104     }
105
106     Try
107     {
108         LocalePropertiesPtr localeInfo = priv->getObject();
109         Converter convert(context);
110
111         if (JSStringIsEqualToUTF8CString(propertyName, LOCALE_LANGUAGE_PROPERTY)) {
112             return convert.toJSValueRef(localeInfo->language);
113         } else if (JSStringIsEqualToUTF8CString(propertyName, LOCALE_COUNTRY_PROPERTY)) {
114             return convert.toJSValueRef(localeInfo->country);
115         }
116     }
117     Catch(Exception)
118     {
119         LoggerE("Exception: " << _rethrown_exception.GetMessage());
120     }
121     return JSValueMakeUndefined(context);
122 }
123 }
124 }