Add Assembly property to NUIGadgetInfo (#5228)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Gadget / Tizen.NUI / NUIGadgetInfo.cs
1 /*
2  * Copyright (c) 2023 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.ComponentModel;
20 using System.IO;
21 using System.Reflection;
22 using System.Runtime.InteropServices;
23 using Tizen.Applications;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// This class provides properties to get information the gadget.
29     /// </summary>
30     /// <since_tizen> 10 </since_tizen>
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class NUIGadgetInfo
33     {
34         private const string MetadataUIGadgetDll = "http://tizen.org/metadata/ui-gadget/dll";
35         private const string MetadataUIGadgetResourceDll = "http://tizen.org/metadata/ui-gadget/resource/dll";
36         private const string MetadataUIGadgetResourceClassName = "http://tizen.org/metadata/ui-gadget/resource/class-name";
37         private string _resourcePath = string.Empty;
38
39         internal NUIGadgetInfo(string packageId)
40         {
41             PackageId = packageId;
42             Log.Warn("PackageId: " + PackageId);
43             AllowedPath = Application.Current.DirectoryInfo.Resource + "mount/allowed/";
44             Log.Warn("AllowedPath: " + AllowedPath);
45             GlobalPath = Application.Current.DirectoryInfo.Resource + "mount/global/";
46             Log.Warn("GlobalPath: " + GlobalPath);
47         }
48
49         private string AllowedPath { get; set; }
50
51         private string GlobalPath { get; set; }
52
53         /// <summary>
54         /// Gets the package ID of the gadget.
55         /// </summary>
56         /// <since_tizen> 10 </since_tizen>
57         public string PackageId { get; private set; }
58
59         /// <summary>
60         /// Gets the resource type of the gadget.
61         /// </summary>
62         /// <since_tizen> 10 </since_tizen>
63         public string ResourceType { get; private set; }
64
65         /// <summary>
66         /// Gets the resource version of the gadget.
67         /// </summary>
68         /// <since_tizen> 10 </since_tizen>
69         public string ResourceVersion { get; private set; }
70
71         /// <summary>
72         /// Gets the resource path of the gadget.
73         /// </summary>
74         /// <since_tizen> 10 </since_tizen>
75         public string ResourcePath
76         {
77             get
78             {
79                 if (!string.IsNullOrEmpty(_resourcePath))
80                     return _resourcePath;
81
82                 if (File.Exists(GlobalPath + ExecutableFile))
83                 {
84                     _resourcePath = GlobalPath;
85                 }
86                 else if (File.Exists(AllowedPath + ExecutableFile))
87                 {
88                     _resourcePath = AllowedPath;
89                 }
90
91                 return _resourcePath;
92             }
93         }
94
95         /// <summary>
96         /// Gets the executable file of the gadget.
97         /// </summary>
98         /// <since_tizen> 10 </since_tizen>
99         public string ExecutableFile { get; internal set; }
100
101         /// <summary>
102         /// Gets the metadata of the gadget.
103         /// </summary>
104         /// <since_tizen> 10 </since_tizen>
105         public IDictionary<string, string> Metadata { get; private set; }
106
107         internal string ResourceFile { get; set; }
108
109         internal string ResourceClassName { get; set; }
110
111         internal Assembly Assembly { get; set; }
112
113         internal static NUIGadgetInfo CreateNUIGadgetInfo(string packageId)
114         {
115             Interop.PackageManagerInfo.ErrorCode errorCode = Interop.PackageManagerInfo.PackageInfoGet(packageId, out IntPtr handle);
116             if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
117             {
118                 Log.Error("Failed to get package info. error = " + errorCode);
119                 return null;
120             }
121
122             NUIGadgetInfo info = new NUIGadgetInfo(packageId);
123
124             errorCode = Interop.PackageManagerInfo.PackageInfoGetResourceType(handle, out IntPtr resourceTypePtr);
125             if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
126             {
127                 Log.Error("Failed to get resource type. error = " + errorCode);
128             }
129             else
130             {
131                 info.ResourceType = Marshal.PtrToStringAnsi(resourceTypePtr);
132             }
133
134             errorCode = Interop.PackageManagerInfo.PackageInfoGetResourceVersion(handle, out IntPtr resourceVersionPtr);
135             if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
136             {
137                 Log.Error("Failed to get resource version. error = " + errorCode);
138             }
139             else
140             {
141                 info.ResourceVersion = Marshal.PtrToStringAnsi(resourceVersionPtr);
142             }
143
144             Dictionary<string, string> metadata = new Dictionary<string, string>();
145             int callback(string key, string value, IntPtr userData)
146             {
147                 Log.Info("key: " + key + ", value: " + value);
148                 if (key.Length != 0)
149                 {
150                     if (!metadata.ContainsKey(key))
151                     {
152                         metadata.Add(key, value);
153                     }
154                 }
155                 return 0;
156             }
157
158             errorCode = Interop.PackageManagerInfo.PackageInfoForeachMetadata(handle, callback, IntPtr.Zero);
159             if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
160             {
161                 Log.Error("Failed to retrieve meatadata. error = " + errorCode);
162             }
163
164             info.Metadata = metadata;
165
166             if (info.Metadata.TryGetValue(MetadataUIGadgetDll, out string executableFile))
167             {
168                 info.ExecutableFile = executableFile;
169                 Log.Info("ExecutableFile: " + info.ExecutableFile);
170             }
171             else
172             {
173                 Log.Error("Failed to find metadata. " + MetadataUIGadgetDll);
174             }
175
176             if (info.Metadata.TryGetValue(MetadataUIGadgetResourceDll, out string resourceFile))
177             {
178                 info.ResourceFile = resourceFile;
179                 Log.Info("LocaleFile: " + info.ResourceFile);
180             }
181             else
182             {
183                 Log.Warn("There is no locale dll");
184             }
185
186             if (info.Metadata.TryGetValue(MetadataUIGadgetResourceClassName, out string resourceClassName))
187             {
188                 info.ResourceClassName = resourceClassName;
189                 Log.Info("LocaleClassName: " + info.ResourceClassName);
190             }
191             else
192             {
193                 Log.Warn("There is no locale class");
194             }
195
196             errorCode = Interop.PackageManagerInfo.PackageInfoDestroy(handle);
197             if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
198             {
199                 Log.Warn("Failed to destroy package info. error = " + errorCode);
200             }
201
202             return info;
203         }
204     }
205 }