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> 4 </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> 4 </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> 4 </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> 4 </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> 4 </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> 4 </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;
140 /// MtpStorage destructor.
150 /// <since_tizen> 4 </since_tizen>
151 public void Dispose()
154 GC.SuppressFinalize(this);
157 private void Dispose(bool disposing)
164 // Free managed objects.
165 foreach (MtpObject mtpObject in _objectList)
168 _objectList.Remove(mtpObject);
171 //Free unmanaged objects
176 internal int GetHandle()
178 return _storageHandle;
182 /// Gets the root folder object.
184 /// <returns>List of storage objects.</returns>
185 /// <feature>http://tizen.org/feature/network.mtp</feature>
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.
190 /// <since_tizen> 4 </since_tizen>
191 public MtpObject GetRootObject()
193 _rootObject = new MtpObject(_deviceHandle, 0);
199 /// Gets the list of objects.
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>
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.
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)
218 int ret = Interop.Mtp.GetObjectHandles(_deviceHandle, _storageHandle, parentObject.GetHandle(), (int)fileType, out objectPtr, out count);
219 if (ret != (int)MtpError.None)
221 Log.Error(Globals.LogTag, "Failed to get object handle lists, Error - " + (MtpError)ret);
222 MtpErrorFactory.ThrowMtpException(ret);
225 for (int i = 0; i < count; i++)
227 int objectID = Marshal.ReadInt32(objectPtr);
229 MtpObject objectItem = new MtpObject(_deviceHandle, objectID);
230 _objectList.Add(objectItem);
231 objectPtr += sizeof(int);