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 <FBaseUtilStringTokenizer.h>
34 #include <FBase_StringConverter.h>
35 #include <FLclLocale.h>
36 #include <FSysSettingInfo.h>
37 #include <FSclAccount.h>
38 #include <FSclAccountProvider.h>
39 #include <FSclTypes.h>
40 #include "FScl_AccountImpl.h"
41 #include "FScl_AccountManagerUtil.h"
42 #include "FScl_AccountProviderImpl.h"
44 using namespace Tizen::App;
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Base::Utility;
48 using namespace Tizen::Locales;
49 using namespace Tizen::System;
51 namespace Tizen { namespace Social
54 static const wchar_t* _SETTING_LOCALE_KEY = L"http://tizen.org/setting/locale.language";
55 static const wchar_t* _DEFAULT_LOCALE = L"default";
56 static const wchar_t* _LOCALE_DELIMETER = L"_";
59 OnAccountTypeLabelReceived(char* pAppId, char* pLabel, char* pLocale, void* pUserData)
63 String languageLocale;
64 String convertedLanguageLocale;
66 result r = SettingInfo::GetValue(String(_SETTING_LOCALE_KEY), languageLocale);
67 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
69 StringTokenizer strTok(languageLocale, String(_LOCALE_DELIMETER));
72 strTok.GetNextToken(token);
73 LanguageCode languageCode = Locale::StringToLanguageCode(token);
74 convertedLanguageLocale.Append(Locale::LanguageCodeToTwoLetterLanguageCodeString(languageCode));
75 convertedLanguageLocale.Append(String(_LOCALE_DELIMETER));
77 strTok.GetNextToken(token);
78 convertedLanguageLocale.Append(token);
80 String* pDisplayName = static_cast<String*> (pUserData);
81 if (convertedLanguageLocale.Equals(String(pLocale)))
83 *pDisplayName = String(pLabel);
85 else if((String(_DEFAULT_LOCALE)).Equals(String(pLocale)) && pDisplayName->IsEmpty())
87 *pDisplayName = String(pLabel);
95 OnAccountCustomDataReceived(char* pKey, char* pValue, void* pUserData)
97 if (pKey != null && pValue != null)
99 std::unique_ptr<String> pExtendedDataKey(new (std::nothrow) String(pKey));
100 SysTryReturn(NID_SCL, pExtendedDataKey != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
102 std::unique_ptr<String> pExtendedDataValue(new (std::nothrow) String(pValue));
103 SysTryReturn(NID_SCL, pExtendedDataValue != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
105 HashMap* pExtendedData = static_cast<HashMap*> (pUserData);
107 result r = pExtendedData->Add(pExtendedDataKey.get(), pExtendedDataValue.get());
108 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
110 pExtendedDataKey.release();
111 pExtendedDataValue.release();
118 OnAccountProviderCapabilityReceived(char* pAppId, char* pKey, void* pUserData)
122 std::unique_ptr<String> pCapability(new (std::nothrow) String(pKey));
123 SysTryReturn(NID_SCL, pCapability != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
125 ArrayList* pCapabilityList = static_cast<ArrayList*> (pUserData);
127 result r = pCapabilityList->Add(pCapability.get());
128 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
130 pCapability.release();
137 _AccountManagerUtil::CreateAccountHandleN(const Account account)
141 account_h accountHandle = null;
142 int ret = account_create(&accountHandle);
143 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
146 std::unique_ptr<char[]> pCharArrayUserName(_StringConverter::CopyToCharArrayN(account.GetUserName()));
147 SysTryCatch(NID_SCL, pCharArrayUserName != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
149 ret = account_set_user_name(accountHandle, pCharArrayUserName.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));
152 // Set the package name
154 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
155 SysTryCatch(NID_SCL, pApp != null, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
157 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
158 SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
160 ret = account_set_package_name(accountHandle, pCharArrayAppId.get());
161 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
164 // Set the extended data
166 std::unique_ptr<IMap, AllElementsDeleter> pExtendedData(account.GetExtendedDataN());
167 SysTryCatch(NID_SCL, pExtendedData != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
169 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
170 SysTryCatch(NID_SCL, pMapEnum != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
175 while (pMapEnum->MoveNext() == E_SUCCESS)
177 pKey = static_cast<String*> (pMapEnum->GetKey());
178 std::unique_ptr<char[]> pCharArrayKey(_StringConverter::CopyToCharArrayN(*pKey));
179 SysTryCatch(NID_SCL, pCharArrayKey != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
181 pVal = static_cast<String*> (pMapEnum->GetValue());
182 std::unique_ptr<char[]> pCharArrayVal(_StringConverter::CopyToCharArrayN(*pVal));
183 SysTryCatch(NID_SCL, pCharArrayVal != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
185 ret = account_set_custom(accountHandle, pCharArrayKey.get(), pCharArrayVal.get());
186 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_OUT_OF_MEMORY, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
187 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
189 pCharArrayKey.reset();
190 pCharArrayVal.reset();
194 SetLastResult(E_SUCCESS);
195 return accountHandle;
198 ret = account_destroy(accountHandle);
199 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
205 _AccountManagerUtil::ConvertAccountHandleToAccountN(account_h accountHandle)
209 std::unique_ptr<Account> pAccount(new (std::nothrow) Account(L""));
210 SysTryReturn(NID_SCL, pAccount != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
212 _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(*(pAccount));
214 // Set the account ID
216 int ret = account_get_account_id(accountHandle, &accountDbId);
217 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
219 pAccountImpl->SetId(accountDbId);
221 // Set the account provider
222 char* pTempAppId = null;
224 ret = account_get_package_name(accountHandle, &pTempAppId);
225 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));
227 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
229 account_type_h accountTypeHandle = null;
230 ret = account_type_create(&accountTypeHandle);
231 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
233 ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
234 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
236 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
237 account_type_destroy(accountTypeHandle);
241 std::unique_ptr<AccountProvider> pAccountProvider(ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
242 SysTryReturn(NID_SCL, pAccountProvider != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
244 pAccountImpl->SetAccountProvider(*pAccountProvider);
246 account_type_destroy(accountTypeHandle);
249 char* pTempUserName = null;
251 ret = account_get_user_name(accountHandle, &pTempUserName);
252 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));
254 std::unique_ptr<char[], _CharDeleter> pCharArrayUserName(pTempUserName);
256 result r = pAccountImpl->SetUserName(pTempUserName);
257 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
259 // Set the extended data
260 std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new HashMap());
261 SysTryReturn(NID_SCL, pExtendedData != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
263 r = pExtendedData->Construct();
264 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
266 ret = account_get_custom_all(accountHandle, OnAccountCustomDataReceived, pExtendedData.get());
267 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
270 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
272 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
273 SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
278 while (pMapEnum->MoveNext() == E_SUCCESS)
280 pKey = static_cast<String*> (pMapEnum->GetKey());
281 pVal = static_cast<String*> (pMapEnum->GetValue());
283 r = pAccountImpl->SetExtendedData(*pKey, *pVal);
284 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
287 SetLastResult(E_SUCCESS);
288 return pAccount.release();
292 _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(account_type_h accountTypeHandle)
296 std::unique_ptr<AccountProvider> pAccountProvider(new (std::nothrow) AccountProvider());
297 SysTryReturn(NID_SCL, pAccountProvider != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
299 _AccountProviderImpl* pAccountProviderImpl = _AccountProviderImpl::GetInstance(*(pAccountProvider));
302 char* pTempAppId = null;
304 int ret = account_type_get_app_id(accountTypeHandle, &pTempAppId);
305 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && pTempAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
307 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
309 pAccountProviderImpl->SetAppId(pTempAppId);
311 // Set the capability list
312 std::unique_ptr<ArrayList, AllElementsDeleter> pCapabilityList(new ArrayList());
313 SysTryReturn(NID_SCL, pCapabilityList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
315 result r = pCapabilityList->Construct();
316 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pCapabilityList.", GetErrorMessage(r));
318 ret = account_type_query_provider_feature_by_app_id(OnAccountProviderCapabilityReceived, pCharArrayAppId.get(), pCapabilityList.get());
319 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));
321 if (pCapabilityList != null)
323 std::unique_ptr<IEnumerator> pEnum(pCapabilityList->GetEnumeratorN());
324 SysTryReturn(NID_SCL, pEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
326 String* pTempCapability = null;
328 while (pEnum->MoveNext() == E_SUCCESS)
330 pTempCapability = static_cast<String*> (pEnum->GetCurrent());
332 r = pAccountProviderImpl->AddCapability(*pTempCapability);
333 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
337 // Set the display name
338 std::unique_ptr<String> pDisplayName(new (std::nothrow) String());
339 SysTryReturn(NID_SCL, pDisplayName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
341 ret = account_type_get_label(accountTypeHandle, OnAccountTypeLabelReceived, pDisplayName.get());
342 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
345 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
347 pAccountProviderImpl->SetDisplayName(*pDisplayName);
350 char* pTempIconPath = null;
352 ret = account_type_get_icon_path(accountTypeHandle, &pTempIconPath);
353 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
355 if (pTempIconPath != null)
357 pAccountProviderImpl->SetIconPath(pTempIconPath);
361 // Set the small icon path
362 char* pTempSmallIconPath = null;
364 ret = account_type_get_small_icon_path(accountTypeHandle, &pTempSmallIconPath);
365 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
367 if (pTempSmallIconPath != null)
369 pAccountProviderImpl->SetSmallIconPath(pTempSmallIconPath);
370 free(pTempSmallIconPath);
373 // Set the multiple accounts support
374 int multipleAccountsSupport = 0;
375 ret = account_type_get_multiple_account_support(accountTypeHandle, &multipleAccountsSupport);
376 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
378 pAccountProviderImpl->SetMultipleAccountsSupport(multipleAccountsSupport);
380 SetLastResult(E_SUCCESS);
381 return pAccountProvider.release();