Merge "Fix SystemInfo cache" into tizen_2.1
[platform/framework/native/appfw.git] / src / server / app / package / FAppPkg_PackageManagerServer.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 /**
17  * @file        FAppPkg_PackageManagerServer.cpp
18  * @brief       This is the implementation for the _PackageManagerServer class.
19  */
20
21 #include <unique_ptr.h>
22
23 #include <package-manager.h>
24 #include <pkgmgr-info.h>
25 #include <package-manager-types.h>
26
27 #include <FAppPkgPackageAppInfo.h>
28 #include <FAppPkgPackageInfo.h>
29 #include <FBaseString.h>
30 #include <FBaseSysLog.h>
31 #include <FIoFile.h>
32 #include <FAppPkg_PackageManagerServer.h>
33 #include <FAppPkg_PackageAppInfoImpl.h>
34 #include <FAppPkg_PackageInfoImpl.h>
35 #include <FAppPkg_PackageParser.h>
36 #include <FBase_StringConverter.h>
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
40
41 namespace Tizen { namespace App { namespace Package
42 {
43
44 _PackageManagerServer::_PackageManagerServer(void)
45 {
46 }
47
48 _PackageManagerServer::~_PackageManagerServer(void)
49 {
50 }
51
52 _PackageManagerServer*
53 _PackageManagerServer::GetInstance(void)
54 {
55         static _PackageManagerServer* pPackageManagerServer = null;
56
57         if (pPackageManagerServer == null)
58         {
59                 pPackageManagerServer = new (std::nothrow) _PackageManagerServer();
60                 SysTryReturn(NID_APP, pPackageManagerServer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPackageManagerServer is null.");
61         }
62
63         return pPackageManagerServer;
64 }
65
66 PackageInfo*
67 _PackageManagerServer::GetPackageInfoFromFileN(const String& filePath) const
68 {
69         SysTryReturn(NID_APP, filePath.IsEmpty() == false, null, E_INVALID_ARG, "filePath is empty.");
70         SysTryReturn(NID_APP, File::IsFileExist(filePath) == true, null, E_FILE_NOT_FOUND, "package is not existed.");
71
72         String extension = File::GetFileExtension(filePath);
73         SysTryReturn(NID_APP, extension.IsEmpty() == false, null, E_INVALID_ARG, "extension is empty.");
74
75         std::unique_ptr<char[]> pPackagePath(_StringConverter::CopyToCharArrayN(filePath));
76         SysTryReturn(NID_APP, pPackagePath, null, E_OUT_OF_MEMORY, "pPackagePath is null.");
77
78         std::unique_ptr<char[]> pExtension(_StringConverter::CopyToCharArrayN(extension));
79         SysTryReturn(NID_APP, pExtension, null, E_OUT_OF_MEMORY, "pExtension is null.");
80
81         std::unique_ptr< PackageInfo > pPackageInfo(new (std::nothrow) PackageInfo);
82         SysTryReturn(NID_APP, pPackageInfo, null, E_OUT_OF_MEMORY, "pPackageInfo instance must not be null.");
83
84         SysLog(NID_APP, "packagePath = [%s], extension = [%s]", pPackagePath.get(), pExtension.get());
85
86         bool res = true;
87         result r = E_SUCCESS;
88
89         if (strcasecmp(pExtension.get(), "tpk") == 0)
90         {
91                 _PackageParser packageParser;
92
93                 res = packageParser.Construct(pPackageInfo.get());
94                 SysTryReturn(NID_APP, res, null, E_PARSING_FAILED, "Construct() is failed. [%s]", pPackagePath.get());
95
96                 res = packageParser.Parse(filePath);
97                 SysTryReturn(NID_APP, res, null, E_PARSING_FAILED, "Parse() is failed. [%s]", pPackagePath.get());
98         }
99         else if (strcasecmp(pExtension.get(), "wgt") == 0)
100         {
101                 pkgmgr_info* pPkgmgrInfo = null;
102
103                 pPkgmgrInfo = pkgmgr_client_check_pkginfo_from_file(pPackagePath.get());
104                 SysTryReturn(NID_APP, pPkgmgrInfo, null, E_PARSING_FAILED, "pkgmgr_client_check_pkginfo_from_file(%s) is failed.", pPackagePath.get());
105
106                 _package_manager_pkg_detail_info_t* pPkgInfo = (_package_manager_pkg_detail_info_t*) pPkgmgrInfo;
107                 _PackageInfoImpl* pPackageInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
108
109                 SysLog(NID_APP, "package(%s), version(%s), label(%s), description(%s), author(%s), icon_size(%d), pkgname(%s)", pPkgInfo->pkgid, pPkgInfo->version, pPkgInfo->label,
110                                 pPkgInfo->pkg_description, pPkgInfo->author, pPkgInfo->icon_size, pPkgInfo->pkg_name);
111
112                 pPackageInfoImpl->SetType(PACKAGE_TYPE_WGT);
113                 pPackageInfoImpl->SetId(pPkgInfo->pkgid);
114                 pPackageInfoImpl->SetVersion(pPkgInfo->version);
115                 pPackageInfoImpl->SetDisplayName(pPkgInfo->label);
116                 pPackageInfoImpl->SetDescription(pPkgInfo->pkg_description);
117                 pPackageInfoImpl->SetAuthor(pPkgInfo->author);
118                 pPackageInfoImpl->SetMainAppId(pPkgInfo->pkg_name);
119
120                 if (pPkgInfo->privilege_list)
121                 {
122                         GList* pList = null;
123                         pList = g_list_first(pPkgInfo->privilege_list);
124                         while (pList)
125                         {
126                                 char* pPrivilege = (char*)pList->data;
127                                 if (pPrivilege)
128                                 {
129                                         pPackageInfoImpl->AddPrivilege(*new (std::nothrow) String(pPrivilege));
130                                         free(pPrivilege);
131                                 }
132                                 pList = g_list_next(pList);
133                         }
134                         g_list_free(pPkgInfo->privilege_list);
135                 }
136
137                 std::unique_ptr< PackageAppInfo > pPackageAppInfo(new (std::nothrow) PackageAppInfo);
138                 if (pPackageAppInfo)
139                 {
140                         _PackageAppInfoImpl* pPackageAppInfoImpl = _PackageAppInfoImpl::GetInstance(pPackageAppInfo.get());
141
142                         pPackageAppInfoImpl->SetAppId(pPkgInfo->pkg_name);
143                         pPackageAppInfoImpl->SetAppName(pPkgInfo->label);
144                         pPackageAppInfoImpl->SetAppDisplayName(pPkgInfo->label);
145                         pPackageAppInfoImpl->SetMainApp(true);
146                         if ((pPkgInfo->icon_buf) && (pPkgInfo->icon_size > 0))
147                         {
148                                 String iconPath("/tmp/icon.png");
149                                 File file;
150                                 r = file.Construct(iconPath, "w+");
151                                 r = file.Write(pPkgInfo->icon_buf, pPkgInfo->icon_size);
152
153                                 pPackageAppInfoImpl->SetAppTempIconPath(iconPath);
154                         }
155
156                         pPackageInfoImpl->AddPackageAppInfo(*pPackageAppInfo.release());
157                 }
158                 else
159                 {
160                         SysLog(NID_APP, "pPackageAppInfo instance must not be null.");
161                         pkgmgr_client_free_pkginfo(pPkgmgrInfo);
162                         return null;
163                 }
164
165                 pkgmgr_client_free_pkginfo(pPkgmgrInfo);
166         }
167         else
168         {
169                 SysTryReturn(NID_APP, false, null, E_UNSUPPORTED_FORMAT, "invalid extension! - packagePath = [%s], extension = [%s]", pPackagePath.get(), pExtension.get());
170         }
171
172         return pPackageInfo.release();
173 }
174
175 long long
176 _PackageManagerServer::GetSize(const PackageId& packageId) const
177 {
178         int res = PMINFO_R_OK;
179         pkgmgrinfo_pkginfo_h handle = null;
180         int size = 0;
181
182         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
183         SysTryReturn(NID_APP, pPackageId, 0, E_OUT_OF_MEMORY, "pPackageId is null.");
184
185         res = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &handle);
186         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_PKG_NOT_INSTALLED, "pkgmgrinfo_pkginfo_get_pkginfo() is failed. result=[%d], package=[%s]", res, pPackageId.get());
187         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "[E_SYSTEM] handle is null.");
188
189         res = pkgmgrinfo_pkginfo_get_total_size(handle, &size);
190         if (res == PMINFO_R_OK)
191         {
192                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_total_size(): size = [%d]", size);
193         }
194         else
195         {
196                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_total_size() is failed. result = [%d]", res);
197         }
198
199         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
200
201         return (long long) size;
202 }
203
204 long long
205 _PackageManagerServer::GetDataSize(const PackageId& packageId) const
206 {
207         int res = PMINFO_R_OK;
208         pkgmgrinfo_pkginfo_h handle = null;
209         int dataSize = 0;
210
211         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
212         SysTryReturn(NID_APP, pPackageId, 0, E_OUT_OF_MEMORY, "pPackageId is null.");
213
214         res = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &handle);
215         SysTryReturn(NID_APP, res == PMINFO_R_OK, 0, E_PKG_NOT_INSTALLED, "pkgmgrinfo_pkginfo_get_pkginfo() is failed. result=[%d], package=[%s]", res, pPackageId.get());
216         SysTryReturn(NID_APP, handle, 0, E_SYSTEM, "[E_SYSTEM] handle is null.");
217
218         res = pkgmgrinfo_pkginfo_get_data_size(handle, &dataSize);
219         if (res == PMINFO_R_OK)
220         {
221                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_total_size(): dataSize = [%d]", dataSize);
222         }
223         else
224         {
225                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_total_size() is failed. result = [%d]", res);
226         }
227
228         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
229
230         return (long long) dataSize;
231 }
232
233 PackageType
234 _PackageManagerServer::GetType(const PackageId& packageId) const
235 {
236         int res = PMINFO_R_OK;
237         pkgmgrinfo_pkginfo_h handle = null;
238         PackageType packageType = PACKAGE_TYPE_TPK;
239         char* pType = null;
240         String type;
241
242         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
243         SysTryReturn(NID_APP, pPackageId, PACKAGE_TYPE_TPK, E_OUT_OF_MEMORY, "pPackageId is null.");
244
245         res = pkgmgrinfo_pkginfo_get_pkginfo(pPackageId.get(), &handle);
246         SysTryReturn(NID_APP, res == PMINFO_R_OK, PACKAGE_TYPE_TPK, E_PKG_NOT_INSTALLED, "pkgmgrinfo_pkginfo_get_pkginfo() is failed. result=[%d], package=[%s]", res, pPackageId.get());
247         SysTryReturn(NID_APP, handle, PACKAGE_TYPE_TPK, E_SYSTEM, "[E_SYSTEM] handle is null.");
248
249         res = pkgmgrinfo_pkginfo_get_type(handle, &pType);
250         if (res == PMINFO_R_OK)
251         {
252                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_type(): type = [%s]", pType);
253                 type = *pType;
254
255                 if (strcmp(pType, "tpk") == 0)
256                 {
257                         packageType = PACKAGE_TYPE_TPK;
258                 }
259                 else if (strcmp(pType, "wgt") == 0)
260                 {
261                         packageType = PACKAGE_TYPE_WGT;
262                 }
263         }
264         else
265         {
266                 SysLog(NID_APP, "pkgmgrinfo_pkginfo_get_type() is failed. result = [%d]", res);
267         }
268
269         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
270
271         return packageType;
272 }
273
274 } } } // Tizen::App::Package