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