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;
21 using System.Reflection;
22 using System.Runtime.InteropServices;
23 using Tizen.Applications;
28 /// This class provides properties to get information the gadget.
30 /// <since_tizen> 10 </since_tizen>
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public class NUIGadgetInfo
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;
39 internal NUIGadgetInfo(string packageId)
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);
49 private string AllowedPath { get; set; }
51 private string GlobalPath { get; set; }
54 /// Gets the package ID of the gadget.
56 /// <since_tizen> 10 </since_tizen>
57 public string PackageId { get; private set; }
60 /// Gets the resource type of the gadget.
62 /// <since_tizen> 10 </since_tizen>
63 public string ResourceType { get; private set; }
66 /// Gets the resource version of the gadget.
68 /// <since_tizen> 10 </since_tizen>
69 public string ResourceVersion { get; private set; }
72 /// Gets the resource path of the gadget.
74 /// <since_tizen> 10 </since_tizen>
75 public string ResourcePath
79 if (!string.IsNullOrEmpty(_resourcePath))
82 if (File.Exists(GlobalPath + ExecutableFile))
84 _resourcePath = GlobalPath;
86 else if (File.Exists(AllowedPath + ExecutableFile))
88 _resourcePath = AllowedPath;
96 /// Gets the executable file of the gadget.
98 /// <since_tizen> 10 </since_tizen>
99 public string ExecutableFile { get; internal set; }
102 /// Gets the metadata of the gadget.
104 /// <since_tizen> 10 </since_tizen>
105 public IDictionary<string, string> Metadata { get; private set; }
107 internal string ResourceFile { get; set; }
109 internal string ResourceClassName { get; set; }
111 internal NUIGadgetAssembly Assembly { get; private set; }
113 internal static NUIGadgetInfo CreateNUIGadgetInfo(string packageId)
115 Interop.PackageManagerInfo.ErrorCode errorCode = Interop.PackageManagerInfo.PackageInfoGet(packageId, out IntPtr handle);
116 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
118 Log.Error("Failed to get package info. error = " + errorCode);
122 NUIGadgetInfo info = new NUIGadgetInfo(packageId);
124 errorCode = Interop.PackageManagerInfo.PackageInfoGetResourceType(handle, out IntPtr resourceTypePtr);
125 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
127 Log.Error("Failed to get resource type. error = " + errorCode);
131 info.ResourceType = Marshal.PtrToStringAnsi(resourceTypePtr);
134 errorCode = Interop.PackageManagerInfo.PackageInfoGetResourceVersion(handle, out IntPtr resourceVersionPtr);
135 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
137 Log.Error("Failed to get resource version. error = " + errorCode);
141 info.ResourceVersion = Marshal.PtrToStringAnsi(resourceVersionPtr);
144 Dictionary<string, string> metadata = new Dictionary<string, string>();
145 int callback(string key, string value, IntPtr userData)
147 Log.Info("key: " + key + ", value: " + value);
150 if (!metadata.ContainsKey(key))
152 metadata.Add(key, value);
158 errorCode = Interop.PackageManagerInfo.PackageInfoForeachMetadata(handle, callback, IntPtr.Zero);
159 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
161 Log.Error("Failed to retrieve meatadata. error = " + errorCode);
164 info.Metadata = metadata;
166 if (info.Metadata.TryGetValue(MetadataUIGadgetDll, out string executableFile))
168 info.ExecutableFile = executableFile;
169 Log.Info("ExecutableFile: " + info.ExecutableFile);
173 Log.Error("Failed to find metadata. " + MetadataUIGadgetDll);
176 if (info.Metadata.TryGetValue(MetadataUIGadgetResourceDll, out string resourceFile))
178 info.ResourceFile = resourceFile;
179 Log.Info("LocaleFile: " + info.ResourceFile);
183 Log.Warn("There is no locale dll");
186 if (info.Metadata.TryGetValue(MetadataUIGadgetResourceClassName, out string resourceClassName))
188 info.ResourceClassName = resourceClassName;
189 Log.Info("LocaleClassName: " + info.ResourceClassName);
193 Log.Warn("There is no locale class");
196 errorCode = Interop.PackageManagerInfo.PackageInfoDestroy(handle);
197 if (errorCode != Interop.PackageManagerInfo.ErrorCode.None)
199 Log.Warn("Failed to destroy package info. error = " + errorCode);
202 info.Assembly = new NUIGadgetAssembly(info.ResourcePath + info.ExecutableFile);