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