Fix SettingClient to get instance.
[platform/framework/native/appfw.git] / src / app / FApp_AppMessageImpl.cpp
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         FApp_AppMessageImpl.cpp
19  * @brief       This is the implementation for the _AppMessageImpl.cpp class.
20  */
21
22 #include <typeinfo>
23 #include <unique_ptr.h>
24
25 #include <appsvc/appsvc.h>
26
27 #include <FBaseSysLog.h>
28 #include <FBaseString.h>
29 #include <FBaseColIList.h>
30 #include <FBaseColIMap.h>
31 #include <FBaseColIEnumerator.h>
32 #include <FBaseColArrayList.h>
33
34 #include <FBase_StringConverter.h>
35
36 #include "FApp_AppMessageImpl.h"
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40
41 namespace Tizen { namespace App
42 {
43
44 _AppMessageImpl::_AppMessageImpl(void)
45 : __pBundle(bundle_create())
46 {
47         SysAssert(__pBundle != NULL);
48 }
49
50 _AppMessageImpl::_AppMessageImpl(const _AppMessageImpl&rhs)
51 : __pBundle(bundle_dup(rhs.__pBundle))
52 {
53         SysAssert(__pBundle != NULL);
54 }
55
56 _AppMessageImpl::~_AppMessageImpl(void)
57 {
58         SysAssert(__pBundle != NULL);
59         bundle_free(__pBundle);
60 }
61
62 _AppMessageImpl&
63 _AppMessageImpl::operator =(const _AppMessageImpl& rhs)
64 {
65         if (&rhs != this)
66         {
67                 if (__pBundle)
68                 {
69                         bundle_free(__pBundle);
70                         __pBundle = NULL;
71                 }
72
73                 __pBundle = bundle_dup(rhs.__pBundle);
74                 SysAssert(__pBundle != NULL);
75         }
76
77         return *this;
78 }
79
80 String
81 _AppMessageImpl::GetValue(const String& key) const
82 {
83         return GetValue(key.GetPointer());
84 }
85
86 String
87 _AppMessageImpl::GetValue(const wchar_t key[]) const
88 {
89         SysAssert(__pBundle != NULL);
90
91         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
92
93         return String(appsvc_get_data(__pBundle, pKey.get()));
94 }
95
96 result
97 _AppMessageImpl::AddData(const String& key, const String& value)
98 {
99         SysAssert(__pBundle != NULL);
100
101         return AddData(__pBundle, key, value);
102 }
103
104 result
105 _AppMessageImpl::AddData(bundle* pBundle, const String& key, const String& value)
106 {
107         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
108         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(value));
109
110         appsvc_add_data(pBundle, pKey.get(), pVal.get());
111
112         return E_SUCCESS;
113 }
114
115 result
116 _AppMessageImpl::RemoveData(const String& key)
117 {
118         SysAssert(__pBundle != NULL);
119
120         return RemoveData(__pBundle, key);
121 }
122
123 result
124 _AppMessageImpl::RemoveData(bundle* pBundle, const String& key)
125 {
126         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
127
128         bundle_del(pBundle, pKey.get());
129
130         return E_SUCCESS;
131 }
132
133 result
134 _AppMessageImpl::SetOperation(bundle* pBundle, const String& operation)
135 {
136         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
137
138         appsvc_set_operation(pBundle, pVal.get());
139
140         return E_SUCCESS;
141 }
142
143 result
144 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
145 {
146         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
147
148         appsvc_set_uri(pBundle, pVal.get());
149
150         return E_SUCCESS;
151 }
152
153 result
154 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
155 {
156         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
157
158         appsvc_set_mime(pBundle, pVal.get());
159
160         return E_SUCCESS;
161 }
162
163 result
164 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
165 {
166         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
167
168         appsvc_set_category(pBundle, pVal.get());
169
170         return E_SUCCESS;
171 }
172
173 result
174 _AppMessageImpl::AddData(const IList* pList)
175 {
176         SysAssert(__pBundle != NULL);
177
178         return AddData(__pBundle, pList);
179 }
180
181 result
182 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
183 {
184         if (pList == null)
185         {
186                 return E_SUCCESS;
187         }
188
189         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
190         SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
191
192         String key;
193         String value;
194         while (pEnum->MoveNext() == E_SUCCESS)
195         {
196                 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
197
198                 int index = -1;
199                 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
200                 {
201                         continue;
202                 }
203                 pStr->SubString(0, index, key);
204                 if (key.IsEmpty())
205                 {
206                         continue;
207                 }
208
209                 pStr->SubString(index + 1, value);
210
211                 AddData(pBundle, key, value);
212
213                 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
214         }
215
216         return E_SUCCESS;
217 }
218
219 result
220 _AppMessageImpl::AddData(const IMap* pMap)
221 {
222         SysAssert(__pBundle != NULL);
223
224         return AddStringMap(__pBundle, pMap);
225 }
226
227 result
228 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
229 {
230         if (pMap == null || pMap->GetCount() == 0)
231         {
232                 SysLog(NID_APP, "No element added for bundle.");
233                 return E_SUCCESS;
234         }
235
236         std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
237         while(pEnum->MoveNext() == E_SUCCESS)
238         {
239                 const String* pKey = static_cast<const String*>(pEnum->GetKey());
240                 const Object* pObj = pEnum->GetValue();
241
242                 if (pKey && pObj)
243                 {
244                         if (typeid(*pObj) == typeid(const String))
245                         {
246                                 const String* pVal = static_cast<const String*>(pEnum->GetValue());
247                                 if (pVal)
248                                 {
249                                         _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
250                                 }
251                         }
252                         else if (typeid(*pObj) == typeid(const ArrayList))
253                         {
254                                 const ArrayList* pVal = static_cast<const ArrayList*>(pEnum->GetValue());
255                                 if (pVal)
256                                 {
257                                         _AppMessageImpl::AddValueArray(pBundle, *pKey, pVal);
258                                 }
259                         }
260                 }
261         }
262
263         return E_SUCCESS;
264 }
265
266 ArrayList*
267 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const String& key)
268 {
269         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
270
271         return GetValueArrayN(pBundle, pKey.get());
272 }
273
274 ArrayList*
275 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const char* pKey)
276 {
277         int len = 0;
278         const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
279         if (len == 0)
280         {
281                 return null;
282         }
283
284         ArrayList* pArray = new (std::nothrow) ArrayList(SingleObjectDeleter);
285         SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating array failed.");
286
287         pArray->Construct();
288
289         for (int i = 0; i < len; i++)
290         {
291                 const char* pStr = pStrArray[i];
292                 if (pStr)
293                 {
294                         pArray->Add(new (std::nothrow) String(pStr));
295                 }
296         }
297
298         if (pArray->GetCount() == 0)
299         {
300                 delete pArray;
301                 pArray = null;
302         }
303
304         return pArray;
305 }
306
307 result
308 _AppMessageImpl::AddValueArray(const String& key, const IList* pList)
309 {
310         SysAssert(__pBundle != NULL);
311
312         return AddValueArray(__pBundle, key, pList);
313 }
314
315 result
316 _AppMessageImpl::AddValueArraySingle(const String& key, const String& value)
317 {
318         SysAssert(__pBundle != NULL);
319
320         ArrayList arr;
321         arr.Construct();
322
323         arr.Add(value);
324
325         return AddValueArray(__pBundle, key, &arr);
326 }
327
328
329 result
330 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
331 {
332         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
333
334         return AddValueArray(pBundle, pKey.get(), pList);
335 }
336
337 result
338 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
339 {
340         SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
341
342         if (pList == null || pList->GetCount() == 0)
343         {
344                 SysLog(NID_APP, "No element added for bundle.");
345                 return E_SUCCESS;
346         }
347
348         int i = 0;
349         const int count = pList->GetCount();
350
351         const char** pSa = new (std::nothrow) const char*[count];
352         SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with count %d.", count);
353
354         // element is deliverately iterate with GetAt() for IList
355         for (i = 0; i < count; i++)
356         {
357                 pSa[i] = null;
358
359                 const String* pStr = static_cast<const String*>(pList->GetAt(i));
360                 if (pStr)
361                 {
362                         pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
363                         //SysLog(NID_APP, "%s", pSa[i]);
364                 }
365         }
366
367         int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
368
369         for (i = 0; i < count; i++)
370         {
371                 delete[] pSa[i];
372         }
373
374         delete[] pSa;
375
376         return (ret == 0) ? E_SUCCESS : E_SYSTEM;
377 }
378
379 }} // Tizen::App