Setting since_tizen 3/4 on Tizen.NET API
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Storage / Storage / Storage.cs
1 /*
2 * Copyright (c) 2016 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
19 namespace Tizen.System
20 {
21     /// <summary>
22     /// The class to access the storage device information.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public class Storage
26     {
27         private const string LogTag = "Tizen.System";
28         private Interop.Storage.StorageState _state;
29         private ulong _totalSpace;
30
31         internal Storage(int storageID, Interop.Storage.StorageArea storageType, Interop.Storage.StorageState storagestate, string rootDirectory)
32         {
33             Id = storageID;
34             StorageType = (StorageArea)storageType;
35             RootDirectory = rootDirectory;
36             _state = storagestate;
37
38             Interop.Storage.ErrorCode err = Interop.Storage.StorageGetTotalSpace(Id, out _totalSpace);
39             if (err != Interop.Storage.ErrorCode.None)
40             {
41                 Log.Warn(LogTag, string.Format("Failed to get total storage space for storage Id: {0}. err = {1}", Id, err));
42             }
43
44             s_stateChangedEventCallback = (id, state, userData) =>
45             {
46                 if (id == Id)
47                 {
48                     _state = state;
49                     s_stateChangedEventHandler?.Invoke(this, EventArgs.Empty);
50                 }
51             };
52         }
53
54         private EventHandler s_stateChangedEventHandler;
55         private Interop.Storage.StorageStateChangedCallback s_stateChangedEventCallback;
56
57         private void RegisterStateChangedEvent()
58         {
59             Interop.Storage.ErrorCode err = Interop.Storage.StorageSetStateChanged(Id, s_stateChangedEventCallback, IntPtr.Zero);
60             if (err != Interop.Storage.ErrorCode.None)
61             {
62                 Log.Warn(LogTag, string.Format("Failed to Register state changed event callback for storage Id: {0}. err = {1}", Id, err));
63             }
64         }
65
66         private void UnregisterStateChangedEvent()
67         {
68             Interop.Storage.ErrorCode err = Interop.Storage.StorageUnsetStateChanged(Id, s_stateChangedEventCallback);
69             if (err != Interop.Storage.ErrorCode.None)
70             {
71                 Log.Warn(LogTag, string.Format("Failed to Register state changed event callback for storage Id: {0}. err = {1}", Id, err));
72             }
73         }
74
75         /// <summary>
76         /// StorageStateChanged event. This event is occurred when a storage state changes.
77         /// </summary>
78         /// <remarks>
79         /// The storage state will be updated before calling the event handler.
80         /// </remarks>
81         /// <since_tizen> 3 </since_tizen>
82         /// <example>
83         /// <code>
84         /// myStorage.StorageStateChanged += (s, e) =>
85         /// {
86         ///     var storage = s as Storage;
87         ///     Console.WriteLine(string.Format("State Changed to {0}", storage.State));
88         /// }
89         /// </code>
90         /// </example>
91         public event EventHandler StorageStateChanged
92         {
93             add
94             {
95                 if (s_stateChangedEventHandler == null)
96                 {
97                     _state = (Interop.Storage.StorageState)State;
98                     RegisterStateChangedEvent();
99                 }
100                 s_stateChangedEventHandler += value;
101             }
102             remove
103             {
104                 if (s_stateChangedEventHandler != null)
105                 {
106                     s_stateChangedEventHandler -= value;
107                     if (s_stateChangedEventHandler == null)
108                     {
109                         UnregisterStateChangedEvent();
110                     }
111                 }
112             }
113         }
114
115         /// <summary>
116         /// The storage ID.
117         /// </summary>
118         /// <since_tizen> 3 </since_tizen>
119         public int Id { get; }
120         /// <summary>
121         /// The type of storage.
122         /// </summary>
123         /// <since_tizen> 3 </since_tizen>
124         public StorageArea StorageType { get; }
125         /// <summary>
126         /// The root directory for the storage.
127         /// </summary>
128         /// <since_tizen> 3 </since_tizen>
129         public string RootDirectory { get; }
130         /// <summary>
131         /// The total storage size in bytes.
132         /// </summary>
133         /// <since_tizen> 3 </since_tizen>
134         public ulong TotalSpace { get { return _totalSpace; } }
135
136         /// <summary>
137         /// The StorageState.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         public StorageState State
141         {
142             get
143             {
144                 if (s_stateChangedEventHandler == null)
145                 {
146                     Interop.Storage.ErrorCode err = Interop.Storage.StorageGetState(Id, out _state);
147                     if (err != Interop.Storage.ErrorCode.None)
148                     {
149                         Log.Warn(LogTag, string.Format("Failed to get storage state for storage Id: {0}. err = {1}", Id, err));
150                     }
151                 }
152                 return (StorageState)_state;
153             }
154         }
155
156         /// <summary>
157         /// The available storage size in bytes.
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         public ulong AvaliableSpace
161         {
162             get
163             {
164                 ulong available;
165                 Interop.Storage.ErrorCode err = Interop.Storage.StorageGetAvailableSpace(Id, out available);
166                 if (err != Interop.Storage.ErrorCode.None)
167                 {
168                     Log.Warn(LogTag, string.Format("Failed to get available storage stace for storage Id: {0}. err = {1}", Id, err));
169                 }
170
171                 return available;
172             }
173         }
174
175         /// <summary>
176         /// Absolute path for a given directory type in the storage.
177         /// </summary>
178         /// <remarks>
179         /// The returned directory path may not exist, so you must make sure that it exists before using it.
180         /// For accessing internal storage except the ringtones directory, the application should have http://tizen.org/privilege/mediastorage privilege.
181         /// For accessing ringtones directory, the application should have http://tizen.org/privilege/systemsettings privilege.
182         /// For accessing external storage, the application should have http://tizen.org/privilege/externalstorage privilege.
183         /// </remarks>
184         /// <since_tizen> 3 </since_tizen>
185         /// <param name="dirType">Directory type.</param>
186         /// <returns>Absolute path for a given directory type in the storage.</returns>
187         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
188         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory exception.</exception>
189         /// <exception cref="NotSupportedException">Thrown when failed if the storage is not supported or the application does not have the permission to access the directory path.</exception>
190         /// <privilege>http://tizen.org/privilege/mediastorage</privilege>
191         /// <privilege>http://tizen.org/privilege/systemsettings</privilege>
192         /// <privilege>http://tizen.org/privilege/externalstorage</privilege>
193         /// <example>
194         /// <code>
195         /// // To get the video directories for all the supported storage,
196         /// var storageList = StorageManager.Storages as List&lt;Storage&gt;;
197         /// foreach (var storage in storageList)
198         /// {
199         ///     string pathForVideoDir = storage.GetAbsolutePath(DirectoryType.Videos);
200         /// }
201         /// </code>
202         /// </example>
203         public string GetAbsolutePath(DirectoryType dirType)
204         {
205             string path;
206             Interop.Storage.ErrorCode err = Interop.Storage.StorageGetAbsoluteDirectory(Id, (Interop.Storage.DirectoryType)dirType, out path);
207             if (err != Interop.Storage.ErrorCode.None)
208             {
209                 Log.Warn(LogTag, string.Format("Failed to get package Id. err = {0}", err));
210                 switch (err)
211                 {
212                     case Interop.Storage.ErrorCode.InvalidParameter:
213                         throw new ArgumentException("Invalid Arguments");
214                     case Interop.Storage.ErrorCode.OutOfMemory:
215                         throw new OutOfMemoryException("Out of Memory");
216                     case Interop.Storage.ErrorCode.NotSupported:
217                         throw new NotSupportedException("Operation Not Supported");
218                     default:
219                         throw new InvalidOperationException("Error = " + err);
220                 }
221             }
222             return path;
223         }
224     }
225 }