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