Release 4.0.0-preview1-00258
[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> 5 </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> 5 </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> 5 </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> 5 </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> 5 </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> 5 </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         ~MtpStorage()
140         {
141             Dispose(false);
142         }
143
144         public void Dispose()
145         {
146             Dispose(true);
147             GC.SuppressFinalize(this);
148         }
149
150         private void Dispose(bool disposing)
151         {
152             if (disposed)
153                 return;
154
155             if (disposing)
156             {
157                 // Free managed objects.
158                 foreach (MtpObject mtpObject in _objectList)
159                 {
160                     mtpObject.Dispose();
161                     _objectList.Remove(mtpObject);
162                 }
163
164                 //Free unmanaged objects
165                 disposed = true;
166             }
167         }
168
169         internal int GetHandle()
170         {
171             return _storageHandle;
172         }
173
174         /// <summary>
175         /// Gets the root folder object.
176         /// </summary>
177         /// <returns>List of storage objects.</returns>
178         /// <feature>http://tizen.org/feature/network.mtp</feature>
179         /// <remarks>
180         /// http://tizen.org/privilege/mediastorage is needed if input or output path are relevant to media storage.
181         /// http://tizen.org/privilege/externalstorage is needed if input or output path are relevant to external storage.
182         /// </remarks>
183         /// <since_tizen> 5 </since_tizen>
184         public MtpObject GetRootObject()
185         {
186             _rootObject = new MtpObject(_deviceHandle, 0);
187
188             return _rootObject;
189         }
190
191         /// <summary>
192         /// Gets the list of objects.
193         /// </summary>
194         /// <param name="parentObject">The parent object handle. If parentHandle is 0, it means "root folder" of mtp storage.</param>
195         /// <param name="fileType">The file type what you want.</param>
196         /// <returns>List of objects.</returns>
197         /// <feature>http://tizen.org/feature/network.mtp</feature>
198         /// <remarks>
199         /// http://tizen.org/privilege/mediastorage is needed if input or output path are relevant to media storage.
200         /// http://tizen.org/privilege/externalstorage is needed if input or output path are relevant to external storage.
201         /// </remarks>
202         /// <exception cref="NotSupportedException">Thrown when Mtp is not supported.</exception>
203         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
204         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
205         /// <since_tizen> 5 </since_tizen>
206         public IEnumerable<MtpObject> GetObjectList(MtpObject parentObject, MtpFileType fileType)
207         {
208             IntPtr objectPtr;
209             int count = 0;
210
211             int ret = Interop.Mtp.GetObjectHandles(_deviceHandle, _storageHandle, parentObject.GetHandle(), (int)fileType, out objectPtr, out count);
212             if (ret != (int)MtpError.None)
213             {
214                 Log.Error(Globals.LogTag, "Failed to get object handle lists, Error - " + (MtpError)ret);
215                 MtpErrorFactory.ThrowMtpException(ret);
216             }
217
218             for (int i = 0; i < count; i++)
219             {
220                 int objectID = Marshal.ReadInt32(objectPtr);
221
222                 MtpObject objectItem = new MtpObject(_deviceHandle, objectID);
223                 _objectList.Add(objectItem);
224                 objectPtr += sizeof(int);
225             }
226
227             return _objectList;
228         }
229     }
230 }