Tizen 2.1 base
[apps/osp/Home.git] / src / HmHomePresentationModel.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        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
21  */
22
23 #include <FApp.h>
24 #include <FBase.h>
25 #include <FIo.h>
26 #include "HmHomeItemInfo.h"
27 #include "HmHomePresentationModel.h"
28
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;
34
35 HomePresentationModel::HomePresentationModel(void)
36         : __pDataBaseManager(null)
37         , __pDataListener(null)
38         , __totalApps(0)
39         , __totalPages(0)
40         , __pPackMgr(null)
41         , __pAppHashMap(null)
42         , __pPackageInfoRetrieveThread(null)
43 {
44         //empty implementation
45 }
46
47 HomePresentationModel::~HomePresentationModel(void)
48 {
49         __pPackMgr->RemovePackageInstallationEventListener(*this);
50
51         if (__pPackageInfoRetrieveThread != null)
52         {
53                 __pPackageInfoRetrieveThread->Join();
54                 delete __pPackageInfoRetrieveThread;
55                 __pPackageInfoRetrieveThread = null;
56         }
57
58         if (__pAppHashMap != null)
59         {
60                 IList* pKeysList = __pAppHashMap->GetKeysN();
61
62                 for (int keyCount = 0; keyCount < pKeysList->GetCount(); keyCount++)
63                 {
64                         ArrayList* pAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keyCount))));
65
66                         if (pAppList != null)
67                         {
68                                 pAppList->RemoveAll(true);
69                         }
70                 }
71
72                 pKeysList->RemoveAll();
73                 delete pKeysList;
74                 __pAppHashMap->RemoveAll(true);
75                 delete __pAppHashMap;
76                 __pAppHashMap = null;
77         }
78
79         __pDataListener = null;
80 }
81
82 result
83 HomePresentationModel::Construct(void)
84 {
85         result r = E_SUCCESS;
86         __totalPages = 1;
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();
93
94 CATCH:
95
96         return r;
97 }
98
99 HomeItemInfo*
100 HomePresentationModel::GetItem(int pageNumber, int index) const
101 {
102         ArrayList* pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
103         if (pAppsList != null)
104         {
105                 return static_cast<HomeItemInfo*>(pAppsList->GetAt(index));
106         }
107         return null;
108 }
109
110 int
111 HomePresentationModel::GetItemCount(int pageNumber) const
112 {
113         if (__pAppHashMap != null)
114         {
115                 ArrayList* pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(pageNumber)));
116
117                 if (pAppsList != null)
118                 {
119                         return pAppsList->GetCount();
120                 }
121         }
122         return 0;
123 }
124
125 int
126 HomePresentationModel::GetPageCount(void) const
127 {
128         return __totalPages;
129 }
130
131 void
132 HomePresentationModel::LanguageChanged(void)
133 {
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();
142
143         while (pMapEnum->MoveNext() == E_SUCCESS)
144         {
145                 ArrayList* pAppsArrayList = static_cast<ArrayList*>(pMapEnum->GetCurrent());
146
147                 if (pAppsArrayList != null)
148                 {
149                         IEnumerator* pAppsEnum = pAppsArrayList->GetEnumeratorN();
150
151                         if (pAppsEnum)
152                         {
153                                 while (pAppsEnum->MoveNext() == E_SUCCESS)
154                                 {
155                                         HomeItemInfo* pAppInfo = static_cast<HomeItemInfo*>(pAppsEnum->GetCurrent());
156
157                                         if (pAppInfo != null)
158                                         {
159                                                 PackageInfo* pPackInfo = __pPackMgr->GetPackageInfoN(pAppInfo->GetPackageId());
160
161                                                 if (pPackInfo != null)
162                                                 {
163                                                         PackageAppInfo* pPackageAppInfo = pPackInfo->GetPackageAppInfoN(pAppInfo->GetAppId());
164
165                                                         if (pPackageAppInfo != null)
166                                                         {
167                                                                 String appNewName = pPackageAppInfo->GetAppDisplayName();
168
169                                                                 if (pAppInfo->GetAppName().CompareTo(appNewName) != 0)
170                                                                 {
171                                                                         pAppInfo->SetApplicationName(appNewName);
172                                                                         pAppInfo->SetApplicationIcon(null);
173                                                                 }
174
175                                                                 delete pPackageAppInfo;
176                                                                 pPackageAppInfo = null;
177                                                         }
178
179                                                         delete pPackInfo;
180                                                         pPackInfo = null;
181                                                 }
182                                         }
183                                 }
184
185                                 delete pAppsEnum;
186                                 pAppsEnum = null;
187                         }
188                 }
189         }
190
191 CATCH:
192         if (pMapEnum != null)
193         {
194                 delete pMapEnum;
195                 pMapEnum = null;
196         }
197         if (pAppsValues != null)
198         {
199                 pAppsValues->RemoveAll(false);
200                 delete pAppsValues;
201                 pAppsValues = null;
202         }
203 }
204
205 void
206 HomePresentationModel::RearrangeItems(HomeItemInfo* pMovedAppInfo, int newPageNumber, int newIndex)
207 {
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)));
219
220         if ((newPageNumber == movedPageNumber) && (newIndex == movedAppIndex))
221         {
222                 return;
223         }
224
225         pPagesToUpdate = new ArrayList();
226         pPagesToUpdate->Construct();
227         pPageToUpdate = new (std::nothrow) Integer(movedPageNumber);
228         r = pPagesToUpdate->Add(pPageToUpdate);
229
230         if (r != E_SUCCESS)
231         {
232                 delete pPageToUpdate;
233                 pPageToUpdate = null;
234         }
235
236
237         if (movedPageNumber != newPageNumber)
238         {
239                 pPageToUpdate = new (std::nothrow) Integer(newPageNumber);
240                 r = pPagesToUpdate->Add(pPageToUpdate);
241
242                 if (r != E_SUCCESS)
243                 {
244                         delete pPageToUpdate;
245                         pPageToUpdate = null;
246                 }
247         }
248
249         if (pMovedToAppList != null && pMovedFromAppList != null)
250         {
251                 {
252                         if (movedPageNumber == newPageNumber)
253                         {
254
255                                 if (newIndex != -1 && movedAppIndex != newIndex)
256                                 {
257
258                                         if (newIndex - movedAppIndex == 1)
259                                         {
260                                                 pMovedToAppList->InsertAt(pMovedAppInfo, newIndex + 1);
261                                         }
262                                         else
263                                         {
264                                                 pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
265                                         }
266
267                                         if (newIndex > movedAppIndex)
268                                         {
269                                                 pMovedFromAppList->RemoveAt(movedAppIndex);
270                                         }
271                                         else
272                                         {
273                                                 pMovedFromAppList->RemoveAt(movedAppIndex + 1);
274                                         }
275
276                                         UpdatePositions(*pMovedFromAppList);
277                                 }
278                         }
279                         else
280                         {
281                                 int numberOfIconsInPage = pMovedToAppList->GetCount();
282
283                                 if (numberOfIconsInPage < MAX_ICONS_PER_PAGE)
284                                 {
285                                         pMovedAppInfo->SetPositionInHomeScreen(newPageNumber, newIndex);
286                                         pMovedToAppList->InsertAt(pMovedAppInfo, newIndex);
287                                         pMovedFromAppList->RemoveAt(movedAppIndex, false);
288                                         UpdatePositions(*pMovedFromAppList);
289                                         UpdatePositions(*pMovedToAppList);
290                                 }
291                                 else if (numberOfIconsInPage >= MAX_ICONS_PER_PAGE)
292                                 {
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);
301
302                                         if (pItemToGetANewLocationFor != null)
303                                         {
304                                                 nextPageNumber = newPageNumber + 1;
305                                                 // checks for the successor page having the number of icons not exceeding its maximum limit
306
307                                                 while (nextPageNumber <= __totalPages)
308                                                 {
309                                                         pNextPageAppList = static_cast<ArrayList*>(__pAppHashMap->GetValue(Integer(nextPageNumber)));
310
311                                                         if (pNextPageAppList->GetCount() < MAX_ICONS_PER_PAGE /*|| nextPageNumber == pageNumber*/)
312                                                         {
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);
319                                                                 break;
320                                                         }
321
322                                                         nextPageNumber++;
323                                                 }
324
325                                                 if (!foundLocation && __totalPages < MAX_PAGE_COUNT)
326                                                 {
327                                                         result r = E_SUCCESS;
328                                                         Integer* pPageNumber = null;
329                                                         ArrayList* pNewAppList = new (std::nothrow) ArrayList();
330                                                         pNewAppList->Construct();
331
332                                                         __totalPages++;
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));
338
339                                                         if (IsFailed(r))
340                                                         {
341                                                                 delete pPageNumber;
342                                                                 pPageNumber = null;
343                                                                 delete pNewAppList;
344                                                                 pNewAppList = null;
345                                                         }
346
347                                                         pPageToUpdate = new Integer(__totalPages);
348                                                         pPagesToUpdate->Add(pPageToUpdate);
349                                                         foundLocation = true;
350                                                 }
351
352                                                 if (pNextPageAppList != null)
353                                                 {
354                                                         UpdatePositions(*pNextPageAppList);
355                                                 }
356
357                                                 UpdatePositions(*pMovedFromAppList);
358                                                 UpdatePositions(*pMovedToAppList);
359                                         }
360                                 }
361                         }
362                 }
363         }
364
365         __pDataListener->OnUpdatePageRequestedN(pPagesToUpdate);
366         return;
367 }
368
369 void
370 HomePresentationModel::SetHomePackageEventListener(IHomePresentationModelEventListener* pListener)
371 {
372         __pDataListener = pListener;
373 }
374
375 result
376 HomePresentationModel::UpdatePageData(void)
377 {
378         result r = E_INVALID_ARG;
379
380         if (__pAppHashMap != null)
381         {
382                 IList* pKeys = __pAppHashMap->GetKeysN();
383
384                 if (pKeys != null)
385                 {
386                         for (int pageCount = 0; pageCount < pKeys->GetCount(); pageCount++)
387                         {
388                                 ArrayList* pPageApps = (ArrayList*) __pAppHashMap->GetValue(*pKeys->GetAt(pageCount));
389
390                                 if (pPageApps != null)
391                                 {
392
393                                         for (int appsCount = 0; appsCount < pPageApps->GetCount(); appsCount++)
394                                         {
395                                                 HomeItemInfo* pItemInfo = static_cast<HomeItemInfo*>(pPageApps->GetAt(appsCount));
396
397                                                 if (pItemInfo != null)
398                                                 {
399                                                         r = UpdateDataToDatabase(static_cast<HomeItemInfo*>(pItemInfo));
400                                                 }
401                                         }
402                                 }
403                         }
404
405                         pKeys->RemoveAll(false);
406                         delete pKeys;
407                         pKeys = null;
408                 }
409         }
410
411         return r;
412 }
413
414 result
415 HomePresentationModel::HomeApplicationsComparer::Compare(const Object& obj1, const Object& obj2, int& cmp) const
416 {
417         result r = E_FAILURE;
418         const HomeItemInfo* pObj1 = static_cast<const HomeItemInfo*>(&obj1);
419         const HomeItemInfo* pObj2 = static_cast<const HomeItemInfo*>(&obj2);
420
421         if (pObj1 != null && pObj2 != null)
422         {
423                 int pageIndex1 = 0;
424                 int pageNumber = 0;
425                 int pageIndex2 = 0;
426                 pObj1->GetPositionInHomeScreen(pageNumber, pageIndex1);
427                 pObj2->GetPositionInHomeScreen(pageNumber, pageIndex2);
428
429                 cmp = Integer::Compare(pageIndex1, pageIndex2);
430                 r = E_SUCCESS;
431         }
432
433         return r;
434 }
435
436 void
437 HomePresentationModel::OnPackageInstallationCompleted(const PackageId& packageId, PackageInstallationResult installationResult)
438 {
439         if (installationResult == PACKAGE_INSTALLATION_RESULT_SUCCESS)
440         {
441                 NotifyInstallationEvent(packageId);
442         }
443         else
444         {
445                 //Show error
446         }
447         return;
448 }
449
450 void
451 HomePresentationModel::OnPackageUninstallationCompleted(const PackageId& packageId, bool uninstallationResult)
452 {
453         ArrayList* pAppsList = null;
454         HomeItemInfo* pItemInfo = null;
455
456         if (uninstallationResult)
457         {
458                 if (__pDataListener != null && !packageId.IsEmpty())
459                 {
460
461                         pItemInfo = FindAppObjectInList(packageId);
462                         do
463                         {
464                                 if (pItemInfo != null)
465                                 {
466                                         int pageNumber = 0;
467                                         int appIndex = 0;
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())
473                                         {
474                                                 __pDataListener->OnUnInstallationCompleted(*pItemInfo);
475                                         }
476
477                                         if (pItemInfo != null)
478                                         {
479                                                 delete pItemInfo;
480                                                 pItemInfo = null;
481                                         }
482                                         pItemInfo = FindAppObjectInList(packageId);
483                                 }
484                         }
485                         while (pItemInfo != null);
486                 }
487         }
488         else
489         {
490                 //Show error
491         }
492
493         return;
494 }
495
496 void
497 HomePresentationModel::OnPackageInfoRetrievedN(Tizen::Base::Collection::HashMap* pAppsList, int totalApps, int totalPages)
498 {
499         UiApp* pApp = UiApp::GetInstance();
500
501         if (pAppsList != null && totalApps != 0)
502         {
503                 result r = E_SUCCESS;
504                 __pAppHashMap = pAppsList;
505                 __totalPages = totalPages;
506                 __totalApps = totalApps;
507                 r = PrepareApplicationsList();
508
509                 if (pApp != null)
510                 {
511                         ArrayList* pListToSend = new (std::nothrow) ArrayList();
512
513                         if (pListToSend != null)
514                         {
515                                 r = pListToSend->Construct();
516
517                                 if (r == E_SUCCESS)
518                                 {
519                                         Integer* pResToSend = new (std::nothrow) Integer(r);
520                                         r = pListToSend->Add(pResToSend);
521
522                                         if (r == E_SUCCESS)
523                                         {
524                                                 r = pApp->SendUserEvent(MODEL_REQUEST_INITIALIZE, pListToSend);
525                                                 return;
526                                         }
527                                         else
528                                         {
529                                                 delete pResToSend;
530                                                 delete pListToSend;
531                                         }
532                                 }
533                         }
534                 }
535         }
536
537         if (pApp != null)
538         {
539                 pApp->SendUserEvent(MODEL_REQUEST_INITIALIZE, null);
540         }
541
542         return;
543 }
544
545 HomeItemInfo*
546 HomePresentationModel::FindAppObjectInList(const Tizen::Base::String& packageIdToFind)
547 {
548         IList* pKeysList = __pAppHashMap->GetKeysN();
549
550         if (pKeysList != null)
551         {
552                 for (int keysCount = 0; keysCount < pKeysList->GetCount(); keysCount++)
553                 {
554                         ArrayList* pList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keysCount))));
555
556                         for (int itemCount = (pList->GetCount() - 1); itemCount >= 0; itemCount--)
557                         {
558                                 HomeItemInfo* pApplicationInfo = static_cast<HomeItemInfo*>(pList->GetAt(itemCount));
559
560                                 if (pApplicationInfo != null)
561                                 {
562                                         String packageId = pApplicationInfo->GetPackageId();
563
564                                         if (packageId.CompareTo(packageIdToFind) == 0)
565                                         {
566                                                 pKeysList->RemoveAll();
567                                                 delete pKeysList;
568                                                 return static_cast<HomeItemInfo*>(pApplicationInfo);
569                                         }
570                                 }
571                         }
572                 }
573                 pKeysList->RemoveAll();
574                 delete pKeysList;
575                 pKeysList = null;
576         }
577
578         return null;
579
580 }
581
582 result
583 HomePresentationModel::InitializeDatabase(void)
584 {
585         result r = E_SUCCESS;
586
587         __pDataBaseManager = GenericDatabaseManager::GetInstance();
588
589         if (__pDataBaseManager != null)
590         {
591                 ArrayList* pColumnNames = null;
592                 ArrayList* pColumnTypes = null;
593                 String* pTempColumnData = null;
594
595                 String databaseName = App::GetInstance()->GetAppRootPath();
596                 databaseName.Append(HOME_DATABASE_NAME);
597
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();
605
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));
617
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));
629
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));
640
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));
651
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));
662
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));
673
674                 r = __pDataBaseManager->CreateTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes);
675                 TryCatch(r == E_SUCCESS, , "HmDatabaseManager::CreateTable failed with error %s", GetErrorMessage(r));
676 CATCH:
677                 if (pColumnNames != null)
678                 {
679                         pColumnNames->RemoveAll(true);
680                         delete pColumnNames;
681                         pColumnNames = null;
682                 }
683
684                 if (pColumnTypes != null)
685                 {
686                         pColumnTypes->RemoveAll(true);
687                         delete pColumnTypes;
688                         pColumnTypes = null;
689                 }
690         }
691         return r;
692 }
693
694 result
695 HomePresentationModel::InsertPageDataInDataBase(const HomeItemInfo* pItemInfo)
696 {
697
698         result r = E_INVALID_ARG;
699
700         if (pItemInfo != null)
701         {
702                 ArrayList* pColumnNames = null;
703                 ArrayList* pColumnTypes = null;
704                 ArrayList* pColumnValues = null;
705                 String* pTempString = null;
706                 Integer* pTempInt = null;
707                 int pageNumber = 0;
708                 int pageIndex = 0;
709
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();
717
718                 pItemInfo->GetPositionInHomeScreen(pageNumber, pageIndex);
719
720                 //Application app id
721                 pTempString = new (std::nothrow) String(APPS_APP_ID);
722                 r = pColumnNames->Add(pTempString);
723                 TryCatch(r == E_SUCCESS, delete pTempString;
724                                  pTempString = null,
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;
729                                  pTempInt = null,
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;
734                                  pTempString = null,
735                                  "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
736
737                 //Application Name
738                 pTempString = new (std::nothrow) String(APPS_APP_NAME);
739                 r = pColumnNames->Add(pTempString);
740                 TryCatch(r == E_SUCCESS, delete pTempString;
741                                  pTempString = null,
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;
746                                  pTempInt = null,
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;
751                                  pTempString = null,
752                                  "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
753
754                 //Application IconPath
755                 pTempString = new (std::nothrow) String(APPS_APP_ICON);
756                 r = pColumnNames->Add(pTempString);
757                 TryCatch(r == E_SUCCESS, delete pTempString;
758                                  pTempString = null,
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;
763                                  pTempInt = null,
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;
768                                  pTempString = null,
769                                  "pColumnNames->Add(pTempString); failed %s", GetErrorMessage(r));
770
771                 //Application pageNumber
772                 pTempString = new (std::nothrow) String(APPS_APP_PAGE);
773                 r = pColumnNames->Add(pTempString);
774                 TryCatch(r == E_SUCCESS, delete pTempString;
775                                  pTempString = null,
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;
780                                  pTempInt = null,
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;
785                                  pTempInt = null,
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;
791                                  pTempString = null,
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;
796                                  pTempInt = null,
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;
801                                  pTempInt = null,
802                                  "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
803
804                 //update
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);
809
810 CATCH:
811
812                 if (pColumnNames != null)
813                 {
814                         pColumnNames->RemoveAll(true);
815                         delete pColumnNames;
816                         pColumnNames = null;
817                 }
818
819                 if (pColumnTypes != null)
820                 {
821                         pColumnTypes->RemoveAll(true);
822                         delete pColumnTypes;
823                         pColumnTypes = null;
824                 }
825
826                 if (pColumnValues != null)
827                 {
828                         pColumnValues->RemoveAll(true);
829                         delete pColumnValues;
830                         pColumnValues = null;
831                 }
832
833         }
834
835         return r;
836 }
837
838 result
839 HomePresentationModel::NotifyInstallationEvent(const PackageId& packageId)
840 {
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;
848         int indexOf = 0;
849         r = tempPackageId.LastIndexOf(replace, 0, indexOf);
850
851         if (!IsFailed(r) && indexOf)
852         {
853                 tempPackageId.Remove(0, replace.GetLength());
854         }
855
856         if (__pPackMgr == null)
857         {
858                 __pPackMgr = PackageManager::GetInstance();
859         }
860
861         if (__pPackMgr != null)
862         {
863                 PackageInfo* pPackInfo = __pPackMgr->GetPackageInfoN(packageId);
864
865                 if (pPackInfo != null)
866                 {
867                         IList* pAppsInPack = pPackInfo->GetPackageAppInfoListN();
868
869                         if (pAppsInPack != null)
870                         {
871                                 IEnumerator* pPackageAppInfoEnum = pAppsInPack->GetEnumeratorN();
872
873                                 while (pPackageAppInfoEnum != null && pPackageAppInfoEnum->MoveNext() == E_SUCCESS)
874                                 {
875                                         ArrayList* pAppsList = null;
876                                         bool foundPosition = false;
877                                         IList* pKeysList = __pAppHashMap->GetKeysN();
878                                         PackageAppInfo* pPackageAppInfo = static_cast<PackageAppInfo*>(pPackageAppInfoEnum->GetCurrent());
879
880                                         if (pKeysList != null && pPackageAppInfo)
881                                         {
882                                                 for (int keysCount = (pKeysList->GetCount() - 1); keysCount >= 0; keysCount--)
883                                                 {
884                                                         pItemInfo = FindAppObjectInList(tempPackageId);
885
886                                                         if (pItemInfo != null)
887                                                         {
888                                                                 pItemInfo->Initialize(pPackageAppInfo, pPackInfo);
889                                                                 pItemInfo->SetApplicationIcon(null);
890                                                                 isalreadyExist = true;
891                                                                 foundPosition = true;
892                                                                 break;
893                                                         }
894                                                 }
895
896                                                 if (!isalreadyExist)
897                                                 {
898                                                         for (int keysCount = (pKeysList->GetCount() - 1); keysCount >= 0; keysCount--)
899                                                         {
900                                                                 pAppsList = static_cast<ArrayList*>(__pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keysCount))));
901
902                                                                 if (pAppsList != null)
903                                                                 {
904                                                                         if (pAppsList->GetCount() < MAX_ICONS_PER_PAGE)
905                                                                         {
906                                                                                 pItemInfo = new (std::nothrow) HomeItemInfo();
907
908                                                                                 if (pItemInfo != null)
909                                                                                 {
910                                                                                         if (pItemInfo->Initialize(pPackageAppInfo, pPackInfo) == E_SUCCESS)
911                                                                                         {
912                                                                                                 if (pItemInfo->IsVisibleInMainMenu())
913                                                                                                 {
914                                                                                                         __totalApps++;
915                                                                                                         pItemInfo->SetPositionInHomeScreen(keysCount + 1, pAppsList->GetCount());
916                                                                                                         pAppsList->Add(pItemInfo);
917                                                                                                         foundPosition = true;
918                                                                                                         break;
919                                                                                                 }
920                                                                                                 else
921                                                                                                 {
922                                                                                                         continue;
923                                                                                                 }
924
925                                                                                         }
926                                                                                         else
927                                                                                         {
928                                                                                                 delete pItemInfo;
929                                                                                                 pItemInfo = null;
930                                                                                         }
931                                                                                 }
932                                                                         }
933                                                                 }
934                                                         }
935                                                 }
936
937                                                 if (!foundPosition)
938                                                 {
939                                                         pNewAppList = new (std::nothrow) ArrayList();
940
941                                                         if (pNewAppList != null)
942                                                         {
943                                                                 r = pNewAppList->Construct();
944
945                                                                 if (pItemInfo == null)
946                                                                 {
947                                                                         pItemInfo = new (std::nothrow) HomeItemInfo();
948                                                                         r = pItemInfo->Initialize(pPackageAppInfo, pPackInfo);
949                                                                         TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
950                                                                                          GetErrorMessage(r));
951                                                                 }
952                                                                 if (pItemInfo->IsVisibleInMainMenu())
953                                                                 {
954                                                                         __totalApps++;
955                                                                         __totalPages++;
956                                                                         pItemInfo->SetPositionInHomeScreen(__totalPages, 0);
957                                                                         r = pNewAppList->Add(pItemInfo);
958                                                                         TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
959                                                                                          GetErrorMessage(r));
960                                                                         pNewKey = new (std::nothrow) Integer(__totalPages);
961                                                                         r = __pAppHashMap->Add(pNewKey, pNewAppList);
962                                                                         TryCatch(r == E_SUCCESS, , "failed to construct pNewAppList %s",
963                                                                                          GetErrorMessage(r));
964                                                                 }
965                                                         }
966                                                 }
967                                         }
968                                         if (pKeysList != null)
969                                         {
970                                                 pKeysList->RemoveAll();
971                                                 delete pKeysList;
972                                                 pKeysList = null;
973                                         }
974                                 }
975                                 if (pPackageAppInfoEnum != null)
976                                 {
977                                         delete pPackageAppInfoEnum;
978                                         pPackageAppInfoEnum = null;
979                                 }
980                                 if (pAppsInPack != null)
981                                 {
982                                         delete pAppsInPack;
983                                         pAppsInPack = null;
984                                 }
985                         }
986
987                         delete pPackInfo;
988                         pPackInfo = null;
989                 }
990                 else
991                 {
992                         r = E_SUCCESS;
993                 }
994         }
995         else
996         {
997                 r = GetLastResult();
998         }
999
1000         if (__pDataListener != null && pItemInfo != null)
1001         {
1002                 __pDataListener->OnInstallationCompleted(*pItemInfo /*, isalreadyExist*/);
1003         }
1004
1005         return r;
1006
1007 CATCH:
1008
1009         if (pItemInfo != null)
1010         {
1011                 delete pItemInfo;
1012                 pItemInfo = null;
1013         }
1014
1015         if (pNewAppList != null)
1016         {
1017                 delete pNewAppList;
1018                 pNewAppList = null;
1019         }
1020
1021         if (pNewKey != null)
1022         {
1023                 delete pNewKey;
1024                 pNewKey = null;
1025         }
1026
1027         return r;
1028 }
1029
1030 result
1031 HomePresentationModel::PrepareApplicationsList(void)
1032 {
1033         result r = E_SUCCESS;
1034         r = UpdateWithPositions();
1035         SortPages();
1036         return r;
1037 }
1038
1039 void
1040 HomePresentationModel::SortPages(void)
1041 {
1042         if (__pAppHashMap != null)
1043         {
1044                 IList* pKeys = null;
1045                 ArrayList* pValues = null;
1046                 IEnumerator* pKeysEnum = null;
1047
1048                 pKeys = __pAppHashMap->GetKeysN();
1049
1050                 if (pKeys != null)
1051                 {
1052                         pKeysEnum = pKeys->GetEnumeratorN();
1053
1054                         if (pKeysEnum != null)
1055                         {
1056                                 while (pKeysEnum != null && pKeysEnum->MoveNext() == E_SUCCESS)
1057                                 {
1058                                         Integer* pKeyName = static_cast<Integer*>(pKeysEnum->GetCurrent());
1059                                         pValues = static_cast<ArrayList*>(__pAppHashMap->GetValue(*pKeyName));
1060
1061                                         if (pValues != null)
1062                                         {
1063                                                 pValues->Sort(__comparerObject);
1064                                                 UpdatePositions(*pValues);
1065                                         }
1066                                 }
1067                         }
1068
1069                         if (pKeysEnum != null)
1070                         {
1071                                 delete pKeysEnum;
1072                                 pKeysEnum = null;
1073                         }
1074
1075                         pKeys->RemoveAll(false);
1076                         delete pKeys;
1077                         pKeys = null;
1078                 }
1079         }
1080 }
1081
1082 result
1083 HomePresentationModel::UpdateDataToDatabase(HomeItemInfo* pItemInfo)
1084 {
1085         result r = E_INVALID_ARG;
1086
1087         if (pItemInfo != null)
1088         {
1089                 ArrayList* pColumnNames = null;
1090                 ArrayList* pColumnTypes = null;
1091                 ArrayList* pColumnValues = null;
1092                 String* whereCondition = null;
1093                 String* pTempString = null;
1094                 Integer* pTempInt = null;
1095                 int pageNumber = 0;
1096                 int pageIndex = 0;
1097
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();
1105
1106                 pItemInfo->GetPositionInHomeScreen(pageNumber, pageIndex);
1107
1108                 //Application pageNumber
1109                 pTempString = new (std::nothrow) String(APPS_APP_PAGE);
1110                 r = pColumnNames->Add(pTempString);
1111                 TryCatch(r == E_SUCCESS, delete pTempString;
1112                                  pTempString = null,
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;
1117                                  pTempInt = null,
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;
1122                                  pTempInt = null,
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;
1128                                  pTempString = null,
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;
1133                                  pTempInt = null,
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;
1138                                  pTempInt = null,
1139                                  "pColumnValues->Add(pTempInt); failed %s", GetErrorMessage(r));
1140
1141                 //update
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'\"');
1147
1148                 r = __pDataBaseManager->UpdateTable(HOME_APPLICATIONS_TABLE, pColumnNames, pColumnTypes, pColumnValues, whereCondition);
1149
1150 CATCH:
1151
1152                 if (pColumnNames != null)
1153                 {
1154                         pColumnNames->RemoveAll(true);
1155                         delete pColumnNames;
1156                         pColumnNames = null;
1157                 }
1158
1159                 if (pColumnTypes != null)
1160                 {
1161                         pColumnTypes->RemoveAll(true);
1162                         delete pColumnTypes;
1163                         pColumnTypes = null;
1164                 }
1165
1166                 if (pColumnValues != null)
1167                 {
1168                         pColumnValues->RemoveAll(true);
1169                         delete pColumnValues;
1170                         pColumnValues = null;
1171                 }
1172
1173                 if (whereCondition)
1174                 {
1175                         delete whereCondition;
1176                         whereCondition = null;
1177                 }
1178
1179         }
1180
1181         return r;
1182 }
1183
1184 void
1185 HomePresentationModel::UpdatePositions(Tizen::Base::Collection::ArrayList& pAppListToUpdate)
1186 {
1187         for (int appIndex = 0; appIndex < pAppListToUpdate.GetCount(); appIndex++)
1188         {
1189                 HomeItemInfo* pItemInfo = static_cast<HomeItemInfo*>(pAppListToUpdate.GetAt(appIndex));
1190                 pItemInfo->SetItemIndex(appIndex);
1191         }
1192
1193         pAppListToUpdate.Sort(__comparerObject);
1194 }
1195
1196 result
1197 HomePresentationModel::UpdateWithPositions(void)
1198 {
1199         result r = E_INVALID_STATE;
1200         //Sanity test
1201         if (__pDataBaseManager == null)
1202         {
1203                 r = InitializeDatabase();
1204
1205                 if (IsFailed(r))
1206                 {
1207                         AppLogException("Home_Exception::InitializeDataBase() failed with error = %s", GetErrorMessage(r));
1208                         return r;
1209                 }
1210         }
1211
1212         if (__pAppHashMap != null)
1213         {
1214                 IList* pKeys = null;
1215                 ArrayList* pValues = null;
1216                 IEnumerator* pKeysEnum = null;
1217
1218                 pKeys = __pAppHashMap->GetKeysN();
1219
1220                 if (pKeys != null)
1221                 {
1222                         pKeysEnum = pKeys->GetEnumeratorN();
1223
1224                         if (pKeysEnum != null)
1225                         {
1226                                 while (pKeysEnum != null && pKeysEnum->MoveNext() == E_SUCCESS)
1227                                 {
1228                                         Integer* pKeyName = static_cast<Integer*>(pKeysEnum->GetCurrent());
1229                                         pValues = static_cast<ArrayList*>(__pAppHashMap->GetValue(*pKeyName));
1230
1231                                         if (pValues != null)
1232                                         {
1233
1234                                                 for (int appCount = 0; appCount < pValues->GetCount(); appCount++)
1235                                                 {
1236                                                         HomeItemInfo* pItemInfo = dynamic_cast<HomeItemInfo*>(pValues->GetAt(appCount));
1237
1238                                                         if (pItemInfo != null)
1239                                                         {
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);
1249
1250                                                                 if (!IsFailed(r) && pRowData->GetCount())
1251                                                                 {
1252
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)));
1256
1257                                                                         if (pPageNumber != null && pPageIndex != null)
1258                                                                         {
1259                                                                                 pItemInfo->SetPositionInHomeScreen(pPageNumber->ToInt(), pPageIndex->ToInt());
1260                                                                         }
1261
1262                                                                 }
1263                                                                 else
1264                                                                 {
1265                                                                         r = InsertPageDataInDataBase(pItemInfo);
1266                                                                         r = E_SUCCESS;
1267                                                                 }
1268
1269                                                                 if (pRowData != null)
1270                                                                 {
1271                                                                         pRowData->RemoveAll(true);
1272                                                                         delete pRowData;
1273                                                                         pRowData = null;
1274                                                                 }
1275                                                         }
1276                                                 }
1277                                         }
1278                                 }
1279                         }
1280                         //CATCH:
1281                         if (pKeysEnum != null)
1282                         {
1283                                 delete pKeysEnum;
1284                                 pKeysEnum = null;
1285                         }
1286
1287                         pKeys->RemoveAll(false);
1288                         delete pKeys;
1289                         pKeys = null;
1290                 }
1291         }
1292
1293         return r;
1294
1295 }