Fix system-server for settings
[platform/framework/native/appfw.git] / src / system / FSys_SettingClientEx.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
17 /**
18  * @file                FSys_SettingClientEx.h
19  * @brief               This is the header file for _SettingClientEx class.
20  */
21
22 #ifndef _FSYSTEM_INTERNAL_SETTING_CLIENT_EX_H_
23 #define _FSYSTEM_INTERNAL_SETTING_CLIENT_EX_H_
24
25 #include <FBaseColArrayList.h>
26 #include <FBaseColHashMap.h>
27 #include <FBaseObject.h>
28 #include <FBaseRtEvent.h>
29 #include <FBaseRtIEventArg.h>
30 #include <FBaseRtIEventListener.h>
31 #include <FBaseString.h>
32 #include <FBaseInteger.h>
33 #include <FBaseUuId.h>
34 #include <FSysISettingEventListener.h>
35 #include <FSysISettingInfoSetValueAsyncResultListener.h>
36
37 #include <FIo_IIpcClientEventListener.h>
38 #include <FIo_IpcClient.h>
39
40 namespace Tizen { namespace System
41 {
42
43 class _SettingEventArg : public Tizen::Base::Runtime::IEventArg
44 {
45 public:
46         Tizen::Base::String KeyName;
47 };
48
49 class _SettingEvent : public Tizen::Base::Runtime::Event
50 {
51 public:
52         _SettingEvent()
53         : __count(0)
54         {
55         }
56
57         result AddSettingEventListener(ISettingEventListener& listener)
58         {
59                 result  r = AddListener(listener);
60                 SysTryReturnResult(NID_SYS, __count >= 0, E_SYSTEM, "Listenr count[%d] is error.", __count);
61
62                 if(r == E_SUCCESS)
63                 {
64                         __count++;
65                 }
66                 return r;
67         }
68
69         result RemoveSettingEventListener(ISettingEventListener& listener)
70         {
71                 result  r = RemoveListener(listener);
72                 SysTryReturnResult(NID_SYS, __count > 0, E_SYSTEM, "Listenr count[%d] is error.", __count);
73                 if(r == E_SUCCESS)
74                 {
75                         __count--;
76                 }
77                 return r;
78         }
79
80         int GetCount(void)
81         {
82                 return __count;
83         }
84
85 private:
86         int __count;
87 protected:
88         virtual void FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg)
89         {
90                 ISettingEventListener* pListener = dynamic_cast<ISettingEventListener*> (&listener);
91                 const _SettingEventArg* pArg = dynamic_cast<const _SettingEventArg*>(&arg);
92
93                 if(pListener == null || pArg == null)
94                 {
95                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listner or arguemnt");
96                         return;
97                 }
98
99                 Tizen::Base::String value =pArg->KeyName;
100                 pListener->OnSettingChanged(value);
101         }
102 };
103
104 class _SettingAsyncEventArg : public Tizen::Base::Runtime::IEventArg
105 {
106 public:
107         Tizen::Base::String Key;
108         result errorCode;
109 };
110
111 class _SettingAsyncEvent : public Tizen::Base::Runtime::Event
112 {
113 protected:
114         virtual void FireImpl(Tizen::Base::Runtime::IEventListener& listener, const Tizen::Base::Runtime::IEventArg& arg)
115         {
116                 ISettingInfoSetValueAsyncResultListener* pListener = dynamic_cast<ISettingInfoSetValueAsyncResultListener*> (&listener);
117                 const _SettingAsyncEventArg* pArg = dynamic_cast<const _SettingAsyncEventArg*>(&arg);
118
119                 if(pListener == null || pArg == null)
120                 {
121                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listner or arguemnts [%x/%x].", pListener, pArg);
122                         return;
123                 }
124
125                 result r = pArg->errorCode;
126                 pListener->OnResultReceivedForSetValueAsync(pArg->Key, r);
127                 RemoveListener(listener);
128         }
129 };
130
131 class _SettingClientEx
132         : public Tizen::Base::Object
133         , public Tizen::Io::_IIpcClientEventListener
134 {
135 private:
136         _SettingClientEx(void);
137         virtual ~_SettingClientEx(void);
138
139 public:
140         result GetValue(const Tizen::Base::String& key, bool& value);
141         result GetValueForPrivilegedKey(const Tizen::Base::String& key, bool& value);
142         result GetValue(const Tizen::Base::String& key, double& value);
143         result GetValue(const Tizen::Base::String& key, int& value);
144         result GetValue(const Tizen::Base::String& key, long long& value);
145         result GetValue(const Tizen::Base::String& key, Tizen::Base::String& value);
146         result GetValue(const Tizen::Base::String& key, Tizen::Base::UuId& value);
147
148         bool HasKey(const Tizen::Base::String& key);
149
150         result SetValue(const Tizen::Base::String& key, const bool& value);
151         result SetValueForPrivilegedKey(const Tizen::Base::String& key, bool value);
152         result SetValueAsync(const Tizen::Base::String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener);
153         result SetValueAsyncForPrivilegedKey(const Tizen::Base::String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener);
154         result SetValue(const Tizen::Base::String& key, const int& value);
155         result SetValue(const Tizen::Base::String& key, const Tizen::Base::String& value);
156         result SetValueForPrivilegedKey(const Tizen::Base::String& key, Tizen::Base::String value);
157
158         result AddSettingEventListenerForInternal(ISettingEventListener& listener);
159         result RemoveSettingEventListenerForInternal(ISettingEventListener& listener);
160         result AddSettingEventListener(ISettingEventListener& listener);
161         result RemoveSettingEventListener(ISettingEventListener& listener);
162
163         result SetSettingEventListener(ISettingEventListener* pListener);
164
165         result ResetToFactoryDefault(void);
166
167         void OnIpcResponseReceived(Tizen::Io::_IpcClient& client, const IPC::Message& message);
168
169         static _SettingClientEx* GetInstance(void);
170
171 private:
172         void OnDataReceived(const Tizen::Base::Collection::ArrayList& data);
173         result ConvertCode(Tizen::Base::String code);
174
175         Tizen::Base::String ConvertKeyOspToTizen(Tizen::Base::String ospKey);
176         result ConvertKeyTizenToOsp(Tizen::Base::String tizenKey, Tizen::Base::String& ospKey);
177
178         result GetOnService(Tizen::Base::String commandId, Tizen::Base::String key, Tizen::Base::String& response);
179         result SetOnService(Tizen::Base::String commandId, Tizen::Base::String key, Tizen::Base::String value);
180         result SetVolume(const Tizen::Base::String& soundCategory, int level);
181
182         result SubscribeEvent(void);
183
184         static void InitSettingClient(void);
185 private:
186         Tizen::Base::Collection::HashMap        __asyncEventList;
187         Tizen::Io::_IpcClient                   __ipcClient;
188
189         bool                                    __subscribed;
190         bool                                    __subscribedForInternal;
191         ISettingEventListener*                  __pListener;
192
193         _SettingEvent*                          __pSettingEvent;
194         _SettingEvent*                          __pSettingEventForInternal;
195         static _SettingClientEx*                        __pSettingClient;
196 };
197
198 } } // Tizen::System
199
200 #endif //_FSYSTEM_INTERNAL_SETTING_CLIENT_EX_H_