Setting since_tizen 3/4 on Tizen.NET API
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Mtp / Tizen.Network.Mtp / MtpStorage.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 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
20
21 namespace Tizen.Network.Mtp
22 {
23     /// <summary>
24     /// A class for Mtp Storage information. It allows applications to handle storage information.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public class MtpStorage : IDisposable
28     {
29         private int _deviceHandle = -1;
30         private int _storageHandle = -1;
31         private bool disposed = false;
32         private List<int> _objectHandleList = new List<int>();
33         private List<MtpObject> _objectList = new List<MtpObject>();
34         private MtpObject _rootObject;
35
36         /// <summary>
37         /// Gets the description of the storage information.
38         /// </summary>
39         /// <value>Description of storage.</value>
40         /// <since_tizen> 4 </since_tizen>
41         public string Description
42         {
43             get
44             {
45                 IntPtr strPtr;
46                 int ret = Interop.Mtp.StorageInformation.GetDescription(_deviceHandle, _storageHandle, out strPtr);
47                 if (ret != (int)MtpError.None)
48                 {
49                     Log.Error(Globals.LogTag, "Failed to get description, Error - " + (MtpError)ret);
50                     return "";
51                 }
52                 return Marshal.PtrToStringAnsi(strPtr);
53             }
54         }
55
56         /// <summary>
57         /// Gets the free space of the storage information in bytes.
58         /// </summary>
59         /// <value>Free space of storage(bytes).</value>
60         /// <since_tizen> 4 </since_tizen>
61         public UInt64 FreeSpace
62         {
63             get
64             {
65                 UInt64 freeSpace;
66                 int ret = Interop.Mtp.StorageInformation.GetFreeSpace(_deviceHandle, _storageHandle, out freeSpace);
67                 if (ret != (int)MtpError.None)
68                 {
69                     Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
70                 }
71                 return freeSpace;
72             }
73         }
74
75         /// <summary>
76         /// Gets the max capacity of the storage information in bytes.
77         /// </summary>
78         /// <value>Max capacity of storage(bytes).</value>
79         /// <since_tizen> 4 </since_tizen>
80         public UInt64 MaxCapacity
81         {
82             get
83             {
84                 UInt64 maxCapacity;
85                 int ret = Interop.Mtp.StorageInformation.GetMaxCapacity(_deviceHandle, _storageHandle, out maxCapacity);
86                 if (ret != (int)MtpError.None)
87                 {
88                     Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
89                 }
90                 return maxCapacity;
91             }
92         }
93
94         /// <summary>
95         /// Gets the storage type of the storage information.
96         /// </summary>
97         /// <value>Type of storage.</value>
98         /// <since_tizen> 4 </since_tizen>
99         public MtpStorageType StorageType
100         {
101             get
102             {
103                 int storageType;
104                 int ret = Interop.Mtp.StorageInformation.GetStorageType(_deviceHandle, _storageHandle, out storageType);
105                 if (ret != (int)MtpError.None)
106                 {
107                     Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
108                 }
109                 return (MtpStorageType)storageType;
110             }
111         }
112
113         /// <summary>
114         /// Gets the volume identifier of the storage information.
115         /// </summary>
116         /// <value>Volume identifier of stroage.</value>
117         /// <since_tizen> 4 </since_tizen>
118         public string VolumeIdentifier
119         {
120             get
121             {
122                 IntPtr strPtr;
123                 int ret = Interop.Mtp.StorageInformation.GetVolumeIdentifier(_deviceHandle, _storageHandle, out strPtr);
124                 if (ret != (int)MtpError.None)
125                 {
126                     Log.Error(Globals.LogTag, "Failed to get volume identifier, Error - " + (MtpError)ret);
127                     return "";
128                 }
129                 return Marshal.PtrToStringAnsi(strPtr);
130             }
131         }
132
133         internal MtpStorage(int deviceHandle, int storageHandle)
134         {
135             _deviceHandle = deviceHandle;
136             _storageHandle = storageHandle;
137         }
138
139         /// <summary>
140         /// MtpStorage destructor.
141         /// </summary>
142         ~MtpStorage()
143         {
144             Dispose(false);
145         }
146
147         /// <summary>
148         /// Dispose
149         /// </summary>
150         /// <since_tizen> 4 </since_tizen>
151         public void Dispose()
152         {
153             Dispose(true);
154             GC.SuppressFinalize(this);
155         }
156
157         private void Dispose(bool disposing)
158         {
159             if (disposed)
160                 return;
161
162             if (disposing)
163             {
164                 // Free managed objects.
165                 foreach (MtpObject mtpObject in _objectList)
166                 {
167                     mtpObject.Dispose();
168                     _objectList.Remove(mtpObject);
169                 }
170
171                 //Free unmanaged objects
172                 disposed = true;
173             }
174         }
175
176         internal int GetHandle()
177         {
178             return _storageHandle;
179         }
180
181         /// <summary>
182         /// Gets the root folder object.
183         /// </summary>
184         /// <returns>List of storage objects.</returns>
185         /// <feature>http://tizen.org/feature/network.mtp</feature>
186         /// <remarks>
187         /// http://tizen.org/privilege/mediastorage is needed if input or output path are relevant to media storage.
188         /// http://tizen.org/privilege/externalstorage is needed if input or output path are relevant to external storage.
189         /// </remarks>
190         /// <since_tizen> 4 </since_tizen>
191         public MtpObject GetRootObject()
192         {
193             _rootObject = new MtpObject(_deviceHandle, 0);
194
195             return _rootObject;
196         }
197
198         /// <summary>
199         /// Gets the list of objects.
200         /// </summary>
201         /// <param name="parentObject">The parent object handle. If parentHandle is 0, it means "root folder" of mtp storage.</param>
202         /// <param name="fileType">The file type what you want.</param>
203         /// <returns>List of objects.</returns>
204         /// <feature>http://tizen.org/feature/network.mtp</feature>
205         /// <remarks>
206         /// http://tizen.org/privilege/mediastorage is needed if input or output path are relevant to media storage.
207         /// http://tizen.org/privilege/externalstorage is needed if input or output path are relevant to external storage.
208         /// </remarks>
209         /// <exception cref="NotSupportedException">Thrown when Mtp is not supported.</exception>
210         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
211         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
212         /// <since_tizen> 4 </since_tizen>
213         public IEnumerable<MtpObject> GetObjectList(MtpObject parentObject, MtpFileType fileType)
214         {
215             IntPtr objectPtr;
216             int count = 0;
217
218             int ret = Interop.Mtp.GetObjectHandles(_deviceHandle, _storageHandle, parentObject.GetHandle(), (int)fileType, out objectPtr, out count);
219             if (ret != (int)MtpError.None)
220             {
221                 Log.Error(Globals.LogTag, "Failed to get object handle lists, Error - " + (MtpError)ret);
222                 MtpErrorFactory.ThrowMtpException(ret);
223             }
224
225             for (int i = 0; i < count; i++)
226             {
227                 int objectID = Marshal.ReadInt32(objectPtr);
228
229                 MtpObject objectItem = new MtpObject(_deviceHandle, objectID);
230                 _objectList.Add(objectItem);
231                 objectPtr += sizeof(int);
232             }
233
234             return _objectList;
235         }
236     }
237 }