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