2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 * @file AccAccountPresentationModel.cpp
19 * @brief This is the implementation file for AccountPresentationModel class.
24 #include <FGraphics.h>
28 #include "AccAccountPresentationModel.h"
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Media;
35 using namespace Tizen::Social;
37 AccountPresentationModel* AccountPresentationModel::__pInstance = null;
39 AccountPresentationModel::AccountPresentationModel()
40 : __pAccountList(null)
41 , __pAccountProviderList(null)
45 AccountPresentationModel::~AccountPresentationModel()
47 delete __pAccountList;
48 delete __pAccountProviderList;
51 AccountPresentationModel*
52 AccountPresentationModel::GetInstance(void)
55 if (__pInstance == null)
58 AppAssertf(__pInstance != null, "[E_FAILURE] Unable to initialize AccountPresentationModel.");
64 AccountPresentationModel::CreateInstance(void)
67 AccountPresentationModel* pPm = new (std::nothrow) AccountPresentationModel();
68 result r = pPm->Construct();
69 TryCatch(r == E_SUCCESS, , "[%s] Failed to construct AccountPresentationModel.", GetErrorMessage(r));
72 std::atexit(DestroyInstance);
80 AccountPresentationModel::DestroyInstance(void)
86 AccountPresentationModel::Construct(void)
90 AccountAccessor* pAccountAccessor = AccountAccessor::GetInstance();
91 pAccountAccessor->SetEventListener(this);
92 IList* pAccountList = pAccountAccessor->GetAllAccountsN();
94 __pAccountList = new (std::nothrow) ArrayList(SingleObjectDeleter);
95 __pAccountList->Construct(*pAccountList);
97 pAccountList->RemoveAll(false);
100 __pAccountProviderList = new (std::nothrow) ArrayList(SingleObjectDeleter);
101 __pAccountProviderList->Construct();
103 IList* pAccountProviderList = pAccountAccessor->GetAllAccountProvidersN();
104 IEnumerator* pEnum = pAccountProviderList->GetEnumeratorN();
105 while (pEnum->MoveNext() == E_SUCCESS)
107 AccountProvider* pAccountProvider = static_cast<AccountProvider*>(pEnum->GetCurrent());
108 AppLogDebug("AppId: %ls", pAccountProvider->GetAppId().GetPointer());
110 if (pAccountProvider->GetAppId() == L"org.tizen.email")
114 else if (!pAccountProvider->IsMultipleAccountSupported())
116 IList* pAccountList = pAccountAccessor->GetAccountsByAccountProviderN(pAccountProvider->GetAppId());
117 if (pAccountList->GetCount() > 0)
125 AppLogDebug("%ls is added to list.", pAccountProvider->GetAppId().GetPointer());
126 __pAccountProviderList->Add(new (std::nothrow) AccountProvider(*pAccountProvider));
129 delete pAccountProviderList;
134 AccountPresentationModel::AddAccountChangedEventListener(IAccountPresentationModelListener& listener)
136 return __accountListeners.Add(&listener);
140 AccountPresentationModel::RemoveAccountChangedEventListener(IAccountPresentationModelListener& listener)
142 return __accountListeners.Remove(listener);
146 AccountPresentationModel::GetAccountCount(void) const
148 AppLogDebug("Enter");
149 return __pAccountList->GetCount();
153 AccountPresentationModel::GetAccountProviderCount(void) const
155 AppLogDebug("Enter");
156 return __pAccountProviderList->GetCount();
160 AccountPresentationModel::GetAccountInfo(AccountInfoType type, int index) const
162 AppLogDebug("index: %d", index);
167 case ACCOUNT_INFO_TYPE_USER_NAME:
168 info = static_cast<Account*>(__pAccountList->GetAt(index))->GetUserName();
170 // case ACCOUNT_INFO_TYPE_CAPABILITIES:
172 // Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(index));
173 // IList* pList = pAccount->GetAccountProvider().GetCapabilitiesN();
174 // info = ParseCapability(*pList);
178 case ACCOUNT_INFO_TYPE_PROVIDER_NAME:
179 info = static_cast<Account*>(__pAccountList->GetAt(index))->GetAccountProvider().GetDisplayName();
189 AccountPresentationModel::GetAccountProviderName(int index) const
191 AppLogDebug("index: %d", index);
192 AccountProvider* pAccountProvider = static_cast<AccountProvider*>(__pAccountProviderList->GetAt(index));
193 return pAccountProvider->GetDisplayName();
196 Tizen::Graphics::Bitmap*
197 AccountPresentationModel::GetAccountIconN(int index) const
199 AppLogDebug("index: %d", index);
200 Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(index));
201 String path = pAccount->GetAccountProvider().GetIconPath();
202 return GetBitmapN(path);
205 Tizen::Graphics::Bitmap*
206 AccountPresentationModel::GetAccountProviderIconN(int index) const
208 AppLogDebug("index: %d", index);
209 String path = static_cast<AccountProvider*>(__pAccountProviderList->GetAt(index))->GetIconPath();
210 return GetBitmapN(path);
214 AccountPresentationModel::GetAccountAppId(int index) const
216 AppLogDebug("index: %d", index);
217 Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(index));
218 return pAccount->GetAccountProvider().GetAppId();
222 AccountPresentationModel::GetAccountProviderAppId(int index) const
224 AppLogDebug("index: %d", index);
225 return static_cast<AccountProvider*>(__pAccountProviderList->GetAt(index))->GetAppId();
228 Tizen::Social::AccountId
229 AccountPresentationModel::GetAccountId(int index) const
231 AppLogDebug("index: %d", index);
232 return static_cast<Account*>(__pAccountList->GetAt(index))->GetId();
236 AccountPresentationModel::SetRunningAccountProviderAppId(const Tizen::App::AppId& appId)
238 AppLogDebug("AppId: %ls", appId.GetPointer());
239 __runningAccountProviderAppId = AppId(appId);
243 AccountPresentationModel::GetRunningAccountProviderAppId(void) const
245 AppLogDebug("AppId: %ls", __runningAccountProviderAppId.GetPointer());
246 return __runningAccountProviderAppId;
250 AccountPresentationModel::OnAccountAdded(Tizen::Social::AccountId accountId)
252 AppLogDebug("AccountId: %d", accountId);
253 AccountAccessor* pAccountAccessor = AccountAccessor::GetInstance();
254 Account account = pAccountAccessor->GetAccount(accountId);
255 __pAccountList->Add(new (std::nothrow) Account(account));
257 AccountProvider accountProvider = account.GetAccountProvider();
258 if (!accountProvider.IsMultipleAccountSupported())
260 result r = __pAccountProviderList->Remove(accountProvider);
261 AppLogDebug("[%s] Try to remove accountProvider", GetErrorMessage(r));
264 FireAccountChangedEvent();
268 AccountPresentationModel::OnAccountRemoved(Tizen::Social::AccountId accountId)
270 AppLogDebug("AccountId: %d", accountId);
271 AccountAccessor* pAccountAccessor = AccountAccessor::GetInstance();
272 Account account = pAccountAccessor->GetAccount(accountId);
274 for (int i=0; i<__pAccountList->GetCount(); i++)
276 Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(i));
277 if (pAccount->GetId() == accountId)
279 result r = __pAccountList->RemoveAt(i);
280 TryReturnVoid(r == E_SUCCESS, "[%s] Failed to remove account", GetErrorMessage(r));
285 __pAccountProviderList->RemoveAll();
287 IList* pAccountProviderList = pAccountAccessor->GetAllAccountProvidersN();
288 IEnumerator* pEnum = pAccountProviderList->GetEnumeratorN();
289 while (pEnum->MoveNext() == E_SUCCESS)
291 AccountProvider* pAccountProvider = static_cast<AccountProvider*>(pEnum->GetCurrent());
292 AppLogDebug("AppId: %ls", pAccountProvider->GetAppId().GetPointer());
294 if (pAccountProvider->GetAppId() == L"org.tizen.email")
298 else if (!pAccountProvider->IsMultipleAccountSupported())
300 IList* pAccountList = pAccountAccessor->GetAccountsByAccountProviderN(pAccountProvider->GetAppId());
301 if (pAccountList->GetCount() > 0)
309 AppLogDebug("%ls is added to list.", pAccountProvider->GetAppId().GetPointer());
310 __pAccountProviderList->Add(new (std::nothrow) AccountProvider(*pAccountProvider));
313 delete pAccountProviderList;
315 FireAccountChangedEvent();
319 AccountPresentationModel::OnAccountUpdated(Tizen::Social::AccountId accountId)
321 AppLogDebug("AccountId: %d", accountId);
322 Account account = AccountAccessor::GetInstance()->GetAccount(accountId);
323 for (int i=0; i<__pAccountList->GetCount(); i++)
325 Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(i));
326 if (pAccount->GetId() == accountId)
328 result r = __pAccountList->RemoveAt(i);
329 TryReturnVoid(r == E_SUCCESS, "[%s] Failed to remove account", GetErrorMessage(r));
334 __pAccountList->Add(new Account(account));
335 FireAccountChangedEvent();
339 AccountPresentationModel::FireAccountChangedEvent(void)
341 AppLogDebug("Enter");
342 if (__accountListeners.GetCount() > 0)
344 IEnumerator* pEnum = __accountListeners.GetEnumeratorN();
345 while (pEnum->MoveNext() == E_SUCCESS)
347 static_cast<IAccountPresentationModelListener*>(pEnum->GetCurrent())->OnAccountListChanged();
353 Tizen::Graphics::Bitmap*
354 AccountPresentationModel::GetBitmapN(const Tizen::Base::String& path) const
356 Bitmap* pBitmap = null;
357 Image* pImage = new (std::nothrow) Image();
359 if (path.EndsWith(L"jpg") || path.EndsWith(L"JPG") || path.EndsWith(L"jpeg") || path.EndsWith(L"JPEG"))
361 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_RGB565);
363 else if (path.EndsWith(L"bmp") || path.EndsWith(L"BMP"))
365 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_RGB565);
367 else if (path.EndsWith(L"png") || path.EndsWith(L"PNG") || path.EndsWith(L"wbmp") || path.EndsWith(L"WBMP"))
369 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_ARGB8888);
371 else if (path.EndsWith(L"gif") || path.EndsWith(L"GIF"))
373 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_RGB565);
380 //Tizen::Base::String
381 //AccountPresentationModel::ParseCapability(const Tizen::Base::Collection::IList& capabilityList)
383 // if (capabilityList.GetCount() == 0)
385 // return L"Signed in";
388 // int otherCount = 0;
389 // String capability;
390 // IEnumerator* pEnum = capabilityList.GetEnumeratorN();
391 // while(pEnum->MoveNext() == E_SUCCESS)
393 // String value = *static_cast<String*>(pEnum->GetCurrent());
395 // if (value.StartsWith(L"http://", 0) && value.Contains(L"/account/capability/"))
397 // value.Replace(L"http://", L"");
398 // Utility::StringTokenizer tokeneizer(value, L"/");
399 // if (tokeneizer.GetTokenCount() == 4)
402 // tokeneizer.GetNextToken(token);
403 // value.Replace(token, L"");
404 // if (value.StartsWith(L"/account/capability/", 0))
406 // value.Replace(L"/account/capability/", L"");
407 // AppLogDebug("capability: %ls", value.GetPointer());
408 // capability.Append(value);
409 // capability.Append(L" ");
415 // capability.Append(L"other");
416 // if (otherCount > 0)
418 // capability.Append(otherCount);
421 // capability.Append(L" ");
424 // return capability;