2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Reflection;
21 using System.Runtime.InteropServices;
22 using Tizen.Applications;
24 using SystemIO = System.IO;
29 /// This class provides properties to get information the gadget.
31 /// <since_tizen> 10 </since_tizen>
32 [EditorBrowsable(EditorBrowsableState.Never)]
33 public class NUIGadgetInfo
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";
39 internal NUIGadgetInfo(string packageId)
41 PackageId = packageId;
42 Log.Warn("PackageId: " + PackageId);
46 /// Gets the package ID of the gadget.
48 /// <since_tizen> 10 </since_tizen>
49 public string PackageId { get; private set; }
52 /// Gets the resource type of the gadget.
54 /// <since_tizen> 10 </since_tizen>
55 public string ResourceType { get; private set; }
58 /// Gets the resource version of the gadget.
60 /// <since_tizen> 10 </since_tizen>
61 public string ResourceVersion { get; private set; }
64 /// Gets the resource path of the gadget.
66 /// <since_tizen> 10 </since_tizen>
67 public string ResourcePath
73 /// Gets the executable file of the gadget.
75 /// <since_tizen> 10 </since_tizen>
76 public string ExecutableFile { get; internal set; }
79 /// Gets the metadata of the gadget.
81 /// <since_tizen> 10 </since_tizen>
82 public IDictionary<string, string> Metadata { get; private set; }
84 internal string ResourceFile { get; set; }
86 internal string ResourceClassName { get; set; }
88 internal Assembly Assembly { get; set; }
90 internal static NUIGadgetInfo CreateNUIGadgetInfo(string packageId)
92 Interop.PackageManagerInfo.ErrorCode errorCode = Interop.PackageManagerInfo.PackageInfoGet(packageId, out IntPtr handle);
93 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
95 Log.Error("Failed to get package info. error = " + errorCode);
99 NUIGadgetInfo info = new NUIGadgetInfo(packageId);
101 errorCode = Interop.PackageManagerInfo.PackageInfoGetResourceType(handle, out IntPtr resourceTypePtr);
102 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
104 Log.Error("Failed to get resource type. error = " + errorCode);
108 info.ResourceType = Marshal.PtrToStringAnsi(resourceTypePtr);
111 errorCode = Interop.PackageManagerInfo.PackageInfoGetResourceVersion(handle, out IntPtr resourceVersionPtr);
112 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
114 Log.Error("Failed to get resource version. error = " + errorCode);
118 info.ResourceVersion = Marshal.PtrToStringAnsi(resourceVersionPtr);
121 Dictionary<string, string> metadata = new Dictionary<string, string>();
122 int callback(string key, string value, IntPtr userData)
124 Log.Info("key: " + key + ", value: " + value);
127 if (!metadata.ContainsKey(key))
129 metadata.Add(key, value);
135 errorCode = Interop.PackageManagerInfo.PackageInfoForeachMetadata(handle, callback, IntPtr.Zero);
136 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
138 Log.Error("Failed to retrieve meatadata. error = " + errorCode);
141 info.Metadata = metadata;
143 if (info.Metadata.TryGetValue(MetadataUIGadgetDll, out string executableFile))
145 info.ExecutableFile = executableFile;
146 Log.Info("ExecutableFile: " + info.ExecutableFile);
150 Log.Error("Failed to find metadata. " + MetadataUIGadgetDll);
153 if (info.Metadata.TryGetValue(MetadataUIGadgetResourceDll, out string resourceFile))
155 info.ResourceFile = resourceFile;
156 Log.Info("LocaleFile: " + info.ResourceFile);
160 Log.Warn("There is no locale dll");
163 if (info.Metadata.TryGetValue(MetadataUIGadgetResourceClassName, out string resourceClassName))
165 info.ResourceClassName = resourceClassName;
166 Log.Info("LocaleClassName: " + info.ResourceClassName);
170 Log.Warn("There is no locale class");
173 errorCode = Interop.PackageManagerInfo.PackageInfoDestroy(handle);
174 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
176 Log.Warn("Failed to destroy package info. error = " + errorCode);
179 info.ResourcePath = SystemIO.Path.GetDirectoryName(Application.Current.ApplicationInfo.ExecutablePath) + "/";