Flora license update
[apps/osp/Home.git] / src / HmPackageInfoListRetrieveThread.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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        PackageInfoListRetrieveThread.h
19  * @brief       The file contains declaration of PackageInfoListRetrieveThread,
20  * which is a worker thread and being used to retrieve the list PackageInfos, for the Applications installed on device.
21  */
22
23 #include <FApp.h>
24 #include "HmHomeItemInfo.h"
25 #include "HmPackageInfoListRetrieveThread.h"
26 #include "HmTypes.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::App::Package;
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32
33 PackageInfoListRetrieveThread::PackageInfoListRetrieveThread(IPackageInfoListListener* pPackageListener)
34         : __pPackageListener(pPackageListener)
35 {
36         // No implementation required
37
38 }
39
40 PackageInfoListRetrieveThread::~PackageInfoListRetrieveThread(void)
41 {
42         // No implementation required
43 }
44
45 result
46 PackageInfoListRetrieveThread::SetPackageType(void)
47 {
48         return Start();
49 }
50
51 Object*
52 PackageInfoListRetrieveThread::Run(void)
53 {
54         result r = E_SUCCESS;
55         PackageManager* pPackMgr = PackageManager::GetInstance();
56         String currentAppName = UiApp::GetInstance()->GetAppName();
57         int currentPage = 1;
58         int currentPosition = 0;
59         int totalApps = 0;
60         int totalPages = 0;
61         HashMap* pAppHashMap = null;
62         IList* pPackList = null;
63         IEnumerator* pEnum = null;
64
65         if (pAppHashMap == null)
66         {
67                 ArrayList* pArrayList = null;
68                 Integer* pKey = null;
69                 // constructs hashmap to maintain key(page number) value(list of icons associated to the page) pairs of all pages
70                 pAppHashMap = new (std::nothrow) HashMap();
71                 r = pAppHashMap->Construct();
72                 pArrayList = new (std::nothrow) ArrayList();
73                 r = pArrayList->Construct();
74                 pKey = new (std::nothrow) Integer(currentPage);
75                 r = pAppHashMap->Add(pKey, pArrayList); //transfers ownership to the hashmap
76                 TryCatch(r == E_SUCCESS, delete pKey;
77                                  pKey = null;
78                                  delete pArrayList;
79                                  pArrayList = null, "pAppHashMap->Add(*pKey, *pArrayList) failed with error = %s", GetErrorMessage(r));
80
81         }
82
83         if (pPackMgr != null)
84         {
85                 pPackList = pPackMgr->GetPackageInfoListN();
86
87                 if (pPackList == null)
88                 {
89                         __pPackageListener->OnPackageInfoRetrievedN(pAppHashMap, totalApps, totalPages);
90                         return null;
91                 }
92
93                 pEnum = pPackList->GetEnumeratorN();
94
95                 while (pEnum != null && pEnum->MoveNext() == E_SUCCESS)
96                 {
97 //                      PackageType type = PACKAGE_TYPE_TPK;
98                         HomeItemInfo* pItemInfo = null;
99                         ArrayList* pAppsList = null;
100                         IList* pPackAppInfoList = null;
101                         IEnumerator* pAppInfoEnumerator = null;
102                         PackageInfo* pInfo = static_cast<PackageInfo*>(pEnum->GetCurrent());
103                         TryCatch(pInfo != null, r = GetLastResult(), "pInfo is null error %s", GetErrorMessage(GetLastResult()));
104                         pPackAppInfoList = pInfo->GetPackageAppInfoListN();
105
106                         if (pPackAppInfoList != null)
107                         {
108                                 pAppInfoEnumerator = pPackAppInfoList->GetEnumeratorN();
109
110                                 while (pAppInfoEnumerator != null && pAppInfoEnumerator->MoveNext() == E_SUCCESS)
111                                 {
112                                         PackageAppInfo* pPackAppInfo = static_cast<PackageAppInfo*>(pAppInfoEnumerator->GetCurrent());
113                                         pAppsList = static_cast<ArrayList*>(pAppHashMap->GetValue(Integer(currentPage)));
114                                         TryCatch(pAppsList != null, r = GetLastResult(), "pInfo is null error %s", GetErrorMessage(GetLastResult()));
115                                         // creates an instance of HomeItemInfo for each application to store the details of the application.
116                                         pItemInfo = new (std::nothrow) HomeItemInfo();
117                                         r = pItemInfo->Initialize(pPackAppInfo, pInfo);
118
119                                         if (!pItemInfo->IsVisibleInMainMenu() || pItemInfo->GetIconPath().IsEmpty())
120                                         {
121                                                 delete pItemInfo;
122                                                 pItemInfo = null;
123                                                 continue;
124                                         }
125                                         totalApps++;
126                                         pItemInfo->SetPositionInHomeScreen(currentPage, currentPosition);
127
128                                         // adds to the list if not service application
129                                         //                              if (!pItemInfo->GetAppName().Contains(L"service")
130                                         //                                              && !pItemInfo->GetAppName().Equals(currentAppName, true))
131                                         {
132                                                 currentPosition++;
133                                                 r = pAppsList->Add(pItemInfo);
134
135                                                 if (IsFailed(r))
136                                                 {
137                                                         AppLogException("Failed to pAppsList->Add(pItemInfo) error %s", GetErrorMessage(r));
138                                                         delete pItemInfo;
139                                                         pItemInfo = null;
140                                                         continue;
141                                                 }
142
143                                                 if (currentPosition >= MAX_ICONS_PER_PAGE)
144                                                 {
145                                                         currentPage++;
146                                                         currentPosition = 0;
147                                                         ArrayList* pArrayList = new (std::nothrow) ArrayList();
148                                                         pArrayList->Construct();
149                                                         Integer* pKey = new (std::nothrow) Integer(currentPage);
150                                                         pAppHashMap->Add(pKey, pArrayList); //transfers ownership to the hashmap
151                                                 }
152                                         }
153                                         //                              else
154                                         //                              {
155                                         //                                      delete pItemInfo;
156                                         //                                      pItemInfo = null;
157                                         //                              }
158                                 }
159                                 if (pAppInfoEnumerator != null)
160                                 {
161                                         delete pAppInfoEnumerator;
162                                         pAppInfoEnumerator = null;
163                                 }
164                         }
165
166                         pPackAppInfoList->RemoveAll();
167                         delete pPackAppInfoList;
168                         pPackAppInfoList = null;
169                 }
170
171                 if (currentPosition == 0 && currentPage > 1)
172                 {
173                         pAppHashMap->Remove(Integer(currentPage), true);
174                         totalPages = currentPage - 1;
175                 }
176                 else
177                 {
178                         totalPages = currentPage;
179                 }
180
181                 __pPackageListener->OnPackageInfoRetrievedN(pAppHashMap, totalApps, totalPages);
182                 pAppHashMap = null; //ownership transferred to the caller
183
184                 if (pEnum != null)
185                 {
186                         delete pEnum;
187                         pEnum = null;
188                 }
189
190                 if (pPackList != null)
191                 {
192                         pPackList->RemoveAll(true);
193                         delete pPackList;
194                         pPackList = null;
195                 }
196         }
197
198         return null;
199
200 CATCH:
201
202         if (pEnum != null)
203         {
204                 delete pEnum;
205                 pEnum = null;
206         }
207
208         if (pPackList != null)
209         {
210                 pPackList->RemoveAll(true);
211                 delete pPackList;
212                 pPackList = null;
213         }
214
215         if (pAppHashMap != null)
216         {
217                 if (pAppHashMap->GetCount() > 0)
218                 {
219                         IList* pKeysList = pAppHashMap->GetKeysN();
220
221                         for (int keyCount = 0; keyCount < pKeysList->GetCount(); keyCount++)
222                         {
223                                 ArrayList* pAppList = static_cast<ArrayList*>(pAppHashMap->GetValue(*static_cast<Integer*>(pKeysList->GetAt(keyCount))));
224
225                                 if (pAppList != null)
226                                 {
227                                         pAppList->RemoveAll(true);
228                                 }
229                         }
230
231                         pKeysList->RemoveAll();
232                         delete pKeysList;
233                 }
234
235                 pAppHashMap->RemoveAll(true);
236                 delete pAppHashMap;
237                 pAppHashMap = null;
238         }
239
240         return null;
241 }