Merge "Apply pkgs config for haptic" into tizen_2.1
[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::SetOperation(bundle* pBundle, const String& operation)
116 {
117         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
118
119         appsvc_set_operation(pBundle, pVal.get());
120
121         return E_SUCCESS;
122 }
123
124 result
125 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
126 {
127         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
128
129         appsvc_set_uri(pBundle, pVal.get());
130
131         return E_SUCCESS;
132 }
133
134 result
135 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
136 {
137         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
138
139         appsvc_set_mime(pBundle, pVal.get());
140
141         return E_SUCCESS;
142 }
143
144 result
145 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
146 {
147         std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
148
149         appsvc_set_category(pBundle, pVal.get());
150
151         return E_SUCCESS;
152 }
153
154 result
155 _AppMessageImpl::AddData(const IList* pList)
156 {
157         SysAssert(__pBundle != NULL);
158
159         return AddData(__pBundle, pList);
160 }
161
162 result
163 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
164 {
165         if (pList == null)
166         {
167                 return E_SUCCESS;
168         }
169
170         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
171         SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
172
173         String key;
174         String value;
175         while (pEnum->MoveNext() == E_SUCCESS)
176         {
177                 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
178
179                 int index = -1;
180                 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
181                 {
182                         continue;
183                 }
184                 pStr->SubString(0, index, key);
185                 if (key.IsEmpty())
186                 {
187                         continue;
188                 }
189
190                 pStr->SubString(index + 1, value);
191
192                 AddData(pBundle, key, value);
193
194                 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
195         }
196
197         return E_SUCCESS;
198 }
199
200 result
201 _AppMessageImpl::AddData(const IMap* pMap)
202 {
203         SysAssert(__pBundle != NULL);
204
205         return AddStringMap(__pBundle, pMap);
206 }
207
208 result
209 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
210 {
211         if (pMap == null || pMap->GetCount() == 0)
212         {
213                 SysLog(NID_APP, "No element added for bundle.");
214                 return E_SUCCESS;
215         }
216
217         std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
218         while(pEnum->MoveNext() == E_SUCCESS)
219         {
220                 const String* pKey = static_cast<const String*>(pEnum->GetKey());
221                 const Object* pObj = pEnum->GetValue();
222
223                 if (pKey && pObj)
224                 {
225                         if (typeid(*pObj) == typeid(const String))
226                         {
227                                 const String* pVal = static_cast<const String*>(pEnum->GetValue());
228                                 if (pVal)
229                                 {
230                                         _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
231                                 }
232                         }
233                 }
234         }
235
236         return E_SUCCESS;
237 }
238
239 ArrayList*
240 _AppMessageImpl::GetValueArray(bundle* pBundle, const String& key)
241 {
242         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
243
244         return GetValueArray(pBundle, pKey.get());
245 }
246
247 ArrayList*
248 _AppMessageImpl::GetValueArray(bundle* pBundle, const char* pKey)
249 {
250         int len = 0;
251         const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
252         if (len == 0)
253         {
254                 return null;
255         }
256
257         ArrayList* pArray = new (std::nothrow) ArrayList(SingleObjectDeleter);
258         SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating array failed.");
259
260         pArray->Construct();
261
262         for (int i = 0; i < len; i++)
263         {
264                 const char* pStr = pStrArray[i];
265                 if (pStr)
266                 {
267                         pArray->Add(new (std::nothrow) String(pStr));
268                 }
269         }
270
271         if (pArray->GetCount() == 0)
272         {
273                 delete pArray;
274                 pArray = null;
275         }
276
277         return pArray;
278 }
279
280 result
281 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
282 {
283         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
284
285         return AddValueArray(pBundle, pKey.get(), pList);
286 }
287
288 result
289 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
290 {
291         SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
292
293         if (pList == null || pList->GetCount() == 0)
294         {
295                 SysLog(NID_APP, "No element added for bundle.");
296                 return E_SUCCESS;
297         }
298
299         int i = 0;
300         const int count = pList->GetCount();
301
302         const char** pSa = new (std::nothrow) const char*[count];
303         SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with cound %d.", count);
304
305         // element is deliverately iterate with GetAt() for IList
306         for (i = 0; i < count; i++)
307         {
308                 pSa[i] = null;
309
310                 const String* pStr = static_cast<const String*>(pList->GetAt(i));
311                 if (pStr)
312                 {
313                         pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
314                         //SysLog(NID_APP, "%s", pSa[i]);
315                 }
316         }
317
318         int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
319
320         for (i = 0; i < count; i++)
321         {
322                 delete[] pSa[i];
323         }
324
325         delete[] pSa;
326
327         return (ret == 0) ? E_SUCCESS : E_SYSTEM;
328 }
329
330 }} // Tizen::App