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