Release 9.0.0.16887
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.PackageManager / Tizen.Applications / PackageArchive.cs
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20
21 namespace Tizen.Applications
22 {
23     /// <summary>
24     /// This class provides the methods and properties to get information about the package archive.
25     /// </summary>
26     /// <since_tizen> 6 </since_tizen>
27     public class PackageArchive
28     {
29         private const string LogTag = "Tizen.Applications";
30
31         private string _path = string.Empty;
32         private string _id = string.Empty;
33         private string _type = string.Empty;
34         private string _version = string.Empty;
35         private string _apiVersion = string.Empty;
36         private string _description = string.Empty;
37         private string _label = string.Empty;
38         private string _author = string.Empty;
39         private IntPtr _icon = IntPtr.Zero;
40         private int _iconSize = 0;
41         private List<PackageDependencyInformation> _dependency_to;
42
43         private PackageArchive(string archivePath)
44         {
45             _path = archivePath;
46         }
47
48         /// <summary>
49         /// The package ID.
50         /// </summary>
51         /// <since_tizen> 6 </since_tizen>
52         public string Id { get { return _id; } }
53
54         /// <summary>
55         /// Type of the package.
56         /// </summary>
57         /// <since_tizen> 6 </since_tizen>
58         public string Type { get { return _type; } }
59
60         /// <summary>
61         /// Version of the package.
62         /// </summary>
63         /// <since_tizen> 6 </since_tizen>
64         public string Version { get { return _version; } }
65
66         /// <summary>
67         /// Api version of the package.
68         /// </summary>
69         /// <since_tizen> 6 </since_tizen>
70         public string ApiVersion { get { return _apiVersion; } }
71
72         /// <summary>
73         /// Description of the package.
74         /// </summary>
75         /// <since_tizen> 6 </since_tizen>
76         public string Description { get { return _description; } }
77
78         /// <summary>
79         /// Label of the package.
80         /// </summary>
81         /// <since_tizen> 6 </since_tizen>
82         public string Label { get { return _label; } }
83
84         /// <summary>
85         /// Author of the package.
86         /// </summary>
87         /// <since_tizen> 6 </since_tizen>
88         public string Author { get { return _author; } }
89
90         /// <summary>
91         /// Icon of the package.
92         /// </summary>
93         /// <since_tizen> 6 </since_tizen>
94         public byte[] Icon
95         {
96             get
97             {
98                 byte[] byteArray = new byte[_iconSize];
99                 Marshal.Copy(_icon, byteArray, 0, _iconSize);
100                 return byteArray;
101             }
102         }
103
104         /// <summary>
105         /// Packages that this package is required.
106         /// </summary>
107         /// <since_tizen> 6 </since_tizen>
108         public List<PackageDependencyInformation> DependencyTo { get { return _dependency_to; } }
109
110         // This method assumes that given arguments are already validated and have valid values.
111         internal static PackageArchive CreatePackageArchive(IntPtr handle, string archivePath)
112         {
113             PackageArchive packageArchive = new PackageArchive(archivePath);
114
115             var err = Interop.PackageManager.ErrorCode.None;
116             err = Interop.PackageArchive.PackageArchiveInfoGetPackage(handle, out packageArchive._id);
117             if (err != Interop.PackageManager.ErrorCode.None)
118             {
119                 Log.Warn(LogTag, "Failed to get package id from " + archivePath);
120             }
121             err = Interop.PackageArchive.PackageArchiveInfoGetType(handle, out packageArchive._type);
122             if (err != Interop.PackageManager.ErrorCode.None)
123             {
124                 Log.Warn(LogTag, "Failed to get package type from " + archivePath);
125             }
126             err = Interop.PackageArchive.PackageArchiveInfoGetVersion(handle, out packageArchive._version);
127             if (err != Interop.PackageManager.ErrorCode.None)
128             {
129                 Log.Warn(LogTag, "Failed to get package version from " + archivePath);
130             }
131             err = Interop.PackageArchive.PackageArchiveInfoGetApiVersion(handle, out packageArchive._apiVersion);
132             if (err != Interop.PackageManager.ErrorCode.None)
133             {
134                 Log.Warn(LogTag, "Failed to get package api version from " + archivePath);
135             }
136             err = Interop.PackageArchive.PackageArchiveInfoGetDescription(handle, out packageArchive._description);
137             if (err != Interop.PackageManager.ErrorCode.None)
138             {
139                 Log.Warn(LogTag, "Failed to get package description from " + archivePath);
140             }
141             err = Interop.PackageArchive.PackageArchiveInfoGetLabel(handle, out packageArchive._label);
142             if (err != Interop.PackageManager.ErrorCode.None)
143             {
144                 Log.Warn(LogTag, "Failed to get package label from " + archivePath);
145             }
146             err = Interop.PackageArchive.PackageArchiveInfoGetAuthor(handle, out packageArchive._author);
147             if (err != Interop.PackageManager.ErrorCode.None)
148             {
149                 Log.Warn(LogTag, "Failed to get package author from " + archivePath);
150             }
151             err = Interop.PackageArchive.PackageArchiveInfoGetIcon(handle, out packageArchive._icon, out packageArchive._iconSize);
152             if (err != Interop.PackageManager.ErrorCode.None)
153             {
154                 Log.Warn(LogTag, "Failed to get package author from " + archivePath);
155             }
156             packageArchive._dependency_to = GetPackageArchiveDependencyInformation(handle);
157
158             return packageArchive;
159         }
160
161         internal static PackageArchive GetPackageArchive(string archivePath)
162         {
163             IntPtr packageArchiveInfoHandle;
164             Interop.PackageManager.ErrorCode err = Interop.PackageArchive.PackageArchiveInfoCreate(archivePath, out packageArchiveInfoHandle);
165             if (err != Interop.PackageManager.ErrorCode.None)
166             {
167                 throw PackageManagerErrorFactory.GetException(err, string.Format("Failed to create native handle for package archive info of {0}", archivePath));
168             }
169
170             PackageArchive packageArchive = CreatePackageArchive(packageArchiveInfoHandle, archivePath);
171
172             err = Interop.PackageArchive.PackageArchiveInfoDestroy(packageArchiveInfoHandle);
173             if (err != Interop.PackageManager.ErrorCode.None)
174             {
175                 Log.Warn(LogTag, string.Format("Failed to destroy native handle for package archive info of {0}. err = {1}", archivePath, err));
176             }
177             return packageArchive;
178         }
179
180         private static List<PackageDependencyInformation> GetPackageArchiveDependencyInformation(IntPtr handle)
181         {
182             List<PackageDependencyInformation> dependencies = new List<PackageDependencyInformation>();
183             Interop.Package.PackageInfoDependencyInfoCallback dependencyInfoCb = (from, to, type, requiredVersion, userData) =>
184             {
185                 dependencies.Add(PackageDependencyInformation.GetPackageDependencyInformation(from, to, type, requiredVersion));
186                 return true;
187             };
188
189             Interop.PackageManager.ErrorCode err = Interop.PackageArchive.PackageArchiveInfoForeachDirectDependency(handle, dependencyInfoCb, IntPtr.Zero);
190             if (err != Interop.PackageManager.ErrorCode.None)
191             {
192                 Log.Warn(LogTag, string.Format("Failed to get dependency info. err = {0}", err));
193             }
194             return dependencies;
195         }
196     }
197 }