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