afc3b1e96b7fa8b6013f98f9ad80c5aa8cecfdc9
[framework/web/wrt-plugins-common.git] / src / modules / tizen / VConf / Key.h
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 #ifndef WRTDEVICEAPIS_VCONFKEY_H_
17 #define WRTDEVICEAPIS_VCONFKEY_H_
18
19 #include <string>
20 #include <vconf.h>
21 #include <dpl/noncopyable.h>
22 #include <dpl/mutex.h>
23 #include "Node.h"
24
25 namespace WrtDeviceApis {
26 namespace VConf {
27 typedef void (*KeyChangeCallback)(const Node*, void*);
28
29 class Key : private DPL::Noncopyable
30 {
31   public:
32     /**
33      * Creates vconf key wrapper.
34      * @param name Key name or NULL (default).
35      */
36     explicit Key(const char* name = NULL);
37
38     ~Key();
39
40     /**
41      * Gets boolean value.
42      * @return Value of the key.
43      * @throw ConversionException If unable to get value.
44      */
45     bool getBool() const;
46
47     /**
48      * Sets boolean value for this key.
49      * @param value Value to set.
50      * @throw PlatformException If unable to perform the request.
51      */
52     void setValue(bool value);
53
54     /**
55      * Gets integer value.
56      * @return Value of the key.
57      * @throw ConversionException If unable to get value.
58      */
59     int getInt() const;
60
61     /**
62      * Sets integer value for this key.
63      * @param value Value to set.
64      * @throw PlatformException If unable to perform the request.
65      */
66     void setValue(int value);
67
68     /**
69      * Gets double value.
70      * @return Value of the key.
71      * @throw ConversionException If unable to get value.
72      */
73     double getDouble() const;
74
75     /**
76      * Sets double value for this key.
77      * @param value Value to set.
78      * @throw PlatformException If unable to perform the request.
79      */
80     void setValue(double value);
81
82     /**
83      * Gets string value.
84      * @return Value of the key.
85      * @throw ConversionException If unable to get value.
86      */
87     std::string getString() const;
88
89     /**
90      * Sets string value for this key.
91      * @param value Value to set.
92      * @throw PlatformException If unable to perform the request.
93      */
94     void setValue(const std::string& value);
95
96     /**
97      * Sets c-string value for this key.
98      * @param value Value to set.
99      * @throw PlatformException If unable to perform the request.
100      */
101     void setValue(const char* value);
102
103     /**
104      * Attaches on key value change callback.
105      * @param callback Callback function.
106      * @param data User data.
107      * @return True if callback has been successfully attached, false otherwsie.
108      * @remarks Does NOT take ownership over user data.
109      * @remarks Creating two independent keys then attaching the same callback to
110      *          each and detaching it from one of them causes disconnection of
111      *          this callback from vconf engine.
112      */
113     bool attachCallback(KeyChangeCallback callback,
114             void* data);
115
116     /**
117      * Detaches on key value change callback.
118      */
119     void detachCallback();
120
121     /**
122      * Resets this object to wrap other key around.
123      * @param name Key name or NULL (default).
124      */
125     void reset(const char* name = NULL);
126
127   private:
128     /**
129      * Callback set to platform.
130      * @param node Key.
131      * @param data User data.
132      */
133     static void callback(keynode_t* node,
134             void* data);
135
136   private:
137     std::string m_name; ///< Key name.
138     KeyChangeCallback m_callback; ///< Callback.
139     void* m_callbackData; ///< Callback user data.
140     DPL::Mutex m_mtx; ///< Synchronize access to callback.
141 };
142 } // VConf
143 } // WrtDeviceApis
144 #endif /* WRTDEVICEAPIS_VCONFKEY_H_ */