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