[C# MTP] Add initial codes
[platform/core/csapi/mtp.git] / 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 informations. It allows applications to handle storage informations.
25     /// </summary>
26     public class MtpStorage : IDisposable
27     {
28         private int _deviceHandle = -1;
29         private int _storageHandle = -1;
30         private bool disposed = false;
31         private List<int> _objectHandleList = new List<int>();
32         private List<MtpObject> _objectList = new List<MtpObject>();
33         //private int _objectHandle = 0;
34
35         /// <summary>
36         /// The description.
37         /// </summary>
38         public string Description
39         {
40             get
41             {
42                 IntPtr strPtr;
43                 int ret = Interop.Mtp.StorageInformation.GetDescription(_deviceHandle, _storageHandle, out strPtr);
44                 if (ret != (int)MtpError.None)
45                 {
46                     Log.Error(Globals.LogTag, "Failed to get description, Error - " + (MtpError)ret);
47                     return "";
48                 }
49                 return Marshal.PtrToStringAnsi(strPtr);
50             }
51         }
52
53         /// <summary>
54         /// The free space.
55         /// </summary>
56         public UInt64 FreeSpace
57         {
58             get
59             {
60                 UInt64 freeSpace;
61                 int ret = Interop.Mtp.StorageInformation.GetFreeSpace(_deviceHandle, _storageHandle, out freeSpace);
62                 if (ret != (int)MtpError.None)
63                 {
64                     Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
65                 }
66                 return freeSpace;
67             }
68         }
69
70         /// <summary>
71         /// The max capacity.
72         /// </summary>
73         public UInt64 MaxCapacity
74         {
75             get
76             {
77                 UInt64 maxCapacity;
78                 int ret = Interop.Mtp.StorageInformation.GetMaxCapacity(_deviceHandle, _storageHandle, out maxCapacity);
79                 if (ret != (int)MtpError.None)
80                 {
81                     Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
82                 }
83                 return maxCapacity;
84             }
85         }
86
87         /// <summary>
88         /// The storage type.
89         /// </summary>
90         public MtpStorageType StorageType
91         {
92             get
93             {
94                 int storageType;
95                 int ret = Interop.Mtp.StorageInformation.GetStorageType(_deviceHandle, _storageHandle, out storageType);
96                 if (ret != (int)MtpError.None)
97                 {
98                     Log.Error(Globals.LogTag, "Failed to get free space, Error - " + (MtpError)ret);
99                 }
100                 return (MtpStorageType)storageType;
101             }
102         }
103
104         /// <summary>
105         /// The volume identifier.
106         /// </summary>
107         public string VolumeIdentifier
108         {
109             get
110             {
111                 IntPtr strPtr;
112                 int ret = Interop.Mtp.StorageInformation.GetVolumeIdentifier(_deviceHandle, _storageHandle, out strPtr);
113                 if (ret != (int)MtpError.None)
114                 {
115                     Log.Error(Globals.LogTag, "Failed to get volume identifier, Error - " + (MtpError)ret);
116                     return "";
117                 }
118                 return Marshal.PtrToStringAnsi(strPtr);
119             }
120         }
121
122         internal MtpStorage(int deviceHandle, int storageHandle)
123         {
124             _deviceHandle = deviceHandle;
125             _storageHandle = storageHandle;
126         }
127
128         ~MtpStorage()
129         {
130             Dispose(false);
131         }
132
133         public void Dispose()
134         {
135             Dispose(true);
136             GC.SuppressFinalize(this);
137         }
138
139         private void Dispose(bool disposing)
140         {
141             if (disposed)
142                 return;
143
144             if (disposing)
145             {
146                 // Free managed objects.
147 /*                foreach (SmartcardChannel channel in _basicChannelList)
148                 {
149                     channel.Dispose();
150                     _basicChannelList.Remove(channel);
151                 }
152
153                 foreach (SmartcardChannel channel in _logicalChannelList)
154                 {
155                     channel.Dispose();
156                     _logicalChannelList.Remove(channel);
157                 }
158 */            }
159             //Free unmanaged objects
160             disposed = true;
161         }
162
163         internal int GetHandle()
164         {
165             return _storageHandle;
166         }
167
168         /// <summary>
169         /// Gets the list of storages.
170         /// </summary>
171         /// <returns>List of storage objects.</returns>
172         public IEnumerable<MtpObject> GetObjectHandles(int parentHandle, MtpFileType fileType)
173         {
174             IntPtr objectPtr;
175             int count = 0;
176
177             int ret = Interop.Mtp.GetObjectHandles(_deviceHandle, _storageHandle, parentHandle, (int)fileType, out objectPtr, out count);
178             if (ret != (int)MtpError.None)
179             {
180                 Log.Error(Globals.LogTag, "Failed to get object handle lists, Error - " + (MtpError)ret);
181                 MtpErrorFactory.ThrowMtpException(ret);
182             }
183
184             for (int i = 0; i < count; i++)
185             {
186                 int objectID = Marshal.ReadInt32(objectPtr);
187
188                 MtpObject objectItem = new MtpObject(_deviceHandle, objectID);
189                 _objectList.Add(objectItem);
190                 objectPtr += sizeof(int);
191             }
192
193             return _objectList;
194         }
195     }
196 }