2 // Open Service Platform
3 // Copyright (c) 2013 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.
18 * @file FScl_AccountManagerUtil.cpp
19 * @brief This is implementation for the _AccountManagerUtil class.
21 * This file contains the definitions of the _AccountManagerUtil class.
25 #include <unique_ptr.h>
27 #include <FAppTypes.h>
28 #include <FBaseColHashMap.h>
29 #include <FBaseColIMap.h>
30 #include <FBaseResult.h>
31 #include <FBaseString.h>
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
34 #include <FLclLocale.h>
35 #include <FLclLocaleManager.h>
36 #include <FSclAccount.h>
37 #include <FSclAccountProvider.h>
38 #include <FSclTypes.h>
39 #include "FScl_AccountImpl.h"
40 #include "FScl_AccountManagerUtil.h"
41 #include "FScl_AccountProviderImpl.h"
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Locales;
48 namespace Tizen { namespace Social
51 static const wchar_t* _DEFAULT_LOCALE = L"en_GB";
54 OnAccountTypeLabelReceived(char* pAppId, char* pLabel, char* pLocale, void* pUserData)
58 LocaleManager localeManager;
59 Locale systemLocale = localeManager.GetSystemLocale();
60 SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
62 LanguageCode languageCode = systemLocale.GetLanguageCode();
63 String localeStr(Locale::LanguageCodeToTwoLetterLanguageCodeString(languageCode));
65 String countryCodeStr(systemLocale.GetCountryCodeString());
66 localeStr.Append(L"_");
67 localeStr.Append(countryCodeStr);
69 String* pDisplayName = static_cast<String*> (pUserData);
70 if (localeStr.Equals(String(pLocale)))
72 *pDisplayName = String(pLabel);
74 else if((String(_DEFAULT_LOCALE)).Equals(String(pLocale)) && pDisplayName->IsEmpty())
76 *pDisplayName = String(pLabel);
84 OnAccountCustomDataReceived(char* pKey, char* pValue, void* pUserData)
86 if (pKey != null && pValue != null)
88 std::unique_ptr<String> pExtendedDataKey(new (std::nothrow) String(pKey));
89 SysTryReturn(NID_SCL, pExtendedDataKey != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
91 std::unique_ptr<String> pExtendedDataValue(new (std::nothrow) String(pValue));
92 SysTryReturn(NID_SCL, pExtendedDataValue != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
94 HashMap* pExtendedData = static_cast<HashMap*> (pUserData);
96 result r = pExtendedData->Add(pExtendedDataKey.get(), pExtendedDataValue.get());
97 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
99 pExtendedDataKey.release();
100 pExtendedDataValue.release();
107 OnAccountProviderCapabilityReceived(char* pAppId, char* pKey, void* pUserData)
111 std::unique_ptr<String> pCapability(new (std::nothrow) String(pKey));
112 SysTryReturn(NID_SCL, pCapability != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
114 ArrayList* pCapabilityList = static_cast<ArrayList*> (pUserData);
116 result r = pCapabilityList->Add(pCapability.get());
117 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
119 pCapability.release();
126 _AccountManagerUtil::CreateAccountHandleN(const Account account)
130 account_h accountHandle = null;
131 int ret = account_create(&accountHandle);
132 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
135 std::unique_ptr<char[]> pCharArrayUserName(_StringConverter::CopyToCharArrayN(account.GetUserName()));
136 SysTryCatch(NID_SCL, pCharArrayUserName != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
138 ret = account_set_user_name(accountHandle, pCharArrayUserName.get());
139 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
141 // Set the package name
143 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
144 SysTryCatch(NID_SCL, pApp != null, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
146 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
147 SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
149 ret = account_set_package_name(accountHandle, pCharArrayAppId.get());
150 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
153 // Set the extended data
155 std::unique_ptr<IMap, AllElementsDeleter> pExtendedData(account.GetExtendedDataN());
156 SysTryCatch(NID_SCL, pExtendedData != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
158 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
159 SysTryCatch(NID_SCL, pMapEnum != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
164 while (pMapEnum->MoveNext() == E_SUCCESS)
166 pKey = static_cast<String*> (pMapEnum->GetKey());
167 std::unique_ptr<char[]> pCharArrayKey(_StringConverter::CopyToCharArrayN(*pKey));
168 SysTryCatch(NID_SCL, pCharArrayKey != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
170 pVal = static_cast<String*> (pMapEnum->GetValue());
171 std::unique_ptr<char[]> pCharArrayVal(_StringConverter::CopyToCharArrayN(*pVal));
172 SysTryCatch(NID_SCL, pCharArrayVal != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
174 ret = account_set_custom(accountHandle, pCharArrayKey.get(), pCharArrayVal.get());
175 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_OUT_OF_MEMORY, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
176 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
178 pCharArrayKey.reset();
179 pCharArrayVal.reset();
183 SetLastResult(E_SUCCESS);
184 return accountHandle;
187 ret = account_destroy(accountHandle);
188 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
194 _AccountManagerUtil::ConvertAccountHandleToAccountN(account_h accountHandle)
198 std::unique_ptr<Account> pAccount(new (std::nothrow) Account(L""));
199 SysTryReturn(NID_SCL, pAccount != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
201 _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(*(pAccount));
203 // Set the account ID
205 int ret = account_get_account_id(accountHandle, &accountDbId);
206 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
208 pAccountImpl->SetId(accountDbId);
210 // Set the account provider
211 char* pTempAppId = null;
213 ret = account_get_package_name(accountHandle, &pTempAppId);
214 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && pTempAppId != null, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
216 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
218 account_type_h accountTypeHandle = null;
219 ret = account_type_create(&accountTypeHandle);
220 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
222 ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
223 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
225 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
226 account_type_destroy(accountTypeHandle);
230 std::unique_ptr<AccountProvider> pAccountProvider(ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
231 SysTryReturn(NID_SCL, pAccountProvider != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
233 pAccountImpl->SetAccountProvider(*pAccountProvider);
235 account_type_destroy(accountTypeHandle);
238 char* pTempUserName = null;
240 ret = account_get_user_name(accountHandle, &pTempUserName);
241 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && pTempUserName != null, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
243 std::unique_ptr<char[], _CharDeleter> pCharArrayUserName(pTempUserName);
245 result r = pAccountImpl->SetUserName(pTempUserName);
246 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
248 // Set the extended data
249 std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new HashMap());
250 SysTryReturn(NID_SCL, pExtendedData != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
252 r = pExtendedData->Construct();
253 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
255 ret = account_get_custom_all(accountHandle, OnAccountCustomDataReceived, pExtendedData.get());
256 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
259 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
261 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
262 SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
267 while (pMapEnum->MoveNext() == E_SUCCESS)
269 pKey = static_cast<String*> (pMapEnum->GetKey());
270 pVal = static_cast<String*> (pMapEnum->GetValue());
272 r = pAccountImpl->SetExtendedData(*pKey, *pVal);
273 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
276 SetLastResult(E_SUCCESS);
277 return pAccount.release();
281 _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(account_type_h accountTypeHandle)
285 std::unique_ptr<AccountProvider> pAccountProvider(new (std::nothrow) AccountProvider());
286 SysTryReturn(NID_SCL, pAccountProvider != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
288 _AccountProviderImpl* pAccountProviderImpl = _AccountProviderImpl::GetInstance(*(pAccountProvider));
291 char* pTempAppId = null;
293 int ret = account_type_get_app_id(accountTypeHandle, &pTempAppId);
294 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && pTempAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
296 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
298 pAccountProviderImpl->SetAppId(pTempAppId);
300 // Set the capability list
301 std::unique_ptr<ArrayList, AllElementsDeleter> pCapabilityList(new ArrayList());
302 SysTryReturn(NID_SCL, pCapabilityList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
304 result r = pCapabilityList->Construct();
305 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pCapabilityList.", GetErrorMessage(r));
307 ret = account_type_query_provider_feature_by_app_id(OnAccountProviderCapabilityReceived, pCharArrayAppId.get(), pCapabilityList.get());
308 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE || ret == ACCOUNT_ERROR_RECORD_NOT_FOUND, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
310 if (pCapabilityList != null)
312 std::unique_ptr<IEnumerator> pEnum(pCapabilityList->GetEnumeratorN());
313 SysTryReturn(NID_SCL, pEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
315 String* pTempCapability = null;
317 while (pEnum->MoveNext() == E_SUCCESS)
319 pTempCapability = static_cast<String*> (pEnum->GetCurrent());
321 r = pAccountProviderImpl->AddCapability(*pTempCapability);
322 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
326 // Set the display name
327 std::unique_ptr<String> pDisplayName(new (std::nothrow) String());
328 SysTryReturn(NID_SCL, pDisplayName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
330 ret = account_type_get_label(accountTypeHandle, OnAccountTypeLabelReceived, pDisplayName.get());
331 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
334 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
336 pAccountProviderImpl->SetDisplayName(*pDisplayName);
339 char* pTempIconPath = null;
341 ret = account_type_get_icon_path(accountTypeHandle, &pTempIconPath);
342 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
344 if (pTempIconPath != null)
346 pAccountProviderImpl->SetIconPath(pTempIconPath);
350 // Set the small icon path
351 char* pTempSmallIconPath = null;
353 ret = account_type_get_small_icon_path(accountTypeHandle, &pTempSmallIconPath);
354 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
356 if (pTempSmallIconPath != null)
358 pAccountProviderImpl->SetSmallIconPath(pTempSmallIconPath);
359 free(pTempSmallIconPath);
362 // Set the multiple accounts support
363 int multipleAccountsSupport = 0;
364 ret = account_type_get_multiple_account_support(accountTypeHandle, &multipleAccountsSupport);
365 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
367 pAccountProviderImpl->SetMultipleAccountsSupport(multipleAccountsSupport);
369 SetLastResult(E_SUCCESS);
370 return pAccountProvider.release();