[N_SE-32914, 33582] Fixed priority converting codes
[framework/osp/social.git] / src / FScl_AccountManagerUtil.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file                FScl_AccountManagerUtil.cpp
19  * @brief       This is implementation for the _AccountManagerUtil class.
20  *
21  * This file contains the definitions of the _AccountManagerUtil class.
22  */
23
24 #include <new>
25 #include <unique_ptr.h>
26 #include <FAppApp.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"
42
43 using namespace Tizen::App;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Locales;
47
48 namespace Tizen { namespace Social
49 {
50
51 static const wchar_t* _DEFAULT_LOCALE = L"en_GB";
52
53 bool
54 OnAccountTypeLabelReceived(char* pAppId, char* pLabel, char* pLocale, void* pUserData)
55 {
56         if (pLabel != null)
57         {
58                 LocaleManager localeManager;
59                 Locale systemLocale = localeManager.GetSystemLocale();
60                 SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
61
62                 LanguageCode languageCode = systemLocale.GetLanguageCode();
63                 String localeStr(Locale::LanguageCodeToTwoLetterLanguageCodeString(languageCode));
64
65                 String countryCodeStr(systemLocale.GetCountryCodeString());
66                 localeStr.Append(L"_");
67                 localeStr.Append(countryCodeStr);
68
69                 String* pDisplayName = static_cast<String*> (pUserData);
70                 if (localeStr.Equals(String(pLocale)))
71                 {
72                         *pDisplayName = String(pLabel);
73                 }
74                 else if((String(_DEFAULT_LOCALE)).Equals(String(pLocale)) && pDisplayName->IsEmpty())
75                 {
76                         *pDisplayName = String(pLabel);
77                 }
78         }
79
80         return true;
81 }
82
83 bool
84 OnAccountCustomDataReceived(char* pKey, char* pValue, void* pUserData)
85 {
86         if (pKey != null && pValue != null)
87         {
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));
90
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));
93
94                 HashMap* pExtendedData = static_cast<HashMap*> (pUserData);
95
96                 result r = pExtendedData->Add(pExtendedDataKey.get(), pExtendedDataValue.get());
97                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
98
99                 pExtendedDataKey.release();
100                 pExtendedDataValue.release();
101         }
102
103         return true;
104 }
105
106 bool
107 OnAccountProviderCapabilityReceived(char* pAppId, char* pKey, void* pUserData)
108 {
109         if (pKey != null)
110         {
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));
113
114                 ArrayList* pCapabilityList = static_cast<ArrayList*> (pUserData);
115
116                 result r = pCapabilityList->Add(pCapability.get());
117                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
118
119                 pCapability.release();
120         }
121
122         return true;
123 }
124
125 account_h
126 _AccountManagerUtil::CreateAccountHandleN(const Account account)
127 {
128         ClearLastResult();
129
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));
133
134         // Set the user name
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));
137
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));
140
141         // Set the package name
142         {
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));
145
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));
148
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));
151         }
152
153         // Set the extended data
154         {
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));
157
158                 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
159                 SysTryCatch(NID_SCL, pMapEnum != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
160
161                 String* pKey = null;
162                 String* pVal = null;
163
164                 while (pMapEnum->MoveNext() == E_SUCCESS)
165                 {
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));
169
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));
173
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));
177
178                         pCharArrayKey.reset();
179                         pCharArrayVal.reset();
180                 }
181         }
182
183         SetLastResult(E_SUCCESS);
184         return accountHandle;
185
186 CATCH:
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));
189
190         return null;
191 }
192
193 Account*
194 _AccountManagerUtil::ConvertAccountHandleToAccountN(account_h accountHandle)
195 {
196         ClearLastResult();
197
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));
200
201         _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(*(pAccount));
202
203         // Set the account ID
204         int accountDbId = 0;
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));
207
208         pAccountImpl->SetId(accountDbId);
209
210         // Set the account provider
211         char* pTempAppId = null;
212
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));
215
216         std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
217
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));
221
222         ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
223         if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
224         {
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);
227                 return null;
228         }
229
230         std::unique_ptr<AccountProvider> pAccountProvider(ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
231         SysTryReturn(NID_SCL, pAccountProvider != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
232
233         pAccountImpl->SetAccountProvider(*pAccountProvider);
234
235         account_type_destroy(accountTypeHandle);
236
237         // Set the user name
238         char* pTempUserName = null;
239
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));
242
243         std::unique_ptr<char[], _CharDeleter> pCharArrayUserName(pTempUserName);
244
245         result r = pAccountImpl->SetUserName(pTempUserName);
246         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
247
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));
251
252         r = pExtendedData->Construct();
253         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
254
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));
257
258         r = GetLastResult();
259         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
260
261         std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
262         SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
263
264         String* pKey = null;
265         String* pVal = null;
266
267         while (pMapEnum->MoveNext() == E_SUCCESS)
268         {
269                 pKey = static_cast<String*> (pMapEnum->GetKey());
270                 pVal = static_cast<String*> (pMapEnum->GetValue());
271
272                 r = pAccountImpl->SetExtendedData(*pKey, *pVal);
273                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
274         }
275
276         SetLastResult(E_SUCCESS);
277         return pAccount.release();
278 }
279
280 AccountProvider*
281 _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(account_type_h accountTypeHandle)
282 {
283         ClearLastResult();
284
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.");
287
288         _AccountProviderImpl* pAccountProviderImpl = _AccountProviderImpl::GetInstance(*(pAccountProvider));
289
290         // Set the app ID
291         char* pTempAppId = null;
292
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.");
295
296         std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
297
298         pAccountProviderImpl->SetAppId(pTempAppId);
299
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));
303
304         result r = pCapabilityList->Construct();
305         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pCapabilityList.", GetErrorMessage(r));
306
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));
309
310         if (pCapabilityList != null)
311         {
312                 std::unique_ptr<IEnumerator> pEnum(pCapabilityList->GetEnumeratorN());
313                 SysTryReturn(NID_SCL, pEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
314
315                 String* pTempCapability = null;
316
317                 while (pEnum->MoveNext() == E_SUCCESS)
318                 {
319                         pTempCapability = static_cast<String*> (pEnum->GetCurrent());
320
321                         r = pAccountProviderImpl->AddCapability(*pTempCapability);
322                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
323                 }
324         }
325
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.");
329
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.");
332
333         r = GetLastResult();
334         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
335
336         pAccountProviderImpl->SetDisplayName(*pDisplayName);
337
338         // Set the icon path
339         char* pTempIconPath = null;
340
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.");
343
344         if (pTempIconPath != null)
345         {
346                 pAccountProviderImpl->SetIconPath(pTempIconPath);
347                 free(pTempIconPath);
348         }
349
350         // Set the small icon path
351         char* pTempSmallIconPath = null;
352
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.");
355
356         if (pTempSmallIconPath != null)
357         {
358                 pAccountProviderImpl->SetSmallIconPath(pTempSmallIconPath);
359                 free(pTempSmallIconPath);
360         }
361
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.");
366
367         pAccountProviderImpl->SetMultipleAccountsSupport(multipleAccountsSupport);
368
369         SetLastResult(E_SUCCESS);
370         return pAccountProvider.release();
371 }
372
373 }} // Tizen::Social