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>
25 #include <appsvc/appsvc.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseString.h>
29 #include <FBaseColIList.h>
30 #include <FBaseColIMap.h>
31 #include <FBaseColIEnumerator.h>
32 #include <FBaseColArrayList.h>
34 #include <FBase_StringConverter.h>
36 #include "FApp_AppMessageImpl.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
41 namespace Tizen { namespace App
44 _AppMessageImpl::_AppMessageImpl(void)
45 : __pBundle(bundle_create())
47 SysAssert(__pBundle != NULL);
50 _AppMessageImpl::_AppMessageImpl(const _AppMessageImpl&rhs)
51 : __pBundle(bundle_dup(rhs.__pBundle))
53 SysAssert(__pBundle != NULL);
56 _AppMessageImpl::_AppMessageImpl(const String& appId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
57 : __pBundle(bundle_create())
59 SysAssert(__pBundle != NULL);
61 SetApplicationId(__pBundle, appId);
63 SetOperation(__pBundle, oId);
67 SetUri(__pBundle, *pUri);
72 SetMime(__pBundle, *pMime);
78 _AppMessageImpl::~_AppMessageImpl(void)
80 bundle_free(__pBundle);
84 _AppMessageImpl::operator =(const _AppMessageImpl& rhs)
90 bundle_free(__pBundle);
94 __pBundle = bundle_dup(rhs.__pBundle);
95 SysAssert(__pBundle != NULL);
102 _AppMessageImpl::GetValue(const String& key) const
104 return GetValue(key.GetPointer());
108 _AppMessageImpl::GetValue(const wchar_t key[]) const
110 SysAssert(__pBundle != NULL);
112 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
114 return String(appsvc_get_data(__pBundle, pKey.get()));
118 _AppMessageImpl::AddData(const String& key, const String& value)
120 SysAssert(__pBundle != NULL);
122 return AddData(__pBundle, key, value);
126 _AppMessageImpl::AddData(bundle* pBundle, const String& key, const String& value)
128 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
129 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(value));
131 appsvc_add_data(pBundle, pKey.get(), pVal.get());
137 _AppMessageImpl::RemoveData(const String& key)
139 SysAssert(__pBundle != NULL);
141 return RemoveData(__pBundle, key);
145 _AppMessageImpl::RemoveData(bundle* pBundle, const String& key)
147 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
149 bundle_del(pBundle, pKey.get());
155 _AppMessageImpl::GetApplicationId(const bundle* pBundle)
157 return String(appsvc_get_appid(const_cast<bundle*>(pBundle)));
161 _AppMessageImpl::SetApplicationId(bundle* pBundle, const String& appId)
163 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(appId));
165 // alias appid handling is done internally
166 appsvc_set_appid(pBundle, pVal.get());
172 _AppMessageImpl::GetOperation(const bundle* pBundle)
174 return String(appsvc_get_operation(const_cast<bundle*>(pBundle)));
178 _AppMessageImpl::SetOperation(bundle* pBundle, const char* pOperation)
180 appsvc_set_operation(pBundle, pOperation);
186 _AppMessageImpl::SetOperation(bundle* pBundle, const String& operation)
188 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
190 return SetOperation(pBundle, pVal.get());
194 _AppMessageImpl::GetUri(const bundle* pBundle)
196 return String(appsvc_get_uri(const_cast<bundle*>(pBundle)));
200 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
202 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
204 appsvc_set_uri(pBundle, pVal.get());
210 _AppMessageImpl::GetMime(const bundle* pBundle)
212 return String(appsvc_get_mime(const_cast<bundle*>(pBundle)));
216 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
218 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
220 appsvc_set_mime(pBundle, pVal.get());
226 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
228 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
230 appsvc_set_category(pBundle, pVal.get());
236 _AppMessageImpl::AddData(const IList* pList)
238 SysAssert(__pBundle != NULL);
240 return AddData(__pBundle, pList);
244 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
251 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
252 SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
256 while (pEnum->MoveNext() == E_SUCCESS)
258 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
261 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
265 pStr->SubString(0, index, key);
271 pStr->SubString(index + 1, value);
273 AddData(pBundle, key, value);
275 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
282 _AppMessageImpl::AddData(const IMap* pMap)
284 SysAssert(__pBundle != NULL);
286 return AddStringMap(__pBundle, pMap);
290 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
292 if (pMap == null || pMap->GetCount() == 0)
294 SysLog(NID_APP, "No element added for bundle.");
298 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
299 while(pEnum->MoveNext() == E_SUCCESS)
301 const String* pKey = static_cast<const String*>(pEnum->GetKey());
302 const Object* pObj = pEnum->GetValue();
306 if (typeid(*pObj) == typeid(const String))
308 const String* pVal = static_cast<const String*>(pEnum->GetValue());
311 _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
314 else if (typeid(*pObj) == typeid(const ArrayList))
316 const ArrayList* pVal = static_cast<const ArrayList*>(pEnum->GetValue());
319 _AppMessageImpl::AddValueArray(pBundle, *pKey, pVal);
329 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const String& key)
331 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
333 return GetValueArrayN(pBundle, pKey.get());
337 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const char* pKey)
340 const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
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.");
351 for (int i = 0; i < len; i++)
353 const char* pStr = pStrArray[i];
356 pArray->Add(new (std::nothrow) String(pStr));
360 if (pArray->GetCount() == 0)
370 _AppMessageImpl::AddValueArray(const String& key, const IList* pList)
372 SysAssert(__pBundle != NULL);
374 return AddValueArray(__pBundle, key, pList);
378 _AppMessageImpl::AddValueArraySingle(const String& key, const String& value)
380 SysAssert(__pBundle != NULL);
387 return AddValueArray(__pBundle, key, &arr);
392 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
394 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
396 return AddValueArray(pBundle, pKey.get(), pList);
400 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
402 SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
404 if (pList == null || pList->GetCount() == 0)
406 SysLog(NID_APP, "No element added for bundle.");
411 const int count = pList->GetCount();
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);
416 // element is deliverately iterate with GetAt() for IList
417 for (i = 0; i < count; i++)
421 const String* pStr = static_cast<const String*>(pList->GetAt(i));
424 pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
425 //SysLog(NID_APP, "%s", pSa[i]);
429 int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
431 for (i = 0; i < count; i++)
438 return (ret == 0) ? E_SUCCESS : E_SYSTEM;