Check icon path
[apps/native/home/homescreen.git] / mainmenu / mainmenu-package-manager.cpp
1 /*
2  *  mainmenu package manager
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <string>
21 #include <sstream>
22 #include <iostream>
23 #include <memory>
24 #include <algorithm>
25 #include <db-util.h>
26 #include <pkgmgr-info.h>
27
28
29
30 #include "cluster-home-debug.h"
31 #include "mainmenu-package-manager.h"
32
33
34
35 using namespace std;
36
37 CMainMenuPackageManager::CMainMenuPackageManager()
38 : m_pClient(NULL)
39 , m_bListenCbPaused(false)
40 #ifdef TIZEN_3_0
41 , m_hEdbus(NULL)
42 , m_connEdbus(NULL)
43 #endif
44 , m_bMMCMounted(false)
45 {
46
47 }
48
49
50 CMainMenuPackageManager::~CMainMenuPackageManager()
51 {
52
53         //UnregisterEdbusSignalHandler();
54 }
55
56 static void _OnMMCStatusChangedCb(keynode_t * node, void *data)
57 {
58         int status;
59         int ret = vconf_get_int("memory/sysman/mmc", &status);
60
61         CMainMenuPackageManager *pManager = static_cast<CMainMenuPackageManager*>(data);
62
63         pManager->OnMMCStatusChanged(status);
64
65         HOME_DBG("memory/sysman/mmc ret[%d] status [%d]", ret, status);
66 }
67
68 void CMainMenuPackageManager::OnMMCStatusChanged(int status)
69 {
70         HOME_DBG("BEGIN status[%d]", status);
71         m_bMMCMounted = status;
72 #ifdef TIZEN_3_0
73
74         if (PMINFO_R_OK !=  pkgmgrinfo_appinfo_get_unmounted_list(_GetUnmountedAppsInfoCb, this))
75         {
76                 HOME_ERR("pkgmgrinfo_appinfo_get_unmounted_list return false");
77                 return;
78         }
79
80         if (status == 1)
81                 if (PMINFO_R_OK !=  pkgmgrinfo_appinfo_get_mounted_list(_GetMountedAppsInfoCb, this))
82                 {
83                         HOME_ERR("pkgmgrinfo_appinfo_get_mounted_list return false");
84                         return;
85                 }
86 #endif
87         for(auto iter = m_AppInfoCache.begin(); iter != m_AppInfoCache.end(); iter++)
88         {
89                 BoxDataPtr appInfoData = iter->second;
90                 if (appInfoData->IsMMCApp())
91                 {
92                         HOME_DBG("app id [%s]", appInfoData->GetAppId().c_str());
93                         m_fAppMoved(appInfoData->GetAppId().c_str());
94                 }
95         }
96         HOME_DBG("END status[%d]", status);
97 }
98
99 int CMainMenuPackageManager::_PkgMgrClientListenCb(uid_t target_uid, int req_id, const char *pkg_type, const char *package, const char *key, const char *val, const void *pmsg, void *data)
100 {
101         CMainMenuPackageManager *pManager = static_cast<CMainMenuPackageManager*>(data);
102
103         pManager->OnClientListenCb( target_uid,req_id, pkg_type, package, key, val, pmsg, data);
104
105         return 0;
106 }
107
108 #ifdef TIZEN_3_0
109 static void _OnBootingDone(void *data, DBusMessage *msg)
110 {
111         int ret, status;
112
113         ret = vconf_get_int("memory/sysman/mmc", &status);
114         HOME_DBG("memory/sysman/mmc ret[%d] status [%d]", ret, status);
115
116         CMainMenuPackageManager *pManager = static_cast<CMainMenuPackageManager*>(data);
117
118         pManager->OnMMCStatusChanged(status);
119
120         pManager->UnregisterEdbusSignalHandler();
121 }
122 #endif
123
124
125 bool CMainMenuPackageManager::_RegisterEdbusSignalHandler()
126 {
127         int retry;
128
129         HOME_DBG("Register edbus signal handler");
130         retry = 0;
131 #ifdef TIZEN_3_0
132         while (e_dbus_init() == 0) {
133                 retry++;
134                 if (retry >= 10) {
135                         HOME_ERR("e_dbus_init failed");
136                         return FALSE;
137                 }
138         }
139         m_connEdbus = e_dbus_bus_get(DBUS_BUS_SYSTEM);
140         if (!(m_connEdbus)) {
141                 HOME_ERR("e_dbus_bus_get failed");
142                 goto edbus_handler_out;
143         }
144
145         m_hEdbus = e_dbus_signal_handler_add(m_connEdbus, NULL, "/Org/Tizen/System/DeviceD/Core",
146                                                                 "org.tizen.system.deviced.core", "BootingDone", _OnBootingDone, NULL);
147         if (!(m_hEdbus)) {
148                 HOME_ERR( "edbus_coord_rotation_handler failed");
149                 goto edbus_handler_connection_out;
150         }
151 #endif
152         return true;
153
154 #ifdef TIZEN_3_0
155 edbus_handler_connection_out:
156         
157         if (m_connEdbus)
158                 e_dbus_connection_close(m_connEdbus);
159         m_connEdbus = NULL;
160 edbus_handler_out:
161         e_dbus_shutdown();
162         return FALSE;
163 #endif
164 }
165
166 void CMainMenuPackageManager::UnregisterEdbusSignalHandler()
167 {
168 #ifdef TIZEN_3_0
169
170         if (m_connEdbus && m_hEdbus) {
171                 e_dbus_signal_handler_del(m_connEdbus, m_hEdbus);
172                 m_hEdbus = NULL;
173
174         }
175
176         if (m_connEdbus) {
177                 e_dbus_connection_close(m_connEdbus);
178                 m_connEdbus = NULL;
179         }
180         e_dbus_shutdown();
181 #endif
182 }
183
184
185 bool CMainMenuPackageManager::Initialize(const std::function<void(const char*)>& fAppInstalled,
186                                                                                 const std::function<void(const char*)>& fAppUpdated,
187                                                                                 const std::function<void(const char*)>& fAppUninstalled,
188                                                                                 const std::function<void(const char*)>& fAppMoved)
189 {
190         int ret;//, status;
191         m_fAppInstalled = fAppInstalled;
192         m_fAppUpdated = fAppUpdated;
193         m_fAppUninstalled = fAppUninstalled;
194         m_fAppMoved = fAppMoved;
195
196         if (vconf_notify_key_changed("memory/sysman/mmc", _OnMMCStatusChangedCb, this) != 0)
197         {
198                 HOME_DBG("Failed to notify memory/sysman/mmc");
199         }
200
201         m_pClient = pkgmgr_client_new(PC_LISTENING);
202         if ( (ret = pkgmgr_client_listen_status( m_pClient, _PkgMgrClientListenCb, this)) != PKGMGR_R_OK)
203         {
204                 HOME_ERR("pkgmgr_client_listen_status return [%d]", ret);
205                 //return false;
206         }
207
208         return true;
209 }
210
211 bool CMainMenuPackageManager::Terminate(void)
212 {
213         if (pkgmgr_client_free(m_pClient) != PKGMGR_R_OK)
214         {
215                 HOME_ERR("cannot free pkgmgr_client for listen.");
216                 return false;
217         }
218
219         return true;
220 }
221
222 int CMainMenuPackageManager::_PkgMgrAppInfoGetListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
223 {
224         std::vector<std::string>* pList = static_cast<std::vector<std::string>*>(user_data);
225         char *appid = NULL;
226         bool bNoDisplay = false;
227
228         if (PMINFO_R_OK != pkgmgrinfo_appinfo_is_nodisplay(handle, &bNoDisplay)) return 0;
229
230         HOME_DBG("NoDisplay [%d]", bNoDisplay);
231         if (bNoDisplay == false)
232         {
233                 if (PMINFO_R_OK != pkgmgrinfo_appinfo_get_appid(handle, &appid)) return 0;
234
235                 HOME_DBG("app Id [%s]", appid);
236
237                 pList->push_back(appid);
238         }
239         else
240         {
241                 HOME_ERR("this package is no display");
242         }
243
244         for(auto iter = pList->begin(); iter != pList->end(); iter++)
245         {
246                 HOME_ERR("##### [%s]", iter->c_str());
247         }
248
249         return 0;
250 }
251
252 int CMainMenuPackageManager::_PkgMgrDeactivatedAppInfoGetListCb(pkgmgrinfo_pkginfo_h handle, void *user_data)
253 {
254         std::vector<std::string>* pList = static_cast<std::vector<std::string>*>(user_data);
255         char *sAppId = NULL;
256
257         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_get_mainappid(handle, &sAppId)) return 0;
258
259         HOME_DBG("Deactivated app Id [%s]", sAppId);
260
261         if (sAppId)
262         {
263                 pList->push_back(sAppId);
264         }
265
266         return 0;
267 }
268
269 int CMainMenuPackageManager::_PkgMgrDeactivatedAppInfoGetCb(pkgmgrinfo_appinfo_h handle, void *user_data)
270 {
271         std::vector<std::string>* pList = static_cast<std::vector<std::string>*>(user_data);
272
273         char *sPkgId = NULL;
274         char *sName = NULL;
275         char *sIcon = NULL;
276
277         if (PMINFO_R_OK != pkgmgrinfo_appinfo_get_pkgid(handle, &sPkgId))
278         {
279                 VIEWER_ERR("Failed to get pkgid");
280         }
281         if (PMINFO_R_OK != pkgmgrinfo_appinfo_get_icon(handle, &sIcon))
282         {
283                 VIEWER_ERR("Failed to get icon");
284         }
285         if (PMINFO_R_OK != pkgmgrinfo_appinfo_get_label(handle, &sName))
286         {
287                 VIEWER_ERR("Failed to get name");
288         }
289
290         // CHECK : is other info needed?
291
292         VIEWER_DBG("Deactivated pkg info [%s][%s][%s]", sPkgId, sIcon, sName);
293
294         if (sPkgId)
295         {
296                 pList->push_back(sPkgId);
297         }
298         if (sIcon && *sIcon)
299         {
300                 pList->push_back(sIcon);
301         }
302         else
303         {
304                 pList->push_back(RESDIR"/images/default_app_icon.png");
305
306         }
307
308         if (sName)
309         {
310                 pList->push_back(sName);
311         }
312
313         return 0;
314 }
315
316 int CMainMenuPackageManager::_PkgMgrDeactivatedPkgInfoGetCb(pkgmgrinfo_pkginfo_h handle, void *user_data)
317 {
318         std::vector<std::string>* pList = static_cast<std::vector<std::string>*>(user_data);
319
320         char *sPkgId = NULL;
321         char *sPkgType = NULL;
322
323         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_get_pkgid(handle, &sPkgId))
324         {
325                 VIEWER_ERR("Failed to get pkgid");
326         }
327         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_get_type(handle, &sPkgType))
328         {
329                 VIEWER_ERR("Failed to get type");
330         }
331
332         VIEWER_DBG("Deactivated pkg info [%s][%s]", sPkgId, sPkgType);
333
334         if (sPkgType)
335         {
336                 pList->push_back(sPkgType);
337         }
338
339         return 0;
340 }
341
342 std::vector<std::string> CMainMenuPackageManager::_GetAppIds(const char* pPackage)
343 {
344         HOME_ERR("BEGIN");
345         std::vector<std::string> list;
346         pkgmgrinfo_pkginfo_h handle = NULL;
347         if (pkgmgrinfo_pkginfo_get_pkginfo(pPackage, &handle) == PMINFO_R_OK )
348         {
349                 if (pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, _PkgMgrAppInfoGetListCb, &list) != PMINFO_R_OK)
350                 {
351                         HOME_ERR("pkgmgrinfo_appinfo_get_list  != PMINFO_R_OK");
352                 }
353
354                 if (handle)
355                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
356
357         }
358         else
359         {
360                 HOME_ERR("pkgmgrinfo_pkginfo_get_pkginfo[%s]  != PMINFO_R_OK ", pPackage);
361         }
362
363         for(auto iter = list.begin(); iter != list.end(); iter++)
364         {
365                 HOME_ERR("##### [%s]", iter->c_str());
366         }
367
368
369         HOME_ERR("END");
370
371         return list;
372 }
373
374 bool CMainMenuPackageManager::StartListenCb()
375 {
376         m_bListenCbPaused = false;
377
378         for(auto iter = m_PkgReqQueue.begin(); iter != m_PkgReqQueue.end(); iter++)
379         {
380                 _DoPkgJob(*iter);
381         }
382
383         m_PkgReqQueue.clear();
384         return true;
385 }
386
387 bool CMainMenuPackageManager::PauseListenCb()
388 {
389         m_bListenCbPaused = true;
390         return true;
391 }
392
393 bool CMainMenuPackageManager::_DoPkgJob(shared_ptr<PkgData> data)
394 {
395         if ( data->m_eType == REQ_INSTALL || data->m_eType == REQ_UPDATE)
396                 data->m_AppIds = _GetAppIds(data->m_strPackage.c_str());
397         HOME_ERR("#Step 3 size[%d]", data->m_AppIds.size());
398         for(auto iter2 = data->m_AppIds.begin(); iter2 != data->m_AppIds.end(); iter2++)
399         {
400                 std::string strAppId(*iter2);
401                 HOME_ERR("appId[%s]", strAppId.c_str());
402
403                 auto iter = m_AppInfoCache.find(strAppId);
404
405                 if (iter != m_AppInfoCache.end())
406                         m_AppInfoCache.erase(iter);
407
408                 switch(data->m_eType)
409                 {
410                 case REQ_INSTALL:
411                 {
412                         BoxDataPtr data(new CMainMenuBoxData);
413                         if (_GetAppInfo(strAppId, data))
414                         {
415                                 if (data->IsEnabled())
416                                         m_fAppInstalled(strAppId.c_str());
417                         }
418                         break;
419                 }
420                 case REQ_UNINSTALL:
421                         m_fAppUninstalled(strAppId.c_str());
422                         break;
423                 case REQ_UPDATE:
424                 {
425                         BoxDataPtr data(new CMainMenuBoxData);
426                         if (_GetAppInfo(strAppId, data))
427                         {
428                                 if (data->IsEnabled())
429                                         m_fAppUpdated(strAppId.c_str());
430                                 else
431                                         m_fAppUninstalled(strAppId.c_str());
432
433                         }
434                         break;
435                 }
436                 case REQ_NONE:
437                         break;
438                 }
439         }
440
441         return true;
442 }
443
444 int CMainMenuPackageManager::OnClientListenCb(uid_t target_uid,int req_id, const char *pkg_type, const char *package, const char *key, const char *val, const void *pmsg, void *data)
445 {
446         HOME_DBG("req_id[%d] pkg_type[%s] package[%s] key[%s] val[%s] pmsg[%s]", req_id, pkg_type, package, key, val, pmsg);
447
448         std::string strKey(key), strVal(val), strPkg(package);
449
450         if (strKey.compare("start") == 0 )
451         {
452                 shared_ptr<PkgData> data(new PkgData);
453
454                 if (strVal.compare("install") == 0) {
455                         data->m_eType = REQ_INSTALL;
456                 } else if (strVal.compare("update") == 0) {
457                         data->m_eType = REQ_UPDATE;
458                 } else if (strVal.compare("uninstall") == 0) {
459                         data->m_eType = REQ_UNINSTALL;
460                         data->m_AppIds = _GetAppIds(package);
461                 } else {
462                         data->m_eType = REQ_NONE;
463                         HOME_ERR("Req Val [%s:%s]", key, val);
464                 }
465
466                 data->m_nReqId = req_id;
467                 data->m_strPackage = strPkg;
468
469                 m_PkgReqList[strPkg] = data;
470         }
471         else if (strKey.compare("end") == 0 && strVal.compare("ok") == 0)
472         {
473                 HOME_ERR("#Step 1");
474                 auto iter = m_PkgReqList.find(strPkg);
475                 if (iter != m_PkgReqList.end())
476                 {
477                         HOME_ERR("#Step 2");
478                         shared_ptr<PkgData> data = m_PkgReqList[strPkg];
479                         m_PkgReqList.erase(iter);
480
481                         if (m_bListenCbPaused)
482                         {
483                                 m_PkgReqQueue.push_back(data);
484                         }
485                         else
486                         {
487                                 _DoPkgJob(data);
488                         }
489                 }
490
491         }
492         else if (strKey.compare("end") == 0 && strVal.compare("fail") == 0)
493         {
494                 auto iter = m_PkgReqList.find(strPkg);
495                 if (iter != m_PkgReqList.end())
496                 {
497                         m_PkgReqList.erase(iter);
498                 }
499         }
500
501         return 0;
502 }
503
504 bool CMainMenuPackageManager::GetAppInfo(string appId, BoxDataPtr data)
505 {
506         auto iter = m_AppInfoCache.find(appId);
507
508         if (iter != m_AppInfoCache.end())
509         {
510                 BoxDataPtr appInfoData = iter->second;
511
512                 if (appInfoData->GetName().length() > 0 && appInfoData->GetIconPath().length())
513                 {
514                         data->SetPkgId(appInfoData->GetPkgId());
515                         data->SetPkgType(appInfoData->GetPkgType());
516
517                         data->SetIconPath(appInfoData->GetIconPath());
518
519                         data->SetName(appInfoData->GetName());
520                         data->SetNoDisplay(appInfoData->IsNoDisplay());
521                         data->SetEnabled(appInfoData->IsEnabled());
522                         data->SetRemovable(appInfoData->IsRemovable());
523                         data->SetMenuLaunch(appInfoData->IsMenuLaunch());
524                         data->SetTime(appInfoData->GetTime());
525                         data->SetPreloaded(appInfoData->IsPreloaded());
526                         data->SetSystem(appInfoData->IsSystem());
527                         data->SetSupportDisable(appInfoData->IsSupportDisable());
528                         //data->SetBadgeCount(appInfoData->GetBadgeCount());
529                         data->SetMMCApp(appInfoData->IsMMCApp());
530                         data->SetUnmountedApp(appInfoData->IsUnmountedApp());
531
532                         HOME_ERR("Find a App Info AppId[%s] Name[%s] Icon[%s] enable[%d] system[%d]", appId.c_str(),
533                                         appInfoData->GetName().c_str(), appInfoData->GetIconPath().c_str(), appInfoData->IsEnabled(), appInfoData->IsSystem());
534                         return true;
535                 }
536
537                 m_AppInfoCache.erase(iter);
538         }
539
540         return _GetAppInfo(appId, data);
541 }
542
543 bool CMainMenuPackageManager::_GetAppInfo(string appId, BoxDataPtr data)
544 {
545         bool result;
546         pkgmgrinfo_appinfo_h app_handle = NULL;
547         int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &app_handle);
548
549         if (PMINFO_R_OK != ret) {
550                 HOME_ERR("pkgmgrinfo_appinfo_get_appinfo return [%d]", ret);
551                 return false;
552         }
553
554         result = _GetAppInfo(app_handle, data);
555
556         pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
557
558         return result;
559
560
561 }
562
563 bool CMainMenuPackageManager::GetAppName(string appId, BoxDataPtr data)
564 {
565         return _GetAppInfo(appId, data);
566 }
567
568 bool CMainMenuPackageManager::GetDeactivatedAppInfo(std::string appId, BoxDataPtr data)
569 {
570         auto iter = m_AppInfoCache.find(appId);
571
572         if (iter != m_AppInfoCache.end())
573         {
574                 HOME_DBG("Find a App Info[%s]", appId.c_str());
575                 BoxDataPtr appInfoData = iter->second;
576
577                 if (appInfoData->GetName().length() > 0 && appInfoData->GetIconPath().length())
578                 {
579                         data->SetPkgId(appInfoData->GetPkgId());
580                         data->SetPkgType(appInfoData->GetPkgType());
581
582                         data->SetIconPath(appInfoData->GetIconPath());
583                         data->SetName(appInfoData->GetName());
584                         return true;
585                 }
586
587                 m_AppInfoCache.erase(iter);
588         }
589
590         return _GetDeactivatedAppInfo(appId, data);
591 }
592
593 bool CMainMenuPackageManager::_GetDeactivatedAppInfo(std::string appId, BoxDataPtr data)
594 {
595         pkgmgrinfo_appinfo_filter_h handle = NULL;
596
597         if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_create(&handle))
598         {
599                 HOME_DBG("Failed to create appinfo filter");
600                 return false;
601         }
602
603         if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_ID, appId.c_str()))
604         {
605                 HOME_DBG("Failed to add filter");
606                 pkgmgrinfo_appinfo_filter_destroy(handle);
607                 return false;
608         }
609         
610 #ifdef TIZEN_3_0
611         if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_DISABLE, true))
612         {
613                 HOME_DBG("Failed to add filter");
614                 pkgmgrinfo_appinfo_filter_destroy(handle);
615                 return false;
616         }
617 #endif
618
619         // TODO: to get app info, create new struct
620         std::vector<std::string> list;
621         if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, _PkgMgrDeactivatedAppInfoGetCb, &list))
622         {
623                 HOME_DBG("Failed to foreach appinfo");
624                 pkgmgrinfo_appinfo_filter_destroy(handle);
625                 return false;
626         }
627
628         if (list.size() == 3)
629         {
630                 data->SetPkgId(list[0]);
631                 data->SetIconPath(list[1]);
632                 data->SetName(list[2]);
633
634                 _GetDeactivatedPkgInfo(list[0], data);
635         }
636         else
637         {
638                 HOME_DBG("Failed to get [%s] size[%d]", appId.c_str(), list.size());
639                 pkgmgrinfo_appinfo_filter_destroy(handle);
640                 return false;
641         }
642
643         //m_AppInfoCache[data->GetAppId()] = data;
644
645         pkgmgrinfo_appinfo_filter_destroy(handle);
646         return true;
647 }
648
649 bool CMainMenuPackageManager::_GetDeactivatedPkgInfo(std::string pkgId, BoxDataPtr data)
650 {
651         pkgmgrinfo_pkginfo_filter_h handle = NULL;
652
653         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_filter_create(&handle))
654         {
655                 HOME_DBG("Failed to create pkginfo filter");
656                 return false;
657         }
658
659         if( NULL == handle )
660         {
661                 HOME_DBG("Error: Handle is NULL!");
662                 return false;
663         }
664
665         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgId.c_str()))
666         {
667                 HOME_DBG("Failed to add filter");
668                 pkgmgrinfo_pkginfo_filter_destroy(handle);
669                 return false;
670         }
671
672 #ifdef TIZEN_3_0
673         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, true))
674         {
675                 HOME_DBG("Failed to add filter");
676                 pkgmgrinfo_pkginfo_filter_destroy(handle);
677                 return false;
678         }
679 #endif
680         // TODO: to get app info, create new struct
681         std::vector<std::string> list;
682         if (PMINFO_R_OK != pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, _PkgMgrDeactivatedPkgInfoGetCb, &list))
683         {
684                 HOME_DBG("Failed to foreach pkginfo");
685                 pkgmgrinfo_pkginfo_filter_destroy(handle);
686                 return false;
687         }
688
689         if (list.size() == 1)
690         {
691                 data->SetPkgType(list[0]);
692         }
693         else
694         {
695                 HOME_DBG("Failed to get [%s] size[%d]", pkgId.c_str(), list.size());
696                 pkgmgrinfo_pkginfo_filter_destroy(handle);
697                 return false;
698         }
699
700         pkgmgrinfo_pkginfo_filter_destroy(handle);
701         return true;
702 }
703
704 #define METADATA_MENU_LAUNCH_KEY "http://developer.samsung.com/tizen/metadata/menu/launch/operation"
705 #define METADATA_MENU_LAUNCH_VALUE "http://tizen.org/appcontrol/operation/main"
706 static int _app_metadata_list_cb(const char *metadata_name, const char *metadata_value, void *user_data)
707 {
708         RETV_IF(NULL == user_data, 0);
709
710         if (!metadata_name || !metadata_value) return 0;
711         if (!strcmp(metadata_name , METADATA_MENU_LAUNCH_KEY) &&
712                 !strcmp(metadata_value, METADATA_MENU_LAUNCH_VALUE))
713         {
714                 * (bool *) user_data = true;
715         }
716
717         return 0;
718 }
719
720
721 bool CMainMenuPackageManager::_IsMenuLaunch(std::string appId)
722 {
723         bool menu_launch = false;
724
725         pkgmgrinfo_appinfo_filter_h handle = NULL;
726         pkgmgrinfo_appinfo_h app_handle = NULL;
727
728         RETV_IF(PMINFO_R_OK != pkgmgrinfo_appinfo_filter_create(&handle), NULL);
729         if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_ID, appId.c_str())) goto ERROR;
730
731         if (menu_launch) {
732                 pkgmgrinfo_appinfo_filter_destroy(handle);
733                 return true;
734         }
735
736         if (PMINFO_R_OK != pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &app_handle)) goto ERROR;
737         if (PMINFO_R_OK != pkgmgrinfo_appinfo_foreach_metadata(app_handle, _app_metadata_list_cb, &menu_launch)) goto ERROR;
738
739         pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
740         pkgmgrinfo_appinfo_filter_destroy(handle);
741         return menu_launch;
742
743 ERROR:
744         if (app_handle) pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
745         if (handle) pkgmgrinfo_appinfo_filter_destroy(handle);
746         return false;
747 }
748
749 bool CMainMenuPackageManager::_GetAppInfo(pkgmgrinfo_appinfo_h app_handle, BoxDataPtr data)
750 {
751         char *appid = NULL;
752         char *pkg_type = NULL;
753         char *pkg_id = NULL;
754         char *name = NULL;
755         char *icon = NULL;
756         bool nodisplay = false;
757         bool enabled = true;
758         bool is_system = false;
759         bool removable = true;
760         int installed_time = 0;
761         bool is_preload = false;
762         bool is_support_disable = false;
763         int ret;
764 //      int count;
765
766         ret = pkgmgrinfo_appinfo_get_appid(app_handle, &appid);
767         if (PMINFO_R_OK != ret) {
768                 HOME_ERR("pkgmgrinfo_appinfo_get_appid return [%d]", ret);
769                 goto ERROR;
770         }
771
772         ret = pkgmgrinfo_appinfo_get_pkgid(app_handle, &pkg_id);
773         if (PMINFO_R_OK != ret) {
774                 HOME_ERR("pkgmgrinfo_appinfo_get_pkgid return [%d]", ret);
775                 goto ERROR;
776         }
777
778         ret = pkgmgrinfo_appinfo_get_icon(app_handle, &icon);
779         if (PMINFO_R_OK != ret) {
780                 HOME_ERR("pkgmgrinfo_appinfo_get_icon return [%d]", ret);
781                 goto ERROR;
782         }
783
784         ret = pkgmgrinfo_appinfo_get_label(app_handle, &name);
785         if (PMINFO_R_OK != ret) {
786                 HOME_ERR("pkgmgrinfo_appinfo_get_label return [%d]", ret);
787                 goto ERROR;
788         }
789
790
791         ret = pkgmgrinfo_appinfo_is_nodisplay(app_handle, &nodisplay);
792         if (PMINFO_R_OK != ret) {
793                 HOME_ERR("pkgmgrinfo_appinfo_is_nodisplay return [%d]", ret);
794                 goto ERROR;
795         }
796
797         ret = pkgmgrinfo_appinfo_is_enabled(app_handle, &enabled);
798         if (PMINFO_R_OK != ret) {
799                 HOME_ERR("pkgmgrinfo_appinfo_is_enabled return [%d]", ret);
800                 goto ERROR;
801         }
802
803 #ifdef TIZEN_3_0
804         ret = pkgmgrinfo_appinfo_get_pkgtype(app_handle, &pkg_type);
805         if (PMINFO_R_OK != ret) {
806                 HOME_ERR("pkgmgrinfo_pkginfo_get_type return [%d]", ret);
807                 goto ERROR;
808         }
809         ret = pkgmgrinfo_appinfo_is_system(app_handle, &is_system);
810         if (PMINFO_R_OK != ret) {
811                 HOME_ERR("pkgmgrinfo_pkginfo_is_system return [%d]", ret);
812                 goto ERROR;
813         }
814         ret = pkgmgrinfo_appinfo_is_removable(app_handle, &removable);
815         if (PMINFO_R_OK != ret) {
816                 HOME_ERR("pkgmgrinfo_pkginfo_is_removable return [%d]", ret);
817                 goto ERROR;
818         }
819
820         ret = pkgmgrinfo_appinfo_get_installed_time(app_handle, &installed_time);
821         if (PMINFO_R_OK != ret) {
822                 HOME_ERR("pkgmgrinfo_pkginfo_get_installed_time return [%d]", ret);
823                 goto ERROR;
824         }
825
826         ret = pkgmgrinfo_appinfo_is_preload(app_handle, &is_preload);
827         if (PMINFO_R_OK != ret) {
828                 HOME_ERR("pkgmgrinfo_appinfo_is_preload return [%d]", ret);
829                 goto ERROR;
830         }
831
832         ret = pkgmgrinfo_appinfo_is_support_disable(app_handle, &is_support_disable);
833         if (PMINFO_R_OK != ret) {
834                 HOME_ERR("pkgmgrinfo_pkginfo_is_support_disable return [%d]", ret);
835                 goto ERROR;
836         }
837 #endif
838
839         if (pkg_id)
840                 data->SetPkgId(pkg_id);
841         if (pkg_type)
842                 data->SetPkgType(pkg_type);
843
844         if(icon && *icon)
845         {
846                 FILE *fp = fopen(icon, "r");
847                 if( fp )
848                 {
849                         fclose( fp );
850                         data->SetIconPath(icon);
851                 }
852                 else
853                         data->SetIconPath(RESDIR"/images/default_app_icon.png");
854         }
855         else {
856                 data->SetIconPath(RESDIR"/images/default_app_icon.png");
857         }
858
859
860         if(appid)
861                 data->SetAppId(appid);
862
863         if(name)
864                 data->SetName(name);
865         data->SetNoDisplay(nodisplay);
866
867         data->SetEnabled(enabled);
868
869         data->SetRemovable(removable);;
870         data->SetTime(installed_time);
871         data->SetPreloaded(is_preload);
872         data->SetSystem(is_system);
873         data->SetSupportDisable(is_support_disable);
874
875         HOME_ERR("AppId[%s] Name[%s] Icon[%s] enable[%d] system[%d]", appid, data->GetName().c_str(), data->GetIconPath().c_str(), enabled, is_system);
876
877         m_AppInfoCache[data->GetAppId()] = data;
878
879         return true;
880
881 ERROR:
882         HOME_ERR("AppId[%s] failed to get pkginfo", appid);
883
884         return false;
885 }
886
887 int CMainMenuPackageManager::_GetAllAppsInfoCb(pkgmgrinfo_appinfo_h handle, void *user_data)
888 {
889         RETV_IF(NULL == handle, 0);
890         RETV_IF(NULL == user_data, 0);
891         CMainMenuPackageManager* pThis = (CMainMenuPackageManager*)user_data;
892         BoxDataPtr data(new CMainMenuBoxData);
893
894         pThis->_GetAppInfo(handle, data);
895
896         return 0;
897 }
898
899
900 int CMainMenuPackageManager::_GetUnmountedAppsInfoCb(pkgmgrinfo_appinfo_h handle, void *user_data)
901 {
902         RETV_IF(NULL == handle, 0);
903         RETV_IF(NULL == user_data, 0);
904         CMainMenuPackageManager* pThis = (CMainMenuPackageManager*)user_data;
905
906         BoxDataPtr data(new CMainMenuBoxData);
907
908
909         pThis->_GetAppInfo(handle, data);
910
911         data->SetIconPath(RESDIR"/images/uninstalled_sd.png");
912         data->SetMMCApp(true);
913         data->SetUnmountedApp(true);
914
915         HOME_DBG("app id [%s]", data->GetAppId().c_str());
916         return 0;
917 }
918
919
920 int CMainMenuPackageManager::_GetMountedAppsInfoCb(pkgmgrinfo_appinfo_h handle, void *user_data)
921 {
922         RETV_IF(NULL == handle, 0);
923         RETV_IF(NULL == user_data, 0);
924         CMainMenuPackageManager* pThis = (CMainMenuPackageManager*)user_data;
925
926         BoxDataPtr data(new CMainMenuBoxData);
927
928         pThis->_GetAppInfo(handle, data);
929         data->SetMMCApp(true);
930
931         HOME_DBG("app id [%s]", data->GetAppId().c_str());
932         return 0;
933 }
934
935
936 bool CMainMenuPackageManager::GetAppIdList(std::vector<std::string>& list)
937 {
938         int ret, status;
939         pkgmgrinfo_appinfo_filter_h handle = NULL;
940         HOME_WARN("# BEGIN");
941
942         if (m_AppInfoCache.size() == 0)
943         {
944                 RETV_IF(PMINFO_R_OK != pkgmgrinfo_appinfo_filter_create(&handle), false);
945                 if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_NODISPLAY, false)) goto ERROR;
946         //      if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_DISABLE, false)) goto ERROR;
947                 if (PMINFO_R_OK != pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, CMainMenuPackageManager::_GetAllAppsInfoCb, this)) goto ERROR;
948
949 #ifdef TIZEN_3_0
950                 if (PMINFO_R_OK !=  pkgmgrinfo_appinfo_get_unmounted_list(_GetUnmountedAppsInfoCb, this)) goto ERROR;
951 #endif
952                 ret = vconf_get_int("memory/sysman/mmc", &status);
953 #ifdef TIZEN_3_0
954
955                 if (ret == 0 && status == 1)
956                         if (PMINFO_R_OK !=  pkgmgrinfo_appinfo_get_mounted_list(_GetMountedAppsInfoCb, this)) goto ERROR;
957 #endif
958         }
959
960         for(auto iter = m_AppInfoCache.begin(); iter != m_AppInfoCache.end(); iter++)
961         {
962                 BoxDataPtr appInfoData = iter->second;
963                 if (appInfoData->IsEnabled())
964                         list.push_back(appInfoData->GetAppId());
965         }
966
967         std::sort(list.begin(), list.end());
968
969         pkgmgrinfo_appinfo_filter_destroy(handle);
970         HOME_WARN("# END");
971         return true;
972
973 ERROR:
974         if (handle) pkgmgrinfo_appinfo_filter_destroy(handle);
975
976         return false;
977 }