2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FApp_AppMessageImpl.cpp
19 * @brief This is the implementation for the _AppMessageImpl.cpp class.
23 #include <unique_ptr.h>
26 #include <appsvc/appsvc.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseString.h>
30 #include <FBaseColIList.h>
31 #include <FBaseColIMap.h>
32 #include <FBaseColIEnumerator.h>
33 #include <FBaseColArrayList.h>
35 #include <FBase_StringConverter.h>
37 #include "FApp_AppMessageImpl.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
42 namespace Tizen { namespace App
45 _AppMessageImpl::_AppMessageImpl(void)
46 : __pBundle(bundle_create())
48 SysAssert(__pBundle != NULL);
51 _AppMessageImpl::_AppMessageImpl(const bundle* pBundle)
52 : __pBundle(bundle_dup(const_cast<bundle*>(pBundle)))
54 SysAssert(__pBundle != NULL);
57 _AppMessageImpl::_AppMessageImpl(const _AppMessageImpl&rhs)
58 : __pBundle(bundle_dup(rhs.__pBundle))
60 SysAssert(__pBundle != NULL);
63 _AppMessageImpl::_AppMessageImpl(const String& appId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
64 : __pBundle(bundle_create())
66 SysAssert(__pBundle != NULL);
68 SetApplicationId(__pBundle, appId);
70 SetOperation(__pBundle, oId);
74 SetUri(__pBundle, *pUri);
79 SetMime(__pBundle, *pMime);
85 _AppMessageImpl::~_AppMessageImpl(void)
87 bundle_free(__pBundle);
91 _AppMessageImpl::operator =(const _AppMessageImpl& rhs)
97 bundle_free(__pBundle);
101 __pBundle = bundle_dup(rhs.__pBundle);
102 SysAssert(__pBundle != NULL);
109 _AppMessageImpl::GetValue(const String& key) const
111 return GetValue(key.GetPointer());
115 _AppMessageImpl::GetValue(const wchar_t key[]) const
117 SysAssert(__pBundle != NULL);
119 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
121 return String(appsvc_get_data(__pBundle, pKey.get()));
125 _AppMessageImpl::AddData(const String& key, const String& value)
127 SysAssert(__pBundle != NULL);
129 return AddData(__pBundle, key, value);
133 _AppMessageImpl::AddData(bundle* pBundle, const String& key, const String& value)
135 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
136 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(value));
138 appsvc_add_data(pBundle, pKey.get(), pVal.get());
144 _AppMessageImpl::RemoveData(const String& key)
146 SysAssert(__pBundle != NULL);
148 return RemoveData(__pBundle, key);
152 _AppMessageImpl::RemoveData(bundle* pBundle, const String& key)
154 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
156 bundle_del(pBundle, pKey.get());
162 _AppMessageImpl::GetApplicationId(const bundle* pBundle)
164 return String(appsvc_get_appid(const_cast<bundle*>(pBundle)));
168 _AppMessageImpl::SetApplicationId(bundle* pBundle, const String& appId)
170 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(appId));
172 // alias appid handling is done internally
173 appsvc_set_appid(pBundle, pVal.get());
179 _AppMessageImpl::GetOperation(const bundle* pBundle)
181 return String(appsvc_get_operation(const_cast<bundle*>(pBundle)));
185 _AppMessageImpl::SetOperation(bundle* pBundle, const char* pOperation)
187 appsvc_set_operation(pBundle, pOperation);
193 _AppMessageImpl::SetOperation(bundle* pBundle, const String& operation)
195 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
197 return SetOperation(pBundle, pVal.get());
201 _AppMessageImpl::GetUri(const bundle* pBundle)
203 return String(appsvc_get_uri(const_cast<bundle*>(pBundle)));
207 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
209 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
211 appsvc_set_uri(pBundle, pVal.get());
217 _AppMessageImpl::GetMime(const bundle* pBundle)
219 return String(appsvc_get_mime(const_cast<bundle*>(pBundle)));
223 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
225 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
227 appsvc_set_mime(pBundle, pVal.get());
233 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
235 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
237 appsvc_set_category(pBundle, pVal.get());
243 _AppMessageImpl::AddData(const IList* pList)
245 SysAssert(__pBundle != NULL);
247 return AddData(__pBundle, pList);
251 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
258 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
259 SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
263 while (pEnum->MoveNext() == E_SUCCESS)
265 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
268 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
272 pStr->SubString(0, index, key);
278 pStr->SubString(index + 1, value);
280 AddData(pBundle, key, value);
282 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
289 _AppMessageImpl::AddData(const IMap* pMap)
291 SysAssert(__pBundle != NULL);
293 return AddStringMap(__pBundle, pMap);
297 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
299 if (pMap == null || pMap->GetCount() == 0)
301 SysLog(NID_APP, "No element added for bundle.");
305 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
306 while(pEnum->MoveNext() == E_SUCCESS)
308 const String* pKey = static_cast<const String*>(pEnum->GetKey());
309 const Object* pObj = pEnum->GetValue();
313 if (typeid(*pObj) == typeid(const String))
315 const String* pVal = static_cast<const String*>(pEnum->GetValue());
318 _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
321 else if (typeid(*pObj) == typeid(const ArrayList))
323 const ArrayList* pVal = static_cast<const ArrayList*>(pEnum->GetValue());
326 _AppMessageImpl::AddValueArray(pBundle, *pKey, pVal);
336 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const String& key)
338 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
340 return GetValueArrayN(pBundle, pKey.get());
344 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const char* pKey)
347 const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
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.");
358 for (int i = 0; i < len; i++)
360 const char* pStr = pStrArray[i];
363 pArray->Add(new (std::nothrow) String(pStr));
367 if (pArray->GetCount() == 0)
377 _AppMessageImpl::AddValueArray(const String& key, const IList* pList)
379 SysAssert(__pBundle != NULL);
381 return AddValueArray(__pBundle, key, pList);
385 _AppMessageImpl::AddValueArraySingle(const String& key, const String& value)
387 SysAssert(__pBundle != NULL);
394 return AddValueArray(__pBundle, key, &arr);
399 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
401 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
403 return AddValueArray(pBundle, pKey.get(), pList);
407 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
409 SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
411 if (pList == null || pList->GetCount() == 0)
413 SysLog(NID_APP, "No element added for bundle.");
418 const int count = pList->GetCount();
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);
423 // element is deliverately iterate with GetAt() for IList
424 for (i = 0; i < count; i++)
428 const String* pStr = static_cast<const String*>(pList->GetAt(i));
431 pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
432 //SysLog(NID_APP, "%s", pSa[i]);
436 int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
438 for (i = 0; i < count; i++)
445 return (ret == 0) ? E_SUCCESS : E_SYSTEM;