Revert "Temporal reincarnation of removed API"
[platform/framework/native/appfw.git] / src / app / package / FAppPkg_PackageManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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  * @file        FAppPkg_PackageManagerImpl.cpp
18  * @brief       This is the implementation for the _PackageManagerImpl class.
19  */
20 #include <cstdio>
21 #include <cstdlib>
22 #include <new>
23 #include <memory>
24 #include <unistd.h>
25 #include <dlog.h>
26 #include <unique_ptr.h>
27
28 #include <package-manager-types.h>
29
30 #include <FAppPkgPackageManager.h>
31 #include <FAppPkgPackageAppInfo.h>
32 #include <FAppPkgPackageInfo.h>
33 #include <FAppPkgIPackageInstallationEventListener.h>
34 #include <FAppPkgIPackageInstallationResponseListener.h>
35 #include <FAppPkgIPackageUninstallationResponseListener.h>
36 #include <FBaseSysLog.h>
37 #include <FIo.h>
38 #include <FIoRegistry.h>
39 #include <FSecPrivilegeInfo.h>
40 #include <FSysSystemTime.h>
41 #include <FBase_StringConverter.h>
42
43 #include "FAppPkg_PackageManagerImpl.h"
44 #include "FAppPkg_PackageInfoImpl.h"
45 #include "FAppPkg_PackageAppInfoImpl.h"
46 #include "FAppPkg_PackageParser.h"
47 #include "FApp_AppInfo.h"
48 #include "FApp_PackageManagerProxy.h"
49
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Runtime;
52 using namespace Tizen::Base::Collection;
53 using namespace Tizen::Base::Utility;
54 using namespace Tizen::Io;
55 using namespace Tizen::Security;
56 using namespace Tizen::System;
57
58 namespace Tizen { namespace App { namespace Package
59 {
60
61 PackageManager* pPackageManagerInstance = null;
62
63 Tizen::Base::Collection::HashMap _PackageManagerImpl::__installationList;
64
65 void
66 _PackageManagerEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
67 {
68         IPackageInstallationEventListener* pListener = dynamic_cast<IPackageInstallationEventListener*> (&listener);
69         SysTryReturnVoidResult(NID_APP, pListener != null, E_OUT_OF_MEMORY, "pListener instance must not be null.");
70
71         const _PackageManagerEventArg* pArg = dynamic_cast<const _PackageManagerEventArg*>(&arg);
72         SysTryReturnVoidResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "pArg instance must not be null.");
73
74         if (pArg->__eventKey == L"end")
75         {
76                 if (pArg->__eventValue == L"ok")
77                 {
78                         if (pArg->__install)
79                         {
80                                 SysLog(NID_APP, "Installation is Completed. [Package = %ls]", pArg->__packageId.GetPointer());
81                                 pListener->OnPackageInstallationCompleted(pArg->__packageId, PACKAGE_INSTALLATION_RESULT_SUCCESS);
82                         }
83                         else
84                         {
85                                 SysLog(NID_APP, "Uninstallation is Completed. [Package = %ls]", pArg->__packageId.GetPointer());
86                                 pListener->OnPackageUninstallationCompleted(pArg->__packageId, true);
87                         }
88                 }
89                 else
90                 {
91                         if (pArg->__install == false)
92                         {
93                                 SysLog(NID_APP, "Uninstallation is Completed(Error). [Package = %ls]", pArg->__packageId.GetPointer());
94                                 pListener->OnPackageUninstallationCompleted(pArg->__packageId, false);
95                         }
96                 }
97         }
98         else if (pArg->__eventKey == L"error")
99         {
100                 if (pArg->__install)
101                 {
102                         if (pArg->__eventValue == L"62")
103                         {
104                                 SysLog(NID_APP, "Installation is Completed(Error = STORAGE_FULL(62)). [Package = %ls]", pArg->__packageId.GetPointer());
105                                 pListener->OnPackageInstallationCompleted(pArg->__packageId, PACKAGE_INSTALLATION_RESULT_STORAGE_FULL);
106                         }
107                         else
108                         {
109                                 SysLog(NID_APP, "Installation is Completed(Error). [Package = %ls]", pArg->__packageId.GetPointer());
110                                 pListener->OnPackageInstallationCompleted(pArg->__packageId, PACKAGE_INSTALLATION_RESULT_INVALID_PACKAGE);
111                         }
112                 }
113         }
114         else if (pArg->__eventKey == L"install_percent")
115         {
116                 std::unique_ptr<char[]> pProgress(_StringConverter::CopyToCharArrayN(pArg->__eventValue));
117
118                 int progress = 0;
119                 sscanf(pProgress.get(), "%d", &progress);
120
121                 SysLog(NID_APP, "InstallationInProgress [%d]", progress);
122                 pListener->OnPackageInstallationInProgress(pArg->__packageId, progress);
123         }
124 }
125
126 PackageId
127 _PackageManagerImpl::GetPackageIdByAppId(const AppId& appId)
128 {
129         SysTryReturn(NID_APP, appId.IsEmpty() == false, L"", E_INVALID_ARG, "appId is empty");
130
131         PackageId packageId;
132         int result = 0;
133         char* pPackageId = null;
134         pkgmgrinfo_appinfo_h pAppInfoHandle = null;
135
136         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
137         SysTryReturn(NID_APP, pAppId, L"", E_OUT_OF_MEMORY, "pAppId is null");
138
139         result = pkgmgrinfo_appinfo_get_appinfo(pAppId.get(), &pAppInfoHandle);
140         SysTryReturn(NID_APP, result == 0, L"", E_PKG_NOT_INSTALLED, "pkgmgrinfo_appinfo_get_appinfo() failed. result=[%d], app=[%s]", result, pAppId.get());
141
142         result = pkgmgrinfo_appinfo_get_pkgname(pAppInfoHandle, &pPackageId);
143
144         packageId.Append(pPackageId);
145
146         if(pAppInfoHandle)
147         {
148                 pkgmgrinfo_appinfo_destroy_appinfo(pAppInfoHandle);
149         }
150
151         SysLog(NID_APP, "app = [%ls], package = [%ls]", appId.GetPointer(), packageId.GetPointer());
152
153         return packageId;
154 }
155
156 result
157 _PackageManagerImpl::AddEventListener(IPackageInstallationEventListener* pListener, int priority)
158 {
159         SysTryReturnResult(NID_APP, (priority < 3) && (priority >= 0), E_INVALID_ARG, "priority is invalid.");
160
161         result r = E_SUCCESS;
162         int res = 0;
163
164         std::unique_ptr< IEnumeratorT<_PackageManagerEvent*> > pEnum(__packageEventListenerList.GetEnumeratorN());
165         SysTryReturnResult(NID_APP, pEnum, E_OUT_OF_MEMORY, "The memory is insufficient.");
166
167         while (pEnum->MoveNext() == E_SUCCESS)
168         {
169                 _PackageManagerEvent*   pEvent = null;
170                 pEnum->GetCurrent(pEvent);
171                 if (pEvent)
172                 {
173                         if (pEvent->__pListener == pListener)
174                         {
175                                 SysLog(NID_APP, "The event listener already exist. listener = [0x%x]", pListener);
176                                 return E_OBJ_ALREADY_EXIST;
177                         }
178                 }
179         }
180
181         if (__pListeningClient == null)
182         {
183                 __pListeningClient = pkgmgr_client_new(PC_LISTENING);
184                 SysTryReturnResult(NID_APP, __pListeningClient != null, E_SYSTEM, "pkgmgr_client_new(PC_LISTENING) failed");
185
186                 res = pkgmgr_client_listen_status(__pListeningClient, PackageEventHandler, this);
187                 if (res < PKGMGR_R_OK)
188                 {
189                         pkgmgr_client_free(__pListeningClient);
190                         __pListeningClient = null;
191
192                         if (res == PKGMGR_R_EINVAL)
193                         {
194                                 SysLog(NID_APP, "pkgmgr_client_listen_status() returns ARGV error");
195                                 return E_INVALID_ARG;
196                         }
197                         else if (res == PKGMGR_R_ECOMM)
198                         {
199                                 SysLog(NID_APP, "pkgmgr_client_listen_status() returns COMM error");
200                         }
201                         else if (res == PKGMGR_R_ETIMEOUT)
202                         {
203                                 SysLog(NID_APP, "pkgmgr_client_listen_status() returns TIME_OUT error");
204                         }
205                         else if (res == PKGMGR_R_ERROR)
206                         {
207                                 SysLog(NID_APP, "pkgmgr_client_listen_status() returns Unknown error");
208                         }
209                         else
210                         {
211                                 SysLog(NID_APP, "pkgmgr_client_listen_status() returns Unknown error, res = %d", res);
212                         }
213
214                         return E_SYSTEM;
215                 }
216         }
217
218         _PackageManagerEvent* pEvent = new (std::nothrow) _PackageManagerEvent();
219         SysTryReturnResult(NID_APP, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
220
221         r = pEvent->Construct();
222         SysTryReturnResult(NID_APP, r == E_SUCCESS, E_SYSTEM, "pEvent->Construct() failed.");
223
224         pEvent->__pListener = pListener;
225
226         r = pEvent->AddListener(*pListener, true);
227         SysTryReturnResult(NID_APP, r == E_SUCCESS, r, "pEvent->AddListener() failed. [%s]", GetErrorMessage(r));
228
229         r = __packageEventListenerList.Add(pEvent);
230         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
231
232         SysLog(NID_APP, "Add PackageInstallationEventListener = [0x%x]", pListener);
233         return E_SUCCESS;
234 }
235
236 result
237 _PackageManagerImpl::RemoveEventListener(IPackageInstallationEventListener* pListener)
238 {
239         std::unique_ptr< IEnumeratorT<_PackageManagerEvent*> > pEnum(__packageEventListenerList.GetEnumeratorN());
240         SysTryReturnResult(NID_APP, pEnum, E_OUT_OF_MEMORY, "The memory is insufficient.");
241
242         result r = E_SUCCESS;
243
244         while (pEnum->MoveNext() == E_SUCCESS)
245         {
246                 _PackageManagerEvent*   pEvent = null;
247                 pEnum->GetCurrent(pEvent);
248                 if (pEvent)
249                 {
250                         if (pEvent->__pListener == pListener)
251                         {
252                                 r = __packageEventListenerList.Remove(pEvent);
253                                 SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
254
255                                 delete pEvent;
256
257                                 SysLog(NID_APP, "Remove PackageInstallationEventListener = [0x%x]", pListener);
258                                 return E_SUCCESS;
259                         }
260                 }
261         }
262
263         return E_OBJ_NOT_FOUND;
264 }
265
266 PackageInfo*
267 _PackageManagerImpl::GetPackageInfoN(const PackageId& packageId) const
268 {
269         SysTryReturn(NID_APP, packageId.IsEmpty() == false, null, E_INVALID_ARG, "[E_INVALID_ARG] packageId is empty.");
270         SysTryReturn(NID_APP, packageId.GetLength() < NATIVE_APP_MAX_APPID_LENGTH, null, E_INVALID_ARG, "packageId is invalid.(length)");
271
272         result r = E_SUCCESS;
273         PackageInfo* pPackageInfo = null;
274
275         pPackageInfo = new (std::nothrow) PackageInfo;
276         SysTryReturn(NID_APP, pPackageInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageInfo instance must not be null.");
277
278         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo);
279         r = pPackageInfoImpl->Construct(packageId);
280         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[E_PKG_NOT_INSTALLED] package (%ls) is not found.", packageId.GetPointer());
281
282         return pPackageInfo;
283
284 CATCH:
285         delete pPackageInfo;
286         return null;
287 }
288
289 PackageAppInfo*
290 _PackageManagerImpl::GetPackageAppInfoN(const AppId& appId) const
291 {
292         SysTryReturn(NID_APP, appId.IsEmpty() == false, null, E_INVALID_ARG, "[E_INVALID_ARG] appId is empty.");
293
294         result r = E_SUCCESS;
295         PackageAppInfo* pPackageAppInfo = null;
296
297         pPackageAppInfo = new (std::nothrow) PackageAppInfo;
298         SysTryReturn(NID_APP, pPackageAppInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageAppInfo instance must not be null.");
299
300         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo);
301         r = pPackageAppInfoImpl->Construct(appId);
302         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[E_PKG_NOT_INSTALLED] app (%ls) is not found.", appId.GetPointer());
303
304         return pPackageAppInfo;
305
306 CATCH:
307         delete pPackageAppInfo;
308         return null;
309 }
310
311 IList*
312 _PackageManagerImpl::GetPackageInfoListN(void) const
313 {
314         result r = E_SUCCESS;
315         int res = 0;
316         ArrayList* pList = null;
317
318         pList = new (std::nothrow) ArrayList();
319         SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
320         pList->Construct();
321
322         res = pkgmgrinfo_pkginfo_get_list(PackageInfoEventHandler, pList);
323         if (res != 0)
324         {
325                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_list() is failed. result = [%d]", res);
326
327                 pList->RemoveAll(true);
328                 pList = null;
329         }
330
331         SetLastResult(r);
332         return pList;
333 }
334
335 bool
336 _PackageManagerImpl::IsPackageInstalled(const PackageId& packageId) const
337 {
338         SysTryReturn(NID_APP, packageId.IsEmpty() == false, false, E_INVALID_ARG, "packageId is empty");
339
340         int result = 0;
341         pkgmgrinfo_pkginfo_h pPackageInfoHandle = null;
342
343         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
344         SysTryReturn(NID_APP, pPackageId, false, E_OUT_OF_MEMORY, "pPackageId is null");
345
346         result = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &pPackageInfoHandle);
347         if (result != 0)
348         {
349                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], package=[%s]", result, pPackageId.get());
350                 return false;
351         }
352
353         if(pPackageInfoHandle)
354         {
355                 pkgmgrinfo_pkginfo_destroy_pkginfo(pPackageInfoHandle);
356         }
357
358         SysLog(NID_APP, "package = [%ls] is installed.", packageId.GetPointer());
359
360         return true;
361 }
362
363 result
364 _PackageManagerImpl::InstallPackage(const PackageId& packageId, const String& packagePath, IPackageInstallationResponseListener* pListener)
365 {
366         SysTryReturnResult(NID_APP, packageId.IsEmpty() == false, E_INVALID_ARG, "packageId is empty.");
367         SysTryReturnResult(NID_APP, packagePath.IsEmpty() == false, E_INVALID_ARG, "packagePath is empty.");
368         SysTryReturnResult(NID_APP, File::IsFileExist(packagePath) == true, E_INVALID_ARG, "packagePath is not existed.");
369
370         String extension = File::GetFileExtension(packagePath);
371         SysTryReturnResult(NID_APP, extension.IsEmpty() == false, E_INVALID_ARG, "extension is empty.");
372
373         std::unique_ptr<char[]> pPackagePath(_StringConverter::CopyToCharArrayN(packagePath));
374         SysTryReturnResult(NID_APP, pPackagePath, E_OUT_OF_MEMORY, "pPackagePath is null.");
375
376         std::unique_ptr<char[]> pExtension(_StringConverter::CopyToCharArrayN(extension));
377         SysTryReturnResult(NID_APP, pExtension, E_OUT_OF_MEMORY, "pExtension is null.");
378
379         if ((strcasecmp(pExtension.get(), "tpk") == 0) || (strcasecmp(pExtension.get(), "wgt") == 0))
380         {
381                 SysLog(NID_APP, "package = [%ls], packagePath = [%s], extension = [%s]", packageId.GetPointer(), pPackagePath.get(), pExtension.get());
382         }
383         else
384         {
385                 SysLog(NID_APP, "invalid extension! - packagePath = [%s], extension = [%s]", pPackagePath.get(), pExtension.get());
386                 return E_SYSTEM;
387         }
388
389         // optionalData = 12345abcde
390         String optionalData;
391         optionalData.Append(packageId);
392
393         std::unique_ptr<char[]> pOptionalData(_StringConverter::CopyToCharArrayN(optionalData));
394         SysTryReturnResult(NID_APP, pOptionalData, E_OUT_OF_MEMORY, "pOptionalData is null.");
395
396         SysLog(NID_APP, "optionalData = [%s]", pOptionalData.get());
397
398         result r = E_SUCCESS;
399         _PackageManagerProxy packageManagerProxy;
400         r = packageManagerProxy.Construct();
401         SysTryReturnResult(NID_APP, r == E_SUCCESS, E_SYSTEM, "packageManagerProxy.Construct() failed.");
402
403         r = packageManagerProxy.InstallPackage(packageId, packagePath, null);
404         SysTryReturnResult(NID_APP, r == E_SUCCESS, r, "packageManagerProxy.InstallPackage() failed.");
405
406         if (__pRequestClient == null)
407         {
408                 __pRequestClient = pkgmgr_client_new(PC_REQUEST);
409                 SysTryReturnResult(NID_APP, __pRequestClient != null, E_SYSTEM, "pkgmgr_client_new(PC_REQUEST) failed");
410         }
411
412         int res = pkgmgr_client_install(__pRequestClient, pExtension.get(), null, pPackagePath.get(), pOptionalData.get(), PM_QUIET, InstallationEventHandler, pListener);
413
414         if (res < PKGMGR_R_OK)
415         {
416                 pkgmgr_client_free(__pRequestClient);
417                 __pRequestClient = null;
418
419                 if (res == PKGMGR_R_EINVAL)
420                 {
421                         SysLog(NID_APP, "pkgmgr_client_install() returns ARGV error");
422                         return E_INVALID_ARG;
423                 }
424                 else if (res == PKGMGR_R_ECOMM)
425                 {
426                         SysLog(NID_APP, "pkgmgr_client_install() returns COMM error");
427                 }
428                 else if (res == PKGMGR_R_ETIMEOUT)
429                 {
430                         SysLog(NID_APP, "pkgmgr_client_install() returns TIME_OUT error");
431                 }
432                 else if (res == PKGMGR_R_ERROR)
433                 {
434                         SysLog(NID_APP, "pkgmgr_client_install() returns Unknown error");
435                 }
436                 else
437                 {
438                         SysLog(NID_APP, "pkgmgr_client_install() returns Unknown error, res = %d", res);
439                 }
440
441                 return E_SYSTEM;
442         }
443
444         return E_SUCCESS;
445 }
446
447 result
448 _PackageManagerImpl::UninstallPackage(const PackageId& packageId, IPackageUninstallationResponseListener* pListener)
449 {
450         SysTryReturnResult(NID_APP, packageId.IsEmpty() == false, E_INVALID_ARG, "packageId is empty.");
451         SysTryReturnResult(NID_APP, packageId.GetLength() < NATIVE_APP_MAX_APPID_LENGTH, E_INVALID_ARG, "The packageId is invalid.(length)");
452         SysTryReturnResult(NID_APP, IsPackageInstalled(packageId) == true, E_PKG_NOT_INSTALLED, "package is not installed.");
453
454         result r = E_SUCCESS;
455         int res = 0;
456         char* pType = null;
457         pkgmgrinfo_pkginfo_h pPackageInfoHandle = null;
458
459         _PackageManagerProxy packageManagerProxy;
460         r = packageManagerProxy.Construct();
461         SysTryReturnResult(NID_APP, r == E_SUCCESS, E_SYSTEM, "packageManagerProxy.Construct() failed.");
462
463         r = packageManagerProxy.UninstallPackage(packageId, pListener);
464         SysTryReturnResult(NID_APP, r == E_SUCCESS, r, "packageManagerProxy.UninstallPackage() failed.");
465
466         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
467         SysTryReturnResult(NID_APP, pPackageId != null, E_OUT_OF_MEMORY, "pPackageId is null");
468
469         res = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &pPackageInfoHandle);
470         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], package=[%s]", res, pPackageId.get());
471
472         res = pkgmgrinfo_pkginfo_get_type(pPackageInfoHandle, &pType);
473         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_type() failed. result=[%d], package=[%s]", res, pPackageId.get());
474
475         if (__pRequestClient == null)
476         {
477                 __pRequestClient = pkgmgr_client_new(PC_REQUEST);
478                 SysTryReturnResult(NID_APP, __pRequestClient != null, E_SYSTEM, "pkgmgr_client_new(PC_REQUEST) failed");
479         }
480
481         res = pkgmgr_client_uninstall(__pRequestClient, pType, pPackageId.get(), PM_QUIET, UninstallationEventHandler, pListener);
482         SysLog(NID_APP, "pkgmgr_client_uninstall - req=[%d], package=[%s], type=[%s]", res, pPackageId.get(), pType);
483
484         if(pPackageInfoHandle)
485         {
486                 pkgmgrinfo_pkginfo_destroy_pkginfo(pPackageInfoHandle);
487         }
488
489         if (res < PKGMGR_R_OK)
490         {
491                 pkgmgr_client_free(__pRequestClient);
492                 __pRequestClient = null;
493
494                 if (res == PKGMGR_R_EINVAL)
495                 {
496                         SysLog(NID_APP, "pkgmgr_client_uninstall() returns ARGV error");
497                         return E_INVALID_ARG;
498                 }
499                 else if (res == PKGMGR_R_ECOMM)
500                 {
501                         SysLog(NID_APP, "pkgmgr_client_uninstall() returns COMM error");
502                 }
503                 else if (res == PKGMGR_R_ETIMEOUT)
504                 {
505                         SysLog(NID_APP, "pkgmgr_client_uninstall() returns TIME_OUT error");
506                 }
507                 else if (res == PKGMGR_R_ERROR)
508                 {
509                         SysLog(NID_APP, "pkgmgr_client_uninstall() returns Unknown error");
510                 }
511                 else
512                 {
513                         SysLog(NID_APP, "pkgmgr_client_uninstall() returns Unknown error, res = %d", res);
514                 }
515
516                 return E_SYSTEM;
517         }
518
519         return E_SUCCESS;
520 }
521
522 result
523 _PackageManagerImpl::MoveToExternalStorage(const PackageId& packageId)
524 {
525         SysTryReturnResult(NID_APP, packageId.IsEmpty() == false, E_INVALID_ARG, "packageId is empty");
526         SysTryReturnResult(NID_APP, IsPackageInstalled(packageId) == true, E_PKG_NOT_INSTALLED, "package is not installed.");
527
528         int res = 0;
529         char* pType = null;
530         pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
531         pkgmgrinfo_pkginfo_h packageInfoHandle = null;
532
533         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
534         SysTryReturnResult(NID_APP, pPackageId != null, E_OUT_OF_MEMORY, "pPackageId is null");
535
536         res = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &packageInfoHandle);
537         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], package=[%s]", res, pPackageId.get());
538
539         res = pkgmgrinfo_pkginfo_get_type(packageInfoHandle, &pType);
540         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_type() failed. result=[%d], package=[%s]", res, pPackageId.get());
541
542         res = pkgmgrinfo_pkginfo_get_installed_storage(packageInfoHandle, &storage);
543         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_installed_storage() failed. result=[%d], package=[%s]", res, pPackageId.get());
544
545         if (storage == PMINFO_EXTERNAL_STORAGE)
546         {
547                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_installed_storage(): storage = [%d][E_INVALID_OPERATION]", storage);
548                 return E_INVALID_OPERATION;
549         }
550
551         if (__pRequestClient == null)
552         {
553                 __pRequestClient = pkgmgr_client_new(PC_REQUEST);
554                 SysTryReturnResult(NID_APP, __pRequestClient != null, E_SYSTEM, "pkgmgr_client_new(PC_REQUEST) failed");
555         }
556
557         SysLog(NID_APP, "pkgmgr_client_request_service(PM_REQUEST_MOVE, 1, %s, %s)", pType, pPackageId.get());
558         res = pkgmgr_client_request_service(PM_REQUEST_MOVE, 1, __pRequestClient, pType, pPackageId.get(), null, null, null);
559
560         if (packageInfoHandle)
561         {
562                 pkgmgrinfo_pkginfo_destroy_pkginfo(packageInfoHandle);
563         }
564
565         if (res < PKGMGR_R_OK)
566         {
567                 pkgmgr_client_free(__pRequestClient);
568                 __pRequestClient = null;
569
570                 if (res == PKGMGR_R_EINVAL)
571                 {
572                         SysLog(NID_APP, "pkgmgr_client_move() returns ARGV error");
573                         return E_INVALID_ARG;
574                 }
575                 else if (res == PKGMGR_R_ERROR)
576                 {
577                         SysLog(NID_APP, "pkgmgr_client_move() returns Unknown error");
578                 }
579                 else
580                 {
581                         SysLog(NID_APP, "pkgmgr_client_move() returns Unknown error, res = %d", res);
582                 }
583
584                 return E_SYSTEM;
585         }
586
587         return E_SUCCESS;
588 }
589
590 result
591 _PackageManagerImpl::MoveToInternalStorage(const PackageId& packageId)
592 {
593         SysTryReturnResult(NID_APP, packageId.IsEmpty() == false, E_INVALID_ARG, "packageId is empty");
594         SysTryReturnResult(NID_APP, IsPackageInstalled(packageId) == true, E_PKG_NOT_INSTALLED, "package is not installed.");
595
596         int res = 0;
597         char* pType = null;
598         pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
599         pkgmgrinfo_pkginfo_h packageInfoHandle = null;
600
601         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
602         SysTryReturnResult(NID_APP, pPackageId != null, E_OUT_OF_MEMORY, "pPackageId is null");
603
604         res = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &packageInfoHandle);
605         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], package=[%s]", res, pPackageId.get());
606
607         res = pkgmgrinfo_pkginfo_get_type(packageInfoHandle, &pType);
608         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_type() failed. result=[%d], package=[%s]", res, pPackageId.get());
609
610         res = pkgmgrinfo_pkginfo_get_installed_storage(packageInfoHandle, &storage);
611         SysTryReturnResult(NID_APP, res == 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_installed_storage() failed. result=[%d], package=[%s]", res, pPackageId.get());
612
613         if (storage == PMINFO_INTERNAL_STORAGE)
614         {
615                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_installed_storage(): storage = [%d][E_INVALID_OPERATION]", storage);
616                 return E_INVALID_OPERATION;
617         }
618
619         if (__pRequestClient == null)
620         {
621                 __pRequestClient = pkgmgr_client_new(PC_REQUEST);
622                 SysTryReturnResult(NID_APP, __pRequestClient != null, E_SYSTEM, "pkgmgr_client_new(PC_REQUEST) failed");
623         }
624
625         SysLog(NID_APP, "pkgmgr_client_request_service(PM_REQUEST_MOVE, 0, %s, %s)", pType, pPackageId.get());
626         res = pkgmgr_client_request_service(PM_REQUEST_MOVE, 0, __pRequestClient, pType, pPackageId.get(), null, null, null);
627
628         if (packageInfoHandle)
629         {
630                 pkgmgrinfo_pkginfo_destroy_pkginfo(packageInfoHandle);
631         }
632
633         if (res < PKGMGR_R_OK)
634         {
635                 pkgmgr_client_free(__pRequestClient);
636                 __pRequestClient = null;
637
638                 if (res == PKGMGR_R_EINVAL)
639                 {
640                         SysLog(NID_APP, "pkgmgr_client_move() returns ARGV error");
641                         return E_INVALID_ARG;
642                 }
643                 else if (res == PKGMGR_R_ERROR)
644                 {
645                         SysLog(NID_APP, "pkgmgr_client_move() returns Unknown error");
646                 }
647                 else
648                 {
649                         SysLog(NID_APP, "pkgmgr_client_move() returns Unknown error, res = %d", res);
650                 }
651
652                 return E_SYSTEM;
653         }
654
655         return E_SUCCESS;
656 }
657
658 int
659 _PackageManagerImpl::InstallationEventHandler(int reqId, const char* pType, const char* pPackageId, const char* pKey, const char* pVal, const void* pMsg, void* pData)
660 {
661         SysLog(NID_APP, "reqId[%d], Type[%s], Package[%s], Key[%s], Val[%s]", reqId, pType, pPackageId, pKey, pVal);
662
663         if (pData)
664         {
665                 IPackageInstallationResponseListener* pListener = (IPackageInstallationResponseListener*) pData;
666
667                 if (strcmp(pKey, "end") == 0)
668                 {
669                         if (strcmp(pVal, "ok") == 0)
670                         {
671                                 pListener->OnPackageInstallationResponseReceived(pPackageId, PACKAGE_INSTALLATION_RESULT_SUCCESS);
672                                 SysLog(NID_APP, "OnPackageInstallationResponseReceived(PACKAGE_INSTALLATION_RESULT_SUCCESS)");
673                         }
674
675                         delete pListener;
676                 }
677                 else if (strcmp(pKey, "error") == 0)
678                 {
679                         if (strcmp(pVal, "62") == 0)
680                         {
681                                 pListener->OnPackageInstallationResponseReceived(pPackageId, PACKAGE_INSTALLATION_RESULT_STORAGE_FULL);
682                                 SysLog(NID_APP, "OnPackageInstallationResponseReceived(PACKAGE_INSTALLATION_RESULT_STORAGE_FULL)");
683                         }
684                         else
685                         {
686                                 pListener->OnPackageInstallationResponseReceived(pPackageId, PACKAGE_INSTALLATION_RESULT_INVALID_PACKAGE);
687                                 SysLog(NID_APP, "OnPackageInstallationResponseReceived(PACKAGE_INSTALLATION_RESULT_INVALID_PACKAGE)");
688                         }
689                 }
690                 else if (strcmp(pKey, "install_percent") == 0)
691                 {
692                         int progress = 0;
693                         sscanf(pVal, "%d", &progress);
694
695                         pListener->OnPackageInstallationInProgressResponseReceived(pPackageId, progress);
696                         SysLog(NID_APP, "OnPackageInstallationInProgressResponseReceived(%s, %d)", pPackageId, progress);
697                 }
698         }
699
700         return 0;
701 }
702
703 int
704 _PackageManagerImpl::UninstallationEventHandler(int reqId, const char* pType, const char* pPackageId, const char* pKey, const char* pVal, const void* pMsg, void* pData)
705 {
706         SysLog(NID_APP, "reqId[%d], Type[%s], Package[%s], Key[%s], Val[%s]", reqId, pType, pPackageId, pKey, pVal);
707
708         if (pData)
709         {
710                 if (strcmp(pKey, "end") == 0)
711                 {
712                         IPackageUninstallationResponseListener* pListener = (IPackageUninstallationResponseListener*) pData;
713
714                         if (strcmp(pVal, "ok") == 0)
715                         {
716                                 SysLog(NID_APP, "OnPackageUninstallationResponseReceived(true)");
717                                 pListener->OnPackageUninstallationResponseReceived(pPackageId, true);
718                         }
719                         else
720                         {
721                                 SysLog(NID_APP, "OnPackageUninstallationResponseReceived(false)");
722                                 pListener->OnPackageUninstallationResponseReceived(pPackageId, false);
723                         }
724
725                         delete pListener;
726                 }
727         }
728
729         return 0;
730 }
731
732 PackageInfo*
733 _PackageManagerImpl::GetPackageInfoFromFileN(const String& filePath) const
734 {
735         SysTryReturn(NID_APP, filePath.IsEmpty() == false, null, E_INVALID_ARG, "filePath is empty.");
736         SysTryReturn(NID_APP, File::IsFileExist(filePath) == true, null, E_FILE_NOT_FOUND, "package is not existed.");
737
738         String extension = File::GetFileExtension(filePath);
739         SysTryReturn(NID_APP, extension.IsEmpty() == false, null, E_INVALID_ARG, "extension is empty.");
740
741         std::unique_ptr<char[]> pPackagePath(_StringConverter::CopyToCharArrayN(filePath));
742         SysTryReturn(NID_APP, pPackagePath, null, E_OUT_OF_MEMORY, "pPackagePath is null.");
743
744         std::unique_ptr<char[]> pExtension(_StringConverter::CopyToCharArrayN(extension));
745         SysTryReturn(NID_APP, pExtension, null, E_OUT_OF_MEMORY, "pExtension is null.");
746
747         std::unique_ptr< PackageInfo > pPackageInfo(new (std::nothrow) PackageInfo);
748         SysTryReturn(NID_APP, pPackageInfo, null, E_OUT_OF_MEMORY, "pPackageInfo instance must not be null.");
749
750         SysLog(NID_APP, "packagePath = [%s], extension = [%s]", pPackagePath.get(), pExtension.get());
751
752         bool res = true;
753
754         if (strcasecmp(pExtension.get(), "tpk") == 0)
755         {
756                 _PackageParser packageParser;
757
758                 res = packageParser.Construct(pPackageInfo.get());
759                 SysTryReturn(NID_APP, res, null, E_PARSING_FAILED, "Construct() is failed. [%s]", pPackagePath.get());
760
761                 res = packageParser.Parse(filePath);
762                 SysTryReturn(NID_APP, res, null, E_PARSING_FAILED, "Parse() is failed. [%s]", pPackagePath.get());
763         }
764         else if (strcasecmp(pExtension.get(), "wgt") == 0)
765         {
766                 pkgmgr_info* pPkgmgrInfo = null;
767
768                 pPkgmgrInfo = pkgmgr_client_check_pkginfo_from_file(pPackagePath.get());
769                 SysTryReturn(NID_APP, pPkgmgrInfo, null, E_PARSING_FAILED, "pkgmgr_client_check_pkginfo_from_file(%s) is failed.", pPackagePath.get());
770
771                 _package_manager_pkg_detail_info_t* pPkgInfo = (_package_manager_pkg_detail_info_t*) pPkgmgrInfo;
772                 _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
773
774                 SysLog(NID_APP, "package(%s), version(%s), label(%s), description(%s), author(%s), icon_size(%d), pkgname(%s)", pPkgInfo->pkgid, pPkgInfo->version, pPkgInfo->label,
775                                 pPkgInfo->pkg_description, pPkgInfo->author, pPkgInfo->icon_size, pPkgInfo->pkg_name);
776
777                 pPackageInfoImpl->SetType(PACKAGE_TYPE_WGT);
778                 pPackageInfoImpl->SetId(pPkgInfo->pkgid);
779                 pPackageInfoImpl->SetVersion(pPkgInfo->version);
780                 pPackageInfoImpl->SetDisplayName(pPkgInfo->label);
781                 pPackageInfoImpl->SetDescription(pPkgInfo->pkg_description);
782                 pPackageInfoImpl->SetAuthor(pPkgInfo->author);
783                 pPackageInfoImpl->SetMainAppId(pPkgInfo->pkg_name);
784
785                 if (IsHybridPackage(filePath) == true)
786                 {
787                         PackageInfo hybridServiceInfo;
788                         _PackageParser packageParser;
789
790                         res = packageParser.Construct(&hybridServiceInfo);
791                         SysTryReturn(NID_APP, res, null, E_PARSING_FAILED, "Construct() is failed. [%s]", pPackagePath.get());
792
793                         res = packageParser.Parse(filePath);
794                         SysTryReturn(NID_APP, res, null, E_PARSING_FAILED, "Parse() is failed. [%s]", pPackagePath.get());
795
796                         IList* pList = hybridServiceInfo.GetPrivilegeListN();
797                         if (pList)
798                         {
799                                 for (int i = 0; i < pList->GetCount(); i++)
800                                 {
801                                         PrivilegeInfo* pPrivilegeInfo = dynamic_cast < PrivilegeInfo* >(pList->GetAt(i));
802                                         if (pPrivilegeInfo)
803                                         {
804                                                 String privilege = pPrivilegeInfo->GetId();
805                                                 pPackageInfoImpl->AddPrivilege(*new (std::nothrow) String(privilege));
806                                                 SysLog(NID_APP, "privilege[%ls] is added for hybrid.", privilege.GetPointer());
807                                         }
808                                  }
809                         }
810                 }
811
812                 if (pPkgInfo->privilege_list)
813                 {
814                         GList* pList = null;
815                         pList = g_list_first(pPkgInfo->privilege_list);
816                         while (pList)
817                         {
818                                 char* pPrivilege = (char*)pList->data;
819                                 if (pPrivilege)
820                                 {
821                                         pPackageInfoImpl->AddPrivilege(*new (std::nothrow) String(pPrivilege));
822                                         free(pPrivilege);
823                                 }
824                                 pList = g_list_next(pList);
825                         }
826                         g_list_free(pPkgInfo->privilege_list);
827                 }
828
829                 std::unique_ptr< PackageAppInfo > pPackageAppInfo(new (std::nothrow) PackageAppInfo);
830                 if (pPackageAppInfo)
831                 {
832                         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
833
834                         pPackageAppInfoImpl->SetAppId(pPkgInfo->pkg_name);
835                         pPackageAppInfoImpl->SetAppName(pPkgInfo->label);
836                         pPackageAppInfoImpl->SetAppDisplayName(pPkgInfo->label);
837                         pPackageAppInfoImpl->SetMainApp(true);
838                         if ((pPkgInfo->icon_buf) && (pPkgInfo->icon_size > 0))
839                         {
840                                 pPackageAppInfoImpl->SetAppMenuIconBuffer(pPkgInfo->icon_buf, pPkgInfo->icon_size);
841                         }
842
843                         pPackageInfoImpl->AddPackageAppInfo(*pPackageAppInfo.release());
844                 }
845                 else
846                 {
847                         SysLog(NID_APP, "pPackageAppInfo instance must not be null.");
848                         pkgmgr_client_free_pkginfo(pPkgmgrInfo);
849                         return null;
850                 }
851
852                 pkgmgr_client_free_pkginfo(pPkgmgrInfo);
853         }
854         else
855         {
856                 SysTryReturn(NID_APP, false, null, E_UNSUPPORTED_FORMAT, "invalid extension! - packagePath = [%s], extension = [%s]", pPackagePath.get(), pExtension.get());
857         }
858
859         SetLastResult(E_SUCCESS);
860         return pPackageInfo.release();
861 }
862
863 IList*
864 _PackageManagerImpl::GetPackageInfoListN(const IMap& packageFilterMap) const
865 {
866         SysTryReturn(NID_APP, packageFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageFilterMap.GetCount() is invalid.");
867
868         std::unique_ptr<IMapEnumerator> pEnum(packageFilterMap.GetMapEnumeratorN());
869         SysTryReturn(NID_APP, pEnum, null, E_INVALID_ARG, "GetMapEnumeratorN() is failed.");
870
871         std::unique_ptr< ArrayList > pList(new (std::nothrow) ArrayList());
872         SysTryReturn(NID_APP, pList, null, E_OUT_OF_MEMORY, "pList is null.");
873
874         pList->Construct();
875
876         int res = PMINFO_R_OK;
877         pkgmgrinfo_pkginfo_filter_h handle = null;
878
879         res = pkgmgrinfo_pkginfo_filter_create(&handle);
880         SysTryReturn(NID_APP, res == PMINFO_R_OK, null, E_SYSTEM, "pkgmgrinfo_pkginfo_filter_create() is failed. [%d]", res);
881
882         while(pEnum->MoveNext() == E_SUCCESS)
883         {
884                 String* pKey = static_cast< String* >(pEnum->GetKey());
885                 SysTryCatch(NID_APP, pKey, , E_INVALID_ARG, "GetKey() is failed.");
886
887                 Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
888                 SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
889
890                 bool value = pVal->ToBool();
891
892                 SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), value);
893
894                 if ((*pKey) == PACKAGE_FILTER_UNINSTALLABLE)
895                 {
896                         res = pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE, value);
897                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(REMOVABLE, %d) is failed. [%d]", value, res);
898                 }
899                 else if ((*pKey) == PACKAGE_FILTER_DOWNLOADED)
900                 {
901                         res = pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD, !value);
902                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(PRELOAD, %d) is failed. [%d]", !value, res);
903                 }
904                 else if ((*pKey) == PACKAGE_FILTER_APP_SETTING)
905                 {
906                         res = pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING, value);
907                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(APPSETTING, %d) is failed. [%d]", value, res);
908                 }
909                 else if ((*pKey) == PACKAGE_FILTER_TPK)
910                 {
911                         SysTryCatch(NID_APP, value == true, , E_SYSTEM, "Value(false) is invalid for PACKAGE_FILTER_TPK.");
912
913                         res = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_TYPE, "tpk");
914                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_string(TYPE, tpk) is failed. [%d]", res);
915                 }
916                 else if ((*pKey) == PACKAGE_FILTER_EXTERNAL_STORAGE)
917                 {
918                         String installedStorage;
919                         if (value == true)
920                         {
921                                 installedStorage = L"installed_external";
922                         }
923                         else
924                         {
925                                 installedStorage = L"installed_internal";
926                         }
927
928                         std::unique_ptr<char[]> pInstalledStorage(_StringConverter::CopyToCharArrayN(installedStorage));
929                         SysTryCatch(NID_APP, pInstalledStorage, , E_OUT_OF_MEMORY, "pInstalledStorage is null.");
930
931                         SysLog(NID_APP, "Value[%d]->[%s]", value, pInstalledStorage.get());
932
933                         res = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE, pInstalledStorage.get());
934                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_string(STORAGE, %s) is failed. [%d]", pInstalledStorage.get(), res);
935                 }
936                 else
937                 {
938                         SysTryCatch(NID_APP, false, , E_INVALID_ARG, "Invalid key(%ls)", pKey->GetPointer());
939                 }
940         }
941
942         res = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, PackageInfoHandler, pList.get());
943         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_foreach_pkginfo() failed. [%d]", res);
944
945 CATCH:
946         if (handle)
947         {
948                 pkgmgrinfo_pkginfo_filter_destroy(handle);
949         }
950
951         if (pList->GetCount() <= 0)
952         {
953                 SysLog(NID_APP, "pList's count is 0.");
954                 return null;
955         }
956
957         return pList.release();
958 }
959
960 IList*
961 _PackageManagerImpl::GetPackageAppInfoListN(const IMap& packageAppFilterMap) const
962 {
963         SysTryReturn(NID_APP, packageAppFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageAppFilterMap.GetCount() is invalid.");
964
965         std::unique_ptr< IMapEnumerator > pEnum(packageAppFilterMap.GetMapEnumeratorN());
966         SysTryReturn(NID_APP, pEnum, null, E_INVALID_ARG, "GetMapEnumeratorN() is failed.");
967
968         std::unique_ptr< ArrayList > pList(new (std::nothrow) ArrayList());
969         SysTryReturn(NID_APP, pList, null, E_OUT_OF_MEMORY, "pList is null.");
970
971         pList->Construct();
972
973         int res = 0;
974         bool definedKey = false;
975         bool metadataKey = false;
976         ArrayList list;
977         ArrayList metadataList;
978         ArrayList appIdList;
979         pkgmgrinfo_appinfo_filter_h handle = null;
980         pkgmgrinfo_appinfo_metadata_filter_h metaHandle = null;
981
982         res = pkgmgrinfo_appinfo_filter_create(&handle);
983         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_filter_create() is failed. [%d]", res);
984
985         res = pkgmgrinfo_appinfo_metadata_filter_create(&metaHandle);
986         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_metadata_filter_create() is failed. [%d]", res);
987
988         while(pEnum->MoveNext() == E_SUCCESS)
989         {
990                 String* pKey = static_cast< String* >(pEnum->GetKey());
991                 SysTryCatch(NID_APP, pKey, , E_INVALID_ARG, "GetKey() is failed.");
992
993                 if ((*pKey) == PACKAGE_APP_FILTER_MENUICON_VISIBLE)
994                 {
995                         definedKey = true;
996
997                         Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
998                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
999
1000                         bool nodisplay = !(pVal->ToBool());
1001                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), nodisplay);
1002
1003                         res = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_NODISPLAY, nodisplay);
1004                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(NODISPLAY, %d) is failed. [%d]", nodisplay, res);
1005                 }
1006 #if 0
1007                 else if ((*pKey) == PACKAGE_APP_FILTER_LAUNCH_ONBOOT)
1008                 {
1009                         definedKey = true;
1010
1011                         Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
1012                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
1013
1014                         bool value = pVal->ToBool();
1015                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), value);
1016
1017                         res = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_ONBOOT, value);
1018                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(ONBOOT, %d) is failed. [%d]", value, res);
1019                 }
1020                 else if ((*pKey) == PACKAGE_APP_FILTER_AUTO_RESTART)
1021                 {
1022                         definedKey = true;
1023
1024                         Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
1025                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
1026
1027                         bool value = pVal->ToBool();
1028                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), value);
1029
1030                         res = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_AUTORESTART, value);
1031                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(AUTORESTART, %d) is failed. [%d]", value, res);
1032                 }
1033 #endif
1034                 else if ((*pKey) == PACKAGE_APP_FILTER_LAUNCHING_HISTORY_VISIBLE)
1035                 {
1036                         definedKey = true;
1037
1038                         Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
1039                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
1040
1041                         bool value = pVal->ToBool();
1042                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), value);
1043
1044                         res = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_TASKMANAGE, value);
1045                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(TASKMANAGE, %d) is failed. [%d]", value, res);
1046                 }
1047                 else if ((*pKey) == PACKAGE_APP_FILTER_LAUNCH_CONDITION)
1048                 {
1049                         definedKey = true;
1050
1051                         Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
1052                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
1053
1054                         bool value = pVal->ToBool();
1055                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), value);
1056
1057                         res = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION, value);
1058                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(LAUNCHCONDITION, %d) is failed. [%d]", value, res);
1059                 }
1060                 else if ((*pKey) == PACKAGE_APP_FILTER_CATEGORY_HOMEAPP || (*pKey) == PACKAGE_APP_FILTER_CATEGORY_LOCKAPP
1061                                                 || (*pKey) == PACKAGE_APP_FILTER_CATEGORY_MENUAPP)
1062                 {
1063                         definedKey = true;
1064
1065                         std::unique_ptr<char[]> pDefinedKey(_StringConverter::CopyToCharArrayN(*pKey));
1066                         SysTryCatch(NID_APP, pDefinedKey, , E_OUT_OF_MEMORY, "pDefinedKey is null.");
1067
1068                         Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
1069                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
1070
1071                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), pVal->ToBool());
1072                         SysTryCatch(NID_APP, pVal->ToBool() == true, , E_INVALID_ARG, "Value(false) is not allowed.");
1073
1074                         res = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_CATEGORY, pDefinedKey.get());
1075                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_filter_add_string(CATEGORY, %s) is failed. [%d]", pDefinedKey.get(), res);
1076                 }
1077                 else
1078                 {
1079                         metadataKey = true;
1080
1081                         String* pVal = static_cast< String* >(pEnum->GetValue());
1082                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
1083
1084                         SysLog(NID_APP, "Key[%ls], Value[%ls]", pKey->GetPointer(), pVal->GetPointer());
1085
1086                         std::unique_ptr<char[]> pMetaKey(_StringConverter::CopyToCharArrayN(*pKey));
1087                         SysTryCatch(NID_APP, pMetaKey, , E_OUT_OF_MEMORY, "pMetaKey is null.");
1088
1089                         std::unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pVal));
1090                         SysTryCatch(NID_APP, pValue, , E_OUT_OF_MEMORY, "pValue is null.");
1091
1092                         if ((*pVal) == L"*")
1093                         {
1094                                 res = pkgmgrinfo_appinfo_metadata_filter_add(metaHandle, pMetaKey.get(), null);
1095                                 SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_metadata_filter_add(%s, null) is failed. [%d]", pMetaKey.get(), res);
1096                         }
1097                         else
1098                         {
1099                                 res = pkgmgrinfo_appinfo_metadata_filter_add(metaHandle, pMetaKey.get(), pValue.get());
1100                                 SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_metadata_filter_add(%s, %s) is failed. [%d]", pMetaKey.get(), pValue.get(), res);
1101                         }
1102                 }
1103         }
1104
1105         if ((definedKey == true) && (metadataKey == false))
1106         {
1107                 res = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, PackageAppInfoHandler, pList.get());
1108                 SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_filter_foreach_appinfo() failed. [%d]", res);
1109         }
1110         else if ((definedKey == false) && (metadataKey == true))
1111         {
1112                 res = pkgmgrinfo_appinfo_metadata_filter_foreach(metaHandle, PackageAppInfoMetadataHandler, pList.get());
1113                 SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_metadata_filter_foreach() failed. [%d]", res);
1114         }
1115         else
1116         {
1117                 list.Construct();
1118
1119                 res = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, PackageAppInfoHandler, &list);
1120                 if (res != PMINFO_R_OK)
1121                 {
1122                         SysLog(NID_APP, "pkgmgrinfo_appinfo_filter_foreach_appinfo() is failed. result = [%d]", res);
1123                         goto CATCH;
1124                 }
1125
1126                 metadataList.Construct();
1127
1128                 res = pkgmgrinfo_appinfo_metadata_filter_foreach(metaHandle, PackageAppInfoMetadataHandler, &metadataList);
1129                 if (res != PMINFO_R_OK)
1130                 {
1131                         SysLog(NID_APP, "pkgmgrinfo_appinfo_metadata_filter_foreach() is failed. result = [%d]", res);
1132                         goto CATCH;
1133                 }
1134
1135                 for (int i = 0; i < list.GetCount(); i++)
1136                 {
1137                         PackageAppInfo* pPackageAppInfo = dynamic_cast < PackageAppInfo* >(list.GetAt(i));
1138                         if (pPackageAppInfo)
1139                         {
1140                                 SysLog(NID_APP, "PackageAppFilter - App [%ls]", pPackageAppInfo->GetAppId().GetPointer());
1141
1142                                 std::unique_ptr< AppId > pAppId(new (std::nothrow) AppId(pPackageAppInfo->GetAppId()));
1143                                 appIdList.Add(pAppId.release());
1144                         }
1145                 }
1146
1147                 for (int j = 0; j < metadataList.GetCount(); j++)
1148                 {
1149                         PackageAppInfo* pPackageAppInfo = dynamic_cast < PackageAppInfo* >(metadataList.GetAt(j));
1150                         if (pPackageAppInfo)
1151                         {
1152                                 AppId appId = pPackageAppInfo->GetAppId();
1153
1154                                 if (appIdList.Contains(appId) == true)
1155                                 {
1156                                         SysLog(NID_APP, "App [%ls] is matched.", appId.GetPointer());
1157
1158                                         std::unique_ptr< PackageAppInfo > pAppInfo(new (std::nothrow) PackageAppInfo);
1159                                         SysTryCatch(NID_APP, pAppInfo, , E_OUT_OF_MEMORY, "pAppInfo is null.");
1160
1161                                         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pAppInfo.get());
1162                                         pPackageAppInfoImpl->Construct(appId);
1163
1164                                         pList->Add(pAppInfo.release());
1165                                 }
1166                                 else
1167                                 {
1168                                         SysLog(NID_APP, "App [%ls] is not matched.", pPackageAppInfo->GetAppId().GetPointer());
1169                                 }
1170                         }
1171                 }
1172         }
1173
1174 CATCH:
1175         if (handle)
1176         {
1177                 pkgmgrinfo_appinfo_filter_destroy(handle);
1178         }
1179
1180         if (metaHandle)
1181         {
1182                 pkgmgrinfo_appinfo_metadata_filter_destroy(metaHandle);
1183         }
1184
1185         if (pList->GetCount() <= 0)
1186         {
1187                 SysLog(NID_APP, "pList's count is 0.");
1188                 return null;
1189         }
1190
1191         return pList.release();
1192 }
1193
1194 IList*
1195 _PackageManagerImpl::GetPackageAppInfoListN(const IMap& packageFilterMap, const IMap& packageAppFilterMap) const
1196 {
1197         SysTryReturn(NID_APP, packageFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageFilterMap Count is invalid.");
1198         SysTryReturn(NID_APP, packageAppFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageAppFilterMap Count is invalid.");
1199
1200         ArrayList appIdList;
1201
1202         std::unique_ptr< IList > pPackageFilterList(GetPackageInfoListN(packageFilterMap));
1203         if (pPackageFilterList == null)
1204         {
1205                 SysLog(NID_APP, "GetPackageInfoListN(packageFilterMap) is null.");
1206                 return null;
1207         }
1208
1209         for (int i = 0; i < pPackageFilterList->GetCount(); i++)
1210         {
1211                 PackageInfo* pPackageInfo = dynamic_cast < PackageInfo* >(pPackageFilterList->GetAt(i));
1212                 if (pPackageInfo)
1213                 {
1214                         std::unique_ptr< IList > pPackageAppInfoList(pPackageInfo->GetPackageAppInfoListN());
1215                         if (pPackageAppInfoList)
1216                         {
1217                                 for (int j = 0; j < pPackageAppInfoList->GetCount(); j++)
1218                                 {
1219                                         PackageAppInfo* pPackageAppInfo = dynamic_cast < PackageAppInfo* >(pPackageAppInfoList->GetAt(j));
1220                                         if (pPackageAppInfo)
1221                                         {
1222                                                 SysLog(NID_APP, "PackageFilter - App [%ls]", pPackageAppInfo->GetAppId().GetPointer());
1223
1224                                                 std::unique_ptr< AppId > pAppId(new (std::nothrow) AppId(pPackageAppInfo->GetAppId()));
1225                                                 appIdList.Add(pAppId.release());
1226                                         }
1227                                 }
1228                         }
1229                 }
1230         }
1231
1232         std::unique_ptr< ArrayList > pList(new (std::nothrow) ArrayList);
1233         SysTryReturn(NID_APP, pList, null, E_OUT_OF_MEMORY, "pList is null.");
1234
1235         std::unique_ptr< IList > pAppFilterList(GetPackageAppInfoListN(packageAppFilterMap));
1236         if (pAppFilterList == null)
1237         {
1238                 SysLog(NID_APP, "GetPackageAppInfoListN(packageAppFilterMap) is null.");
1239                 return null;
1240         }
1241
1242         for (int k = 0; k < pAppFilterList->GetCount(); k++)
1243         {
1244                 PackageAppInfo* pPackageAppInfo = dynamic_cast < PackageAppInfo* >(pAppFilterList->GetAt(k));
1245                 if (pPackageAppInfo)
1246                 {
1247                         AppId appId = pPackageAppInfo->GetAppId();
1248                         SysLog(NID_APP, "AppFilter - App [%ls]", appId.GetPointer());
1249
1250                         if (appIdList.Contains(appId) == true)
1251                         {
1252                                 SysLog(NID_APP, "App [%ls] is matched.", appId.GetPointer());
1253
1254                                 std::unique_ptr< PackageAppInfo > pPackageAppInfo(new (std::nothrow) PackageAppInfo);
1255                                 SysTryReturn(NID_APP, pPackageAppInfo, null, E_OUT_OF_MEMORY, "PackageAppInfo is null.");
1256
1257                                 _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
1258                                 pPackageAppInfoImpl->Construct(appId);
1259
1260                                 pList->Add(pPackageAppInfo.release());
1261                         }
1262                         else
1263                         {
1264                                 SysLog(NID_APP, "App [%ls] is not matched.", pPackageAppInfo->GetAppId().GetPointer());
1265                         }
1266                 }
1267         }
1268
1269         if (pList->GetCount() <= 0)
1270         {
1271                 SysLog(NID_APP, "pList's count is 0.");
1272                 return null;
1273         }
1274
1275         return pList.release();
1276 }
1277
1278 int
1279 _PackageManagerImpl::PackageInfoHandler(const pkgmgrinfo_pkginfo_h handle, void* pUserData)
1280 {
1281         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "handle must not be null.");
1282         SysTryReturn(NID_APP, pUserData, 0, E_SYSTEM, "pUserData must not be null.");
1283
1284         result r = E_SUCCESS;
1285         int res = PMINFO_R_OK;
1286         char* pPackageId = null;
1287         ArrayList* pList = (ArrayList*)pUserData;
1288
1289         res = pkgmgrinfo_pkginfo_get_pkgname(handle, &pPackageId);
1290         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_pkgname() is failed. [%d]", res);
1291
1292         std::unique_ptr<PackageInfo> pPackageInfo(new (std::nothrow) PackageInfo);
1293         SysTryReturn(NID_APP, pPackageInfo, 0, E_OUT_OF_MEMORY, "pPackageInfo instance must not be null.");
1294
1295         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
1296         r = pPackageInfoImpl->Construct(pPackageId);
1297         SysTryReturn(NID_APP, r == E_SUCCESS, 0, E_SYSTEM, "Construct(%s) is failed.", pPackageId);
1298
1299         pList->Add(*pPackageInfo.release());
1300
1301         return 0;
1302 }
1303
1304 int
1305 _PackageManagerImpl::PackageAppInfoHandler(const pkgmgrinfo_appinfo_h handle, void* pUserData)
1306 {
1307         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "handle must not be null.");
1308         SysTryReturn(NID_APP, pUserData, 0, E_SYSTEM, "pUserData must not be null.");
1309
1310         result r = E_SUCCESS;
1311         int res = PMINFO_R_OK;
1312         char* pAppId = null;
1313         ArrayList* pList = (ArrayList*)pUserData;
1314
1315         res = pkgmgrinfo_appinfo_get_appid(handle, &pAppId);
1316         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_SYSTEM, "pkgmgrinfo_appinfo_get_appid is failed. [%d]", res);
1317
1318         SysLog(NID_APP, "app = [%s]", pAppId);
1319
1320         std::unique_ptr<PackageAppInfo> pPackageAppInfo(new (std::nothrow) PackageAppInfo);
1321         SysTryReturn(NID_APP, pPackageAppInfo, 0, E_OUT_OF_MEMORY, "pPackageAppInfo instance must not be null.");
1322
1323         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
1324         r = pPackageAppInfoImpl->Construct(pAppId);
1325         SysTryReturn(NID_APP, r == E_SUCCESS, 0, E_SYSTEM, "Construct(%s) is failed.", pAppId);
1326
1327         pList->Add(*pPackageAppInfo.release());
1328
1329         return 0;
1330 }
1331
1332 int
1333 _PackageManagerImpl::PackageAppInfoMetadataHandler(const pkgmgrinfo_appinfo_h handle, void* pUserData)
1334 {
1335         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "handle must not be null.");
1336         SysTryReturn(NID_APP, pUserData, 0, E_SYSTEM, "pUserData must not be null.");
1337
1338         result r = E_SUCCESS;
1339         int res = PMINFO_R_OK;
1340         char* pAppId = null;
1341         ArrayList* pList = (ArrayList*)pUserData;
1342
1343         res = pkgmgrinfo_appinfo_get_appid(handle, &pAppId);
1344         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_SYSTEM, "pkgmgrinfo_appinfo_get_appid is failed. [%d]", res);
1345
1346         SysLog(NID_APP, "app = [%s]", pAppId);
1347
1348         std::unique_ptr< PackageAppInfo > pPackageAppInfo(new (std::nothrow) PackageAppInfo);
1349         SysTryReturn(NID_APP, pPackageAppInfo, 0, E_OUT_OF_MEMORY, "pPackageAppInfo instance must not be null.");
1350
1351         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
1352         r = pPackageAppInfoImpl->Construct(pAppId);
1353         SysTryReturn(NID_APP, r == E_SUCCESS, 0, E_SYSTEM, "Construct(%s) is failed.", pAppId);
1354
1355         pList->Add(*pPackageAppInfo.release());
1356
1357         return 0;
1358 }
1359
1360 bool
1361 _PackageManagerImpl::IsHybridPackage(const String& filePath) const
1362 {
1363         SysTryReturn(NID_APP, filePath.IsEmpty() == false, false, E_INVALID_ARG, "filePath is empty.");
1364
1365         bool res = true;
1366         FileUnzipper unzipper;
1367         ZipEntry zipEntry;
1368
1369         result r = unzipper.Construct(filePath);
1370         SysTryReturn(NID_APP, !IsFailed(r), false, E_SYSTEM, "unzipper.Construct() failed.");
1371
1372         r = unzipper.GetEntry(L"info/manifest.xml", zipEntry);
1373         if (!IsFailed(r))
1374         {
1375                 SysLog(NID_APP, "It's hybrid package. [%ls]", filePath.GetPointer());
1376                 res = true;
1377         }
1378         else
1379         {
1380                 SysLog(NID_APP, "It's not hybrid package. [%ls]", filePath.GetPointer());
1381                 res = false;
1382         }
1383
1384         SetLastResult(E_SUCCESS);
1385         return res;
1386 }
1387
1388 _PackageManagerImpl*
1389 _PackageManagerImpl::GetInstance(void)
1390 {
1391         return PackageManager::GetInstance()->__pPackageManagerImpl;
1392 }
1393
1394 result
1395 _PackageManagerImpl::Construct(void)
1396 {
1397         ClearLastResult();
1398
1399         result r = __installationList.Construct();
1400         SysTryReturnResult(NID_APP, r == E_SUCCESS, r, "The memory is insufficient.");
1401
1402         return E_SUCCESS;
1403 }
1404
1405 _PackageManagerImpl::_PackageManagerImpl(void)
1406 :__pRequestClient(null),
1407 __pListeningClient(null)
1408 {
1409 }
1410
1411 _PackageManagerImpl::~_PackageManagerImpl(void)
1412 {
1413 }
1414
1415 void
1416 _PackageManagerImpl::SendPackageEvent(PackageType type, const PackageId& packageId, const char* pEventKey, const char* pEventValue)
1417 {
1418         result r = E_SUCCESS;
1419         bool install = true;
1420         bool move = false;
1421
1422         if (strcmp(pEventKey, "start") == 0)
1423         {
1424                 if ((strcmp(pEventValue, "install") == 0)
1425                                 || (strcmp(pEventValue, "update") == 0)
1426                                 ||(strcmp(pEventValue, "uninstall") == 0)
1427                                 ||(strcmp(pEventValue, "move") == 0))
1428                 {
1429                         PackageId* pPackageId = new (std::nothrow) String(packageId);
1430                         SysTryReturnVoidResult(NID_APP, pPackageId != null, E_OUT_OF_MEMORY, "pPackageId is null.");
1431
1432                         String* pOperation = new (std::nothrow) String(pEventValue);
1433                         SysTryReturnVoidResult(NID_APP, pOperation != null, E_OUT_OF_MEMORY, "pOperation is null.");
1434
1435                         r = __installationList.Add(*pPackageId, *pOperation);
1436                         if (IsFailed(r))
1437                         {
1438                                 delete pPackageId;
1439                                 delete pOperation;
1440                                 SysLog(NID_APP, "Failed to add installation condition.");
1441                                 SetLastResult(E_SYSTEM);
1442                                 return;
1443                         }
1444                 }
1445         }
1446         else if (strcmp(pEventKey, "end") == 0)
1447         {
1448                 String* pOperation = static_cast <String*>(__installationList.GetValue(packageId));
1449                 SysTryReturnVoidResult(NID_APP, pOperation != null, E_SYSTEM, "pOperation is null.");
1450
1451                 if (pOperation->Equals("uninstall", true) == true)
1452                 {
1453                         install = false;
1454                 }
1455                 else if (pOperation->Equals("move", true) == true)
1456                 {
1457                         move = true;
1458                 }
1459
1460                 r = __installationList.Remove(packageId, true);
1461                 SysTryReturnVoidResult(NID_APP, !IsFailed(r), E_SYSTEM, "installationList.Remove(%ls) failed. (%s)", packageId.GetPointer(), GetErrorMessage(r));
1462         }
1463
1464         if ((strcmp(pEventValue, "move") == 0) || (move == true))
1465         {
1466                 SysLog(NID_APP, "Skip move event.");
1467                 return;
1468         }
1469
1470         std::unique_ptr< IEnumeratorT<_PackageManagerEvent*> > pEnum(__packageEventListenerList.GetEnumeratorN());
1471         SysTryReturnVoidResult(NID_APP, pEnum, E_OUT_OF_MEMORY, "The memory is insufficient.");
1472
1473         while (pEnum->MoveNext() == E_SUCCESS)
1474         {
1475                 _PackageManagerEvent*   pEvent = null;
1476                 pEnum->GetCurrent(pEvent);
1477                 if (pEvent)
1478                 {
1479                         _PackageManagerEventArg* pEventArg= new (std::nothrow) _PackageManagerEventArg();
1480                         SysTryReturnVoidResult(NID_APP, pEventArg, E_OUT_OF_MEMORY, "The memory is insufficient.");
1481
1482                         String eventKey(pEventKey);
1483                         String eventValue(pEventValue);
1484
1485                         pEventArg->__packageId = packageId;
1486                         pEventArg->__eventKey = eventKey;
1487                         pEventArg->__eventValue = eventValue;
1488                         pEventArg->__install = install;
1489
1490                         r = pEvent->Fire(*pEventArg);
1491                         if (r != E_SUCCESS)
1492                         {
1493                                 SysLog(NID_APP, "pEvent->Fire(*pEventArg) failed. [%s]", GetErrorMessage(r));
1494                         }
1495
1496                         SysLog(NID_APP, "Package = [%ls], Key = [%ls], Value = [%ls], install = [%d]", pEventArg->__packageId.GetPointer(), pEventArg->__eventKey.GetPointer(), pEventArg->__eventValue.GetPointer(), pEventArg->__install);
1497                 }
1498                 else
1499                 {
1500                         SysLog(NID_APP, "pEvent is null.");
1501                 }
1502         }
1503 }
1504
1505 PackageInfo*
1506 _PackageManagerImpl::GetPackageInfoN(PackageType packageType, const String& packageName) const
1507 {
1508         SysTryReturn(NID_APP, packageName.IsEmpty() == false, null, E_INVALID_ARG, "[E_INVALID_ARG] packageName is empty.");
1509
1510         result r = E_SUCCESS;
1511         Database db;
1512         DbStatement* pStmt = null;
1513         DbEnumerator* pEnum = null;
1514         String query;
1515         PackageInfo *pPackageInfo = null;
1516
1517         query.Format(   1024, L"SELECT PkgInfo.*, AppInfo.APP_MAINMENU_ICON FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and AppInfo.PACKAGE_NAME = '%ls'", packageName.GetPointer());
1518
1519         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1520         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1521
1522         pStmt = CreateStatementN(db, query);
1523
1524         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1525
1526         pEnum = ExecuteStatementN(db, pStmt);
1527         if (pEnum != null)
1528         {
1529                 if (pEnum->MoveNext() == E_SUCCESS)
1530                 {
1531                         pPackageInfo = new (std::nothrow) PackageInfo;
1532                         SysTryReturn(NID_APP, pPackageInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageInfo instance must not be null.");
1533
1534                         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo);
1535
1536                         String id;
1537                         String appVersion;
1538                         String appType;
1539                         String appMimeType;
1540                         String appApiVersion;
1541                         String appName;
1542                         String appVendor;
1543                         String appDescription;
1544                         String appUrl;
1545                         String appCid;
1546                         String appRootPath;
1547                         int appStorageType = 0;
1548                         DateTime appInstallationTime;
1549                         String appIconPath;
1550
1551                         pEnum->GetStringAt(1, id);
1552                         pEnum->GetStringAt(3, appVersion);
1553                         pEnum->GetStringAt(4, appType);
1554                         pEnum->GetStringAt(5, appMimeType);
1555                         pEnum->GetStringAt(6, appApiVersion);
1556                         pEnum->GetStringAt(7, appName);
1557                         pEnum->GetStringAt(8, appVendor);
1558                         pEnum->GetStringAt(9, appDescription);
1559                         pEnum->GetStringAt(10, appUrl);
1560                         pEnum->GetStringAt(11, appCid);
1561                         pEnum->GetStringAt(16, appRootPath);
1562                         pEnum->GetIntAt(17, appStorageType);
1563                         pEnum->GetDateTimeAt(18, appInstallationTime);
1564                         pEnum->GetStringAt(21, appIconPath);
1565
1566                         pPackageInfoImpl->SetAppId(id);
1567                         pPackageInfoImpl->SetAppVersion(appVersion);
1568                         pPackageInfoImpl->SetAppMimeType(appMimeType);
1569                         pPackageInfoImpl->SetAppApiVersion(appApiVersion);
1570                         pPackageInfoImpl->SetAppName(appName);
1571                         pPackageInfoImpl->SetAppVendor(appVendor);
1572                         pPackageInfoImpl->SetAppDescription(appDescription);
1573                         pPackageInfoImpl->SetAppUrl(appUrl);
1574                         pPackageInfoImpl->SetAppCid(appCid);
1575                         pPackageInfoImpl->SetAppRootPath(appRootPath);
1576                         pPackageInfoImpl->SetAppStorageType(appStorageType);
1577                         pPackageInfoImpl->SetAppInstallationTime(appInstallationTime);
1578                         pPackageInfoImpl->SetAppIconPath(appIconPath);
1579                 }
1580
1581                 delete pEnum;
1582         }
1583         else
1584         {
1585                 r = E_OBJ_NOT_FOUND;
1586         }
1587
1588 CATCH:
1589         delete pStmt;
1590         return pPackageInfo;
1591 }
1592
1593 int
1594 _PackageManagerImpl::PackageInfoEventHandler(const pkgmgrinfo_pkginfo_h handle, void* pUserData)
1595 {
1596         SysTryReturn(NID_APP, handle != null, 0, E_SYSTEM, "[E_SYSTEM] handle must not be null.");
1597
1598         result r = E_SUCCESS;
1599         int result = 0;
1600         char* pPackage = null;
1601         ArrayList* pList = (ArrayList*)pUserData;
1602
1603         std::unique_ptr< PackageInfo > pPackageInfo(new (std::nothrow) PackageInfo);
1604         SysTryReturn(NID_APP, pPackageInfo, null, E_OUT_OF_MEMORY, "pPackageInfo instance must not be null.");
1605
1606         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
1607
1608         result = pkgmgrinfo_pkginfo_get_pkgname(handle, &pPackage);
1609         if (result == 0)
1610         {
1611                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_pkgname(): package = [%s]", pPackage);
1612         }
1613         else
1614         {
1615                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_pkgname() is failed. result = [%d]", result);
1616         }
1617
1618         r = pPackageInfoImpl->Construct(pPackage);
1619         SysTryReturn(NID_APP, r == E_SUCCESS, -1, E_SYSTEM, "pPackageInfoImpl->Construct() failed.");
1620
1621         r = pList->Add(*pPackageInfo.release());
1622         SysTryReturn(NID_APP, r == E_SUCCESS, -1, E_SYSTEM, "pList->Add() failed.");
1623
1624         return result;
1625 }
1626
1627 IList*
1628 _PackageManagerImpl::GetFilteredAppIdListN(const String& feature, const String& value) const
1629 {
1630         result r = E_SUCCESS;
1631         Database db;
1632         DbStatement* pStmt = null;
1633         DbEnumerator* pEnum = null;
1634         String query;
1635         ArrayList* pList = null;
1636
1637         query.Format(1024, L"SELECT AppInfo.PACKAGE_NAME FROM AppInfo, AppFeature WHERE AppFeature.ID = AppInfo.UNIQUE_ID and AppFeature.NAME = '%ls' and AppFeature.VALUE = '%ls' COLLATE NOCASE", feature.GetPointer(), value.GetPointer());
1638
1639         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1640         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1641
1642         pStmt = CreateStatementN(db, query);
1643         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1644
1645         pEnum = ExecuteStatementN(db, pStmt);
1646
1647         if (pEnum != null)
1648         {
1649                 pList = new (std::nothrow) ArrayList();
1650                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
1651                 pList->Construct();
1652
1653                 while (pEnum->MoveNext() == E_SUCCESS)
1654                 {
1655                         String* pPackageName = new (std::nothrow) String;
1656                         SysTryReturn(NID_APP, pPackageName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageName instance must not be null.");
1657
1658                         pEnum->GetStringAt(0, *pPackageName);
1659
1660                         pList->Add(*pPackageName);
1661                 }
1662
1663                 delete pEnum;
1664
1665         }
1666         else
1667         {
1668                 r = E_OBJ_NOT_FOUND;
1669         }
1670
1671 CATCH:
1672         delete pStmt;
1673         return pList;
1674 }
1675
1676 IList*
1677 _PackageManagerImpl::GetDataControlInfoN(const String& providerId, const String& type) const
1678 {
1679         result r = E_SUCCESS;
1680         Database db;
1681         DbStatement* pStmt = null;
1682         DbEnumerator* pEnum = null;
1683         String query;
1684         ArrayList* pList = null;
1685
1686         query.Format(1024, L"SELECT AppInfo.PACKAGE_NAME, DataControl.ACCESS FROM AppInfo, DataControl WHERE DataControl.ID = AppInfo.UNIQUE_ID and DataControl.PROVIDER_ID = '%ls' and DataControl.TYPE = '%ls' COLLATE NOCASE", providerId.GetPointer(), type.GetPointer());
1687
1688         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1689         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1690
1691         pStmt = CreateStatementN(db, query);
1692         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1693
1694         pEnum = ExecuteStatementN(db, pStmt);
1695
1696         if (pEnum != null)
1697         {
1698                 pList = new (std::nothrow) ArrayList();
1699                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
1700                 pList->Construct();
1701
1702                 while (pEnum->MoveNext() == E_SUCCESS)
1703                 {
1704                         String* pPackageName = new (std::nothrow) String;
1705                         SysTryReturn(NID_APP, pPackageName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageName instance must not be null.");
1706
1707                         String* pAccess = new (std::nothrow) String;
1708                         SysTryReturn(NID_APP, pAccess != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pAccess instance must not be null.");
1709
1710                         pEnum->GetStringAt(0, *pPackageName);
1711                         pList->Add(*pPackageName);
1712
1713                         pEnum->GetStringAt(1, *pAccess);
1714                         pList->Add(*pAccess);
1715                 }
1716
1717                 delete pEnum;
1718
1719         }
1720         else
1721         {
1722                 r = E_OBJ_NOT_FOUND;
1723         }
1724
1725 CATCH:
1726         delete pStmt;
1727         return pList;
1728 }
1729
1730 IMap*
1731 _PackageManagerImpl::GetUiThemeListN(const PackageId& packageId) const
1732 {
1733         result r = E_SUCCESS;
1734         Database db;
1735         DbStatement* pStmt = null;
1736         DbEnumerator* pEnum = null;
1737         String query;
1738         HashMap* pList = null;
1739
1740         query.Format(1024, L"SELECT AppFeature.NAME, AppFeature.VALUE FROM PkgInfo, AppInfo, AppFeature WHERE (AppFeature.ID = AppInfo.UNIQUE_ID and AppInfo.ID = PkgInfo.UNIQUE_ID and PkgInfo.PKG_ID = '%ls') and (AppFeature.NAME = 'SystemTheme' or AppFeature.NAME = 'UserDefinedTheme')"
1741                         , packageId.GetPointer());
1742
1743         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1744         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1745
1746         pStmt = CreateStatementN(db, query);
1747         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1748
1749         pEnum = ExecuteStatementN(db, pStmt);
1750
1751         if (pEnum != null)
1752         {
1753                 pList = new (std::nothrow) HashMap();
1754                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "HashMap creation failure.");
1755                 pList->Construct();
1756
1757                 while (pEnum->MoveNext() == E_SUCCESS)
1758                 {
1759                         String* pName = new (std::nothrow) String;
1760                         SysTryCatch(NID_APP, pName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pName instance must not be null.");
1761
1762                         String* pValue = new (std::nothrow) String;
1763                         SysTryCatch(NID_APP, pValue != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pValue instance must not be null.");
1764
1765                         pEnum->GetStringAt(0, *pName);
1766                         pEnum->GetStringAt(1, *pValue);
1767
1768                         pList->Add(*pName, *pValue);
1769                 }
1770
1771                 delete pEnum;
1772
1773         }
1774         else
1775         {
1776                 r = E_OBJ_NOT_FOUND;
1777         }
1778
1779 CATCH:
1780         delete pStmt;
1781         return pList;
1782 }
1783
1784 bool
1785 _PackageManagerImpl::IsAppInstalled(const AppId& appId)
1786 {
1787         SysTryReturn(NID_APP, appId.IsEmpty() == false, false, E_INVALID_ARG, "appId is empty.");
1788
1789         int result = 0;
1790         pkgmgrinfo_appinfo_h pAppInfoHandle = null;
1791
1792         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
1793         SysTryReturn(NID_APP, pAppId, false, E_OUT_OF_MEMORY, "pAppId is null.");
1794
1795         result = pkgmgrinfo_appinfo_get_appinfo(pAppId.get(), &pAppInfoHandle);
1796         if (result != 0)
1797         {
1798                 SysLog(NID_APP, "pkgmgrinfo_appinfo_get_appinfo() failed. result=[%d], app=[%s]", result, pAppId.get());
1799                 return false;
1800         }
1801
1802         if(pAppInfoHandle)
1803         {
1804                 pkgmgrinfo_appinfo_destroy_appinfo(pAppInfoHandle);
1805         }
1806
1807         SysLog(NID_APP, "app = [%ls] is installed.", appId.GetPointer());
1808
1809         return true;
1810 }
1811
1812 int
1813 _PackageManagerImpl::PackageEventHandler(int req_id, const char* pkg_type, const char* pkg_name,
1814                                                                                 const char* key, const char* val, const void* pmsg, void* data)
1815 {
1816         SysLog(NID_APP, "PackageEventHandler - req: %d, pkg_type: %s, pkg_name: %s, key: %s, val: %s", req_id, pkg_type,
1817                                 pkg_name, key, val);
1818
1819         PackageType type = PACKAGE_TYPE_TPK;
1820         PackageId packageId(pkg_name);
1821
1822         if (strcmp(pkg_type, "tpk") == 0)
1823         {
1824                 type = PACKAGE_TYPE_TPK;
1825         }
1826         else if (strcmp(pkg_type, "wgt") == 0)
1827         {
1828                 type = PACKAGE_TYPE_WGT;
1829         }
1830         else
1831         {
1832                 SysLog(NID_APP, "Invalid type - pkg_type: %s", pkg_type);
1833                 return 0;
1834         }
1835
1836         _PackageManagerImpl* pThis = _PackageManagerImpl::GetInstance();
1837         pThis->SendPackageEvent(type, packageId, key, val);
1838
1839         return 0;
1840 }
1841
1842 String*
1843 _PackageManagerImpl::GetAppIdOfDataControlN(const String& providerId)
1844 {
1845         result r = E_SUCCESS;
1846         Database db;
1847         DbStatement* pStmt = null;
1848         DbEnumerator* pEnum = null;
1849         String query;
1850         String* pAppId = null;
1851
1852         query.Format(1024, L"SELECT AppInfo.PACKAGE_NAME FROM AppInfo, DataControl WHERE DataControl.PROVIDER_ID = '%ls' and DataControl.ID = AppInfo.UNIQUE_ID",
1853                         providerId.GetPointer());
1854
1855         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1856         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1857
1858         pStmt = CreateStatementN(db, query);
1859         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1860
1861         pEnum = ExecuteStatementN(db, pStmt);
1862
1863         if (pEnum != null)
1864         {
1865                 if (pEnum->MoveNext() == E_SUCCESS)
1866                 {
1867                         pAppId = new (std::nothrow) String;
1868                         SysTryReturn(NID_APP, pAppId != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory");
1869
1870                         pEnum->GetStringAt(0, *pAppId);
1871                 }
1872
1873                 delete pEnum;
1874         }
1875
1876 CATCH:
1877         delete pStmt;
1878         return pAppId;
1879 }
1880
1881 PackageInfo*
1882 _PackageManagerImpl::GetPackageInfoN(const String& providerId, const String& operationId) const
1883 {
1884         result r = E_SUCCESS;
1885         PackageInfo* pPkgInfo = null;
1886         Database db;
1887         DbStatement* pStmt = null;
1888         DbEnumerator* pEnum = null;
1889         String query;
1890
1891         query.Format(1024, L"SELECT PkgInfo.PKG_ID FROM PkgInfo, AppInfo, AppInfoLookup, AppControl, Capability "
1892                         "WHERE AppControl.PROVIDER_ID ='%ls' and Capability.OPERATION_ID ='%ls' and AppControl.ID = AppInfoLookup.AppControlID "
1893                         "and Capability.ID = AppInfoLookup.CapabilityID and AppInfoLookup.AppInfoID = AppInfo.UNIQUE_ID and AppInfo.ID = PkgInfo.UNIQUE_ID "
1894                         "GROUP BY AppInfoID"
1895                         , providerId.GetPointer(), operationId.GetPointer());
1896
1897         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1898         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1899
1900         pStmt = CreateStatementN(db, query);
1901         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1902
1903         pEnum = ExecuteStatementN(db, pStmt);
1904         if (pEnum != null)
1905         {
1906                 if (pEnum->MoveNext() == E_SUCCESS)
1907                 {
1908                         PackageId packageId;
1909                         pEnum->GetStringAt(0, packageId);
1910
1911                         pPkgInfo = GetPackageInfoN(packageId);
1912                 }
1913
1914                 delete pEnum;
1915         }
1916         else
1917         {
1918                 r = E_OBJ_NOT_FOUND;
1919         }
1920
1921 CATCH:
1922         delete pStmt;
1923         return pPkgInfo;
1924 }
1925
1926 ArrayList*
1927 _PackageManagerImpl::GetPackageAppInfoImplListN(const String& packageId) const
1928 {
1929         result r = E_SUCCESS;
1930         Database db;
1931         DbStatement* pStmt = null;
1932         DbEnumerator* pEnum = null;
1933         String query;
1934         //int id = 0;
1935         PackageInfo* pPkgInfo = null;
1936         _PackageInfoImpl* pPackageInfoImpl = null;
1937         ArrayList* pList = null;
1938
1939         pPkgInfo = GetPackageInfoN(packageId);
1940         SysTryCatch(NID_APP, pPkgInfo != null, , r, "[%s] GetPackageInfoN() is failed", GetErrorMessage(r));
1941
1942         pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo);
1943         query.Format(1024, L"SELECT * FROM AppInfo WHERE ID = %d", pPackageInfoImpl->GetUniqueId());
1944
1945         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1946         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1947
1948         pStmt = CreateStatementN(db, query);
1949         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
1950                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1951
1952         pEnum = ExecuteStatementN(db, pStmt);
1953
1954         if (pEnum != null)
1955         {
1956                 pList = new (std::nothrow) ArrayList;
1957                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Container allocation failure.");
1958
1959                 pList->Construct();
1960
1961                 while (pEnum->MoveNext() == E_SUCCESS)
1962                 {
1963                         _PackageAppInfoImpl* pPackageAppInfoImpl = new (std::nothrow) _PackageAppInfoImpl;
1964                         SysTryReturn(NID_APP, pPackageAppInfoImpl != null, null, E_OUT_OF_MEMORY, "pPackageAppInfoImpl instance must not be null.");
1965
1966                         int uniqueId = 0;
1967                         int pkgId = 0;
1968                         String name;
1969                         String defaultapp;
1970                         String mainmenuIcon;
1971                         String settingIcon;
1972                         String quickpanelIcon;
1973                         int appFeature = 0;
1974                         String packageName;
1975
1976                         pEnum->GetIntAt(0, uniqueId);
1977                         pEnum->GetIntAt(1, pkgId);
1978                         pEnum->GetStringAt(2, name);
1979                         pEnum->GetStringAt(3, defaultapp);
1980                         pEnum->GetStringAt(4, mainmenuIcon);
1981                         pEnum->GetStringAt(5, settingIcon);
1982                         pEnum->GetStringAt(7, quickpanelIcon);
1983                         pEnum->GetIntAt(9, appFeature);
1984                         pEnum->GetStringAt(10, packageName);
1985
1986                         pPackageAppInfoImpl->SetUniqueId(uniqueId);
1987                         pPackageAppInfoImpl->SetPkgId(pkgId);
1988                         pPackageAppInfoImpl->SetName(name);
1989                         pPackageAppInfoImpl->SetDefault(defaultapp);
1990                         pPackageAppInfoImpl->SetMainmenuIcon(mainmenuIcon);
1991                         pPackageAppInfoImpl->SetSettingIcon(settingIcon);
1992                         pPackageAppInfoImpl->SetQuickpanelIcon(quickpanelIcon);
1993                         pPackageAppInfoImpl->SetAppFeature(appFeature);
1994                         pPackageAppInfoImpl->SetPackageName(packageName);
1995
1996                         pList->Add(*pPackageAppInfoImpl);
1997                 }
1998
1999                 delete pEnum;
2000         }
2001
2002 CATCH:
2003         delete pPkgInfo;
2004         delete pStmt;
2005         return pList;
2006 }
2007
2008
2009 ArrayList*
2010 _PackageManagerImpl::GetAppLaunchConditionListN(const String& packageName) const
2011 {
2012         result r = E_SUCCESS;
2013         Database db;
2014         DbStatement* pStmt = null;
2015         DbEnumerator* pEnum = null;
2016         String query;
2017         ArrayList* pList = null;
2018
2019         query.Format(1024, L"SELECT LaunchCondition.* FROM LaunchCondition, AppInfo WHERE AppInfo.UNIQUE_ID = LaunchCondition.ID and AppInfo.PACKAGE_NAME = '%ls'", packageName.GetPointer());
2020
2021         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
2022         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
2023
2024         pStmt = CreateStatementN(db, query);
2025         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
2026                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
2027
2028         pEnum = ExecuteStatementN(db, pStmt);
2029         if (pEnum != null)
2030         {
2031                 pList = new (std::nothrow) ArrayList;
2032                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Container allocation failure.");
2033
2034                 pList->Construct();
2035
2036                 while (pEnum->MoveNext() == E_SUCCESS)
2037                 {
2038                         _LaunchConditionInfoImpl* pLaunchCondtion = new (std::nothrow) _LaunchConditionInfoImpl;
2039                         SysTryReturn(NID_APP, pLaunchCondtion != null, null, E_OUT_OF_MEMORY, "pLaunchCondtion instance must not be null.");
2040
2041                         String name;
2042                         String value;
2043
2044                         pEnum->GetStringAt(1, name);
2045                         pEnum->GetStringAt(2, value);
2046
2047                         pLaunchCondtion->SetName(name);
2048                         pLaunchCondtion->SetValue(value);
2049
2050                         pList->Add(*pLaunchCondtion);
2051
2052                         SysLog(NID_APP, "Name[%ls], Value[%ls]", name.GetPointer(), value.GetPointer());
2053                 }
2054
2055                 delete pEnum;
2056         }
2057
2058 CATCH:
2059         delete pStmt;
2060         return pList;
2061 }
2062
2063 result
2064 _PackageManagerImpl::GetPackageName(const PackageId& packageId, const String* pName, char* pPackageName, int bufferSize)
2065 {
2066         result r = E_SUCCESS;
2067         Database db;
2068         DbStatement* pStmt = null;
2069         DbEnumerator* pEnum = null;
2070         String query;
2071         Tizen::Base::String packageName;
2072
2073         if (pName == null)
2074         {
2075                 query.Format(
2076                         1024,
2077                         L"SELECT AppInfo.PACKAGE_NAME FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and AppInfo.APP_DEFAULT = '%s' and PkgInfo.PKG_ID = '%ls'",
2078                         "True", packageId.GetPointer());
2079         }
2080         else
2081         {
2082                 query.Format(
2083                         1024,
2084                         L"SELECT AppInfo.PACKAGE_NAME FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and APP_NAME = '%ls' and PkgInfo.PKG_ID = '%ls'",
2085                         pName->GetPointer(), packageId.GetPointer());
2086         }
2087
2088         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
2089         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
2090
2091         pStmt = CreateStatementN(db, query);
2092         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
2093                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
2094
2095         pEnum = ExecuteStatementN(db, pStmt);
2096         if (pEnum != null)
2097         {
2098                 if (pEnum->MoveNext() == E_SUCCESS)
2099                 {
2100                         pEnum->GetStringAt(0, packageName);
2101                         snprintf(pPackageName, bufferSize, "%ls", packageName.GetPointer());
2102                 }
2103                 delete pEnum;
2104         }
2105         else
2106         {
2107                 r = E_OBJ_NOT_FOUND;
2108         }
2109
2110 CATCH:
2111         delete pStmt;
2112
2113         return r;
2114 }
2115
2116 String
2117 _PackageManagerImpl::GetDefaultAppExecutableName(const PackageId& packageId)
2118 {
2119         result r = E_SUCCESS;
2120         Database db;
2121         DbStatement* pStmt = null;
2122         DbEnumerator* pEnum = null;
2123         String query;
2124         String executableName;
2125
2126         query.Format(
2127                 1024,
2128                 L"SELECT AppInfo.APP_NAME FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and AppInfo.APP_DEFAULT = '%s' and PkgInfo.PKG_ID = '%ls'",
2129                 "True", packageId.GetPointer());
2130
2131         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
2132         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
2133
2134         pStmt = CreateStatementN(db, query);
2135         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
2136                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
2137
2138         pEnum = ExecuteStatementN(db, pStmt);
2139         if (pEnum != null)
2140         {
2141                 if (pEnum->MoveNext() == E_SUCCESS)
2142                 {
2143                         pEnum->GetStringAt(0, executableName);
2144                 }
2145                 delete pEnum;
2146         }
2147         else
2148         {
2149                 r = E_OBJ_NOT_FOUND;
2150                 SetLastResult(r);
2151         }
2152
2153 CATCH:
2154         delete pStmt;
2155
2156         return executableName;
2157 }
2158
2159 DbStatement*
2160 _PackageManagerImpl::CreateStatementN(Database& db, const String& query)
2161 {
2162         result r = E_SUCCESS;
2163         DbStatement* pStmt = null;
2164
2165         for (int i = 0; i < MAX_DATABASE_RETRY_COUNT; i++)
2166         {
2167                 pStmt = db.CreateStatementN(query);
2168                 r = GetLastResult();
2169
2170                 if (r != E_OBJECT_LOCKED)
2171                 {
2172                         break;
2173                 }
2174                 else
2175                 {
2176                         SysLog(NID_APP, "RetryCount[%d] CreateStatementN - E_OBJECT_LOCKED", i);
2177                         delete pStmt;
2178                         pStmt = null;
2179                         usleep(50000);
2180                 }
2181         }
2182
2183         return pStmt;
2184 }
2185
2186 DbEnumerator*
2187 _PackageManagerImpl::ExecuteStatementN(Database& db, const DbStatement* pStmt)
2188 {
2189         result r = E_SUCCESS;
2190         DbEnumerator* pEnum = null;
2191
2192         for (int i = 0; i < MAX_DATABASE_RETRY_COUNT; i++)
2193         {
2194                 pEnum = db.ExecuteStatementN(*pStmt);
2195                 r = GetLastResult();
2196
2197                 if (r != E_OBJECT_LOCKED)
2198                 {
2199                         break;
2200                 }
2201                 else
2202                 {
2203                         SysLog(NID_APP, "RetryCount[%d] ExecuteStatementN - E_OBJECT_LOCKED", i);
2204                         delete pEnum;
2205                         pEnum = null;
2206                         usleep(50000);
2207                 }
2208         }
2209
2210         return pEnum;
2211 }
2212
2213 } } } // Tizen::App::Package