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(void)
58 SysAssert(__pBundle != NULL);
59 bundle_free(__pBundle);
63 _AppMessageImpl::operator =(const _AppMessageImpl& rhs)
69 bundle_free(__pBundle);
73 __pBundle = bundle_dup(rhs.__pBundle);
74 SysAssert(__pBundle != NULL);
81 _AppMessageImpl::GetValue(const String& key) const
83 return GetValue(key.GetPointer());
87 _AppMessageImpl::GetValue(const wchar_t key[]) const
89 SysAssert(__pBundle != NULL);
91 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
93 return String(appsvc_get_data(__pBundle, pKey.get()));
97 _AppMessageImpl::AddData(const String& key, const String& value)
99 SysAssert(__pBundle != NULL);
101 return AddData(__pBundle, key, value);
105 _AppMessageImpl::AddData(bundle* pBundle, const String& key, const String& value)
107 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
108 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(value));
110 appsvc_add_data(pBundle, pKey.get(), pVal.get());
116 _AppMessageImpl::RemoveData(const String& key)
118 SysAssert(__pBundle != NULL);
120 return RemoveData(__pBundle, key);
124 _AppMessageImpl::RemoveData(bundle* pBundle, const String& key)
126 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
128 bundle_del(pBundle, pKey.get());
134 _AppMessageImpl::SetOperation(bundle* pBundle, const String& operation)
136 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
138 appsvc_set_operation(pBundle, pVal.get());
144 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
146 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
148 appsvc_set_uri(pBundle, pVal.get());
154 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
156 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
158 appsvc_set_mime(pBundle, pVal.get());
164 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
166 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
168 appsvc_set_category(pBundle, pVal.get());
174 _AppMessageImpl::AddData(const IList* pList)
176 SysAssert(__pBundle != NULL);
178 return AddData(__pBundle, pList);
182 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
189 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
190 SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
194 while (pEnum->MoveNext() == E_SUCCESS)
196 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
199 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
203 pStr->SubString(0, index, key);
209 pStr->SubString(index + 1, value);
211 AddData(pBundle, key, value);
213 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
220 _AppMessageImpl::AddData(const IMap* pMap)
222 SysAssert(__pBundle != NULL);
224 return AddStringMap(__pBundle, pMap);
228 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
230 if (pMap == null || pMap->GetCount() == 0)
232 SysLog(NID_APP, "No element added for bundle.");
236 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
237 while(pEnum->MoveNext() == E_SUCCESS)
239 const String* pKey = static_cast<const String*>(pEnum->GetKey());
240 const Object* pObj = pEnum->GetValue();
244 if (typeid(*pObj) == typeid(const String))
246 const String* pVal = static_cast<const String*>(pEnum->GetValue());
249 _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
252 else if (typeid(*pObj) == typeid(const ArrayList))
254 const ArrayList* pVal = static_cast<const ArrayList*>(pEnum->GetValue());
257 _AppMessageImpl::AddValueArray(pBundle, *pKey, pVal);
267 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const String& key)
269 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
271 return GetValueArrayN(pBundle, pKey.get());
275 _AppMessageImpl::GetValueArrayN(bundle* pBundle, const char* pKey)
278 const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
284 ArrayList* pArray = new (std::nothrow) ArrayList(SingleObjectDeleter);
285 SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating array failed.");
289 for (int i = 0; i < len; i++)
291 const char* pStr = pStrArray[i];
294 pArray->Add(new (std::nothrow) String(pStr));
298 if (pArray->GetCount() == 0)
308 _AppMessageImpl::AddValueArray(const String& key, const IList* pList)
310 SysAssert(__pBundle != NULL);
312 return AddValueArray(__pBundle, key, pList);
316 _AppMessageImpl::AddValueArraySingle(const String& key, const String& value)
318 SysAssert(__pBundle != NULL);
325 return AddValueArray(__pBundle, key, &arr);
330 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
332 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
334 return AddValueArray(pBundle, pKey.get(), pList);
338 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
340 SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
342 if (pList == null || pList->GetCount() == 0)
344 SysLog(NID_APP, "No element added for bundle.");
349 const int count = pList->GetCount();
351 const char** pSa = new (std::nothrow) const char*[count];
352 SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with count %d.", count);
354 // element is deliverately iterate with GetAt() for IList
355 for (i = 0; i < count; i++)
359 const String* pStr = static_cast<const String*>(pList->GetAt(i));
362 pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
363 //SysLog(NID_APP, "%s", pSa[i]);
367 int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
369 for (i = 0; i < count; i++)
376 return (ret == 0) ? E_SUCCESS : E_SYSTEM;