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 <FBaseColIEnumerator.h>
32 #include <FBase_StringConverter.h>
34 #include "FApp_AppMessageImpl.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
39 namespace Tizen { namespace App
42 _AppMessageImpl::_AppMessageImpl(void)
43 : __pBundle(bundle_create())
45 SysAssert(__pBundle != NULL);
48 _AppMessageImpl::_AppMessageImpl(const _AppMessageImpl&rhs)
49 : __pBundle(bundle_dup(rhs.__pBundle))
51 SysAssert(__pBundle != NULL);
54 _AppMessageImpl::~_AppMessageImpl(void)
56 SysAssert(__pBundle != NULL);
57 bundle_free(__pBundle);
61 _AppMessageImpl::operator =(const _AppMessageImpl& rhs)
67 bundle_free(__pBundle);
71 __pBundle = bundle_dup(rhs.__pBundle);
72 SysAssert(__pBundle != NULL);
79 _AppMessageImpl::GetValue(const String& key) const
81 return GetValue(key.GetPointer());
85 _AppMessageImpl::GetValue(const wchar_t key[]) const
87 SysAssert(__pBundle != NULL);
89 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
91 return String(appsvc_get_data(__pBundle, pKey.get()));
95 _AppMessageImpl::AddData(const String& key, const String& value)
97 SysAssert(__pBundle != NULL);
99 return AddData(__pBundle, key, value);
103 _AppMessageImpl::AddData(bundle* pBundle, const String& key, const String& value)
105 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
106 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(value));
108 appsvc_add_data(pBundle, pKey.get(), pVal.get());
114 _AppMessageImpl::RemoveData(const String& key)
116 SysAssert(__pBundle != NULL);
118 return RemoveData(__pBundle, key);
122 _AppMessageImpl::RemoveData(bundle* pBundle, const String& key)
124 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
126 bundle_del(pBundle, pKey.get());
132 _AppMessageImpl::SetOperation(bundle* pBundle, const String& operation)
134 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
136 appsvc_set_operation(pBundle, pVal.get());
142 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
144 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
146 appsvc_set_uri(pBundle, pVal.get());
152 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
154 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
156 appsvc_set_mime(pBundle, pVal.get());
162 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
164 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
166 appsvc_set_category(pBundle, pVal.get());
172 _AppMessageImpl::AddData(const IList* pList)
174 SysAssert(__pBundle != NULL);
176 return AddData(__pBundle, pList);
180 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
187 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
188 SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
192 while (pEnum->MoveNext() == E_SUCCESS)
194 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
197 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
201 pStr->SubString(0, index, key);
207 pStr->SubString(index + 1, value);
209 AddData(pBundle, key, value);
211 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
218 _AppMessageImpl::AddData(const IMap* pMap)
220 SysAssert(__pBundle != NULL);
222 return AddStringMap(__pBundle, pMap);
226 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
228 if (pMap == null || pMap->GetCount() == 0)
230 SysLog(NID_APP, "No element added for bundle.");
234 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
235 while(pEnum->MoveNext() == E_SUCCESS)
237 const String* pKey = static_cast<const String*>(pEnum->GetKey());
238 const Object* pObj = pEnum->GetValue();
242 if (typeid(*pObj) == typeid(const String))
244 const String* pVal = static_cast<const String*>(pEnum->GetValue());
247 _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
250 else if (typeid(*pObj) == typeid(const ArrayList))
252 const ArrayList* pVal = static_cast<const ArrayList*>(pEnum->GetValue());
255 _AppMessageImpl::AddValueArray(pBundle, *pKey, pVal);
265 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const String& key)
267 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
269 return GetValueArrayN(pBundle, pKey.get());
273 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const char* pKey)
276 const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
282 ArrayList* pArray = new (std::nothrow) ArrayList(SingleObjectDeleter);
283 SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating array failed.");
287 for (int i = 0; i < len; i++)
289 const char* pStr = pStrArray[i];
292 pArray->Add(new (std::nothrow) String(pStr));
296 if (pArray->GetCount() == 0)
306 _AppMessageImpl::AddValueArray(const String& key, const IList* pList)
308 SysAssert(__pBundle != NULL);
310 return AddValueArray(__pBundle, key, pList);
314 _AppMessageImpl::AddValueArraySingle(const String& key, const String& value)
316 SysAssert(__pBundle != NULL);
323 return AddValueArray(__pBundle, key, &arr);
328 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
330 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
332 return AddValueArray(pBundle, pKey.get(), pList);
336 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
338 SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
340 if (pList == null || pList->GetCount() == 0)
342 SysLog(NID_APP, "No element added for bundle.");
347 const int count = pList->GetCount();
349 const char** pSa = new (std::nothrow) const char*[count];
350 SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with count %d.", count);
352 // element is deliverately iterate with GetAt() for IList
353 for (i = 0; i < count; i++)
357 const String* pStr = static_cast<const String*>(pList->GetAt(i));
360 pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
361 //SysLog(NID_APP, "%s", pSa[i]);
365 int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
367 for (i = 0; i < count; i++)
374 return (ret == 0) ? E_SUCCESS : E_SYSTEM;