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