Initialize Tizen 2.3
[framework/osp/social.git] / src / FScl_AccountManagerUtil.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 /**
17  * @file                FScl_AccountManagerUtil.cpp
18  * @brief       This is implementation for the _AccountManagerUtil class.
19  *
20  * This file contains the definitions of the _AccountManagerUtil class.
21  */
22
23 #include <new>
24 #include <unique_ptr.h>
25 #include <FAppApp.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"
42
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;
49
50 namespace Tizen { namespace Social
51 {
52
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"_";
56
57 bool
58 OnAccountTypeLabelReceived(char* pAppId, char* pLabel, char* pLocale, void* pUserData)
59 {
60         if (pLabel != null)
61         {
62                 String languageLocale;
63                 String convertedLanguageLocale;
64
65                 result r = SettingInfo::GetValue(String(_SETTING_LOCALE_KEY), languageLocale);
66                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
67
68                 StringTokenizer strTok(languageLocale, String(_LOCALE_DELIMETER));
69                 String token;
70
71                 strTok.GetNextToken(token);
72                 LanguageCode languageCode = Locale::StringToLanguageCode(token);
73                 convertedLanguageLocale.Append(Locale::LanguageCodeToTwoLetterLanguageCodeString(languageCode));
74                 convertedLanguageLocale.Append(String(_LOCALE_DELIMETER));
75
76                 strTok.GetNextToken(token);
77                 convertedLanguageLocale.Append(token);
78
79                 String* pDisplayName = static_cast<String*> (pUserData);
80                 if (convertedLanguageLocale.Equals(String(pLocale)))
81                 {
82                         *pDisplayName = String(pLabel);
83                 }
84                 else if((String(_DEFAULT_LOCALE)).Equals(String(pLocale)) && pDisplayName->IsEmpty())
85                 {
86                         *pDisplayName = String(pLabel);
87                 }
88         }
89
90         return true;
91 }
92
93 bool
94 OnAccountCustomDataReceived(char* pKey, char* pValue, void* pUserData)
95 {
96         if (pKey != null && pValue != null)
97         {
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));
100
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));
103
104                 HashMap* pExtendedData = static_cast<HashMap*> (pUserData);
105
106                 result r = pExtendedData->Add(pExtendedDataKey.get(), pExtendedDataValue.get());
107                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
108
109                 pExtendedDataKey.release();
110                 pExtendedDataValue.release();
111         }
112
113         return true;
114 }
115
116 bool
117 OnAccountProviderCapabilityReceived(char* pAppId, char* pKey, void* pUserData)
118 {
119         if (pKey != null)
120         {
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));
123
124                 ArrayList* pCapabilityList = static_cast<ArrayList*> (pUserData);
125
126                 result r = pCapabilityList->Add(pCapability.get());
127                 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
128
129                 pCapability.release();
130         }
131
132         return true;
133 }
134
135 account_h
136 _AccountManagerUtil::CreateAccountHandleN(const Account account)
137 {
138         ClearLastResult();
139
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));
143
144         // Set the user name
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));
147
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));
150
151         // Set the package name
152         {
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));
155
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));
158
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));
161         }
162
163         // Set the extended data
164         {
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));
167
168                 std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
169                 SysTryCatch(NID_SCL, pMapEnum != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
170
171                 String* pKey = null;
172                 String* pVal = null;
173
174                 while (pMapEnum->MoveNext() == E_SUCCESS)
175                 {
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));
179
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));
183
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));
187
188                         pCharArrayKey.reset();
189                         pCharArrayVal.reset();
190                 }
191         }
192
193         SetLastResult(E_SUCCESS);
194         return accountHandle;
195
196 CATCH:
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));
199
200         return null;
201 }
202
203 Account*
204 _AccountManagerUtil::ConvertAccountHandleToAccountN(account_h accountHandle)
205 {
206         ClearLastResult();
207
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));
210
211         _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(*(pAccount));
212
213         // Set the account ID
214         int accountDbId = 0;
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));
217
218         pAccountImpl->SetId(accountDbId);
219
220         // Set the account provider
221         char* pTempAppId = null;
222
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));
225
226         std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
227
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));
231
232         ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
233         if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
234         {
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);
237                 return null;
238         }
239
240         std::unique_ptr<AccountProvider> pAccountProvider(ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
241         SysTryReturn(NID_SCL, pAccountProvider != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
242
243         pAccountImpl->SetAccountProvider(*pAccountProvider);
244
245         account_type_destroy(accountTypeHandle);
246
247         // Set the user name
248         char* pTempUserName = null;
249
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));
252
253         std::unique_ptr<char[], _CharDeleter> pCharArrayUserName(pTempUserName);
254
255         result r = pAccountImpl->SetUserName(pTempUserName);
256         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
257
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));
261
262         r = pExtendedData->Construct();
263         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
264
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));
267
268         r = GetLastResult();
269         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
270
271         std::unique_ptr<IMapEnumerator> pMapEnum(pExtendedData->GetMapEnumeratorN());
272         SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
273
274         String* pKey = null;
275         String* pVal = null;
276
277         while (pMapEnum->MoveNext() == E_SUCCESS)
278         {
279                 pKey = static_cast<String*> (pMapEnum->GetKey());
280                 pVal = static_cast<String*> (pMapEnum->GetValue());
281
282                 r = pAccountImpl->SetExtendedData(*pKey, *pVal);
283                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
284         }
285
286         SetLastResult(E_SUCCESS);
287         return pAccount.release();
288 }
289
290 AccountProvider*
291 _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(account_type_h accountTypeHandle)
292 {
293         ClearLastResult();
294
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.");
297
298         _AccountProviderImpl* pAccountProviderImpl = _AccountProviderImpl::GetInstance(*(pAccountProvider));
299
300         // Set the app ID
301         char* pTempAppId = null;
302
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.");
305
306         std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
307
308         pAccountProviderImpl->SetAppId(pTempAppId);
309
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));
313
314         result r = pCapabilityList->Construct();
315         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pCapabilityList.", GetErrorMessage(r));
316
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));
319
320         if (pCapabilityList != null)
321         {
322                 std::unique_ptr<IEnumerator> pEnum(pCapabilityList->GetEnumeratorN());
323                 SysTryReturn(NID_SCL, pEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
324
325                 String* pTempCapability = null;
326
327                 while (pEnum->MoveNext() == E_SUCCESS)
328                 {
329                         pTempCapability = static_cast<String*> (pEnum->GetCurrent());
330
331                         r = pAccountProviderImpl->AddCapability(*pTempCapability);
332                         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
333                 }
334         }
335
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.");
339
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.");
342
343         r = GetLastResult();
344         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
345
346         pAccountProviderImpl->SetDisplayName(*pDisplayName);
347
348         // Set the icon path
349         char* pTempIconPath = null;
350
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.");
353
354         if (pTempIconPath != null)
355         {
356                 pAccountProviderImpl->SetIconPath(pTempIconPath);
357                 free(pTempIconPath);
358         }
359
360         // Set the small icon path
361         char* pTempSmallIconPath = null;
362
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.");
365
366         if (pTempSmallIconPath != null)
367         {
368                 pAccountProviderImpl->SetSmallIconPath(pTempSmallIconPath);
369                 free(pTempSmallIconPath);
370         }
371
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.");
376
377         pAccountProviderImpl->SetMultipleAccountsSupport(multipleAccountsSupport);
378
379         SetLastResult(E_SUCCESS);
380         return pAccountProvider.release();
381 }
382
383 }} // Tizen::Social