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