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 HmHomePresentationModel.cpp
19 * @brief Keeps implementation of the HmHomePresentationModel
20 * Implements the HmHomePresentationModel class, it keeps the data required to show the application icons in the home view
26 #include "HmHomeItemInfo.h"
27 #include "HmHomePresentationModel.h"
29 using namespace Tizen::App;
30 using namespace Tizen::App::Package;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Io;
35 HomePresentationModel::HomePresentationModel(void)
36 : __pDataBaseManager(null)
37 , __pDataListener(null)
42 , __pPackageInfoRetrieveThread(null)
44 //empty implementation
47 HomePresentationModel::~HomePresentationModel(void)
49 __pPackMgr->RemovePackageInstallationEventListener(*this);
51 if (__pPackageInfoRetrieveThread != null)
53 __pPackageInfoRetrieveThread->Join();
54 delete __pPackageInfoRetrieveThread;
55 __pPackageInfoRetrieveThread = null;
58 if (__pAppHashMap != null)
60 IList* pKeysList = __pAppHashMap->GetKeysN();
62 for (int keyCount = 0; keyCount < pKeysList->GetCount(); keyCount++)
64 ArrayList* pAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keyCount))));
68 pAppList->RemoveAll(true);
72 pKeysList->RemoveAll();
74 __pAppHashMap->RemoveAll(true);
79 __pDataListener = null;
83 HomePresentationModel::Construct(void)
87 __pPackageInfoRetrieveThread = new (std::nothrow) PackageInfoListRetrieveThread(this);
88 r = __pPackageInfoRetrieveThread->Construct();
89 TryCatch(r == E_SUCCESS, , "__pPackageInfoRetrieveThread->Construct()", GetErrorMessage(r));
90 __pPackMgr = PackageManager::GetInstance();
91 __pPackMgr->AddPackageInstallationEventListener(*this);
92 r = __pPackageInfoRetrieveThread->SetPackageType();
100 HomePresentationModel::GetItem(int pageNumber, int index) const
102 ArrayList* pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
103 if (pAppsList != null)
105 return static_cast<HomeItemInfo*>(pAppsList->GetAt(index));
111 HomePresentationModel::GetItemCount(int pageNumber) const
113 if (__pAppHashMap != null)
115 ArrayList* pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
117 if (pAppsList != null)
119 return pAppsList->GetCount();
126 HomePresentationModel::GetPageCount(void) const
132 HomePresentationModel::LanguageChanged(void)
134 IList* pAppsValues = null;
135 IEnumerator* pMapEnum = null;
136 TryCatch(__pAppHashMap != null, , "__pAppHashMap is null");
137 pAppsValues = __pAppHashMap->GetValuesN();
138 TryCatch(pAppsValues != null, , "pAppHashMap->GetValuesN returned null");
139 pMapEnum = pAppsValues->GetEnumeratorN();
140 TryCatch(pMapEnum != null, , "pAppsValues->GetEnumeratorN() returned null");
141 __pPackMgr = PackageManager::GetInstance();
143 while (pMapEnum->MoveNext() == E_SUCCESS)
145 ArrayList* pAppsArrayList = static_cast<ArrayList*>(pMapEnum->GetCurrent());
147 if (pAppsArrayList != null)
149 IEnumerator* pAppsEnum = pAppsArrayList->GetEnumeratorN();
153 while (pAppsEnum->MoveNext() == E_SUCCESS)
155 HomeItemInfo* pAppInfo = static_cast<HomeItemInfo*>(pAppsEnum->GetCurrent());
157 if (pAppInfo != null)
159 PackageInfo* pPackInfo = __pPackMgr->GetPackageInfoN(pAppInfo->GetPackageId());
161 if (pPackInfo != null)
163 PackageAppInfo* pPackageAppInfo = pPackInfo->GetPackageAppInfoN(pAppInfo->GetAppId());
165 if (pPackageAppInfo != null)
167 String appNewName = pPackageAppInfo->GetAppDisplayName();
169 if (pAppInfo->GetAppName().CompareTo(appNewName) != 0)
171 pAppInfo->SetApplicationName(appNewName);
172 pAppInfo->SetApplicationIcon(null);
175 delete pPackageAppInfo;
176 pPackageAppInfo = null;
192 if (pMapEnum != null)
197 if (pAppsValues != null)
199 pAppsValues->RemoveAll(false);
206 HomePresentationModel::RearrangeItems(HomeItemInfo* pMovedAppInfo, int newPageNumber, int newIndex)
208 ArrayList* pMovedToAppList = null;
209 ArrayList* pMovedFromAppList = null;
210 ArrayList* pPagesToUpdate = null;
211 Integer* pPageToUpdate = null;
212 result r = E_SUCCESS;
213 int movedPageNumber = 0;
214 int movedAppIndex = 0;
215 // gets the list of applications for the current page
216 pMovedToAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(newPageNumber)));
217 pMovedAppInfo->GetPositionInHomeScreen(movedPageNumber, movedAppIndex);
218 pMovedFromAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(movedPageNumber)));
220 if ((newPageNumber == movedPageNumber) && (newIndex == movedAppIndex))
225 pPagesToUpdate = new ArrayList();
226 pPagesToUpdate->Construct();
227 pPageToUpdate = new (std::nothrow) Integer(movedPageNumber);
228 r = pPagesToUpdate->Add(pPageToUpdate);
232 delete pPageToUpdate;
233 pPageToUpdate = null;
237 if (movedPageNumber != newPageNumber)
239 pPageToUpdate = new (std::nothrow) Integer(newPageNumber);
240 r = pPagesToUpdate->Add(pPageToUpdate);
244 delete pPageToUpdate;
245 pPageToUpdate = null;
249 if (pMovedToAppList != null && pMovedFromAppList != null)
252 if (movedPageNumber == newPageNumber)
255 if (newIndex != -1 && movedAppIndex != newIndex)
258 if (newIndex - movedAppIndex == 1)
260 pMovedToAppList->InsertAt(pMovedAppInfo, newIndex + 1);
264 pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
267 if (newIndex > movedAppIndex)
269 pMovedFromAppList->RemoveAt(movedAppIndex);
273 pMovedFromAppList->RemoveAt(movedAppIndex + 1);
276 UpdatePositions(*pMovedFromAppList);
281 int numberOfIconsInPage = pMovedToAppList->GetCount();
283 if (numberOfIconsInPage < MAX_ICONS_PER_PAGE)
285 pMovedAppInfo->SetPositionInHomeScreen(newPageNumber, newIndex);
286 pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
287 pMovedFromAppList->RemoveAt(movedAppIndex, false);
288 UpdatePositions(*pMovedFromAppList);
289 UpdatePositions(*pMovedToAppList);
291 else if (numberOfIconsInPage >= MAX_ICONS_PER_PAGE)
293 bool foundLocation = false;
294 int nextPageNumber = 0;
295 ArrayList* pNextPageAppList = null;
296 HomeItemInfo* pItemToGetANewLocationFor = static_cast<HomeItemInfo*>(pMovedToAppList->GetAt(pMovedToAppList->GetCount() - 1));
297 pMovedToAppList->Remove(*pItemToGetANewLocationFor, false);
298 pMovedAppInfo->SetPositionInHomeScreen(newPageNumber, newIndex);
299 pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
300 pMovedFromAppList->Remove(*pMovedAppInfo, false);
302 if (pItemToGetANewLocationFor != null)
304 nextPageNumber = newPageNumber + 1;
305 // checks for the successor page having the number of icons not exceeding its maximum limit
307 while (nextPageNumber <= __totalPages)
309 pNextPageAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(nextPageNumber)));
311 if (pNextPageAppList->GetCount() < MAX_ICONS_PER_PAGE /*|| nextPageNumber == pageNumber*/)
313 pItemToGetANewLocationFor->SetPositionInHomeScreen(nextPageNumber, pNextPageAppList->GetCount());
314 pNextPageAppList->Add(pItemToGetANewLocationFor);
315 UpdatePositions(*pNextPageAppList);
316 foundLocation = true;
317 pPageToUpdate = new Integer(nextPageNumber);
318 pPagesToUpdate->Add(pPageToUpdate);
325 if (!foundLocation && __totalPages < MAX_PAGE_COUNT)
327 result r = E_SUCCESS;
328 Integer* pPageNumber = null;
329 ArrayList* pNewAppList = new (std::nothrow) ArrayList();
330 pNewAppList->Construct();
333 pItemToGetANewLocationFor->SetPositionInHomeScreen(__totalPages, 0);
334 pNewAppList->Add(pItemToGetANewLocationFor);
335 pPageNumber = new (std::nothrow) Integer(__totalPages);
336 r = __pAppHashMap->Add(pPageNumber, pNewAppList);
337 pPagesToUpdate->Add(new (std::nothrow) Integer(__totalPages));
347 pPageToUpdate = new Integer(__totalPages);
348 pPagesToUpdate->Add(pPageToUpdate);
349 foundLocation = true;
352 if (pNextPageAppList != null)
354 UpdatePositions(*pNextPageAppList);
357 UpdatePositions(*pMovedFromAppList);
358 UpdatePositions(*pMovedToAppList);
365 __pDataListener->OnUpdatePageRequestedN(pPagesToUpdate);
370 HomePresentationModel::SetHomePackageEventListener(IHomePresentationModelEventListener* pListener)
372 __pDataListener = pListener;
376 HomePresentationModel::UpdatePageData(void)
378 result r = E_INVALID_ARG;
380 if (__pAppHashMap != null)
382 IList* pKeys = __pAppHashMap->GetKeysN();
386 for (int pageCount = 0; pageCount < pKeys->GetCount(); pageCount++)
388 ArrayList* pPageApps = (ArrayList*) __pAppHashMap->GetValue(*pKeys->GetAt(pageCount));
390 if (pPageApps != null)
393 for (int appsCount = 0; appsCount < pPageApps->GetCount(); appsCount++)
395 HomeItemInfo* pItemInfo = static_cast<HomeItemInfo*>(pPageApps->GetAt(appsCount));
397 if (pItemInfo != null)
399 r = UpdateDataToDatabase(static_cast<HomeItemInfo*>(pItemInfo));
405 pKeys->RemoveAll(false);
415 HomePresentationModel::HomeApplicationsComparer::Compare(const Object& obj1, const Object& obj2, int& cmp) const
417 result r = E_FAILURE;
418 const HomeItemInfo* pObj1 = static_cast<const HomeItemInfo*>(&obj1);
419 const HomeItemInfo* pObj2 = static_cast<const HomeItemInfo*>(&obj2);
421 if (pObj1 != null && pObj2 != null)
426 pObj1->GetPositionInHomeScreen(pageNumber, pageIndex1);
427 pObj2->GetPositionInHomeScreen(pageNumber, pageIndex2);
429 cmp = Integer::Compare(pageIndex1, pageIndex2);
437 HomePresentationModel::OnPackageInstallationCompleted(const PackageId& packageId, PackageInstallationResult installationResult)
439 if (installationResult == PACKAGE_INSTALLATION_RESULT_SUCCESS)
441 NotifyInstallationEvent(packageId);
451 HomePresentationModel::OnPackageUninstallationCompleted(const PackageId& packageId, bool uninstallationResult)
453 ArrayList* pAppsList = null;
454 HomeItemInfo* pItemInfo = null;
456 if (uninstallationResult)
458 if (__pDataListener != null && !packageId.IsEmpty())
461 pItemInfo = FindAppObjectInList(packageId);
464 if (pItemInfo != null)
468 pItemInfo->GetPositionInHomeScreen(pageNumber, appIndex);
469 pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
470 pAppsList->Remove(*pItemInfo);
471 UpdatePositions(*pAppsList);
472 if (pItemInfo->IsVisibleInMainMenu())
474 __pDataListener->OnUnInstallationCompleted(*pItemInfo);
477 if (pItemInfo != null)
482 pItemInfo = FindAppObjectInList(packageId);
485 while (pItemInfo != null);
497 HomePresentationModel::OnPackageInfoRetrievedN(Tizen::Base::Collection::HashMap* pAppsList, int totalApps, int totalPages)
499 UiApp* pApp = UiApp::GetInstance();
501 if (pAppsList != null && totalApps != 0)
503 result r = E_SUCCESS;
504 __pAppHashMap = pAppsList;
505 __totalPages = totalPages;
506 __totalApps = totalApps;
507 r = PrepareApplicationsList();
511 ArrayList* pListToSend = new (std::nothrow) ArrayList();
513 if (pListToSend != null)
515 r = pListToSend->Construct();
519 Integer* pResToSend = new (std::nothrow) Integer(r);
520 r = pListToSend->Add(pResToSend);
524 r = pApp->SendUserEvent(MODEL_REQUEST_INITIALIZE, pListToSend);
539 pApp->SendUserEvent(MODEL_REQUEST_INITIALIZE, null);
546 HomePresentationModel::FindAppObjectInList(const Tizen::Base::String& packageIdToFind)
548 IList* pKeysList = __pAppHashMap->GetKeysN();
550 if (pKeysList != null)
552 for (int keysCount = 0; keysCount < pKeysList->GetCount(); keysCount++)
554 ArrayList* pList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keysCount))));
556 for (int itemCount = (pList->GetCount() - 1); itemCount >= 0; itemCount--)
558 HomeItemInfo* pApplicationInfo = static_cast<HomeItemInfo*>(pList->GetAt(itemCount));
560 if (pApplicationInfo != null)
562 String packageId = pApplicationInfo->GetPackageId();
564 if (packageId.CompareTo(packageIdToFind) == 0)
566 pKeysList->RemoveAll();
568 return static_cast<HomeItemInfo*>(pApplicationInfo);
573 pKeysList->RemoveAll();
583 HomePresentationModel::InitializeDatabase(void)
585 result r = E_SUCCESS;
587 __pDataBaseManager = GenericDatabaseManager::GetInstance();
589 if (__pDataBaseManager != null)
591 ArrayList* pColumnNames = null;
592 ArrayList* pColumnTypes = null;
593 String* pTempColumnData = null;
595 String databaseName = App::GetInstance()->GetAppRootPath();
596 databaseName.Append(HOME_DATABASE_NAME);
598 r = __pDataBaseManager->CreateDataBase(databaseName);
599 TryCatch(r == E_SUCCESS, ,
600 "Home_Exception::failed to create Database");
601 pColumnNames = new (std::nothrow) ArrayList();
602 pColumnNames->Construct();
603 pColumnTypes = new (std::nothrow) ArrayList();
604 pColumnTypes->Construct();
606 //Creates table homeapps
607 pTempColumnData = new (std::nothrow) String(APPS_RECORD_ID);
608 r = pColumnNames->Add(pTempColumnData);
609 TryCatch(r == E_SUCCESS, delete pTempColumnData;
610 pTempColumnData = null,
611 "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
612 pTempColumnData = new (std::nothrow) String(DATATYPE_INTEGER_PRIMARY);
613 r = pColumnTypes->Add(pTempColumnData);
614 TryCatch(r == E_SUCCESS, delete pTempColumnData;
615 pTempColumnData = null,
616 "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
618 pTempColumnData = new (std::nothrow) String(APPS_APP_ID);
619 r = pColumnNames->Add(pTempColumnData);
620 TryCatch(r == E_SUCCESS, delete pTempColumnData;
621 pTempColumnData = null,
622 "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
623 pTempColumnData = new (std::nothrow) String(DATATYPE_TEXT);
624 pTempColumnData->Append(DATATYPE_UNIQUE);
625 r = pColumnTypes->Add(pTempColumnData);
626 TryCatch(r == E_SUCCESS, delete pTempColumnData;
627 pTempColumnData = null,
628 "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
630 pTempColumnData = new (std::nothrow) String(APPS_APP_NAME);
631 r = pColumnNames->Add(pTempColumnData);
632 TryCatch(r == E_SUCCESS, delete pTempColumnData;
633 pTempColumnData = null,
634 "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
635 pTempColumnData = new (std::nothrow) String(DATATYPE_TEXT);
636 r = pColumnTypes->Add(pTempColumnData);
637 TryCatch(r == E_SUCCESS, delete pTempColumnData;
638 pTempColumnData = null,
639 "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
641 pTempColumnData = new (std::nothrow) String(APPS_APP_ICON);
642 r = pColumnNames->Add(pTempColumnData);
643 TryCatch(r == E_SUCCESS, delete pTempColumnData;
644 pTempColumnData = null,
645 "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
646 pTempColumnData = new (std::nothrow) String(DATATYPE_TEXT);
647 r = pColumnTypes->Add(pTempColumnData);
648 TryCatch(r == E_SUCCESS, delete pTempColumnData;
649 pTempColumnData = null,
650 "pColumnTypes->Add(*pTempColumnData) failed with error %s", GetErrorMessage(r));
652 pTempColumnData = new (std::nothrow) String(APPS_APP_PAGE);
653 r = pColumnNames->Add(pTempColumnData);
654 TryCatch(r == E_SUCCESS, delete pTempColumnData;
655 pTempColumnData = null,
656 "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
657 pTempColumnData = new (std::nothrow) String(DATATYPE_INTEGER);
658 r = pColumnTypes->Add(pTempColumnData);
659 TryCatch(r == E_SUCCESS, delete pTempColumnData;
660 pTempColumnData = null,
661 "pColumnTypes->Add(*pTempColumnData) failed with error %s", GetErrorMessage(r));
663 pTempColumnData = new (std::nothrow) String(APPS_APP_PAGEINDEX);
664 r = pColumnNames->Add(pTempColumnData);
665 TryCatch(r == E_SUCCESS, delete pTempColumnData;
666 pTempColumnData = null,
667 "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
668 pTempColumnData = new (std::nothrow) String(DATATYPE_INTEGER);
669 r = pColumnTypes->Add(pTempColumnData);
670 TryCatch(r == E_SUCCESS, delete pTempColumnData;
671 pTempColumnData = null,
672 "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
674 r = __pDataBaseManager->CreateTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes);
675 TryCatch(r == E_SUCCESS, , "HmDatabaseManager::CreateTable failed with error %s", GetErrorMessage(r));
677 if (pColumnNames != null)
679 pColumnNames->RemoveAll(true);
684 if (pColumnTypes != null)
686 pColumnTypes->RemoveAll(true);
695 HomePresentationModel::InsertPageDataInDataBase(const HomeItemInfo* pItemInfo)
698 result r = E_INVALID_ARG;
700 if (pItemInfo != null)
702 ArrayList* pColumnNames = null;
703 ArrayList* pColumnTypes = null;
704 ArrayList* pColumnValues = null;
705 String* pTempString = null;
706 Integer* pTempInt = null;
710 //Constructing various array list
711 pColumnNames = new (std::nothrow) ArrayList();
712 pColumnNames->Construct();
713 pColumnTypes = new (std::nothrow) ArrayList();
714 pColumnTypes->Construct();
715 pColumnValues = new (std::nothrow) ArrayList();
716 pColumnValues->Construct();
718 pItemInfo->GetPositionInHomeScreen(pageNumber, pageIndex);
721 pTempString = new (std::nothrow) String(APPS_APP_ID);
722 r = pColumnNames->Add(pTempString);
723 TryCatch(r == E_SUCCESS, delete pTempString;
725 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
726 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_TEXT);
727 r = pColumnTypes->Add(pTempInt);
728 TryCatch(r == E_SUCCESS, delete pTempInt;
730 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
731 pTempString = new (std::nothrow) String(pItemInfo->GetAppId());
732 r = pColumnValues->Add(pTempString);
733 TryCatch(r == E_SUCCESS, delete pTempString;
735 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
738 pTempString = new (std::nothrow) String(APPS_APP_NAME);
739 r = pColumnNames->Add(pTempString);
740 TryCatch(r == E_SUCCESS, delete pTempString;
742 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
743 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_TEXT);
744 r = pColumnTypes->Add(pTempInt);
745 TryCatch(r == E_SUCCESS, delete pTempInt;
747 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
748 pTempString = new (std::nothrow) String(pItemInfo->GetAppName());
749 r = pColumnValues->Add(pTempString);
750 TryCatch(r == E_SUCCESS, delete pTempString;
752 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
754 //Application IconPath
755 pTempString = new (std::nothrow) String(APPS_APP_ICON);
756 r = pColumnNames->Add(pTempString);
757 TryCatch(r == E_SUCCESS, delete pTempString;
759 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
760 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_TEXT);
761 r = pColumnTypes->Add(pTempInt);
762 TryCatch(r == E_SUCCESS, delete pTempInt;
764 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
765 pTempString = new (std::nothrow) String(pItemInfo->GetIconPath());
766 r = pColumnValues->Add(pTempString);
767 TryCatch(r == E_SUCCESS, delete pTempString;
769 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
771 //Application pageNumber
772 pTempString = new (std::nothrow) String(APPS_APP_PAGE);
773 r = pColumnNames->Add(pTempString);
774 TryCatch(r == E_SUCCESS, delete pTempString;
776 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
777 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
778 r = pColumnTypes->Add(pTempInt);
779 TryCatch(r == E_SUCCESS, delete pTempInt;
781 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
782 pTempInt = new (std::nothrow) Integer(pageNumber);
783 r = pColumnValues->Add(pTempInt);
784 TryCatch(r == E_SUCCESS, delete pTempInt;
786 "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
787 //Application pageIndex
788 pTempString = new (std::nothrow) String(APPS_APP_PAGEINDEX);
789 r = pColumnNames->Add(pTempString);
790 TryCatch(r == E_SUCCESS, delete pTempString;
792 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
793 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
794 r = pColumnTypes->Add(pTempInt);
795 TryCatch(r == E_SUCCESS, delete pTempInt;
797 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
798 pTempInt = new (std::nothrow) Integer(pageIndex);
799 r = pColumnValues->Add(pTempInt);
800 TryCatch(r == E_SUCCESS, delete pTempInt;
802 "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
805 // whereCondition = new (std::nothrow)String(APPS_APP_ID);
806 // whereCondition->Append(L" = ");
807 // whereCondition->Append(*pItemInfo->GetAppId());
808 r = __pDataBaseManager->InsertToTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes, pColumnValues);
812 if (pColumnNames != null)
814 pColumnNames->RemoveAll(true);
819 if (pColumnTypes != null)
821 pColumnTypes->RemoveAll(true);
826 if (pColumnValues != null)
828 pColumnValues->RemoveAll(true);
829 delete pColumnValues;
830 pColumnValues = null;
839 HomePresentationModel::NotifyInstallationEvent(const PackageId& packageId)
841 result r = E_SUCCESS;
842 String replace(APP_ID_REPLACE_STR);
843 String tempPackageId(packageId);
844 ArrayList* pNewAppList = null;
845 Integer* pNewKey = null;
846 HomeItemInfo* pItemInfo = null;
847 bool isalreadyExist = false;
849 r = tempPackageId.LastIndexOf(replace, 0, indexOf);
851 if (!IsFailed(r) && indexOf)
853 tempPackageId.Remove(0, replace.GetLength());
856 if (__pPackMgr == null)
858 __pPackMgr = PackageManager::GetInstance();
861 if (__pPackMgr != null)
863 PackageInfo* pPackInfo = __pPackMgr->GetPackageInfoN(packageId);
865 if (pPackInfo != null)
867 IList* pAppsInPack = pPackInfo->GetPackageAppInfoListN();
869 if (pAppsInPack != null)
871 IEnumerator* pPackageAppInfoEnum = pAppsInPack->GetEnumeratorN();
873 while (pPackageAppInfoEnum != null && pPackageAppInfoEnum->MoveNext() == E_SUCCESS)
875 ArrayList* pAppsList = null;
876 bool foundPosition = false;
877 IList* pKeysList = __pAppHashMap->GetKeysN();
878 PackageAppInfo* pPackageAppInfo = static_cast<PackageAppInfo*>(pPackageAppInfoEnum->GetCurrent());
880 if (pKeysList != null && pPackageAppInfo)
882 for (int keysCount = (pKeysList->GetCount() - 1); keysCount >= 0; keysCount--)
884 pItemInfo = FindAppObjectInList(tempPackageId);
886 if (pItemInfo != null)
888 pItemInfo->Initialize(pPackageAppInfo, pPackInfo);
889 pItemInfo->SetApplicationIcon(null);
890 isalreadyExist = true;
891 foundPosition = true;
898 for (int keysCount = (pKeysList->GetCount() - 1); keysCount >= 0; keysCount--)
900 pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keysCount))));
902 if (pAppsList != null)
904 if (pAppsList->GetCount() < MAX_ICONS_PER_PAGE)
906 pItemInfo = new (std::nothrow) HomeItemInfo();
908 if (pItemInfo != null)
910 if (pItemInfo->Initialize(pPackageAppInfo, pPackInfo) == E_SUCCESS)
912 if (pItemInfo->IsVisibleInMainMenu())
915 pItemInfo->SetPositionInHomeScreen(keysCount + 1, pAppsList->GetCount());
916 pAppsList->Add(pItemInfo);
917 foundPosition = true;
939 pNewAppList = new (std::nothrow) ArrayList();
941 if (pNewAppList != null)
943 r = pNewAppList->Construct();
945 if (pItemInfo == null)
947 pItemInfo = new (std::nothrow) HomeItemInfo();
948 r = pItemInfo->Initialize(pPackageAppInfo, pPackInfo);
949 TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
952 if (pItemInfo->IsVisibleInMainMenu())
956 pItemInfo->SetPositionInHomeScreen(__totalPages, 0);
957 r = pNewAppList->Add(pItemInfo);
958 TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
960 pNewKey = new (std::nothrow) Integer(__totalPages);
961 r = __pAppHashMap->Add(pNewKey, pNewAppList);
962 TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
968 if (pKeysList != null)
970 pKeysList->RemoveAll();
975 if (pPackageAppInfoEnum != null)
977 delete pPackageAppInfoEnum;
978 pPackageAppInfoEnum = null;
980 if (pAppsInPack != null)
1000 if (__pDataListener != null && pItemInfo != null)
1002 __pDataListener->OnInstallationCompleted(*pItemInfo /*, isalreadyExist*/);
1009 if (pItemInfo != null)
1015 if (pNewAppList != null)
1021 if (pNewKey != null)
1031 HomePresentationModel::PrepareApplicationsList(void)
1033 result r = E_SUCCESS;
1034 r = UpdateWithPositions();
1040 HomePresentationModel::SortPages(void)
1042 if (__pAppHashMap != null)
1044 IList* pKeys = null;
1045 ArrayList* pValues = null;
1046 IEnumerator* pKeysEnum = null;
1048 pKeys = __pAppHashMap->GetKeysN();
1052 pKeysEnum = pKeys->GetEnumeratorN();
1054 if (pKeysEnum != null)
1056 while (pKeysEnum != null && pKeysEnum->MoveNext() == E_SUCCESS)
1058 Integer* pKeyName = static_cast<Integer*>(pKeysEnum->GetCurrent());
1059 pValues = static_cast<ArrayList*>(__pAppHashMap->GetValue(*pKeyName));
1061 if (pValues != null)
1063 pValues->Sort(__comparerObject);
1064 UpdatePositions(*pValues);
1069 if (pKeysEnum != null)
1075 pKeys->RemoveAll(false);
1083 HomePresentationModel::UpdateDataToDatabase(HomeItemInfo* pItemInfo)
1085 result r = E_INVALID_ARG;
1087 if (pItemInfo != null)
1089 ArrayList* pColumnNames = null;
1090 ArrayList* pColumnTypes = null;
1091 ArrayList* pColumnValues = null;
1092 String* whereCondition = null;
1093 String* pTempString = null;
1094 Integer* pTempInt = null;
1098 //Constructing various array list
1099 pColumnNames = new (std::nothrow) ArrayList();
1100 pColumnNames->Construct();
1101 pColumnTypes = new (std::nothrow) ArrayList();
1102 pColumnTypes->Construct();
1103 pColumnValues = new (std::nothrow) ArrayList();
1104 pColumnValues->Construct();
1106 pItemInfo->GetPositionInHomeScreen(pageNumber, pageIndex);
1108 //Application pageNumber
1109 pTempString = new (std::nothrow) String(APPS_APP_PAGE);
1110 r = pColumnNames->Add(pTempString);
1111 TryCatch(r == E_SUCCESS, delete pTempString;
1113 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
1114 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
1115 r = pColumnTypes->Add(pTempInt);
1116 TryCatch(r == E_SUCCESS, delete pTempInt;
1118 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
1119 pTempInt = new (std::nothrow) Integer(pageNumber);
1120 r = pColumnValues->Add(pTempInt);
1121 TryCatch(r == E_SUCCESS, delete pTempInt;
1123 "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
1124 //Application pageIndex
1125 pTempString = new (std::nothrow) String(APPS_APP_PAGEINDEX);
1126 r = pColumnNames->Add(pTempString);
1127 TryCatch(r == E_SUCCESS, delete pTempString;
1129 "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
1130 pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
1131 r = pColumnTypes->Add(pTempInt);
1132 TryCatch(r == E_SUCCESS, delete pTempInt;
1134 "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
1135 pTempInt = new (std::nothrow) Integer(pageIndex);
1136 r = pColumnValues->Add(pTempInt);
1137 TryCatch(r == E_SUCCESS, delete pTempInt;
1139 "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
1142 whereCondition = new (std::nothrow) String(APPS_APP_ID);
1143 whereCondition->Append(L" = ");
1144 whereCondition->Append(L'\"');
1145 whereCondition->Append(pItemInfo->GetAppId());
1146 whereCondition->Append(L'\"');
1148 r = __pDataBaseManager->UpdateTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes, pColumnValues, whereCondition);
1152 if (pColumnNames != null)
1154 pColumnNames->RemoveAll(true);
1155 delete pColumnNames;
1156 pColumnNames = null;
1159 if (pColumnTypes != null)
1161 pColumnTypes->RemoveAll(true);
1162 delete pColumnTypes;
1163 pColumnTypes = null;
1166 if (pColumnValues != null)
1168 pColumnValues->RemoveAll(true);
1169 delete pColumnValues;
1170 pColumnValues = null;
1175 delete whereCondition;
1176 whereCondition = null;
1185 HomePresentationModel::UpdatePositions(Tizen::Base::Collection::ArrayList& pAppListToUpdate)
1187 for (int appIndex = 0; appIndex < pAppListToUpdate.GetCount(); appIndex++)
1189 HomeItemInfo* pItemInfo = static_cast<HomeItemInfo*>(pAppListToUpdate.GetAt(appIndex));
1190 pItemInfo->SetItemIndex(appIndex);
1193 pAppListToUpdate.Sort(__comparerObject);
1197 HomePresentationModel::UpdateWithPositions(void)
1199 result r = E_INVALID_STATE;
1201 if (__pDataBaseManager == null)
1203 r = InitializeDatabase();
1207 AppLogException("Home_Exception::InitializeDataBase() failed with error = %s", GetErrorMessage(r));
1212 if (__pAppHashMap != null)
1214 IList* pKeys = null;
1215 ArrayList* pValues = null;
1216 IEnumerator* pKeysEnum = null;
1218 pKeys = __pAppHashMap->GetKeysN();
1222 pKeysEnum = pKeys->GetEnumeratorN();
1224 if (pKeysEnum != null)
1226 while (pKeysEnum != null && pKeysEnum->MoveNext() == E_SUCCESS)
1228 Integer* pKeyName = static_cast<Integer*>(pKeysEnum->GetCurrent());
1229 pValues = static_cast<ArrayList*>(__pAppHashMap->GetValue(*pKeyName));
1231 if (pValues != null)
1234 for (int appCount = 0; appCount < pValues->GetCount(); appCount++)
1236 HomeItemInfo* pItemInfo = dynamic_cast<HomeItemInfo*>(pValues->GetAt(appCount));
1238 if (pItemInfo != null)
1240 String whereState(APPS_APP_ID);
1241 String appId = pItemInfo->GetAppId();
1242 HashMap* pRowData = new (std::nothrow) HashMap();
1243 whereState.Append(L" = ");
1244 whereState.Append(L'\"');
1245 whereState.Append(appId);
1246 whereState.Append(L'\"');
1247 pRowData->Construct();
1248 r = __pDataBaseManager->GetDataRowFromTable(HOME_APPLICATIONS_TABLE, whereState, *pRowData);
1250 if (!IsFailed(r) && pRowData->GetCount())
1253 Integer* pPageIndex = null;
1254 Integer* pPageNumber = static_cast<Integer*>(pRowData->GetValue(String(APPS_APP_PAGE)));
1255 pPageIndex = static_cast<Integer*>(pRowData->GetValue(String(APPS_APP_PAGEINDEX)));
1257 if (pPageNumber != null && pPageIndex != null)
1259 pItemInfo->SetPositionInHomeScreen(pPageNumber->ToInt(), pPageIndex->ToInt());
1265 r = InsertPageDataInDataBase(pItemInfo);
1269 if (pRowData != null)
1271 pRowData->RemoveAll(true);
1281 if (pKeysEnum != null)
1287 pKeys->RemoveAll(false);