Merge "Fixed a defect of GsmEncoding" 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
760                 if (pPkgInfo->privilege_list)
761                 {
762                         GList* pList = null;
763                         pList = g_list_first(pPkgInfo->privilege_list);
764                         while (pList)
765                         {
766                                 char* pPrivilege = (char*)pList->data;
767                                 if (pPrivilege)
768                                 {
769                                         pPackageInfoImpl->AddPrivilege(*new (std::nothrow) String(pPrivilege));
770                                         free(pPrivilege);
771                                 }
772                                 pList = g_list_next(pList);
773                         }
774                         g_list_free(pPkgInfo->privilege_list);
775                 }
776
777                 std::unique_ptr< PackageAppInfo > pPackageAppInfo(new (std::nothrow) PackageAppInfo);
778                 SysTryReturn(NID_APP, pPackageAppInfo, null, E_OUT_OF_MEMORY, "pPackageAppInfo instance must not be null.");
779
780                 _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
781
782                 pPackageAppInfoImpl->SetAppId(pPkgInfo->pkg_name);
783                 pPackageAppInfoImpl->SetAppName(pPkgInfo->label);
784                 pPackageAppInfoImpl->SetAppDisplayName(pPkgInfo->label);
785                 pPackageAppInfoImpl->SetMainApp(true);
786                 if ((pPkgInfo->icon_buf) && (pPkgInfo->icon_size > 0))
787                 {
788                         String iconPath("/tmp/icon.png");
789                         File file;
790                         file.Construct(iconPath, "w+");
791                         file.Write(pPkgInfo->icon_buf, pPkgInfo->icon_size);
792
793                         pPackageAppInfoImpl->SetAppTempIconPath(iconPath);
794                 }
795
796                 pPackageInfoImpl->AddPackageAppInfo(*pPackageAppInfo.release());
797
798                 pkgmgr_client_free_pkginfo(pPkgmgrInfo);
799         }
800         else
801         {
802                 SysTryReturn(NID_APP, false, null, E_UNSUPPORTED_FORMAT, "invalid extension! - packagePath = [%s], extension = [%s]", pPackagePath.get(), pExtension.get());
803         }
804
805         return pPackageInfo.release();
806 }
807
808 IList*
809 _PackageManagerImpl::GetPackageInfoListN(const IMap& packageFilterMap) const
810 {
811         SysTryReturn(NID_APP, packageFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageFilterMap.GetCount() is invalid.");
812
813         std::unique_ptr<IMapEnumerator> pEnum(packageFilterMap.GetMapEnumeratorN());
814         SysTryReturn(NID_APP, pEnum, null, E_INVALID_ARG, "GetMapEnumeratorN() is failed.");
815
816         int res = 0;
817         ArrayList* pList = null;
818         pkgmgrinfo_pkginfo_filter_h handle = null;
819
820         res = pkgmgrinfo_pkginfo_filter_create(&handle);
821         SysTryReturn(NID_APP, res == PMINFO_R_OK, null, E_SYSTEM, "pkgmgrinfo_pkginfo_filter_create() is failed. [%d]", res);
822
823         while(pEnum->MoveNext() == E_SUCCESS)
824         {
825                 String* pKey = static_cast< String* >(pEnum->GetKey());
826                 SysTryCatch(NID_APP, pKey, , E_INVALID_ARG, "GetKey() is failed.");
827
828                 Boolean* pVal = static_cast< Boolean* >(pEnum->GetValue());
829                 SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
830
831                 bool value = pVal->ToBool();
832
833                 SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), value);
834
835                 if ((*pKey) == PACKAGE_FILTER_UNINSTALLABLE)
836                 {
837                         res = pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE, value);
838                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(REMOVABLE, %d) is failed. [%d]", value, res);
839                 }
840                 else if ((*pKey) == PACKAGE_FILTER_DOWNLOADED)
841                 {
842                         res = pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD, !value);
843                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(PRELOAD, %d) is failed. [%d]", !value, res);
844                 }
845                 else if ((*pKey) == PACKAGE_FILTER_APP_SETTING)
846                 {
847                         res = pkgmgrinfo_pkginfo_filter_add_bool(handle, PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING, value);
848                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(APPSETTING, %d) is failed. [%d]", value, res);
849                 }
850                 else if ((*pKey) == PACKAGE_FILTER_EXTERNAL_STORAGE)
851                 {
852                         String installedStorage;
853                         if (value == true)
854                         {
855                                 installedStorage = L"installed_external";
856                         }
857                         else
858                         {
859                                 installedStorage = L"installed_internal";
860                         }
861
862                         std::unique_ptr<char[]> pInstalledStorage(_StringConverter::CopyToCharArrayN(installedStorage));
863                         SysTryCatch(NID_APP, pInstalledStorage, , E_OUT_OF_MEMORY, "pInstalledStorage is null.");
864
865                         SysLog(NID_APP, "Value[%d]->[%s]", value, pInstalledStorage.get());
866
867                         res = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE, pInstalledStorage.get());
868                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_string(STORAGE, %s) is failed. [%d]", pInstalledStorage.get(), res);
869                 }
870                 else
871                 {
872                         SysTryCatch(NID_APP, false, , E_INVALID_ARG, "Invalid key(%ls)", pKey->GetPointer());
873                 }
874         }
875
876         pList = new (std::nothrow) ArrayList();
877         SysTryCatch(NID_APP, pList, , E_OUT_OF_MEMORY, "ArrayList creation failure.");
878         pList->Construct();
879
880         res = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, PackageInfoHandler, pList);
881         if (res != 0)
882         {
883                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_filter_foreach_pkginfo() is failed. result = [%d]", res);
884
885                 pList->RemoveAll(true);
886                 pList = null;
887         }
888
889 CATCH:
890         if (handle)
891         {
892                 pkgmgrinfo_pkginfo_filter_destroy(handle);
893         }
894
895         return pList;
896 }
897
898 IList*
899 _PackageManagerImpl::GetPackageAppInfoListN(const IMap& packageAppFilterMap) const
900 {
901         SysTryReturn(NID_APP, packageAppFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageAppFilterMap.GetCount() is invalid.");
902
903         std::unique_ptr<IMapEnumerator> pEnum(packageAppFilterMap.GetMapEnumeratorN());
904         SysTryReturn(NID_APP, pEnum, null, E_INVALID_ARG, "GetMapEnumeratorN() is failed.");
905
906         int res = 0;
907         ArrayList* pList = null;
908         pkgmgrinfo_appinfo_filter_h handle = null;
909
910         res = pkgmgrinfo_appinfo_filter_create(&handle);
911         SysTryReturn(NID_APP, res == PMINFO_R_OK, null, E_SYSTEM, "pkgmgrinfo_appinfo_filter_create() is failed. [%d]", res);
912
913         while(pEnum->MoveNext() == E_SUCCESS)
914         {
915                 String* pKey = static_cast<String*>(pEnum->GetKey());
916                 SysTryCatch(NID_APP, pKey, , E_INVALID_ARG, "GetKey() is failed.");
917
918                 if ((*pKey) == PACKAGE_APP_FILTER_MENUICON_VISIBLE)
919                 {
920                         Boolean* pVal = static_cast<Boolean*>(pEnum->GetValue());
921                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
922
923                         bool nodisplay = !(pVal->ToBool());
924                         SysLog(NID_APP, "Key[%ls], Value[%d]", pKey->GetPointer(), nodisplay);
925
926                         res = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_NODISPLAY, nodisplay);
927                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_pkginfo_filter_add_bool(NODISPLAY, %d) is failed. [%d]", nodisplay, res);
928                 }
929                 else if ((*pKey) == PACKAGE_APP_FILTER_CATEGORY)
930                 {
931                         String* pVal = static_cast<String*>(pEnum->GetValue());
932                         SysTryCatch(NID_APP, pVal, , E_INVALID_ARG, "GetValue() is failed.");
933
934                         SysLog(NID_APP, "Key[%ls], Value[%ls]", pKey->GetPointer(), pVal->GetPointer());
935
936                         std::unique_ptr<char[]> pValue(_StringConverter::CopyToCharArrayN(*pVal));
937                         SysTryCatch(NID_APP, pValue, , E_OUT_OF_MEMORY, "pValue is null.");
938
939                         res = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_CATEGORY, pValue.get());
940                         SysTryCatch(NID_APP, res == PMINFO_R_OK, , E_SYSTEM, "pkgmgrinfo_appinfo_filter_add_string(CATEGORY, %s) is failed. [%d]", pValue.get(), res);
941                 }
942                 else
943                 {
944                         SysTryCatch(NID_APP, false, , E_INVALID_ARG, "Invalid key(%ls)", pKey->GetPointer());
945                 }
946         }
947
948         pList = new (std::nothrow) ArrayList();
949         SysTryCatch(NID_APP, pList, , E_OUT_OF_MEMORY, "ArrayList creation failure.");
950         pList->Construct();
951
952         res = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, PackageAppInfoHandler, pList);
953         if (res != 0)
954         {
955                 SysLog(NID_APP, "pkgmgrinfo_appinfo_filter_foreach_appinfo() is failed. result = [%d]", res);
956
957                 pList->RemoveAll(true);
958                 pList = null;
959         }
960
961 CATCH:
962         if (handle)
963         {
964                 pkgmgrinfo_appinfo_filter_destroy(handle);
965         }
966
967         return pList;
968 }
969
970 IList*
971 _PackageManagerImpl::GetPackageAppInfoListN(const IMap& packageFilterMap, const IMap& packageAppFilterMap) const
972 {
973         SysTryReturn(NID_APP, packageFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageFilterMap.GetCount() is invalid.");
974         SysTryReturn(NID_APP, packageAppFilterMap.GetCount() > 0, null, E_INVALID_ARG, "packageAppFilterMap.GetCount() is invalid.");
975
976         return null;
977 }
978
979 int
980 _PackageManagerImpl::PackageInfoHandler(const pkgmgrinfo_pkginfo_h handle, void* pUserData)
981 {
982         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "handle must not be null.");
983         SysTryReturn(NID_APP, pUserData, 0, E_SYSTEM, "pUserData must not be null.");
984
985         result r = E_SUCCESS;
986         int res = PMINFO_R_OK;
987         char* pPackageId = null;
988         ArrayList* pList = (ArrayList*)pUserData;
989
990         res = pkgmgrinfo_pkginfo_get_pkgname(handle, &pPackageId);
991         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_SYSTEM, "pkgmgrinfo_pkginfo_get_pkgname() is failed. [%d]", res);
992
993         std::unique_ptr<PackageInfo> pPackageInfo(new (std::nothrow) PackageInfo);
994         SysTryReturn(NID_APP, pPackageInfo, 0, E_OUT_OF_MEMORY, "pPackageInfo instance must not be null.");
995
996         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
997         r = pPackageInfoImpl->Construct(pPackageId);
998         SysTryReturn(NID_APP, r == E_SUCCESS, 0, E_SYSTEM, "Construct(%s) is failed.", pPackageId);
999
1000         pList->Add(*pPackageInfo.release());
1001
1002         return 0;
1003 }
1004
1005 int
1006 _PackageManagerImpl::PackageAppInfoHandler(const pkgmgrinfo_appinfo_h handle, void* pUserData)
1007 {
1008         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "handle must not be null.");
1009         SysTryReturn(NID_APP, pUserData, 0, E_SYSTEM, "pUserData must not be null.");
1010
1011         result r = E_SUCCESS;
1012         int res = PMINFO_R_OK;
1013         char* pAppId = null;
1014         ArrayList* pList = (ArrayList*)pUserData;
1015
1016         res = pkgmgrinfo_appinfo_get_appid(handle, &pAppId);
1017         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_SYSTEM, "pkgmgrinfo_appinfo_get_appid is failed. [%d]", res);
1018
1019         std::unique_ptr<PackageAppInfo> pPackageAppInfo(new (std::nothrow) PackageAppInfo);
1020         SysTryReturn(NID_APP, pPackageAppInfo, 0, E_OUT_OF_MEMORY, "pPackageAppInfo instance must not be null.");
1021
1022         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
1023         r = pPackageAppInfoImpl->Construct(pAppId);
1024         SysTryReturn(NID_APP, r == E_SUCCESS, 0, E_SYSTEM, "Construct(%s) is failed.", pAppId);
1025
1026         pList->Add(*pPackageAppInfo.release());
1027
1028         return 0;
1029 }
1030
1031 _PackageManagerImpl*
1032 _PackageManagerImpl::GetInstance(void)
1033 {
1034         return PackageManager::GetInstance()->__pPackageManagerImpl;
1035 }
1036
1037 result
1038 _PackageManagerImpl::Construct(void)
1039 {
1040         ClearLastResult();
1041
1042         result r = __installationList.Construct();
1043         SysTryReturnResult(NID_APP, r == E_SUCCESS, r, "The memory is insufficient.");
1044
1045         return E_SUCCESS;
1046 }
1047
1048 _PackageManagerImpl::_PackageManagerImpl(void)
1049 :__pRequestClient(null),
1050 __pListeningClient(null)
1051 {
1052 }
1053
1054 _PackageManagerImpl::~_PackageManagerImpl(void)
1055 {
1056 }
1057
1058 void
1059 _PackageManagerImpl::SendPackageEvent(PackageType type, const PackageId& packageId, const char* pEventKey, const char* pEventValue)
1060 {
1061         result r = E_SUCCESS;
1062         bool install = true;
1063
1064         if (strcmp(pEventKey, "start") == 0)
1065         {
1066                 if ((strcmp(pEventValue, "install") == 0) || (strcmp(pEventValue, "uninstall") == 0))
1067                 {
1068                         String* pAppId = new (std::nothrow) String(packageId);
1069                         SysTryReturnVoidResult(NID_APP, pAppId != null, E_OUT_OF_MEMORY, "pAppId instance must not be null.");
1070
1071                         String* pOperation = new (std::nothrow) String(pEventValue);
1072                         SysTryReturnVoidResult(NID_APP, pOperation != null, E_OUT_OF_MEMORY, "pOperation instance must not be null.");
1073
1074                         r = __installationList.Add(*pAppId, *pOperation);
1075                         if (IsFailed(r))
1076                         {
1077                                 delete pAppId;
1078                                 delete pOperation;
1079                                 SysLog(NID_APP, "Failed to add installation condition.");
1080                                 SetLastResult(E_SYSTEM);
1081                                 return;
1082                         }
1083                 }
1084         }
1085         else if (strcmp(pEventKey, "end") == 0)
1086         {
1087                 String* pOperation = static_cast <String*>(__installationList.GetValue(packageId));
1088                 if (pOperation)
1089                 {
1090                         if (pOperation->Equals("uninstall", true) == true)
1091                         {
1092                                 install = false;
1093                         }
1094
1095                         r = __installationList.Remove(packageId, true);
1096                         if (IsFailed(r))
1097                         {
1098                                 SysLogException(NID_APP, r, "[%s] Remove Element [%ls] is failed from installing list.", GetErrorMessage(r), packageId.GetPointer());
1099                         }
1100                 }
1101         }
1102
1103         std::unique_ptr< IEnumeratorT<_PackageManagerEvent*> > pEnum(__packageEventListenerList.GetEnumeratorN());
1104         SysTryReturnVoidResult(NID_APP, pEnum, E_OUT_OF_MEMORY, "The memory is insufficient.");
1105
1106         while (pEnum->MoveNext() == E_SUCCESS)
1107         {
1108                 _PackageManagerEvent*   pEvent = null;
1109                 pEnum->GetCurrent(pEvent);
1110                 if (pEvent)
1111                 {
1112                         _PackageManagerEventArg* pEventArg= new (std::nothrow) _PackageManagerEventArg();
1113                         SysTryReturnVoidResult(NID_APP, pEventArg, E_OUT_OF_MEMORY, "The memory is insufficient.");
1114
1115                         String eventKey(pEventKey);
1116                         String eventValue(pEventValue);
1117
1118                         pEventArg->__packageId = packageId;
1119                         pEventArg->__eventKey = eventKey;
1120                         pEventArg->__eventValue = eventValue;
1121                         pEventArg->__install = install;
1122
1123                         r = pEvent->Fire(*pEventArg);
1124                         if (r != E_SUCCESS)
1125                         {
1126                                 SysLog(NID_APP, "pEvent->Fire(*pEventArg) failed. [%s]", GetErrorMessage(r));
1127                         }
1128
1129                         SysLog(NID_APP, "PackageId = [%ls], Key = [%ls], Value = [%ls], install = [%d]", pEventArg->__packageId.GetPointer(), pEventArg->__eventKey.GetPointer(), pEventArg->__eventValue.GetPointer(), pEventArg->__install);
1130                 }
1131                 else
1132                 {
1133                         SysLog(NID_APP, "pEvent is null.");
1134                 }
1135         }
1136 }
1137
1138 PackageInfo*
1139 _PackageManagerImpl::GetPackageInfoN(PackageType packageType, const String& packageName) const
1140 {
1141         SysTryReturn(NID_APP, packageName.IsEmpty() == false, null, E_INVALID_ARG, "[E_INVALID_ARG] packageName is empty.");
1142
1143         result r = E_SUCCESS;
1144         Database db;
1145         DbStatement* pStmt = null;
1146         DbEnumerator* pEnum = null;
1147         String query;
1148         PackageInfo *pPackageInfo = null;
1149
1150         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());
1151
1152         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1153         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1154
1155         pStmt = CreateStatementN(db, query);
1156
1157         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1158
1159         pEnum = ExecuteStatementN(db, pStmt);
1160         if (pEnum != null)
1161         {
1162                 if (pEnum->MoveNext() == E_SUCCESS)
1163                 {
1164                         pPackageInfo = new (std::nothrow) PackageInfo;
1165                         SysTryReturn(NID_APP, pPackageInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageInfo instance must not be null.");
1166
1167                         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo);
1168
1169                         String id;
1170                         String appVersion;
1171                         String appType;
1172                         String appMimeType;
1173                         String appApiVersion;
1174                         String appName;
1175                         String appVendor;
1176                         String appDescription;
1177                         String appUrl;
1178                         String appCid;
1179                         String appRootPath;
1180                         int appStorageType = 0;
1181                         DateTime appInstallationTime;
1182                         String appIconPath;
1183
1184                         pEnum->GetStringAt(1, id);
1185                         pEnum->GetStringAt(3, appVersion);
1186                         pEnum->GetStringAt(4, appType);
1187                         pEnum->GetStringAt(5, appMimeType);
1188                         pEnum->GetStringAt(6, appApiVersion);
1189                         pEnum->GetStringAt(7, appName);
1190                         pEnum->GetStringAt(8, appVendor);
1191                         pEnum->GetStringAt(9, appDescription);
1192                         pEnum->GetStringAt(10, appUrl);
1193                         pEnum->GetStringAt(11, appCid);
1194                         pEnum->GetStringAt(16, appRootPath);
1195                         pEnum->GetIntAt(17, appStorageType);
1196                         pEnum->GetDateTimeAt(18, appInstallationTime);
1197                         pEnum->GetStringAt(21, appIconPath);
1198
1199                         pPackageInfoImpl->SetAppId(id);
1200                         pPackageInfoImpl->SetAppVersion(appVersion);
1201                         pPackageInfoImpl->SetAppMimeType(appMimeType);
1202                         pPackageInfoImpl->SetAppApiVersion(appApiVersion);
1203                         pPackageInfoImpl->SetAppName(appName);
1204                         pPackageInfoImpl->SetAppVendor(appVendor);
1205                         pPackageInfoImpl->SetAppDescription(appDescription);
1206                         pPackageInfoImpl->SetAppUrl(appUrl);
1207                         pPackageInfoImpl->SetAppCid(appCid);
1208                         pPackageInfoImpl->SetAppRootPath(appRootPath);
1209                         pPackageInfoImpl->SetAppStorageType(appStorageType);
1210                         pPackageInfoImpl->SetAppInstallationTime(appInstallationTime);
1211                         pPackageInfoImpl->SetAppIconPath(appIconPath);
1212                 }
1213
1214                 delete pEnum;
1215         }
1216         else
1217         {
1218                 r = E_OBJ_NOT_FOUND;
1219         }
1220
1221 CATCH:
1222         delete pStmt;
1223         return pPackageInfo;
1224 }
1225
1226 int
1227 _PackageManagerImpl::PackageInfoEventHandler(const pkgmgrinfo_pkginfo_h handle, void* pUserData)
1228 {
1229         SysTryReturn(NID_APP, handle != null, 0, E_SYSTEM, "[E_SYSTEM] handle must not be null.");
1230
1231         int result = 0;
1232         char* pPackage = null;
1233         ArrayList* pList = (ArrayList*)pUserData;
1234
1235         PackageInfo *pPackageInfo = new (std::nothrow) PackageInfo;
1236         SysTryReturn(NID_APP, pPackageInfo != null, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageInfo instance must not be null.");
1237
1238         _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo);
1239
1240         result = pkgmgrinfo_pkginfo_get_pkgname(handle, &pPackage);
1241         if (result == 0)
1242         {
1243                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_pkgname(): package = [%s]", pPackage);
1244         }
1245         else
1246         {
1247                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_pkgname() is failed. result = [%d]", result);
1248         }
1249
1250         pPackageInfoImpl->Construct(pPackage);
1251         pList->Add(*pPackageInfo);
1252
1253         return result;
1254 }
1255
1256 IList*
1257 _PackageManagerImpl::GetFilteredAppIdListN(const String& feature, const String& value) const
1258 {
1259         result r = E_SUCCESS;
1260         Database db;
1261         DbStatement* pStmt = null;
1262         DbEnumerator* pEnum = null;
1263         String query;
1264         ArrayList* pList = null;
1265
1266         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());
1267
1268         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1269         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1270
1271         pStmt = CreateStatementN(db, query);
1272         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1273
1274         pEnum = ExecuteStatementN(db, pStmt);
1275
1276         if (pEnum != null)
1277         {
1278                 pList = new (std::nothrow) ArrayList();
1279                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
1280                 pList->Construct();
1281
1282                 while (pEnum->MoveNext() == E_SUCCESS)
1283                 {
1284                         String* pPackageName = new (std::nothrow) String;
1285                         SysTryReturn(NID_APP, pPackageName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageName instance must not be null.");
1286
1287                         pEnum->GetStringAt(0, *pPackageName);
1288
1289                         pList->Add(*pPackageName);
1290                 }
1291
1292                 delete pEnum;
1293
1294         }
1295         else
1296         {
1297                 r = E_OBJ_NOT_FOUND;
1298         }
1299
1300 CATCH:
1301         delete pStmt;
1302         return pList;
1303 }
1304
1305 IList*
1306 _PackageManagerImpl::GetDataControlInfoN(const String& providerId, const String& type) const
1307 {
1308         result r = E_SUCCESS;
1309         Database db;
1310         DbStatement* pStmt = null;
1311         DbEnumerator* pEnum = null;
1312         String query;
1313         ArrayList* pList = null;
1314
1315         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());
1316
1317         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1318         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1319
1320         pStmt = CreateStatementN(db, query);
1321         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1322
1323         pEnum = ExecuteStatementN(db, pStmt);
1324
1325         if (pEnum != null)
1326         {
1327                 pList = new (std::nothrow) ArrayList();
1328                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
1329                 pList->Construct();
1330
1331                 while (pEnum->MoveNext() == E_SUCCESS)
1332                 {
1333                         String* pPackageName = new (std::nothrow) String;
1334                         SysTryReturn(NID_APP, pPackageName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageName instance must not be null.");
1335
1336                         String* pAccess = new (std::nothrow) String;
1337                         SysTryReturn(NID_APP, pAccess != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pAccess instance must not be null.");
1338
1339                         pEnum->GetStringAt(0, *pPackageName);
1340                         pList->Add(*pPackageName);
1341
1342                         pEnum->GetStringAt(1, *pAccess);
1343                         pList->Add(*pAccess);
1344                 }
1345
1346                 delete pEnum;
1347
1348         }
1349         else
1350         {
1351                 r = E_OBJ_NOT_FOUND;
1352         }
1353
1354 CATCH:
1355         delete pStmt;
1356         return pList;
1357 }
1358
1359 IMap*
1360 _PackageManagerImpl::GetUiThemeListN(const PackageId& packageId) const
1361 {
1362         result r = E_SUCCESS;
1363         Database db;
1364         DbStatement* pStmt = null;
1365         DbEnumerator* pEnum = null;
1366         String query;
1367         HashMap* pList = null;
1368
1369         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')"
1370                         , packageId.GetPointer());
1371
1372         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1373         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1374
1375         pStmt = CreateStatementN(db, query);
1376         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1377
1378         pEnum = ExecuteStatementN(db, pStmt);
1379
1380         if (pEnum != null)
1381         {
1382                 pList = new (std::nothrow) HashMap();
1383                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "HashMap creation failure.");
1384                 pList->Construct();
1385
1386                 while (pEnum->MoveNext() == E_SUCCESS)
1387                 {
1388                         String* pName = new (std::nothrow) String;
1389                         SysTryCatch(NID_APP, pName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pName instance must not be null.");
1390
1391                         String* pValue = new (std::nothrow) String;
1392                         SysTryCatch(NID_APP, pValue != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pValue instance must not be null.");
1393
1394                         pEnum->GetStringAt(0, *pName);
1395                         pEnum->GetStringAt(1, *pValue);
1396
1397                         pList->Add(*pName, *pValue);
1398                 }
1399
1400                 delete pEnum;
1401
1402         }
1403         else
1404         {
1405                 r = E_OBJ_NOT_FOUND;
1406         }
1407
1408 CATCH:
1409         delete pStmt;
1410         return pList;
1411 }
1412
1413 bool
1414 _PackageManagerImpl::IsAppInstalled(const AppId& appId)
1415 {
1416         if (appId == _AppInfo::GetPackageId())
1417         {
1418                 return true;
1419         }
1420
1421         int result = 0;
1422         pkgmgrinfo_pkginfo_h pPackageInfoHandle = null;
1423
1424         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(appId));
1425         SysTryReturn(NID_APP, pPackageId, false, E_OUT_OF_MEMORY, "pPackageId is null");
1426
1427         result = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &pPackageInfoHandle);
1428         SysTryReturn(NID_APP, result == 0, false, E_APP_NOT_INSTALLED, "pkgmgrinfo_pkginfo_get_pkginfo() failed. result=[%d], packageId=[%s]", result, pPackageId.get());
1429
1430         if(pPackageInfoHandle)
1431         {
1432                 pkgmgrinfo_pkginfo_destroy_pkginfo(pPackageInfoHandle);
1433         }
1434
1435         return true;
1436 }
1437
1438 int
1439 _PackageManagerImpl::PackageEventHandler(int req_id, const char* pkg_type, const char* pkg_name,
1440                                                                                 const char* key, const char* val, const void* pmsg, void* data)
1441 {
1442         SysLog(NID_APP, "PackageEventHandler - req_id: %d, pkg_type: %s, pkg_name: %s, key: %s, val: %s", req_id, pkg_type,
1443                                 pkg_name, key, val);
1444
1445         PackageType type = PACKAGE_TYPE_TPK;
1446         PackageId packageId(pkg_name);
1447
1448         if (strcmp(pkg_type, "tpk") == 0)
1449         {
1450                 type = PACKAGE_TYPE_TPK;
1451         }
1452         else if (strcmp(pkg_type, "wgt") == 0)
1453         {
1454                 type = PACKAGE_TYPE_WGT;
1455         }
1456         else
1457         {
1458                 SysLog(NID_APP, "Invalid type - pkg_type: %s", pkg_type);
1459                 return 0;
1460         }
1461
1462         _PackageManagerImpl* pThis = _PackageManagerImpl::GetInstance();
1463         pThis->SendPackageEvent(type, packageId, key, val);
1464
1465         return 0;
1466 }
1467
1468 String*
1469 _PackageManagerImpl::GetAppIdOfDataControlN(const String& providerId)
1470 {
1471         result r = E_SUCCESS;
1472         Database db;
1473         DbStatement* pStmt = null;
1474         DbEnumerator* pEnum = null;
1475         String query;
1476         String* pAppId = null;
1477
1478         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",
1479                         providerId.GetPointer());
1480
1481         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1482         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1483
1484         pStmt = CreateStatementN(db, query);
1485         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1486
1487         pEnum = ExecuteStatementN(db, pStmt);
1488
1489         if (pEnum != null)
1490         {
1491                 if (pEnum->MoveNext() == E_SUCCESS)
1492                 {
1493                         pAppId = new (std::nothrow) String;
1494                         SysTryReturn(NID_APP, pAppId != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory");
1495
1496                         pEnum->GetStringAt(0, *pAppId);
1497                 }
1498
1499                 delete pEnum;
1500         }
1501
1502 CATCH:
1503         delete pStmt;
1504         return pAppId;
1505 }
1506
1507 PackageInfo*
1508 _PackageManagerImpl::GetPackageInfoN(const String& providerId, const String& operationId) const
1509 {
1510         result r = E_SUCCESS;
1511         PackageInfo* pPkgInfo = null;
1512         Database db;
1513         DbStatement* pStmt = null;
1514         DbEnumerator* pEnum = null;
1515         String query;
1516
1517         query.Format(1024, L"SELECT PkgInfo.PKG_ID FROM PkgInfo, AppInfo, AppInfoLookup, AppControl, Capability "
1518                         "WHERE AppControl.PROVIDER_ID ='%ls' and Capability.OPERATION_ID ='%ls' and AppControl.ID = AppInfoLookup.AppControlID "
1519                         "and Capability.ID = AppInfoLookup.CapabilityID and AppInfoLookup.AppInfoID = AppInfo.UNIQUE_ID and AppInfo.ID = PkgInfo.UNIQUE_ID "
1520                         "GROUP BY AppInfoID"
1521                         , providerId.GetPointer(), operationId.GetPointer());
1522
1523         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1524         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1525
1526         pStmt = CreateStatementN(db, query);
1527         SysTryCatch(NID_APP, pStmt != null, GetLastResult(), GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1528
1529         pEnum = ExecuteStatementN(db, pStmt);
1530         if (pEnum != null)
1531         {
1532                 if (pEnum->MoveNext() == E_SUCCESS)
1533                 {
1534                         PackageId packageId;
1535                         pEnum->GetStringAt(0, packageId);
1536
1537                         pPkgInfo = GetPackageInfoN(packageId);
1538                 }
1539
1540                 delete pEnum;
1541         }
1542         else
1543         {
1544                 r = E_OBJ_NOT_FOUND;
1545         }
1546
1547 CATCH:
1548         delete pStmt;
1549         return pPkgInfo;
1550 }
1551
1552 ArrayList*
1553 _PackageManagerImpl::GetPackageAppInfoImplListN(const String& packageId) const
1554 {
1555         result r = E_SUCCESS;
1556         Database db;
1557         DbStatement* pStmt = null;
1558         DbEnumerator* pEnum = null;
1559         String query;
1560         //int id = 0;
1561         PackageInfo* pPkgInfo = null;
1562         _PackageInfoImpl* pPackageInfoImpl = null;
1563         ArrayList* pList = null;
1564
1565         pPkgInfo = GetPackageInfoN(packageId);
1566         SysTryCatch(NID_APP, pPkgInfo != null, , r, "[%s] GetPackageInfoN() is failed", GetErrorMessage(r));
1567
1568         pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo);
1569         query.Format(1024, L"SELECT * FROM AppInfo WHERE ID = %d", pPackageInfoImpl->GetUniqueId());
1570
1571         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1572         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1573
1574         pStmt = CreateStatementN(db, query);
1575         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
1576                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1577
1578         pEnum = ExecuteStatementN(db, pStmt);
1579
1580         if (pEnum != null)
1581         {
1582                 pList = new (std::nothrow) ArrayList;
1583                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Container allocation failure.");
1584
1585                 pList->Construct();
1586
1587                 while (pEnum->MoveNext() == E_SUCCESS)
1588                 {
1589                         _PackageAppInfoImpl* pPackageAppInfoImpl = new (std::nothrow) _PackageAppInfoImpl;
1590                         SysTryReturn(NID_APP, pPackageAppInfoImpl != null, null, E_OUT_OF_MEMORY, "pPackageAppInfoImpl instance must not be null.");
1591
1592                         int uniqueId = 0;
1593                         int pkgId = 0;
1594                         String name;
1595                         String defaultapp;
1596                         String mainmenuIcon;
1597                         String settingIcon;
1598                         String quickpanelIcon;
1599                         int appFeature = 0;
1600                         String packageName;
1601
1602                         pEnum->GetIntAt(0, uniqueId);
1603                         pEnum->GetIntAt(1, pkgId);
1604                         pEnum->GetStringAt(2, name);
1605                         pEnum->GetStringAt(3, defaultapp);
1606                         pEnum->GetStringAt(4, mainmenuIcon);
1607                         pEnum->GetStringAt(5, settingIcon);
1608                         pEnum->GetStringAt(7, quickpanelIcon);
1609                         pEnum->GetIntAt(9, appFeature);
1610                         pEnum->GetStringAt(10, packageName);
1611
1612                         pPackageAppInfoImpl->SetUniqueId(uniqueId);
1613                         pPackageAppInfoImpl->SetPkgId(pkgId);
1614                         pPackageAppInfoImpl->SetName(name);
1615                         pPackageAppInfoImpl->SetDefault(defaultapp);
1616                         pPackageAppInfoImpl->SetMainmenuIcon(mainmenuIcon);
1617                         pPackageAppInfoImpl->SetSettingIcon(settingIcon);
1618                         pPackageAppInfoImpl->SetQuickpanelIcon(quickpanelIcon);
1619                         pPackageAppInfoImpl->SetAppFeature(appFeature);
1620                         pPackageAppInfoImpl->SetPackageName(packageName);
1621
1622                         pList->Add(*pPackageAppInfoImpl);
1623                 }
1624
1625                 delete pEnum;
1626         }
1627
1628 CATCH:
1629         delete pPkgInfo;
1630         delete pStmt;
1631         return pList;
1632 }
1633
1634 ArrayList*
1635 _PackageManagerImpl::GetPackageAppFeatureListN(const PackageId& packageId, const Tizen::Base::String& appExecutableName) const
1636 {
1637         ArrayList* pAppInfoList = GetPackageAppInfoImplListN(packageId);
1638         SysTryReturn(NID_APP, pAppInfoList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppInfo list instance must not be null.");
1639
1640         ArrayList* pResult = null;
1641
1642         for (int i = 0; i < pAppInfoList->GetCount(); i++)
1643         {
1644                 const _PackageAppInfoImpl* pAppInfoImpl = static_cast<const _PackageAppInfoImpl*>(pAppInfoList->GetAt(i));
1645                 if (pAppInfoImpl == null)
1646                 {
1647                         continue;
1648                 }
1649
1650                 if (pAppInfoImpl->GetName() == appExecutableName)
1651                 {
1652                         pResult = pAppInfoImpl->GetAppFeatureListN();
1653                         goto OUT;
1654                 }
1655         }
1656
1657 OUT:
1658         pAppInfoList->RemoveAll(true);
1659         delete pAppInfoList;
1660
1661         return pResult;
1662 }
1663
1664 HashMapT<String, _AppFeatureInfoImpl*>*
1665 _PackageManagerImpl::GetPackageAppFeatureMapN(const PackageId& packageId, const Tizen::Base::String& appExecutableName) const
1666 {
1667         ArrayList* pAppInfoList = GetPackageAppInfoImplListN(packageId);
1668         SysTryReturn(NID_APP, pAppInfoList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppInfo list instance must not be null.");
1669
1670         HashMapT<String, _AppFeatureInfoImpl*>* pResult = null;
1671
1672         std::auto_ptr<IEnumerator> pEnum(pAppInfoList->GetEnumeratorN());
1673
1674         while (pEnum->MoveNext() == E_SUCCESS)
1675         {
1676                 const _PackageAppInfoImpl* pAppInfoImpl = static_cast<const _PackageAppInfoImpl*>(pEnum->GetCurrent());
1677                 if (pAppInfoImpl == null)
1678                 {
1679                         continue;
1680                 }
1681
1682                 if (pAppInfoImpl->GetName() == appExecutableName)
1683                 {
1684                         pResult = pAppInfoImpl->GetAppFeatureMapN();
1685                         goto OUT;
1686                 }
1687         }
1688
1689 OUT:
1690         pAppInfoList->RemoveAll(true);
1691         delete pAppInfoList;
1692
1693         return pResult;
1694 }
1695
1696 ArrayList*
1697 _PackageManagerImpl::GetAppLaunchConditionListN(const String& packageName) const
1698 {
1699         result r = E_SUCCESS;
1700         Database db;
1701         DbStatement* pStmt = null;
1702         DbEnumerator* pEnum = null;
1703         String query;
1704         ArrayList* pList = null;
1705
1706         query.Format(1024, L"SELECT LaunchCondition.* FROM LaunchCondition, AppInfo WHERE AppInfo.UNIQUE_ID = LaunchCondition.ID and AppInfo.PACKAGE_NAME = '%ls'", packageName.GetPointer());
1707
1708         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1709         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1710
1711         pStmt = CreateStatementN(db, query);
1712         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
1713                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1714
1715         pEnum = ExecuteStatementN(db, pStmt);
1716         if (pEnum != null)
1717         {
1718                 pList = new (std::nothrow) ArrayList;
1719                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Container allocation failure.");
1720
1721                 pList->Construct();
1722
1723                 while (pEnum->MoveNext() == E_SUCCESS)
1724                 {
1725                         _LaunchConditionInfoImpl* pLaunchCondtion = new (std::nothrow) _LaunchConditionInfoImpl;
1726                         SysTryReturn(NID_APP, pLaunchCondtion != null, null, E_OUT_OF_MEMORY, "pLaunchCondtion instance must not be null.");
1727
1728                         String name;
1729                         String value;
1730
1731                         pEnum->GetStringAt(1, name);
1732                         pEnum->GetStringAt(2, value);
1733
1734                         pLaunchCondtion->SetName(name);
1735                         pLaunchCondtion->SetValue(value);
1736
1737                         pList->Add(*pLaunchCondtion);
1738
1739                         SysLog(NID_APP, "Name[%ls], Value[%ls]", name.GetPointer(), value.GetPointer());
1740                 }
1741
1742                 delete pEnum;
1743         }
1744
1745 CATCH:
1746         delete pStmt;
1747         return pList;
1748 }
1749
1750 result
1751 _PackageManagerImpl::GetPackageName(const PackageId& packageId, const String* pName, char* pPackageName, int bufferSize)
1752 {
1753         result r = E_SUCCESS;
1754         Database db;
1755         DbStatement* pStmt = null;
1756         DbEnumerator* pEnum = null;
1757         String query;
1758         Tizen::Base::String packageName;
1759
1760         if (pName == null)
1761         {
1762                 query.Format(
1763                         1024,
1764                         L"SELECT AppInfo.PACKAGE_NAME FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and AppInfo.APP_DEFAULT = '%s' and PkgInfo.PKG_ID = '%ls'",
1765                         "True", packageId.GetPointer());
1766         }
1767         else
1768         {
1769                 query.Format(
1770                         1024,
1771                         L"SELECT AppInfo.PACKAGE_NAME FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and APP_NAME = '%ls' and PkgInfo.PKG_ID = '%ls'",
1772                         pName->GetPointer(), packageId.GetPointer());
1773         }
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         if (pEnum != null)
1784         {
1785                 if (pEnum->MoveNext() == E_SUCCESS)
1786                 {
1787                         pEnum->GetStringAt(0, packageName);
1788                         snprintf(pPackageName, bufferSize, "%ls", packageName.GetPointer());
1789                 }
1790                 delete pEnum;
1791         }
1792         else
1793         {
1794                 r = E_OBJ_NOT_FOUND;
1795         }
1796
1797 CATCH:
1798         delete pStmt;
1799
1800         return r;
1801 }
1802
1803 String
1804 _PackageManagerImpl::GetDefaultAppExecutableName(const PackageId& packageId)
1805 {
1806         result r = E_SUCCESS;
1807         Database db;
1808         DbStatement* pStmt = null;
1809         DbEnumerator* pEnum = null;
1810         String query;
1811         String executableName;
1812
1813         query.Format(
1814                 1024,
1815                 L"SELECT AppInfo.APP_NAME FROM AppInfo, PkgInfo WHERE AppInfo.ID = PkgInfo.UNIQUE_ID and AppInfo.APP_DEFAULT = '%s' and PkgInfo.PKG_ID = '%ls'",
1816                 "True", packageId.GetPointer());
1817
1818         r = db.Construct(PACKAGE_DATABASE_FILE_NAME, "r");
1819         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] An error occurs while opening a database.", GetErrorMessage(r));
1820
1821         pStmt = CreateStatementN(db, query);
1822         SysTryCatch(NID_APP, pStmt != null, GetLastResult(),
1823                            GetLastResult(), "[%s] An error occurs while creating a database statement.", GetErrorMessage(GetLastResult()));
1824
1825         pEnum = ExecuteStatementN(db, pStmt);
1826         if (pEnum != null)
1827         {
1828                 if (pEnum->MoveNext() == E_SUCCESS)
1829                 {
1830                         pEnum->GetStringAt(0, executableName);
1831                 }
1832                 delete pEnum;
1833         }
1834         else
1835         {
1836                 r = E_OBJ_NOT_FOUND;
1837                 SetLastResult(r);
1838         }
1839
1840 CATCH:
1841         delete pStmt;
1842
1843         return executableName;
1844 }
1845
1846 DbStatement*
1847 _PackageManagerImpl::CreateStatementN(Database& db, const String& query)
1848 {
1849         result r = E_SUCCESS;
1850         DbStatement* pStmt = null;
1851
1852         for (int i = 0; i < MAX_DATABASE_RETRY_COUNT; i++)
1853         {
1854                 pStmt = db.CreateStatementN(query);
1855                 r = GetLastResult();
1856
1857                 if (r != E_OBJECT_LOCKED)
1858                 {
1859                         break;
1860                 }
1861                 else
1862                 {
1863                         SysLog(NID_APP, "RetryCount[%d] CreateStatementN - E_OBJECT_LOCKED", i);
1864                         delete pStmt;
1865                         pStmt = null;
1866                         usleep(50000);
1867                 }
1868         }
1869
1870         return pStmt;
1871 }
1872
1873 DbEnumerator*
1874 _PackageManagerImpl::ExecuteStatementN(Database& db, const DbStatement* pStmt)
1875 {
1876         result r = E_SUCCESS;
1877         DbEnumerator* pEnum = null;
1878
1879         for (int i = 0; i < MAX_DATABASE_RETRY_COUNT; i++)
1880         {
1881                 pEnum = db.ExecuteStatementN(*pStmt);
1882                 r = GetLastResult();
1883
1884                 if (r != E_OBJECT_LOCKED)
1885                 {
1886                         break;
1887                 }
1888                 else
1889                 {
1890                         SysLog(NID_APP, "RetryCount[%d] ExecuteStatementN - E_OBJECT_LOCKED", i);
1891                         delete pEnum;
1892                         pEnum = null;
1893                         usleep(50000);
1894                 }
1895         }
1896
1897         return pEnum;
1898 }
1899
1900 } } } // Tizen::App::Package