2 // Copyright (c) 2013 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.
17 * @file FScl_AccountManagerUtil.cpp
18 * @brief This is implementation for the _AccountManagerUtil class.
20 * This file contains the definitions of the _AccountManagerUtil class.
24 #include <unique_ptr.h>
26 #include <FAppTypes.h>
27 #include <FBaseColHashMap.h>
28 #include <FBaseColIMap.h>
29 #include <FBaseResult.h>
30 #include <FBaseString.h>
31 #include <FBaseSysLog.h>
32 #include <FBaseUtilStringTokenizer.h>
33 #include <FBase_StringConverter.h>
34 #include <FLclLocale.h>
35 #include <FSysSettingInfo.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::Base::Utility;
47 using namespace Tizen::Locales;
48 using namespace Tizen::System;
50 namespace Tizen { namespace Social
53 static const wchar_t* _SETTING_LOCALE_KEY = L"http://tizen.org/setting/locale.language";
54 static const wchar_t* _DEFAULT_LOCALE = L"default";
55 static const wchar_t* _LOCALE_DELIMETER = L"_";
58 OnAccountTypeLabelReceived(char* pAppId, char* pLabel, char* pLocale, void* pUserData)
62 String languageLocale;
63 String convertedLanguageLocale;
65 result r = SettingInfo::GetValue(String(_SETTING_LOCALE_KEY), languageLocale);
66 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
68 StringTokenizer strTok(languageLocale, String(_LOCALE_DELIMETER));
71 strTok.GetNextToken(token);
72 LanguageCode languageCode = Locale::StringToLanguageCode(token);
73 convertedLanguageLocale.Append(Locale::LanguageCodeToTwoLetterLanguageCodeString(languageCode));
74 convertedLanguageLocale.Append(String(_LOCALE_DELIMETER));
76 strTok.GetNextToken(token);
77 convertedLanguageLocale.Append(token);
79 String* pDisplayName = static_cast<String*> (pUserData);
80 if (convertedLanguageLocale.Equals(String(pLocale)))
82 *pDisplayName = String(pLabel);
84 else if((String(_DEFAULT_LOCALE)).Equals(String(pLocale)) && pDisplayName->IsEmpty())
86 *pDisplayName = String(pLabel);
94 OnAccountCustomDataReceived(char* pKey, char* pValue, void* pUserData)
96 if (pKey != null && pValue != null)
98 std::unique_ptr<String> pExtendedDataKey(new (std::nothrow) String(pKey));
99 SysTryReturn(NID_SCL, pExtendedDataKey != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
101 std::unique_ptr<String> pExtendedDataValue(new (std::nothrow) String(pValue));
102 SysTryReturn(NID_SCL, pExtendedDataValue != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
104 HashMap* pExtendedData = static_cast<HashMap*> (pUserData);
106 result r = pExtendedData->Add(pExtendedDataKey.get(), pExtendedDataValue.get());
107 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
109 pExtendedDataKey.release();
110 pExtendedDataValue.release();
117 OnAccountProviderCapabilityReceived(char* pAppId, char* pKey, void* pUserData)
121 std::unique_ptr<String> pCapability(new (std::nothrow) String(pKey));
122 SysTryReturn(NID_SCL, pCapability != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
124 ArrayList* pCapabilityList = static_cast<ArrayList*> (pUserData);
126 result r = pCapabilityList->Add(pCapability.get());
127 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
129 pCapability.release();
136 _AccountManagerUtil::CreateAccountHandleN(const Account account)
140 account_h accountHandle = null;
141 int ret = account_create(&accountHandle);
142 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
145 std::unique_ptr<char[]> pCharArrayUserName(_StringConverter::CopyToCharArrayN(account.GetUserName()));
146 SysTryCatch(NID_SCL, pCharArrayUserName != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
148 ret = account_set_user_name(accountHandle, pCharArrayUserName.get());
149 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
151 // Set the package name
153 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
154 SysTryCatch(NID_SCL, pApp != null, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
156 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(pApp->GetAppId()));
157 SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
159 ret = account_set_package_name(accountHandle, pCharArrayAppId.get());
160 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
163 // Set the extended data
165 std::unique_ptr<IMap, AllElementsDeleter> pExtendedData(account.GetExtendedDataN());
166 SysTryCatch(NID_SCL, pExtendedData != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
168 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
169 SysTryCatch(NID_SCL, pMapEnum != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
174 while (pMapEnum->MoveNext() == E_SUCCESS)
176 pKey = static_cast<String*> (pMapEnum->GetKey());
177 std::unique_ptr<char[]> pCharArrayKey(_StringConverter::CopyToCharArrayN(*pKey));
178 SysTryCatch(NID_SCL, pCharArrayKey != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
180 pVal = static_cast<String*> (pMapEnum->GetValue());
181 std::unique_ptr<char[]> pCharArrayVal(_StringConverter::CopyToCharArrayN(*pVal));
182 SysTryCatch(NID_SCL, pCharArrayVal != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
184 ret = account_set_custom(accountHandle, pCharArrayKey.get(), pCharArrayVal.get());
185 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_OUT_OF_MEMORY, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
186 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
188 pCharArrayKey.reset();
189 pCharArrayVal.reset();
193 SetLastResult(E_SUCCESS);
194 return accountHandle;
197 ret = account_destroy(accountHandle);
198 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
204 _AccountManagerUtil::ConvertAccountHandleToAccountN(account_h accountHandle)
208 std::unique_ptr<Account> pAccount(new (std::nothrow) Account(L""));
209 SysTryReturn(NID_SCL, pAccount != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211 _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(*(pAccount));
213 // Set the account ID
215 int ret = account_get_account_id(accountHandle, &accountDbId);
216 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
218 pAccountImpl->SetId(accountDbId);
220 // Set the account provider
221 char* pTempAppId = null;
223 ret = account_get_package_name(accountHandle, &pTempAppId);
224 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));
226 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
228 account_type_h accountTypeHandle = null;
229 ret = account_type_create(&accountTypeHandle);
230 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
232 ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
233 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
235 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
236 account_type_destroy(accountTypeHandle);
240 std::unique_ptr<AccountProvider> pAccountProvider(ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
241 SysTryReturn(NID_SCL, pAccountProvider != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
243 pAccountImpl->SetAccountProvider(*pAccountProvider);
245 account_type_destroy(accountTypeHandle);
248 char* pTempUserName = null;
250 ret = account_get_user_name(accountHandle, &pTempUserName);
251 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));
253 std::unique_ptr<char[], _CharDeleter> pCharArrayUserName(pTempUserName);
255 result r = pAccountImpl->SetUserName(pTempUserName);
256 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
258 // Set the extended data
259 std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new HashMap());
260 SysTryReturn(NID_SCL, pExtendedData != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
262 r = pExtendedData->Construct();
263 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
265 ret = account_get_custom_all(accountHandle, OnAccountCustomDataReceived, pExtendedData.get());
266 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
269 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
271 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
272 SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
277 while (pMapEnum->MoveNext() == E_SUCCESS)
279 pKey = static_cast<String*> (pMapEnum->GetKey());
280 pVal = static_cast<String*> (pMapEnum->GetValue());
282 r = pAccountImpl->SetExtendedData(*pKey, *pVal);
283 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
286 SetLastResult(E_SUCCESS);
287 return pAccount.release();
291 _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(account_type_h accountTypeHandle)
295 std::unique_ptr<AccountProvider> pAccountProvider(new (std::nothrow) AccountProvider());
296 SysTryReturn(NID_SCL, pAccountProvider != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
298 _AccountProviderImpl* pAccountProviderImpl = _AccountProviderImpl::GetInstance(*(pAccountProvider));
301 char* pTempAppId = null;
303 int ret = account_type_get_app_id(accountTypeHandle, &pTempAppId);
304 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && pTempAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
306 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
308 pAccountProviderImpl->SetAppId(pTempAppId);
310 // Set the capability list
311 std::unique_ptr<ArrayList, AllElementsDeleter> pCapabilityList(new ArrayList());
312 SysTryReturn(NID_SCL, pCapabilityList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
314 result r = pCapabilityList->Construct();
315 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pCapabilityList.", GetErrorMessage(r));
317 ret = account_type_query_provider_feature_by_app_id(OnAccountProviderCapabilityReceived, pCharArrayAppId.get(), pCapabilityList.get());
318 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));
320 if (pCapabilityList != null)
322 std::unique_ptr<IEnumerator> pEnum(pCapabilityList->GetEnumeratorN());
323 SysTryReturn(NID_SCL, pEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
325 String* pTempCapability = null;
327 while (pEnum->MoveNext() == E_SUCCESS)
329 pTempCapability = static_cast<String*> (pEnum->GetCurrent());
331 r = pAccountProviderImpl->AddCapability(*pTempCapability);
332 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
336 // Set the display name
337 std::unique_ptr<String> pDisplayName(new (std::nothrow) String());
338 SysTryReturn(NID_SCL, pDisplayName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
340 ret = account_type_get_label(accountTypeHandle, OnAccountTypeLabelReceived, pDisplayName.get());
341 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
344 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
346 pAccountProviderImpl->SetDisplayName(*pDisplayName);
349 char* pTempIconPath = null;
351 ret = account_type_get_icon_path(accountTypeHandle, &pTempIconPath);
352 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
354 if (pTempIconPath != null)
356 pAccountProviderImpl->SetIconPath(pTempIconPath);
360 // Set the small icon path
361 char* pTempSmallIconPath = null;
363 ret = account_type_get_small_icon_path(accountTypeHandle, &pTempSmallIconPath);
364 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
366 if (pTempSmallIconPath != null)
368 pAccountProviderImpl->SetSmallIconPath(pTempSmallIconPath);
369 free(pTempSmallIconPath);
372 // Set the multiple accounts support
373 int multipleAccountsSupport = 0;
374 ret = account_type_get_multiple_account_support(accountTypeHandle, &multipleAccountsSupport);
375 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
377 pAccountProviderImpl->SetMultipleAccountsSupport(multipleAccountsSupport);
379 SetLastResult(E_SUCCESS);
380 return pAccountProvider.release();