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