2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FApp_AppMessageImpl.cpp
20 * @brief This is the implementation for the _AppMessageImpl.cpp class.
24 #include <unique_ptr.h>
26 #include <appsvc/appsvc.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseString.h>
30 #include <FBaseColIList.h>
31 #include <FBaseColIEnumerator.h>
33 #include <FBase_StringConverter.h>
35 #include "FApp_AppMessageImpl.h"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
40 namespace Tizen { namespace App
43 _AppMessageImpl::_AppMessageImpl(void)
44 : __pBundle(bundle_create())
46 SysAssert(__pBundle != NULL);
49 _AppMessageImpl::_AppMessageImpl(const _AppMessageImpl&rhs)
50 : __pBundle(bundle_dup(rhs.__pBundle))
52 SysAssert(__pBundle != NULL);
55 _AppMessageImpl::~_AppMessageImpl(void)
57 SysAssert(__pBundle != NULL);
58 bundle_free(__pBundle);
62 _AppMessageImpl::operator =(const _AppMessageImpl& rhs)
68 bundle_free(__pBundle);
72 __pBundle = bundle_dup(rhs.__pBundle);
73 SysAssert(__pBundle != NULL);
80 _AppMessageImpl::GetValue(const String& key) const
82 return GetValue(key.GetPointer());
86 _AppMessageImpl::GetValue(const wchar_t key[]) const
88 SysAssert(__pBundle != NULL);
90 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
92 return String(appsvc_get_data(__pBundle, pKey.get()));
96 _AppMessageImpl::AddData(const String& key, const String& value)
98 SysAssert(__pBundle != NULL);
100 return AddData(__pBundle, key, value);
104 _AppMessageImpl::AddData(bundle* pBundle, const String& key, const String& value)
106 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
107 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(value));
109 appsvc_add_data(pBundle, pKey.get(), pVal.get());
115 _AppMessageImpl::RemoveData(const String& key)
117 SysAssert(__pBundle != NULL);
119 return RemoveData(__pBundle, key);
123 _AppMessageImpl::RemoveData(bundle* pBundle, const String& key)
125 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
127 bundle_del(pBundle, pKey.get());
133 _AppMessageImpl::SetOperation(bundle* pBundle, const String& operation)
135 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
137 appsvc_set_operation(pBundle, pVal.get());
143 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
145 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
147 appsvc_set_uri(pBundle, pVal.get());
153 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
155 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
157 appsvc_set_mime(pBundle, pVal.get());
163 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
165 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
167 appsvc_set_category(pBundle, pVal.get());
173 _AppMessageImpl::AddData(const IList* pList)
175 SysAssert(__pBundle != NULL);
177 return AddData(__pBundle, pList);
181 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
188 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
189 SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
193 while (pEnum->MoveNext() == E_SUCCESS)
195 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
198 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
202 pStr->SubString(0, index, key);
208 pStr->SubString(index + 1, value);
210 AddData(pBundle, key, value);
212 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
219 _AppMessageImpl::AddData(const IMap* pMap)
221 SysAssert(__pBundle != NULL);
223 return AddStringMap(__pBundle, pMap);
227 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
229 if (pMap == null || pMap->GetCount() == 0)
231 SysLog(NID_APP, "No element added for bundle.");
235 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
236 while(pEnum->MoveNext() == E_SUCCESS)
238 const String* pKey = static_cast<const String*>(pEnum->GetKey());
239 const Object* pObj = pEnum->GetValue();
243 if (typeid(*pObj) == typeid(const String))
245 const String* pVal = static_cast<const String*>(pEnum->GetValue());
248 _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
251 else if (typeid(*pObj) == typeid(const ArrayList))
253 const ArrayList* pVal = static_cast<const ArrayList*>(pEnum->GetValue());
256 _AppMessageImpl::AddValueArray(pBundle, *pKey, pVal);
266 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const String& key)
268 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
270 return GetValueArrayN(pBundle, pKey.get());
274 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const char* pKey)
277 const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
283 ArrayList* pArray = new (std::nothrow) ArrayList(SingleObjectDeleter);
284 SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating array failed.");
288 for (int i = 0; i < len; i++)
290 const char* pStr = pStrArray[i];
293 pArray->Add(new (std::nothrow) String(pStr));
297 if (pArray->GetCount() == 0)
307 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
309 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
311 return AddValueArray(pBundle, pKey.get(), pList);
315 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
317 SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
319 if (pList == null || pList->GetCount() == 0)
321 SysLog(NID_APP, "No element added for bundle.");
326 const int count = pList->GetCount();
328 const char** pSa = new (std::nothrow) const char*[count];
329 SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with cound %d.", count);
331 // element is deliverately iterate with GetAt() for IList
332 for (i = 0; i < count; i++)
336 const String* pStr = static_cast<const String*>(pList->GetAt(i));
339 pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
340 //SysLog(NID_APP, "%s", pSa[i]);
344 int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
346 for (i = 0; i < count; i++)
353 return (ret == 0) ? E_SUCCESS : E_SYSTEM;