2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 namespace Tizen.Network.Mtp
24 /// A class for Mtp Storage information. It allows applications to handle storage information.
26 /// <since_tizen> 5 </since_tizen>
27 public class MtpStorage : IDisposable
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;
37 /// Gets the description of the storage information.
39 /// <value>Description of storage.</value>
40 /// <since_tizen> 5 </since_tizen>
41 public string Description
46 int ret = Interop.Mtp.StorageInformation.GetDescription(_deviceHandle, _storageHandle, out strPtr);
47 if (ret != (int)MtpError.None)
49 Log.Error(Globals.LogTag, "Failed to get description, Error - " + (MtpError)ret);
52 return Marshal.PtrToStringAnsi(strPtr);
57 /// Gets the free space of the storage information in bytes.
59 /// <value>Free space of storage(bytes).</value>
60 /// <since_tizen> 5 </since_tizen>
61 public UInt64 FreeSpace
66 int ret = Interop.Mtp.StorageInformation.GetFreeSpace(_deviceHandle, _storageHandle, out freeSpace);
67 if (ret != (int)MtpError.None)
69 Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
76 /// Gets the max capacity of the storage information in bytes.
78 /// <value>Max capacity of storage(bytes).</value>
79 /// <since_tizen> 5 </since_tizen>
80 public UInt64 MaxCapacity
85 int ret = Interop.Mtp.StorageInformation.GetMaxCapacity(_deviceHandle, _storageHandle, out maxCapacity);
86 if (ret != (int)MtpError.None)
88 Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
95 /// Gets the storage type of the storage information.
97 /// <value>Type of storage.</value>
98 /// <since_tizen> 5 </since_tizen>
99 public MtpStorageType StorageType
104 int ret = Interop.Mtp.StorageInformation.GetStorageType(_deviceHandle, _storageHandle, out storageType);
105 if (ret != (int)MtpError.None)
107 Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
109 return (MtpStorageType)storageType;
114 /// Gets the volume identifier of the storage information.
116 /// <value>Volume identifier of stroage.</value>
117 /// <since_tizen> 5 </since_tizen>
118 public string VolumeIdentifier
123 int ret = Interop.Mtp.StorageInformation.GetVolumeIdentifier(_deviceHandle, _storageHandle, out strPtr);
124 if (ret != (int)MtpError.None)
126 Log.Error(Globals.LogTag, "Failed to get volume identifier, Error - " + (MtpError)ret);
129 return Marshal.PtrToStringAnsi(strPtr);
133 internal MtpStorage(int deviceHandle, int storageHandle)
135 _deviceHandle = deviceHandle;
136 _storageHandle = storageHandle;
144 public void Dispose()
147 GC.SuppressFinalize(this);
150 private void Dispose(bool disposing)
157 // Free managed objects.
158 foreach (MtpObject mtpObject in _objectList)
161 _objectList.Remove(mtpObject);
164 //Free unmanaged objects
169 internal int GetHandle()
171 return _storageHandle;
175 /// Gets the root folder object.
177 /// <returns>List of storage objects.</returns>
178 /// <feature>http://tizen.org/feature/network.mtp</feature>
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.
183 /// <since_tizen> 5 </since_tizen>
184 public MtpObject GetRootObject()
186 _rootObject = new MtpObject(_deviceHandle, 0);
192 /// Gets the list of objects.
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>
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.
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)
211 int ret = Interop.Mtp.GetObjectHandles(_deviceHandle, _storageHandle, parentObject.GetHandle(), (int)fileType, out objectPtr, out count);
212 if (ret != (int)MtpError.None)
214 Log.Error(Globals.LogTag, "Failed to get object handle lists, Error - " + (MtpError)ret);
215 MtpErrorFactory.ThrowMtpException(ret);
218 for (int i = 0; i < count; i++)
220 int objectID = Marshal.ReadInt32(objectPtr);
222 MtpObject objectItem = new MtpObject(_deviceHandle, objectID);
223 _objectList.Add(objectItem);
224 objectPtr += sizeof(int);