fix to prevent multiple launch appcontrol.
[apps/osp/Account.git] / src / AccAccountPresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 /**
18  * @file                AccAccountPresentationModel.cpp
19  * @brief               This is the implementation file for AccountPresentationModel class.
20  */
21
22 #include <FApp.h>
23 #include <FBase.h>
24 #include <FGraphics.h>
25 #include <FMedia.h>
26 #include <FSocial.h>
27 #include "AccTypes.h"
28 #include "AccAccountPresentationModel.h"
29
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;
36
37 AccountPresentationModel* AccountPresentationModel::__pInstance = null;
38
39 AccountPresentationModel::AccountPresentationModel()
40         : __pAccountList(null)
41         , __pAccountProviderList(null)
42 {
43 }
44
45 AccountPresentationModel::~AccountPresentationModel()
46 {
47         delete __pAccountList;
48         delete __pAccountProviderList;
49 }
50
51 AccountPresentationModel*
52 AccountPresentationModel::GetInstance(void)
53 {
54         AppLogDebug("Enter");
55         if (__pInstance == null)
56         {
57                 CreateInstance();
58                 AppAssertf(__pInstance != null, "[E_FAILURE] Unable to initialize AccountPresentationModel.");
59         }
60         return __pInstance;
61 }
62
63 void
64 AccountPresentationModel::CreateInstance(void)
65 {
66         AppLogDebug("Enter");
67         AccountPresentationModel* pPm = new (std::nothrow) AccountPresentationModel();
68         result r = pPm->Construct();
69         TryCatch(r == E_SUCCESS, , "[%s] Failed to construct AccountPresentationModel.", GetErrorMessage(r));
70
71         __pInstance = pPm;
72         std::atexit(DestroyInstance);
73         return;
74
75 CATCH:
76         delete pPm;
77 }
78
79 void
80 AccountPresentationModel::DestroyInstance(void)
81 {
82         delete __pInstance;
83 }
84
85 result
86 AccountPresentationModel::Construct(void)
87 {
88         AppLogDebug("Enter");
89
90         AccountAccessor* pAccountAccessor = AccountAccessor::GetInstance();
91         pAccountAccessor->SetEventListener(this);
92         IList* pAccountList = pAccountAccessor->GetAllAccountsN();
93
94         __pAccountList = new (std::nothrow) ArrayList(SingleObjectDeleter);
95         __pAccountList->Construct(*pAccountList);
96
97         pAccountList->RemoveAll(false);
98         delete pAccountList;
99
100         __pAccountProviderList = new (std::nothrow) ArrayList(SingleObjectDeleter);
101         __pAccountProviderList->Construct();
102
103         IList* pAccountProviderList = pAccountAccessor->GetAllAccountProvidersN();
104         IEnumerator* pEnum = pAccountProviderList->GetEnumeratorN();
105         while (pEnum->MoveNext() == E_SUCCESS)
106         {
107                 AccountProvider* pAccountProvider = static_cast<AccountProvider*>(pEnum->GetCurrent());
108                 AppLogDebug("AppId: %ls", pAccountProvider->GetAppId().GetPointer());
109
110                 if (pAccountProvider->GetAppId() == L"org.tizen.email")
111                 {
112                         continue;
113                 }
114                 else if (!pAccountProvider->IsMultipleAccountSupported())
115                 {
116                         IList* pAccountList = pAccountAccessor->GetAccountsByAccountProviderN(pAccountProvider->GetAppId());
117                         if (pAccountList->GetCount() > 0)
118                         {
119                                 delete pAccountList;
120                                 continue;
121                         }
122                         delete pAccountList;
123                 }
124
125                 AppLogDebug("%ls is added to list.", pAccountProvider->GetAppId().GetPointer());
126                 __pAccountProviderList->Add(new (std::nothrow) AccountProvider(*pAccountProvider));
127         }
128         delete pEnum;
129         delete pAccountProviderList;
130         return E_SUCCESS;
131 }
132
133 result
134 AccountPresentationModel::AddAccountChangedEventListener(IAccountPresentationModelListener& listener)
135 {
136         return __accountListeners.Add(&listener);
137 }
138
139 result
140 AccountPresentationModel::RemoveAccountChangedEventListener(IAccountPresentationModelListener& listener)
141 {
142         return __accountListeners.Remove(listener);
143 }
144
145 int
146 AccountPresentationModel::GetAccountCount(void) const
147 {
148         AppLogDebug("Enter");
149         return __pAccountList->GetCount();
150 }
151
152 int
153 AccountPresentationModel::GetAccountProviderCount(void) const
154 {
155         AppLogDebug("Enter");
156         return __pAccountProviderList->GetCount();
157 }
158
159 Tizen::Base::String
160 AccountPresentationModel::GetAccountInfo(AccountInfoType type, int index) const
161 {
162         AppLogDebug("index: %d", index);
163         String info;
164
165         switch (type)
166         {
167         case ACCOUNT_INFO_TYPE_USER_NAME:
168                 info = static_cast<Account*>(__pAccountList->GetAt(index))->GetUserName();
169                 break;
170 //      case ACCOUNT_INFO_TYPE_CAPABILITIES:
171 //      {
172 //              Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(index));
173 //              IList* pList = pAccount->GetAccountProvider().GetCapabilitiesN();
174 //              info = ParseCapability(*pList);
175 //              delete pList;
176 //              break;
177 //      }
178         case ACCOUNT_INFO_TYPE_PROVIDER_NAME:
179                 info = static_cast<Account*>(__pAccountList->GetAt(index))->GetAccountProvider().GetDisplayName();
180                 break;
181         default:
182                 break;
183         }
184
185         return info;
186 }
187
188 Tizen::Base::String
189 AccountPresentationModel::GetAccountProviderName(int index) const
190 {
191         AppLogDebug("index: %d", index);
192         AccountProvider* pAccountProvider = static_cast<AccountProvider*>(__pAccountProviderList->GetAt(index));
193         return pAccountProvider->GetDisplayName();
194 }
195
196 Tizen::Graphics::Bitmap*
197 AccountPresentationModel::GetAccountIconN(int index) const
198 {
199         AppLogDebug("index: %d", index);
200         Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(index));
201         String path = pAccount->GetAccountProvider().GetIconPath();
202         return GetBitmapN(path);
203 }
204
205 Tizen::Graphics::Bitmap*
206 AccountPresentationModel::GetAccountProviderIconN(int index) const
207 {
208         AppLogDebug("index: %d", index);
209         String path = static_cast<AccountProvider*>(__pAccountProviderList->GetAt(index))->GetIconPath();
210         return GetBitmapN(path);
211 }
212
213 Tizen::App::AppId
214 AccountPresentationModel::GetAccountAppId(int index) const
215 {
216         AppLogDebug("index: %d", index);
217         Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(index));
218         return pAccount->GetAccountProvider().GetAppId();
219 }
220
221 Tizen::App::AppId
222 AccountPresentationModel::GetAccountProviderAppId(int index) const
223 {
224         AppLogDebug("index: %d", index);
225         return static_cast<AccountProvider*>(__pAccountProviderList->GetAt(index))->GetAppId();
226 }
227
228 Tizen::Social::AccountId
229 AccountPresentationModel::GetAccountId(int index) const
230 {
231         AppLogDebug("index: %d", index);
232         return static_cast<Account*>(__pAccountList->GetAt(index))->GetId();
233 }
234
235 void
236 AccountPresentationModel::SetRunningAccountProviderAppId(const Tizen::App::AppId& appId)
237 {
238         AppLogDebug("AppId: %ls", appId.GetPointer());
239         __runningAccountProviderAppId = AppId(appId);
240 }
241
242 Tizen::App::AppId
243 AccountPresentationModel::GetRunningAccountProviderAppId(void) const
244 {
245         AppLogDebug("AppId: %ls", __runningAccountProviderAppId.GetPointer());
246         return __runningAccountProviderAppId;
247 }
248
249 void
250 AccountPresentationModel::OnAccountAdded(Tizen::Social::AccountId accountId)
251 {
252         AppLogDebug("AccountId: %d", accountId);
253         AccountAccessor* pAccountAccessor = AccountAccessor::GetInstance();
254         Account account = pAccountAccessor->GetAccount(accountId);
255         __pAccountList->Add(new (std::nothrow) Account(account));
256
257         AccountProvider accountProvider = account.GetAccountProvider();
258         if (!accountProvider.IsMultipleAccountSupported())
259         {
260                 result r = __pAccountProviderList->Remove(accountProvider);
261                 AppLogDebug("[%s] Try to remove accountProvider", GetErrorMessage(r));
262         }
263
264         FireAccountChangedEvent();
265 }
266
267 void
268 AccountPresentationModel::OnAccountRemoved(Tizen::Social::AccountId accountId)
269 {
270         AppLogDebug("AccountId: %d", accountId);
271         AccountAccessor* pAccountAccessor = AccountAccessor::GetInstance();
272         Account account = pAccountAccessor->GetAccount(accountId);
273
274         for (int i=0; i<__pAccountList->GetCount(); i++)
275         {
276                 Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(i));
277                 if (pAccount->GetId() == accountId)
278                 {
279                         result r = __pAccountList->RemoveAt(i);
280                         TryReturnVoid(r == E_SUCCESS, "[%s] Failed to remove account", GetErrorMessage(r));
281                         break;
282                 }
283         }
284
285         __pAccountProviderList->RemoveAll();
286
287         IList* pAccountProviderList = pAccountAccessor->GetAllAccountProvidersN();
288         IEnumerator* pEnum = pAccountProviderList->GetEnumeratorN();
289         while (pEnum->MoveNext() == E_SUCCESS)
290         {
291                 AccountProvider* pAccountProvider = static_cast<AccountProvider*>(pEnum->GetCurrent());
292                 AppLogDebug("AppId: %ls", pAccountProvider->GetAppId().GetPointer());
293
294                 if (pAccountProvider->GetAppId() == L"org.tizen.email")
295                 {
296                         continue;
297                 }
298                 else if (!pAccountProvider->IsMultipleAccountSupported())
299                 {
300                         IList* pAccountList = pAccountAccessor->GetAccountsByAccountProviderN(pAccountProvider->GetAppId());
301                         if (pAccountList->GetCount() > 0)
302                         {
303                                 delete pAccountList;
304                                 continue;
305                         }
306                         delete pAccountList;
307                 }
308
309                 AppLogDebug("%ls is added to list.", pAccountProvider->GetAppId().GetPointer());
310                 __pAccountProviderList->Add(new (std::nothrow) AccountProvider(*pAccountProvider));
311         }
312         delete pEnum;
313         delete pAccountProviderList;
314
315         FireAccountChangedEvent();
316 }
317
318 void
319 AccountPresentationModel::OnAccountUpdated(Tizen::Social::AccountId accountId)
320 {
321         AppLogDebug("AccountId: %d", accountId);
322         Account account = AccountAccessor::GetInstance()->GetAccount(accountId);
323         for (int i=0; i<__pAccountList->GetCount(); i++)
324         {
325                 Account* pAccount = static_cast<Account*>(__pAccountList->GetAt(i));
326                 if (pAccount->GetId() == accountId)
327                 {
328                         result r = __pAccountList->RemoveAt(i);
329                         TryReturnVoid(r == E_SUCCESS, "[%s] Failed to remove account", GetErrorMessage(r));
330                         break;
331                 }
332         }
333
334         __pAccountList->Add(new Account(account));
335         FireAccountChangedEvent();
336 }
337
338 void
339 AccountPresentationModel::FireAccountChangedEvent(void)
340 {
341         AppLogDebug("Enter");
342         if (__accountListeners.GetCount() > 0)
343         {
344                 IEnumerator* pEnum = __accountListeners.GetEnumeratorN();
345                 while (pEnum->MoveNext() == E_SUCCESS)
346                 {
347                         static_cast<IAccountPresentationModelListener*>(pEnum->GetCurrent())->OnAccountListChanged();
348                 }
349                 delete pEnum;
350         }
351 }
352
353 Tizen::Graphics::Bitmap*
354 AccountPresentationModel::GetBitmapN(const Tizen::Base::String& path) const
355 {
356         Bitmap* pBitmap = null;
357         Image* pImage = new (std::nothrow) Image();
358         pImage->Construct();
359         if (path.EndsWith(L"jpg") || path.EndsWith(L"JPG") || path.EndsWith(L"jpeg") || path.EndsWith(L"JPEG"))
360         {
361                 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_RGB565);
362         }
363         else if (path.EndsWith(L"bmp") || path.EndsWith(L"BMP"))
364         {
365                 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_RGB565);
366         }
367         else if (path.EndsWith(L"png") || path.EndsWith(L"PNG") || path.EndsWith(L"wbmp") || path.EndsWith(L"WBMP"))
368         {
369                 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_ARGB8888);
370         }
371         else if (path.EndsWith(L"gif") || path.EndsWith(L"GIF"))
372         {
373                 pBitmap = pImage->DecodeN(path, BITMAP_PIXEL_FORMAT_RGB565);
374         }
375
376         delete pImage;
377         return pBitmap;
378 }
379
380 //Tizen::Base::String
381 //AccountPresentationModel::ParseCapability(const Tizen::Base::Collection::IList& capabilityList)
382 //{
383 //      if (capabilityList.GetCount() == 0)
384 //      {
385 //              return L"Signed in";
386 //      }
387 //
388 //      int otherCount = 0;
389 //      String capability;
390 //      IEnumerator* pEnum = capabilityList.GetEnumeratorN();
391 //      while(pEnum->MoveNext() == E_SUCCESS)
392 //      {
393 //              String value = *static_cast<String*>(pEnum->GetCurrent());
394 //
395 //              if (value.StartsWith(L"http://", 0) && value.Contains(L"/account/capability/"))
396 //              {
397 //                      value.Replace(L"http://", L"");
398 //                      Utility::StringTokenizer tokeneizer(value, L"/");
399 //                      if (tokeneizer.GetTokenCount() == 4)
400 //                      {
401 //                              String token;
402 //                              tokeneizer.GetNextToken(token);
403 //                              value.Replace(token, L"");
404 //                              if (value.StartsWith(L"/account/capability/", 0))
405 //                              {
406 //                                      value.Replace(L"/account/capability/", L"");
407 //                                      AppLogDebug("capability: %ls", value.GetPointer());
408 //                                      capability.Append(value);
409 //                                      capability.Append(L" ");
410 //                                      continue;
411 //                              }
412 //                      }
413 //              }
414 //
415 //              capability.Append(L"other");
416 //              if (otherCount > 0)
417 //              {
418 //                      capability.Append(otherCount);
419 //              }
420 //              otherCount++;
421 //              capability.Append(L" ");
422 //      }
423 //      delete pEnum;
424 //      return capability;
425 //}