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