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