+//
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Flora License, Version 1.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://floralicense.org/license/
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an AS IS BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file HmHomePresentationModel.cpp
+ * @brief Keeps implementation of the HmHomePresentationModel
+ * Implements the HmHomePresentationModel class, it keeps the data required to show the application icons in the home view
+ */
+
+#include <FApp.h>
+#include <FBase.h>
+#include <FIo.h>
+#include "HmHomeItemInfo.h"
+#include "HmHomePresentationModel.h"
+
+using namespace Tizen::App;
+using namespace Tizen::App::Package;
+using namespace Tizen::Base;
+using namespace Tizen::Base::Collection;
+using namespace Tizen::Io;
+
+HomePresentationModel::HomePresentationModel(void)
+ : __pDataBaseManager(null)
+ , __pDataListener(null)
+ , __totalApps(0)
+ , __totalPages(0)
+ , __pPackMgr(null)
+ , __pAppHashMap(null)
+ , __pPackageInfoRetrieveThread(null)
+{
+ //empty implementation
+}
+
+HomePresentationModel::~HomePresentationModel(void)
+{
+ __pPackMgr->RemovePackageInstallationEventListener(*this);
+
+ if (__pPackageInfoRetrieveThread != null)
+ {
+ __pPackageInfoRetrieveThread->Join();
+ delete __pPackageInfoRetrieveThread;
+ __pPackageInfoRetrieveThread = null;
+ }
+
+ if (__pAppHashMap != null)
+ {
+ IList* pKeysList = __pAppHashMap->GetKeysN();
+
+ for (int keyCount = 0; keyCount < pKeysList->GetCount(); keyCount++)
+ {
+ ArrayList* pAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keyCount))));
+
+ if (pAppList != null)
+ {
+ pAppList->RemoveAll(true);
+ }
+ }
+
+ pKeysList->RemoveAll();
+ delete pKeysList;
+ __pAppHashMap->RemoveAll(true);
+ delete __pAppHashMap;
+ __pAppHashMap = null;
+ }
+
+ __pDataListener = null;
+}
+
+result
+HomePresentationModel::Construct(void)
+{
+ result r = E_SUCCESS;
+ __totalPages = 1;
+ __pPackageInfoRetrieveThread = new (std::nothrow) PackageInfoListRetrieveThread(this);
+ r = __pPackageInfoRetrieveThread->Construct();
+ TryCatch(r == E_SUCCESS, , "__pPackageInfoRetrieveThread->Construct()", GetErrorMessage(r));
+ __pPackMgr = PackageManager::GetInstance();
+ __pPackMgr->AddPackageInstallationEventListener(*this);
+ r = __pPackageInfoRetrieveThread->SetPackageType();
+
+CATCH:
+
+ return r;
+}
+
+HomeItemInfo*
+HomePresentationModel::GetItem(int pageNumber, int index) const
+{
+ ArrayList* pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
+ if (pAppsList != null)
+ {
+ return static_cast<HomeItemInfo*>(pAppsList->GetAt(index));
+ }
+ return null;
+}
+
+int
+HomePresentationModel::GetItemCount(int pageNumber) const
+{
+ if (__pAppHashMap != null)
+ {
+ ArrayList* pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
+
+ if (pAppsList != null)
+ {
+ return pAppsList->GetCount();
+ }
+ }
+ return 0;
+}
+
+int
+HomePresentationModel::GetPageCount(void) const
+{
+ return __totalPages;
+}
+
+void
+HomePresentationModel::LanguageChanged(void)
+{
+ IList* pAppsValues = null;
+ IEnumerator* pMapEnum = null;
+ TryCatch(__pAppHashMap != null, , "__pAppHashMap is null");
+ pAppsValues = __pAppHashMap->GetValuesN();
+ TryCatch(pAppsValues != null, , "pAppHashMap->GetValuesN returned null");
+ pMapEnum = pAppsValues->GetEnumeratorN();
+ TryCatch(pMapEnum != null, , "pAppsValues->GetEnumeratorN() returned null");
+ __pPackMgr = PackageManager::GetInstance();
+
+ while (pMapEnum->MoveNext() == E_SUCCESS)
+ {
+ ArrayList* pAppsArrayList = static_cast<ArrayList*>(pMapEnum->GetCurrent());
+
+ if (pAppsArrayList != null)
+ {
+ IEnumerator* pAppsEnum = pAppsArrayList->GetEnumeratorN();
+
+ if (pAppsEnum)
+ {
+ while (pAppsEnum->MoveNext() == E_SUCCESS)
+ {
+ HomeItemInfo* pAppInfo = static_cast<HomeItemInfo*>(pAppsEnum->GetCurrent());
+
+ if (pAppInfo != null)
+ {
+ PackageInfo* pPackInfo = __pPackMgr->GetPackageInfoN(pAppInfo->GetPackageId());
+
+ if (pPackInfo != null)
+ {
+ PackageAppInfo* pPackageAppInfo = pPackInfo->GetPackageAppInfoN(pAppInfo->GetAppId());
+
+ if (pPackageAppInfo != null)
+ {
+ String appNewName = pPackageAppInfo->GetAppDisplayName();
+
+ if (pAppInfo->GetAppName().CompareTo(appNewName) != 0)
+ {
+ pAppInfo->SetApplicationName(appNewName);
+ pAppInfo->SetApplicationIcon(null);
+ }
+
+ delete pPackageAppInfo;
+ pPackageAppInfo = null;
+ }
+
+ delete pPackInfo;
+ pPackInfo = null;
+ }
+ }
+ }
+
+ delete pAppsEnum;
+ pAppsEnum = null;
+ }
+ }
+ }
+
+CATCH:
+ if (pMapEnum != null)
+ {
+ delete pMapEnum;
+ pMapEnum = null;
+ }
+ if (pAppsValues != null)
+ {
+ pAppsValues->RemoveAll(false);
+ delete pAppsValues;
+ pAppsValues = null;
+ }
+}
+
+void
+HomePresentationModel::RearrangeItems(HomeItemInfo* pMovedAppInfo, int newPageNumber, int newIndex)
+{
+ ArrayList* pMovedToAppList = null;
+ ArrayList* pMovedFromAppList = null;
+ ArrayList* pPagesToUpdate = null;
+ Integer* pPageToUpdate = null;
+ result r = E_SUCCESS;
+ int movedPageNumber = 0;
+ int movedAppIndex = 0;
+ // gets the list of applications for the current page
+ pMovedToAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(newPageNumber)));
+ pMovedAppInfo->GetPositionInHomeScreen(movedPageNumber, movedAppIndex);
+ pMovedFromAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(movedPageNumber)));
+
+ if ((newPageNumber == movedPageNumber) && (newIndex == movedAppIndex))
+ {
+ return;
+ }
+
+ pPagesToUpdate = new ArrayList();
+ pPagesToUpdate->Construct();
+ pPageToUpdate = new (std::nothrow) Integer(movedPageNumber);
+ r = pPagesToUpdate->Add(pPageToUpdate);
+
+ if (r != E_SUCCESS)
+ {
+ delete pPageToUpdate;
+ pPageToUpdate = null;
+ }
+
+
+ if (movedPageNumber != newPageNumber)
+ {
+ pPageToUpdate = new (std::nothrow) Integer(newPageNumber);
+ r = pPagesToUpdate->Add(pPageToUpdate);
+
+ if (r != E_SUCCESS)
+ {
+ delete pPageToUpdate;
+ pPageToUpdate = null;
+ }
+ }
+
+ if (pMovedToAppList != null && pMovedFromAppList != null)
+ {
+ {
+ if (movedPageNumber == newPageNumber)
+ {
+
+ if (newIndex != -1 && movedAppIndex != newIndex)
+ {
+
+ if (newIndex - movedAppIndex == 1)
+ {
+ pMovedToAppList->InsertAt(pMovedAppInfo, newIndex + 1);
+ }
+ else
+ {
+ pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
+ }
+
+ if (newIndex > movedAppIndex)
+ {
+ pMovedFromAppList->RemoveAt(movedAppIndex);
+ }
+ else
+ {
+ pMovedFromAppList->RemoveAt(movedAppIndex + 1);
+ }
+
+ UpdatePositions(*pMovedFromAppList);
+ }
+ }
+ else
+ {
+ int numberOfIconsInPage = pMovedToAppList->GetCount();
+
+ if (numberOfIconsInPage < MAX_ICONS_PER_PAGE)
+ {
+ pMovedAppInfo->SetPositionInHomeScreen(newPageNumber, newIndex);
+ pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
+ pMovedFromAppList->RemoveAt(movedAppIndex, false);
+ UpdatePositions(*pMovedFromAppList);
+ UpdatePositions(*pMovedToAppList);
+ }
+ else if (numberOfIconsInPage >= MAX_ICONS_PER_PAGE)
+ {
+ bool foundLocation = false;
+ int nextPageNumber = 0;
+ ArrayList* pNextPageAppList = null;
+ HomeItemInfo* pItemToGetANewLocationFor = static_cast<HomeItemInfo*>(pMovedToAppList->GetAt(pMovedToAppList->GetCount() - 1));
+ pMovedToAppList->Remove(*pItemToGetANewLocationFor, false);
+ pMovedAppInfo->SetPositionInHomeScreen(newPageNumber, newIndex);
+ pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
+ pMovedFromAppList->Remove(*pMovedAppInfo, false);
+
+ if (pItemToGetANewLocationFor != null)
+ {
+ nextPageNumber = newPageNumber + 1;
+ // checks for the successor page having the number of icons not exceeding its maximum limit
+
+ while (nextPageNumber <= __totalPages)
+ {
+ pNextPageAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(nextPageNumber)));
+
+ if (pNextPageAppList->GetCount() < MAX_ICONS_PER_PAGE /*|| nextPageNumber == pageNumber*/)
+ {
+ pItemToGetANewLocationFor->SetPositionInHomeScreen(nextPageNumber, pNextPageAppList->GetCount());
+ pNextPageAppList->Add(pItemToGetANewLocationFor);
+ UpdatePositions(*pNextPageAppList);
+ foundLocation = true;
+ pPageToUpdate = new Integer(nextPageNumber);
+ pPagesToUpdate->Add(pPageToUpdate);
+ break;
+ }
+
+ nextPageNumber++;
+ }
+
+ if (!foundLocation && __totalPages < MAX_PAGE_COUNT)
+ {
+ result r = E_SUCCESS;
+ Integer* pPageNumber = null;
+ ArrayList* pNewAppList = new (std::nothrow) ArrayList();
+ pNewAppList->Construct();
+
+ __totalPages++;
+ pItemToGetANewLocationFor->SetPositionInHomeScreen(__totalPages, 0);
+ pNewAppList->Add(pItemToGetANewLocationFor);
+ pPageNumber = new (std::nothrow) Integer(__totalPages);
+ r = __pAppHashMap->Add(pPageNumber, pNewAppList);
+ pPagesToUpdate->Add(new (std::nothrow) Integer(__totalPages));
+
+ if (IsFailed(r))
+ {
+ delete pPageNumber;
+ pPageNumber = null;
+ delete pNewAppList;
+ pNewAppList = null;
+ }
+
+ pPageToUpdate = new Integer(__totalPages);
+ pPagesToUpdate->Add(pPageToUpdate);
+ foundLocation = true;
+ }
+
+ if (pNextPageAppList != null)
+ {
+ UpdatePositions(*pNextPageAppList);
+ }
+
+ UpdatePositions(*pMovedFromAppList);
+ UpdatePositions(*pMovedToAppList);
+ }
+ }
+ }
+ }
+ }
+
+ __pDataListener->OnUpdatePageRequestedN(pPagesToUpdate);
+ return;
+}
+
+void
+HomePresentationModel::SetHomePackageEventListener(IHomePresentationModelEventListener* pListener)
+{
+ __pDataListener = pListener;
+}
+
+result
+HomePresentationModel::UpdatePageData(void)
+{
+ result r = E_INVALID_ARG;
+
+ if (__pAppHashMap != null)
+ {
+ IList* pKeys = __pAppHashMap->GetKeysN();
+
+ if (pKeys != null)
+ {
+ for (int pageCount = 0; pageCount < pKeys->GetCount(); pageCount++)
+ {
+ ArrayList* pPageApps = (ArrayList*) __pAppHashMap->GetValue(*pKeys->GetAt(pageCount));
+
+ if (pPageApps != null)
+ {
+
+ for (int appsCount = 0; appsCount < pPageApps->GetCount(); appsCount++)
+ {
+ HomeItemInfo* pItemInfo = static_cast<HomeItemInfo*>(pPageApps->GetAt(appsCount));
+
+ if (pItemInfo != null)
+ {
+ r = UpdateDataToDatabase(static_cast<HomeItemInfo*>(pItemInfo));
+ }
+ }
+ }
+ }
+
+ pKeys->RemoveAll(false);
+ delete pKeys;
+ pKeys = null;
+ }
+ }
+
+ return r;
+}
+
+result
+HomePresentationModel::HomeApplicationsComparer::Compare(const Object& obj1, const Object& obj2, int& cmp) const
+{
+ result r = E_FAILURE;
+ const HomeItemInfo* pObj1 = static_cast<const HomeItemInfo*>(&obj1);
+ const HomeItemInfo* pObj2 = static_cast<const HomeItemInfo*>(&obj2);
+
+ if (pObj1 != null && pObj2 != null)
+ {
+ int pageIndex1 = 0;
+ int pageNumber = 0;
+ int pageIndex2 = 0;
+ pObj1->GetPositionInHomeScreen(pageNumber, pageIndex1);
+ pObj2->GetPositionInHomeScreen(pageNumber, pageIndex2);
+
+ cmp = Integer::Compare(pageIndex1, pageIndex2);
+ r = E_SUCCESS;
+ }
+
+ return r;
+}
+
+void
+HomePresentationModel::OnPackageInstallationCompleted(const PackageId& packageId, PackageInstallationResult installationResult)
+{
+ if (installationResult == PACKAGE_INSTALLATION_RESULT_SUCCESS)
+ {
+ NotifyInstallationEvent(packageId);
+ }
+ else
+ {
+ //Show error
+ }
+ return;
+}
+
+void
+HomePresentationModel::OnPackageUninstallationCompleted(const PackageId& packageId, bool uninstallationResult)
+{
+ ArrayList* pAppsList = null;
+ HomeItemInfo* pItemInfo = null;
+
+ if (uninstallationResult)
+ {
+ if (__pDataListener != null && !packageId.IsEmpty())
+ {
+
+ pItemInfo = FindAppObjectInList(packageId);
+ do
+ {
+ if (pItemInfo != null)
+ {
+ int pageNumber = 0;
+ int appIndex = 0;
+ pItemInfo->GetPositionInHomeScreen(pageNumber, appIndex);
+ pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
+ pAppsList->Remove(*pItemInfo);
+ UpdatePositions(*pAppsList);
+ if (pItemInfo->IsVisibleInMainMenu())
+ {
+ __pDataListener->OnUnInstallationCompleted(*pItemInfo);
+ }
+
+ if (pItemInfo != null)
+ {
+ delete pItemInfo;
+ pItemInfo = null;
+ }
+ pItemInfo = FindAppObjectInList(packageId);
+ }
+ }
+ while (pItemInfo != null);
+ }
+ }
+ else
+ {
+ //Show error
+ }
+
+ return;
+}
+
+void
+HomePresentationModel::OnPackageInfoRetrievedN(Tizen::Base::Collection::HashMap* pAppsList, int totalApps, int totalPages)
+{
+ UiApp* pApp = UiApp::GetInstance();
+
+ if (pAppsList != null && totalApps != 0)
+ {
+ result r = E_SUCCESS;
+ __pAppHashMap = pAppsList;
+ __totalPages = totalPages;
+ __totalApps = totalApps;
+ r = PrepareApplicationsList();
+
+ if (pApp != null)
+ {
+ ArrayList* pListToSend = new (std::nothrow) ArrayList();
+
+ if (pListToSend != null)
+ {
+ r = pListToSend->Construct();
+
+ if (r == E_SUCCESS)
+ {
+ Integer* pResToSend = new (std::nothrow) Integer(r);
+ r = pListToSend->Add(pResToSend);
+
+ if (r == E_SUCCESS)
+ {
+ r = pApp->SendUserEvent(MODEL_REQUEST_INITIALIZE, pListToSend);
+ return;
+ }
+ else
+ {
+ delete pResToSend;
+ delete pListToSend;
+ }
+ }
+ }
+ }
+ }
+
+ if (pApp != null)
+ {
+ pApp->SendUserEvent(MODEL_REQUEST_INITIALIZE, null);
+ }
+
+ return;
+}
+
+HomeItemInfo*
+HomePresentationModel::FindAppObjectInList(const Tizen::Base::String& packageIdToFind)
+{
+ IList* pKeysList = __pAppHashMap->GetKeysN();
+
+ if (pKeysList != null)
+ {
+ for (int keysCount = 0; keysCount < pKeysList->GetCount(); keysCount++)
+ {
+ ArrayList* pList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keysCount))));
+
+ for (int itemCount = (pList->GetCount() - 1); itemCount >= 0; itemCount--)
+ {
+ HomeItemInfo* pApplicationInfo = static_cast<HomeItemInfo*>(pList->GetAt(itemCount));
+
+ if (pApplicationInfo != null)
+ {
+ String packageId = pApplicationInfo->GetPackageId();
+
+ if (packageId.CompareTo(packageIdToFind) == 0)
+ {
+ pKeysList->RemoveAll();
+ delete pKeysList;
+ return static_cast<HomeItemInfo*>(pApplicationInfo);
+ }
+ }
+ }
+ }
+ pKeysList->RemoveAll();
+ delete pKeysList;
+ pKeysList = null;
+ }
+
+ return null;
+
+}
+
+result
+HomePresentationModel::InitializeDatabase(void)
+{
+ result r = E_SUCCESS;
+
+ __pDataBaseManager = GenericDatabaseManager::GetInstance();
+
+ if (__pDataBaseManager != null)
+ {
+ ArrayList* pColumnNames = null;
+ ArrayList* pColumnTypes = null;
+ String* pTempColumnData = null;
+
+ String databaseName = App::GetInstance()->GetAppRootPath();
+ databaseName.Append(HOME_DATABASE_NAME);
+
+ r = __pDataBaseManager->CreateDataBase(databaseName);
+ TryCatch(r == E_SUCCESS, ,
+ "Home_Exception::failed to create Database");
+ pColumnNames = new (std::nothrow) ArrayList();
+ pColumnNames->Construct();
+ pColumnTypes = new (std::nothrow) ArrayList();
+ pColumnTypes->Construct();
+
+ //Creates table homeapps
+ pTempColumnData = new (std::nothrow) String(APPS_RECORD_ID);
+ r = pColumnNames->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+ pTempColumnData = new (std::nothrow) String(DATATYPE_INTEGER_PRIMARY);
+ r = pColumnTypes->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+
+ pTempColumnData = new (std::nothrow) String(APPS_APP_ID);
+ r = pColumnNames->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+ pTempColumnData = new (std::nothrow) String(DATATYPE_TEXT);
+ pTempColumnData->Append(DATATYPE_UNIQUE);
+ r = pColumnTypes->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+
+ pTempColumnData = new (std::nothrow) String(APPS_APP_NAME);
+ r = pColumnNames->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+ pTempColumnData = new (std::nothrow) String(DATATYPE_TEXT);
+ r = pColumnTypes->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+
+ pTempColumnData = new (std::nothrow) String(APPS_APP_ICON);
+ r = pColumnNames->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+ pTempColumnData = new (std::nothrow) String(DATATYPE_TEXT);
+ r = pColumnTypes->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnTypes->Add(*pTempColumnData) failed with error %s", GetErrorMessage(r));
+
+ pTempColumnData = new (std::nothrow) String(APPS_APP_PAGE);
+ r = pColumnNames->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+ pTempColumnData = new (std::nothrow) String(DATATYPE_INTEGER);
+ r = pColumnTypes->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnTypes->Add(*pTempColumnData) failed with error %s", GetErrorMessage(r));
+
+ pTempColumnData = new (std::nothrow) String(APPS_APP_PAGEINDEX);
+ r = pColumnNames->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnNames->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+ pTempColumnData = new (std::nothrow) String(DATATYPE_INTEGER);
+ r = pColumnTypes->Add(pTempColumnData);
+ TryCatch(r == E_SUCCESS, delete pTempColumnData;
+ pTempColumnData = null,
+ "pColumnTypes->Add(pTempColumnData) failed with error %s", GetErrorMessage(r));
+
+ r = __pDataBaseManager->CreateTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes);
+ TryCatch(r == E_SUCCESS, , "HmDatabaseManager::CreateTable failed with error %s", GetErrorMessage(r));
+CATCH:
+ if (pColumnNames != null)
+ {
+ pColumnNames->RemoveAll(true);
+ delete pColumnNames;
+ pColumnNames = null;
+ }
+
+ if (pColumnTypes != null)
+ {
+ pColumnTypes->RemoveAll(true);
+ delete pColumnTypes;
+ pColumnTypes = null;
+ }
+ }
+ return r;
+}
+
+result
+HomePresentationModel::InsertPageDataInDataBase(const HomeItemInfo* pItemInfo)
+{
+
+ result r = E_INVALID_ARG;
+
+ if (pItemInfo != null)
+ {
+ ArrayList* pColumnNames = null;
+ ArrayList* pColumnTypes = null;
+ ArrayList* pColumnValues = null;
+ String* pTempString = null;
+ Integer* pTempInt = null;
+ int pageNumber = 0;
+ int pageIndex = 0;
+
+ //Constructing various array list
+ pColumnNames = new (std::nothrow) ArrayList();
+ pColumnNames->Construct();
+ pColumnTypes = new (std::nothrow) ArrayList();
+ pColumnTypes->Construct();
+ pColumnValues = new (std::nothrow) ArrayList();
+ pColumnValues->Construct();
+
+ pItemInfo->GetPositionInHomeScreen(pageNumber, pageIndex);
+
+ //Application app id
+ pTempString = new (std::nothrow) String(APPS_APP_ID);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_TEXT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempString = new (std::nothrow) String(pItemInfo->GetAppId());
+ r = pColumnValues->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+
+ //Application Name
+ pTempString = new (std::nothrow) String(APPS_APP_NAME);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_TEXT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempString = new (std::nothrow) String(pItemInfo->GetAppName());
+ r = pColumnValues->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+
+ //Application IconPath
+ pTempString = new (std::nothrow) String(APPS_APP_ICON);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_TEXT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempString = new (std::nothrow) String(pItemInfo->GetIconPath());
+ r = pColumnValues->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+
+ //Application pageNumber
+ pTempString = new (std::nothrow) String(APPS_APP_PAGE);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(pageNumber);
+ r = pColumnValues->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
+ //Application pageIndex
+ pTempString = new (std::nothrow) String(APPS_APP_PAGEINDEX);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(pageIndex);
+ r = pColumnValues->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
+
+ //update
+// whereCondition = new (std::nothrow)String(APPS_APP_ID);
+// whereCondition->Append(L" = ");
+// whereCondition->Append(*pItemInfo->GetAppId());
+ r = __pDataBaseManager->InsertToTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes, pColumnValues);
+
+CATCH:
+
+ if (pColumnNames != null)
+ {
+ pColumnNames->RemoveAll(true);
+ delete pColumnNames;
+ pColumnNames = null;
+ }
+
+ if (pColumnTypes != null)
+ {
+ pColumnTypes->RemoveAll(true);
+ delete pColumnTypes;
+ pColumnTypes = null;
+ }
+
+ if (pColumnValues != null)
+ {
+ pColumnValues->RemoveAll(true);
+ delete pColumnValues;
+ pColumnValues = null;
+ }
+
+ }
+
+ return r;
+}
+
+result
+HomePresentationModel::NotifyInstallationEvent(const PackageId& packageId)
+{
+ result r = E_SUCCESS;
+ String replace(APP_ID_REPLACE_STR);
+ String tempPackageId(packageId);
+ ArrayList* pNewAppList = null;
+ Integer* pNewKey = null;
+ HomeItemInfo* pItemInfo = null;
+ bool isalreadyExist = false;
+ int indexOf = 0;
+ r = tempPackageId.LastIndexOf(replace, 0, indexOf);
+
+ if (!IsFailed(r) && indexOf)
+ {
+ tempPackageId.Remove(0, replace.GetLength());
+ }
+
+ if (__pPackMgr == null)
+ {
+ __pPackMgr = PackageManager::GetInstance();
+ }
+
+ if (__pPackMgr != null)
+ {
+ PackageInfo* pPackInfo = __pPackMgr->GetPackageInfoN(packageId);
+
+ if (pPackInfo != null)
+ {
+ IList* pAppsInPack = pPackInfo->GetPackageAppInfoListN();
+
+ if (pAppsInPack != null)
+ {
+ IEnumerator* pPackageAppInfoEnum = pAppsInPack->GetEnumeratorN();
+
+ while (pPackageAppInfoEnum != null && pPackageAppInfoEnum->MoveNext() == E_SUCCESS)
+ {
+ ArrayList* pAppsList = null;
+ bool foundPosition = false;
+ IList* pKeysList = __pAppHashMap->GetKeysN();
+ PackageAppInfo* pPackageAppInfo = static_cast<PackageAppInfo*>(pPackageAppInfoEnum->GetCurrent());
+
+ if (pKeysList != null && pPackageAppInfo)
+ {
+ for (int keysCount = (pKeysList->GetCount() - 1); keysCount >= 0; keysCount--)
+ {
+ pItemInfo = FindAppObjectInList(tempPackageId);
+
+ if (pItemInfo != null)
+ {
+ pItemInfo->Initialize(pPackageAppInfo, pPackInfo);
+ pItemInfo->SetApplicationIcon(null);
+ isalreadyExist = true;
+ foundPosition = true;
+ break;
+ }
+ }
+
+ if (!isalreadyExist)
+ {
+ for (int keysCount = (pKeysList->GetCount() - 1); keysCount >= 0; keysCount--)
+ {
+ pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keysCount))));
+
+ if (pAppsList != null)
+ {
+ if (pAppsList->GetCount() < MAX_ICONS_PER_PAGE)
+ {
+ pItemInfo = new (std::nothrow) HomeItemInfo();
+
+ if (pItemInfo != null)
+ {
+ if (pItemInfo->Initialize(pPackageAppInfo, pPackInfo) == E_SUCCESS)
+ {
+ if (pItemInfo->IsVisibleInMainMenu())
+ {
+ __totalApps++;
+ pItemInfo->SetPositionInHomeScreen(keysCount + 1, pAppsList->GetCount());
+ pAppsList->Add(pItemInfo);
+ foundPosition = true;
+ break;
+ }
+ else
+ {
+ continue;
+ }
+
+ }
+ else
+ {
+ delete pItemInfo;
+ pItemInfo = null;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (!foundPosition)
+ {
+ pNewAppList = new (std::nothrow) ArrayList();
+
+ if (pNewAppList != null)
+ {
+ r = pNewAppList->Construct();
+
+ if (pItemInfo == null)
+ {
+ pItemInfo = new (std::nothrow) HomeItemInfo();
+ r = pItemInfo->Initialize(pPackageAppInfo, pPackInfo);
+ TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
+ GetErrorMessage(r));
+ }
+ if (pItemInfo->IsVisibleInMainMenu())
+ {
+ __totalApps++;
+ __totalPages++;
+ pItemInfo->SetPositionInHomeScreen(__totalPages, 0);
+ r = pNewAppList->Add(pItemInfo);
+ TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
+ GetErrorMessage(r));
+ pNewKey = new (std::nothrow) Integer(__totalPages);
+ r = __pAppHashMap->Add(pNewKey, pNewAppList);
+ TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
+ GetErrorMessage(r));
+ }
+ }
+ }
+ }
+ if (pKeysList != null)
+ {
+ pKeysList->RemoveAll();
+ delete pKeysList;
+ pKeysList = null;
+ }
+ }
+ if (pPackageAppInfoEnum != null)
+ {
+ delete pPackageAppInfoEnum;
+ pPackageAppInfoEnum = null;
+ }
+ if (pAppsInPack != null)
+ {
+ delete pAppsInPack;
+ pAppsInPack = null;
+ }
+ }
+
+ delete pPackInfo;
+ pPackInfo = null;
+ }
+ else
+ {
+ r = E_SUCCESS;
+ }
+ }
+ else
+ {
+ r = GetLastResult();
+ }
+
+ if (__pDataListener != null && pItemInfo != null)
+ {
+ __pDataListener->OnInstallationCompleted(*pItemInfo /*, isalreadyExist*/);
+ }
+
+ return r;
+
+CATCH:
+
+ if (pItemInfo != null)
+ {
+ delete pItemInfo;
+ pItemInfo = null;
+ }
+
+ if (pNewAppList != null)
+ {
+ delete pNewAppList;
+ pNewAppList = null;
+ }
+
+ if (pNewKey != null)
+ {
+ delete pNewKey;
+ pNewKey = null;
+ }
+
+ return r;
+}
+
+result
+HomePresentationModel::PrepareApplicationsList(void)
+{
+ result r = E_SUCCESS;
+ r = UpdateWithPositions();
+ SortPages();
+ return r;
+}
+
+void
+HomePresentationModel::SortPages(void)
+{
+ if (__pAppHashMap != null)
+ {
+ IList* pKeys = null;
+ ArrayList* pValues = null;
+ IEnumerator* pKeysEnum = null;
+
+ pKeys = __pAppHashMap->GetKeysN();
+
+ if (pKeys != null)
+ {
+ pKeysEnum = pKeys->GetEnumeratorN();
+
+ if (pKeysEnum != null)
+ {
+ while (pKeysEnum != null && pKeysEnum->MoveNext() == E_SUCCESS)
+ {
+ Integer* pKeyName = static_cast<Integer*>(pKeysEnum->GetCurrent());
+ pValues = static_cast<ArrayList*>(__pAppHashMap->GetValue(*pKeyName));
+
+ if (pValues != null)
+ {
+ pValues->Sort(__comparerObject);
+ UpdatePositions(*pValues);
+ }
+ }
+ }
+
+ if (pKeysEnum != null)
+ {
+ delete pKeysEnum;
+ pKeysEnum = null;
+ }
+
+ pKeys->RemoveAll(false);
+ delete pKeys;
+ pKeys = null;
+ }
+ }
+}
+
+result
+HomePresentationModel::UpdateDataToDatabase(HomeItemInfo* pItemInfo)
+{
+ result r = E_INVALID_ARG;
+
+ if (pItemInfo != null)
+ {
+ ArrayList* pColumnNames = null;
+ ArrayList* pColumnTypes = null;
+ ArrayList* pColumnValues = null;
+ String* whereCondition = null;
+ String* pTempString = null;
+ Integer* pTempInt = null;
+ int pageNumber = 0;
+ int pageIndex = 0;
+
+ //Constructing various array list
+ pColumnNames = new (std::nothrow) ArrayList();
+ pColumnNames->Construct();
+ pColumnTypes = new (std::nothrow) ArrayList();
+ pColumnTypes->Construct();
+ pColumnValues = new (std::nothrow) ArrayList();
+ pColumnValues->Construct();
+
+ pItemInfo->GetPositionInHomeScreen(pageNumber, pageIndex);
+
+ //Application pageNumber
+ pTempString = new (std::nothrow) String(APPS_APP_PAGE);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(pageNumber);
+ r = pColumnValues->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
+ //Application pageIndex
+ pTempString = new (std::nothrow) String(APPS_APP_PAGEINDEX);
+ r = pColumnNames->Add(pTempString);
+ TryCatch(r == E_SUCCESS, delete pTempString;
+ pTempString = null,
+ "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(DB_COLUMNTYPE_INT);
+ r = pColumnTypes->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnTypes->Add(pTempInt); failed %s", GetErrorMessage(r));
+ pTempInt = new (std::nothrow) Integer(pageIndex);
+ r = pColumnValues->Add(pTempInt);
+ TryCatch(r == E_SUCCESS, delete pTempInt;
+ pTempInt = null,
+ "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
+
+ //update
+ whereCondition = new (std::nothrow) String(APPS_APP_ID);
+ whereCondition->Append(L" = ");
+ whereCondition->Append(L'\"');
+ whereCondition->Append(pItemInfo->GetAppId());
+ whereCondition->Append(L'\"');
+
+ r = __pDataBaseManager->UpdateTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes, pColumnValues, whereCondition);
+
+CATCH:
+
+ if (pColumnNames != null)
+ {
+ pColumnNames->RemoveAll(true);
+ delete pColumnNames;
+ pColumnNames = null;
+ }
+
+ if (pColumnTypes != null)
+ {
+ pColumnTypes->RemoveAll(true);
+ delete pColumnTypes;
+ pColumnTypes = null;
+ }
+
+ if (pColumnValues != null)
+ {
+ pColumnValues->RemoveAll(true);
+ delete pColumnValues;
+ pColumnValues = null;
+ }
+
+ if (whereCondition)
+ {
+ delete whereCondition;
+ whereCondition = null;
+ }
+
+ }
+
+ return r;
+}
+
+void
+HomePresentationModel::UpdatePositions(Tizen::Base::Collection::ArrayList& pAppListToUpdate)
+{
+ for (int appIndex = 0; appIndex < pAppListToUpdate.GetCount(); appIndex++)
+ {
+ HomeItemInfo* pItemInfo = static_cast<HomeItemInfo*>(pAppListToUpdate.GetAt(appIndex));
+ pItemInfo->SetItemIndex(appIndex);
+ }
+
+ pAppListToUpdate.Sort(__comparerObject);
+}
+
+result
+HomePresentationModel::UpdateWithPositions(void)
+{
+ result r = E_INVALID_STATE;
+ //Sanity test
+ if (__pDataBaseManager == null)
+ {
+ r = InitializeDatabase();
+
+ if (IsFailed(r))
+ {
+ AppLogException("Home_Exception::InitializeDataBase() failed with error = %s", GetErrorMessage(r));
+ return r;
+ }
+ }
+
+ if (__pAppHashMap != null)
+ {
+ IList* pKeys = null;
+ ArrayList* pValues = null;
+ IEnumerator* pKeysEnum = null;
+
+ pKeys = __pAppHashMap->GetKeysN();
+
+ if (pKeys != null)
+ {
+ pKeysEnum = pKeys->GetEnumeratorN();
+
+ if (pKeysEnum != null)
+ {
+ while (pKeysEnum != null && pKeysEnum->MoveNext() == E_SUCCESS)
+ {
+ Integer* pKeyName = static_cast<Integer*>(pKeysEnum->GetCurrent());
+ pValues = static_cast<ArrayList*>(__pAppHashMap->GetValue(*pKeyName));
+
+ if (pValues != null)
+ {
+
+ for (int appCount = 0; appCount < pValues->GetCount(); appCount++)
+ {
+ HomeItemInfo* pItemInfo = dynamic_cast<HomeItemInfo*>(pValues->GetAt(appCount));
+
+ if (pItemInfo != null)
+ {
+ String whereState(APPS_APP_ID);
+ String appId = pItemInfo->GetAppId();
+ HashMap* pRowData = new (std::nothrow) HashMap();
+ whereState.Append(L" = ");
+ whereState.Append(L'\"');
+ whereState.Append(appId);
+ whereState.Append(L'\"');
+ pRowData->Construct();
+ r = __pDataBaseManager->GetDataRowFromTable(HOME_APPLICATIONS_TABLE, whereState, *pRowData);
+
+ if (!IsFailed(r) && pRowData->GetCount())
+ {
+
+ Integer* pPageIndex = null;
+ Integer* pPageNumber = static_cast<Integer*>(pRowData->GetValue(String(APPS_APP_PAGE)));
+ pPageIndex = static_cast<Integer*>(pRowData->GetValue(String(APPS_APP_PAGEINDEX)));
+
+ if (pPageNumber != null && pPageIndex != null)
+ {
+ pItemInfo->SetPositionInHomeScreen(pPageNumber->ToInt(), pPageIndex->ToInt());
+ }
+
+ }
+ else
+ {
+ r = InsertPageDataInDataBase(pItemInfo);
+ r = E_SUCCESS;
+ }
+
+ if (pRowData != null)
+ {
+ pRowData->RemoveAll(true);
+ delete pRowData;
+ pRowData = null;
+ }
+ }
+ }
+ }
+ }
+ }
+ //CATCH:
+ if (pKeysEnum != null)
+ {
+ delete pKeysEnum;
+ pKeysEnum = null;
+ }
+
+ pKeys->RemoveAll(false);
+ delete pKeys;
+ pKeys = null;
+ }
+ }
+
+ return r;
+
+}