[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / ResourceManager.cs
1 /*
2  * Copyright (c) 2017 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.IO;
19 using Tizen.Internals.Errors;
20
21 namespace Tizen.Applications
22 {
23     /// <summary>
24     /// The class for getting the resource path.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public static class ResourceManager
28     {
29         /// <summary>
30         /// Enumeration for the resource category.
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         public enum Category : int
34         {
35             /// <summary>
36             /// Image format.
37             /// </summary>
38             Image = 0,
39
40             /// <summary>
41             /// Layout format.
42             /// </summary>
43             Layout,
44
45             /// <summary>
46             /// Sound format.
47             /// </summary>
48             Sound,
49
50             /// <summary>
51             /// Binary format.
52             /// </summary>
53             Binary
54         }
55
56         private static ErrorCode AppResourceManagerGet(Category category, string id, out string path)
57         {
58             ErrorCode err;
59
60             try
61             {
62                 err = Interop.AppCommon.AppResourceManagerGet(
63                              (Interop.AppCommon.ResourceCategory)category, id, out path);
64             }
65             catch (System.TypeLoadException)
66             {
67                 err = Interop.AppCommon.LegacyAppResourceManagerGet(
68                              (Interop.AppCommon.ResourceCategory)category, id, out path);
69             }
70
71             return err;
72         }
73
74         /// <summary>
75         /// Converts resource ID to the path name.
76         /// </summary>
77         /// <param name="category">Category to search.</param>
78         /// <param name="id">ID to search.</param>
79         /// <returns>Found resource path.</returns>
80         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
81         /// <since_tizen> 3 </since_tizen>
82         public static string GetPath(Category category, string id)
83         {
84             string path;
85             ErrorCode err = AppResourceManagerGet(category, id, out path);
86
87             switch (err)
88             {
89                 case ErrorCode.InvalidParameter:
90                 throw new InvalidOperationException("Invalid parameter");
91
92                 case ErrorCode.OutOfMemory:
93                 throw new InvalidOperationException("Out-of-memory at unmanaged code");
94
95                 case ErrorCode.IoError:
96                 throw new InvalidOperationException("IO error at unmanaged code");
97             }
98
99             return path;
100         }
101
102         /// <summary>
103         /// Converts resource ID to the path name.
104         /// </summary>
105         /// <param name="category">Category to search.</param>
106         /// <param name="id">ID to search.</param>
107         /// <returns>Found resource path or null when the resource doesn't exist.</returns>
108         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
109         /// <since_tizen> 3 </since_tizen>
110         public static string TryGetPath(Category category, string id)
111         {
112             string path;
113             ErrorCode err;
114             string res;
115
116             if (Application.Current != null)
117             {
118                 res = Application.Current.DirectoryInfo.Resource + "res.xml";
119             }
120             else
121             {
122                 res = Interop.AppCommon.AppGetResourcePath() + "res.xml";
123             }
124
125             if (!File.Exists(res))
126                 return null;
127
128             err = AppResourceManagerGet(category, id, out path);
129             switch (err)
130             {
131                 case ErrorCode.InvalidParameter:
132                 throw new InvalidOperationException("Invalid parameter");
133
134                 case ErrorCode.OutOfMemory:
135                 throw new InvalidOperationException("Out-of-memory at unmanaged code");
136
137                 case ErrorCode.IoError:
138                 return null;
139             }
140
141             return path;
142         }
143     }
144 }