5077e80d95742ab31b429c3826880db405dd3e21
[framework/web/wrt-plugins-common.git] / src / modules / tizen / VConf / Key.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "Key.h"
17 #include <cassert>
18 #include <Commons/Exception.h>
19
20 namespace WrtDeviceApis {
21 namespace VConf {
22 Key::Key(const char* name) :
23     m_callback(NULL),
24     m_callbackData(NULL)
25 {
26     if (name != NULL) {
27         m_name = name;
28     }
29 }
30
31 Key::~Key()
32 {
33     detachCallback();
34 }
35
36 bool Key::attachCallback(KeyChangeCallback callback,
37         void* data)
38 {
39     assert(!m_name.empty() && "Key name cannot be empty.");
40     if (!callback) {
41         return false;
42     }
43
44     DPL::Mutex::ScopedLock lock(&m_mtx);
45     bool firstTime = (m_callback == NULL);
46     m_callback = callback;
47     m_callbackData = data;
48
49     if (firstTime) {
50         if (vconf_notify_key_changed(m_name.c_str(), Key::callback,
51                                      this) != 0) {
52             m_callback = NULL;
53             m_callbackData = NULL;
54             return false;
55         }
56     }
57
58     return true;
59 }
60
61 void Key::detachCallback()
62 {
63     DPL::Mutex::ScopedLock lock(&m_mtx);
64     if (m_callback) {
65         assert(!m_name.empty() && "Key name cannot be empty.");
66         vconf_ignore_key_changed(m_name.c_str(), Key::callback);
67         m_callback = NULL;
68         m_callbackData = NULL;
69     }
70 }
71
72 void Key::callback(keynode_t* node,
73         void* data)
74 {
75     Key* this_ = static_cast<Key*>(data);
76     if (this_) {
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);
81         }
82     }
83 }
84
85 bool Key::getBool() const
86 {
87     assert(!m_name.empty() && "Key name cannot be empty.");
88     int result = 0;
89     if (vconf_get_bool(m_name.c_str(), &result) != 0) {
90         Throw(Commons::ConversionException);
91     }
92     return (result != 0);
93 }
94
95 void Key::setValue(bool value)
96 {
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);
100     }
101 }
102
103 int Key::getInt() const
104 {
105     assert(!m_name.empty() && "Key name cannot be empty.");
106     int result = 0;
107     if (vconf_get_int(m_name.c_str(), &result) != 0) {
108         Throw(Commons::ConversionException);
109     }
110     return result;
111 }
112
113 void Key::setValue(int value)
114 {
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);
118     }
119 }
120
121 double Key::getDouble() const
122 {
123     assert(!m_name.empty() && "Key name cannot be empty.");
124     double result = 0;
125     if (vconf_get_dbl(m_name.c_str(), &result) != 0) {
126         Throw(Commons::ConversionException);
127     }
128     return result;
129 }
130
131 void Key::setValue(double value)
132 {
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);
136     }
137 }
138
139 std::string Key::getString() const
140 {
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);
145     }
146     return result;
147 }
148
149 void Key::setValue(const std::string& value)
150 {
151     assert(!m_name.empty() && "Key name cannot be empty.");
152     setValue(value.c_str());
153 }
154
155 void Key::setValue(const char* value)
156 {
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);
160     }
161 }
162
163 void Key::reset(const char* name)
164 {
165     detachCallback();
166     m_name = (name == NULL ? std::string() : name);
167 }
168 } // VConf
169 } // WrtDeviceApis