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::SetOperation(bundle* pBundle, const String& operation)
117 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(operation));
119 appsvc_set_operation(pBundle, pVal.get());
125 _AppMessageImpl::SetUri(bundle* pBundle, const String& uri)
127 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(uri));
129 appsvc_set_uri(pBundle, pVal.get());
135 _AppMessageImpl::SetMime(bundle* pBundle, const String& mime)
137 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(mime));
139 appsvc_set_mime(pBundle, pVal.get());
145 _AppMessageImpl::SetCategory(bundle* pBundle, const String& category)
147 std::unique_ptr<char[]> pVal(_StringConverter::CopyToCharArrayN(category));
149 appsvc_set_category(pBundle, pVal.get());
155 _AppMessageImpl::AddData(const IList* pList)
157 SysAssert(__pBundle != NULL);
159 return AddData(__pBundle, pList);
163 _AppMessageImpl::AddData(bundle* pBundle, const IList* pList)
170 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
171 SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
175 while (pEnum->MoveNext() == E_SUCCESS)
177 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
180 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
184 pStr->SubString(0, index, key);
190 pStr->SubString(index + 1, value);
192 AddData(pBundle, key, value);
194 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
201 _AppMessageImpl::AddData(const IMap* pMap)
203 SysAssert(__pBundle != NULL);
205 return AddStringMap(__pBundle, pMap);
209 _AppMessageImpl::AddStringMap(bundle* pBundle, const IMap* pMap)
211 if (pMap == null || pMap->GetCount() == 0)
213 SysLog(NID_APP, "No element added for bundle.");
217 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
218 while(pEnum->MoveNext() == E_SUCCESS)
220 const String* pKey = static_cast<const String*>(pEnum->GetKey());
221 const Object* pObj = pEnum->GetValue();
225 if (typeid(*pObj) == typeid(const String))
227 const String* pVal = static_cast<const String*>(pEnum->GetValue());
230 _AppMessageImpl::AddData(pBundle, *pKey, *pVal);
240 _AppMessageImpl::GetValueArray(bundle* pBundle, const String& key)
242 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
244 return GetValueArray(pBundle, pKey.get());
248 _AppMessageImpl::GetValueArray(bundle* pBundle, const char* pKey)
251 const char** pStrArray = bundle_get_str_array(pBundle, pKey, &len);
257 ArrayList* pArray = new (std::nothrow) ArrayList(SingleObjectDeleter);
258 SysTryReturn(NID_APP, pArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating array failed.");
262 for (int i = 0; i < len; i++)
264 const char* pStr = pStrArray[i];
267 pArray->Add(new (std::nothrow) String(pStr));
271 if (pArray->GetCount() == 0)
281 _AppMessageImpl::AddValueArray(bundle* pBundle, const String& key, const IList* pList)
283 std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
285 return AddValueArray(pBundle, pKey.get(), pList);
289 _AppMessageImpl::AddValueArray(bundle* pBundle, const char* pKey, const IList* pList)
291 SysTryReturnResult(NID_APP, pBundle != NULL, E_INVALID_ARG, "Empty bundle.");
293 if (pList == null || pList->GetCount() == 0)
295 SysLog(NID_APP, "No element added for bundle.");
300 const int count = pList->GetCount();
302 const char** pSa = new (std::nothrow) const char*[count];
303 SysTryReturnResult(NID_APP, pSa != null, E_OUT_OF_MEMORY, "Memory allocation failure with cound %d.", count);
305 // element is deliverately iterate with GetAt() for IList
306 for (i = 0; i < count; i++)
310 const String* pStr = static_cast<const String*>(pList->GetAt(i));
313 pSa[i] = _StringConverter::CopyToCharArrayN(*pStr);
314 //SysLog(NID_APP, "%s", pSa[i]);
318 int ret = bundle_add_str_array(pBundle, pKey, pSa, count);
320 for (i = 0; i < count; i++)
327 return (ret == 0) ? E_SUCCESS : E_SYSTEM;