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