[NUI] Remove APIs which have been deprecated in API7
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.ThemeManager / Tizen.Applications.ThemeManager / Theme.cs
1 /*
2  * Copyright (c) 2020 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.Runtime.InteropServices;
21
22 namespace Tizen.Applications.ThemeManager
23 {
24     /// <summary>
25     /// 
26     /// </summary>
27     /// <since_tizen> 8 </since_tizen>
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class Theme : IDisposable
30     {
31         private bool _disposed = false;
32         private IntPtr _handle;
33         private string _id = String.Empty;
34         private string _version = String.Empty;
35         private string _toolVersion = String.Empty;
36         private string _title = String.Empty;
37         private string _resolution = String.Empty;
38         private string _preview = String.Empty;
39         private string _description = String.Empty;
40
41         /// <summary>
42         /// A copy constructor of Theme.
43         /// </summary>
44         /// <param name="theme">Theme class.</param>
45         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
46         /// <exception cref="InvalidOperationException">Thrown when failed because of system error.</exception>
47         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
48         /// <since_tizen> 8 </since_tizen>
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         public Theme(Theme theme)
51         {
52             if (theme == null || theme._handle == IntPtr.Zero)
53             {
54                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(Interop.ThemeManager.ErrorCode.InvalidParameter, "Invalid parameter");
55             }
56
57             var err = Interop.ThemeManager.ErrorCode.None;
58             err = Interop.ThemeManager.ThemeClone(theme._handle, out _handle);
59             if (err != Interop.ThemeManager.ErrorCode.None)
60             {
61                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to clone handle");
62             }
63         }
64
65         internal Theme(IntPtr handle)
66         {
67             _handle = handle;
68             var err = Interop.ThemeManager.ErrorCode.None;
69             err = Interop.ThemeManager.GetId(handle, out _id);
70             if (err != Interop.ThemeManager.ErrorCode.None)
71             {
72                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get id");
73             }
74
75             err = Interop.ThemeManager.GetVersion(handle, out _version);
76             if (err != Interop.ThemeManager.ErrorCode.None)
77             {
78                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get version");
79             }
80
81             err = Interop.ThemeManager.GetToolVersion(handle, out _toolVersion);
82             if (err != Interop.ThemeManager.ErrorCode.None)
83             {
84                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get tool version");
85             }
86
87             err = Interop.ThemeManager.GetTitle(handle, out _title);
88             if (err != Interop.ThemeManager.ErrorCode.None)
89             {
90                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get title");
91             }
92
93             err = Interop.ThemeManager.GetResolution(handle, out _resolution);
94             if (err != Interop.ThemeManager.ErrorCode.None)
95             {
96                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get resolution");
97             }
98
99             err = Interop.ThemeManager.GetPreview(handle, out _preview);
100             if (err != Interop.ThemeManager.ErrorCode.None)
101             {
102                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get preview");
103             }
104
105             err = Interop.ThemeManager.GetDescription(handle, out _description);
106             if (err != Interop.ThemeManager.ErrorCode.None)
107             {
108                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get description");
109             }
110         }
111
112         /// <summary>
113         /// A Theme ID
114         /// </summary>
115         /// <since_tizen> 8 </since_tizen>
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public string Id { get { return _id; } }
118
119         /// <summary>
120         /// A Theme Version
121         /// </summary>
122         /// <since_tizen> 8 </since_tizen>
123         [EditorBrowsable(EditorBrowsableState.Never)]
124         public string Version { get { return _version; } }
125
126         /// <summary>
127         /// A Theme ToolVersion
128         /// </summary>
129         /// <since_tizen> 8 </since_tizen>
130         [EditorBrowsable(EditorBrowsableState.Never)]
131         public string ToolVersion { get { return _toolVersion; } }
132
133         /// <summary>
134         /// A Theme Title
135         /// </summary>
136         /// <since_tizen> 8 </since_tizen>
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         public string Title { get { return _title; } }
139
140         /// <summary>
141         /// A Theme Resolution
142         /// </summary>
143         /// <since_tizen> 8 </since_tizen>
144         [EditorBrowsable(EditorBrowsableState.Never)]
145         public string Resolution { get { return _resolution; } }
146
147         /// <summary>
148         /// A Theme Preview
149         /// </summary>
150         /// <since_tizen> 8 </since_tizen>
151         [EditorBrowsable(EditorBrowsableState.Never)]
152         public string Preview { get { return _preview; } }
153
154         /// <summary>
155         /// A Theme Description
156         /// </summary>
157         /// <since_tizen> 8 </since_tizen>
158         [EditorBrowsable(EditorBrowsableState.Never)]
159         public string Description { get { return _description; } }
160
161
162         /// <summary>
163         /// Gets the string corresponding with given key.
164         /// </summary>
165         /// <param name="key">The string key to find information.</param>
166         /// <since_tizen> 8 </since_tizen>
167         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
168         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
169         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
170         [EditorBrowsable(EditorBrowsableState.Never)]
171         public string GetString(string key)
172         {
173             string str;
174             var err = Interop.ThemeManager.GetString(_handle, key, out str);
175             if (err != Interop.ThemeManager.ErrorCode.None)
176             {
177                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get string value of the key");
178             }
179
180             return str;
181         }
182
183         /// <summary>
184         /// Gets the string array corresponding with given key.
185         /// </summary>
186         /// <param name="key">The string key to find information.</param>
187         /// <since_tizen> 8 </since_tizen>
188         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
189         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
190         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
191         [EditorBrowsable(EditorBrowsableState.Never)]
192         public IEnumerable<string> GetStrings(string key)
193         {
194             IntPtr val;
195             int count;
196             string[] strings;
197             var err = Interop.ThemeManager.GetStringArray(_handle, key, out val, out count);
198             if (err != Interop.ThemeManager.ErrorCode.None)
199             {
200                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get string array value of the key");
201             }
202
203             IntPtrToStringArray(val, count, out strings);
204             return strings;
205         }
206
207         /// <summary>
208         /// Gets the integer corresponding with given key.
209         /// </summary>
210         /// <param name="key">The string key to find information.</param>
211         /// <since_tizen> 8 </since_tizen>
212         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
213         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
214         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
215         [EditorBrowsable(EditorBrowsableState.Never)]
216         public int GetInt(string key)
217         {
218             int val;
219             var err = Interop.ThemeManager.GetInt(_handle, key, out val);
220             if (err != Interop.ThemeManager.ErrorCode.None)
221             {
222                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get integer value of the key");
223             }
224
225             return val;
226         }
227
228         /// <summary>
229         /// Gets the float corresponding with given key.
230         /// </summary>
231         /// <param name="key">The string key to find information.</param>
232         /// <since_tizen> 8 </since_tizen>
233         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
234         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
235         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
236         [EditorBrowsable(EditorBrowsableState.Never)]
237         public float GetFloat(string key)
238         {
239             float val;
240             var err = Interop.ThemeManager.GetFloat(_handle, key, out val);
241             if (err != Interop.ThemeManager.ErrorCode.None)
242             {
243                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get float value of the key");
244             }
245
246             return val;
247         }
248
249         /// <summary>
250         /// Gets the bool corresponding with given key.
251         /// </summary>
252         /// <param name="key">The string key to find information.</param>
253         /// <since_tizen> 8 </since_tizen>
254         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
255         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
256         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
257         [EditorBrowsable(EditorBrowsableState.Never)]
258         public bool GetBool(string key)
259         {
260             bool val;
261             var err = Interop.ThemeManager.GetBool(_handle, key, out val);
262             if (err != Interop.ThemeManager.ErrorCode.None)
263             {
264                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get bool value of the key");
265             }
266
267             return val;
268         }
269
270         /// <summary>
271         /// Gets the path corresponding with given key.
272         /// </summary>
273         /// <param name="key">The string key to find information.</param>
274         /// <since_tizen> 8 </since_tizen>
275         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
276         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
277         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
278         [EditorBrowsable(EditorBrowsableState.Never)]
279         public string GetPath(string key)
280         {
281             string val;
282             var err = Interop.ThemeManager.GetPath(_handle, key, out val);
283             if (err != Interop.ThemeManager.ErrorCode.None)
284             {
285                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get path value of the key");
286             }
287
288             return val;
289         }
290
291         /// <summary>
292         /// Gets the path array corresponding with given key.
293         /// </summary>
294         /// <param name="key">The string key to find information.</param>
295         /// <since_tizen> 8 </since_tizen>
296         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
297         /// <exception cref="InvalidOperationException">Thrown when failed because of the system error.</exception>
298         /// <exception cref="OutOfMemoryException">Thrown when failed because of out of memory.</exception>
299         [EditorBrowsable(EditorBrowsableState.Never)]
300         public IEnumerable<string> GetPaths(string key)
301         {
302             IntPtr val;
303             int count;
304             string[] paths;
305             var err = Interop.ThemeManager.GetPathArray(_handle, key, out val, out count);
306             if (err != Interop.ThemeManager.ErrorCode.None)
307             {
308                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to get path array value of the key");
309             }
310
311             IntPtrToStringArray(val, count, out paths);
312             return paths;
313         }
314
315         static void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
316         {
317             managedArray = new string[size];
318             IntPtr[] IntPtrArray = new IntPtr[size];
319             Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
320             for (int iterator = 0; iterator < size; iterator++)
321             {
322                 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);
323             }
324         }
325
326         /// <summary>
327         /// Check the given key is exist or not.
328         /// </summary>
329         /// <param name="key">The string key to find information.</param>
330         /// <since_tizen> 9 </since_tizen>
331         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
332         [EditorBrowsable(EditorBrowsableState.Never)]
333         public bool HasKey(string key)
334         {
335             bool val;
336             var err = Interop.ThemeManager.IsKeyExist(_handle, key, out val);
337             if (err != Interop.ThemeManager.ErrorCode.None)
338             {
339                 throw Interop.ThemeManager.ThemeManagerErrorFactory.GetException(err, "Failed to check existence of the key");
340             }
341
342             return val;
343         }
344
345         /// <summary>
346         /// Releases all resources used by the Theme class.
347         /// </summary>
348         /// <since_tizen> 8 </since_tizen>
349         [EditorBrowsable(EditorBrowsableState.Never)]
350         public void Dispose()
351         {
352             Dispose(true);
353             GC.SuppressFinalize(this);
354         }
355
356         /// <summary>
357         /// Releases the unmanaged resources used by the Theme class specifying whether to perform a normal dispose operation.
358         /// </summary>
359         /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
360         protected virtual void Dispose(bool disposing)
361         {
362             if (_disposed)
363                 return;
364
365             if (_handle != IntPtr.Zero)
366             {
367                 Interop.ThemeManager.ThemeDestroy(_handle);
368                 _handle = IntPtr.Zero;
369             }
370             _disposed = true;
371         }
372     }
373 }