a33b4cdf565cf3f0f64b2556279bfd6ae4a3afde
[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 <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"
43
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;
50
51 namespace Tizen { namespace Social
52 {
53
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"_";
57
58 bool
59 OnAccountTypeLabelReceived(char* pAppId, char* pLabel, char* pLocale, void* pUserData)
60 {
61         if (pLabel != null)
62         {
63                 String languageLocale;
64                 String convertedLanguageLocale;
65
66                 result r = SettingInfo::GetValue(String(_SETTING_LOCALE_KEY), languageLocale);
67                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
68
69                 StringTokenizer strTok(languageLocale, String(_LOCALE_DELIMETER));
70                 String token;
71
72                 strTok.GetNextToken(token);
73                 LanguageCode languageCode = Locale::StringToLanguageCode(token);
74                 convertedLanguageLocale.Append(Locale::LanguageCodeToTwoLetterLanguageCodeString(languageCode));
75                 convertedLanguageLocale.Append(String(_LOCALE_DELIMETER));
76
77                 strTok.GetNextToken(token);
78                 convertedLanguageLocale.Append(token);
79
80                 String* pDisplayName = static_cast<String*> (pUserData);
81                 if (convertedLanguageLocale.Equals(String(pLocale)))
82                 {
83                         *pDisplayName = String(pLabel);
84                 }
85                 else if((String(_DEFAULT_LOCALE)).Equals(String(pLocale)) && pDisplayName->IsEmpty())
86                 {
87                         *pDisplayName = String(pLabel);
88                 }
89         }
90
91         return true;
92 }
93
94 bool
95 OnAccountCustomDataReceived(char* pKey, char* pValue, void* pUserData)
96 {
97         if (pKey != null && pValue != null)
98         {
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));
101
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));
104
105                 HashMap* pExtendedData = static_cast<HashMap*> (pUserData);
106
107                 result r = pExtendedData->Add(pExtendedDataKey.get(), pExtendedDataValue.get());
108                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
109
110                 pExtendedDataKey.release();
111                 pExtendedDataValue.release();
112         }
113
114         return true;
115 }
116
117 bool
118 OnAccountProviderCapabilityReceived(char* pAppId, char* pKey, void* pUserData)
119 {
120         if (pKey != null)
121         {
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));
124
125                 ArrayList* pCapabilityList = static_cast<ArrayList*> (pUserData);
126
127                 result r = pCapabilityList->Add(pCapability.get());
128                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
129
130                 pCapability.release();
131         }
132
133         return true;
134 }
135
136 account_h
137 _AccountManagerUtil::CreateAccountHandleN(const Account account)
138 {
139         ClearLastResult();
140
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));
144
145         // Set the user name
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));
148
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));
151
152         // Set the package name
153         {
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));
156
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));
159
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));
162         }
163
164         // Set the extended data
165         {
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));
168
169                 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
170                 SysTryCatch(NID_SCL, pMapEnum != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
171
172                 String* pKey = null;
173                 String* pVal = null;
174
175                 while (pMapEnum->MoveNext() == E_SUCCESS)
176                 {
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));
180
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));
184
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));
188
189                         pCharArrayKey.reset();
190                         pCharArrayVal.reset();
191                 }
192         }
193
194         SetLastResult(E_SUCCESS);
195         return accountHandle;
196
197 CATCH:
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));
200
201         return null;
202 }
203
204 Account*
205 _AccountManagerUtil::ConvertAccountHandleToAccountN(account_h accountHandle)
206 {
207         ClearLastResult();
208
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));
211
212         _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(*(pAccount));
213
214         // Set the account ID
215         int accountDbId = 0;
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));
218
219         pAccountImpl->SetId(accountDbId);
220
221         // Set the account provider
222         char* pTempAppId = null;
223
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));
226
227         std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
228
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));
232
233         ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
234         if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
235         {
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);
238                 return null;
239         }
240
241         std::unique_ptr<AccountProvider> pAccountProvider(ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
242         SysTryReturn(NID_SCL, pAccountProvider != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
243
244         pAccountImpl->SetAccountProvider(*pAccountProvider);
245
246         account_type_destroy(accountTypeHandle);
247
248         // Set the user name
249         char* pTempUserName = null;
250
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));
253
254         std::unique_ptr<char[], _CharDeleter> pCharArrayUserName(pTempUserName);
255
256         result r = pAccountImpl->SetUserName(pTempUserName);
257         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
258
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));
262
263         r = pExtendedData->Construct();
264         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
265
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));
268
269         r = GetLastResult();
270         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
271
272         std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
273         SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
274
275         String* pKey = null;
276         String* pVal = null;
277
278         while (pMapEnum->MoveNext() == E_SUCCESS)
279         {
280                 pKey = static_cast<String*> (pMapEnum->GetKey());
281                 pVal = static_cast<String*> (pMapEnum->GetValue());
282
283                 r = pAccountImpl->SetExtendedData(*pKey, *pVal);
284                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
285         }
286
287         SetLastResult(E_SUCCESS);
288         return pAccount.release();
289 }
290
291 AccountProvider*
292 _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(account_type_h accountTypeHandle)
293 {
294         ClearLastResult();
295
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.");
298
299         _AccountProviderImpl* pAccountProviderImpl = _AccountProviderImpl::GetInstance(*(pAccountProvider));
300
301         // Set the app ID
302         char* pTempAppId = null;
303
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.");
306
307         std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
308
309         pAccountProviderImpl->SetAppId(pTempAppId);
310
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));
314
315         result r = pCapabilityList->Construct();
316         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pCapabilityList.", GetErrorMessage(r));
317
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));
320
321         if (pCapabilityList != null)
322         {
323                 std::unique_ptr<IEnumerator> pEnum(pCapabilityList->GetEnumeratorN());
324                 SysTryReturn(NID_SCL, pEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
325
326                 String* pTempCapability = null;
327
328                 while (pEnum->MoveNext() == E_SUCCESS)
329                 {
330                         pTempCapability = static_cast<String*> (pEnum->GetCurrent());
331
332                         r = pAccountProviderImpl->AddCapability(*pTempCapability);
333                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
334                 }
335         }
336
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.");
340
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.");
343
344         r = GetLastResult();
345         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
346
347         pAccountProviderImpl->SetDisplayName(*pDisplayName);
348
349         // Set the icon path
350         char* pTempIconPath = null;
351
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.");
354
355         if (pTempIconPath != null)
356         {
357                 pAccountProviderImpl->SetIconPath(pTempIconPath);
358                 free(pTempIconPath);
359         }
360
361         // Set the small icon path
362         char* pTempSmallIconPath = null;
363
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.");
366
367         if (pTempSmallIconPath != null)
368         {
369                 pAccountProviderImpl->SetSmallIconPath(pTempSmallIconPath);
370                 free(pTempSmallIconPath);
371         }
372
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.");
377
378         pAccountProviderImpl->SetMultipleAccountsSupport(multipleAccountsSupport);
379
380         SetLastResult(E_SUCCESS);
381         return pAccountProvider.release();
382 }
383
384 }} // Tizen::Social