2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <Commons/Exception.h>
20 namespace WrtDeviceApis {
22 Key::Key(const char* name) :
36 bool Key::attachCallback(KeyChangeCallback callback,
39 assert(!m_name.empty() && "Key name cannot be empty.");
44 DPL::Mutex::ScopedLock lock(&m_mtx);
45 bool firstTime = (m_callback == NULL);
46 m_callback = callback;
47 m_callbackData = data;
50 if (vconf_notify_key_changed(m_name.c_str(), Key::callback,
53 m_callbackData = NULL;
61 void Key::detachCallback()
63 DPL::Mutex::ScopedLock lock(&m_mtx);
65 assert(!m_name.empty() && "Key name cannot be empty.");
66 vconf_ignore_key_changed(m_name.c_str(), Key::callback);
68 m_callbackData = NULL;
72 void Key::callback(keynode_t* node,
75 Key* this_ = static_cast<Key*>(data);
77 DPL::Mutex::ScopedLock lock(&this_->m_mtx);
78 if (this_->m_callback) {
79 NodePtr ptr(new Node(node));
80 (*this_->m_callback)(ptr.Get(), this_->m_callbackData);
85 bool Key::getBool() const
87 assert(!m_name.empty() && "Key name cannot be empty.");
89 if (vconf_get_bool(m_name.c_str(), &result) != 0) {
90 Throw(Commons::ConversionException);
95 void Key::setValue(bool value)
97 assert(!m_name.empty() && "Key name cannot be empty.");
98 if (vconf_set_bool(m_name.c_str(), value) != 0) {
99 Throw(Commons::PlatformException);
103 int Key::getInt() const
105 assert(!m_name.empty() && "Key name cannot be empty.");
107 if (vconf_get_int(m_name.c_str(), &result) != 0) {
108 Throw(Commons::ConversionException);
113 void Key::setValue(int value)
115 assert(!m_name.empty() && "Key name cannot be empty.");
116 if (vconf_set_int(m_name.c_str(), value) != 0) {
117 Throw(Commons::PlatformException);
121 double Key::getDouble() const
123 assert(!m_name.empty() && "Key name cannot be empty.");
125 if (vconf_get_dbl(m_name.c_str(), &result) != 0) {
126 Throw(Commons::ConversionException);
131 void Key::setValue(double value)
133 assert(!m_name.empty() && "Key name cannot be empty.");
134 if (vconf_set_dbl(m_name.c_str(), value) != 0) {
135 Throw(Commons::PlatformException);
139 std::string Key::getString() const
141 assert(!m_name.empty() && "Key name cannot be empty.");
142 char* result = vconf_get_str(m_name.c_str());
143 if (result == NULL) {
144 Throw(Commons::ConversionException);
149 void Key::setValue(const std::string& value)
151 assert(!m_name.empty() && "Key name cannot be empty.");
152 setValue(value.c_str());
155 void Key::setValue(const char* value)
157 assert(!m_name.empty() && "Key name cannot be empty.");
158 if (vconf_set_str(m_name.c_str(), value) != 0) {
159 Throw(Commons::PlatformException);
163 void Key::reset(const char* name)
166 m_name = (name == NULL ? std::string() : name);