Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / Storage.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
18
19 using System;
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
23
24 namespace Tizen.Content.MediaContent
25 {
26     /// <summary>
27     /// A Storage allows you to manage external storage.
28     /// The system generates the storage id when the external storage is added.And the system manages the media information in each of the storage by using storage id.
29     /// So you can get the information from the storage that you want to view.
30     /// </summary>
31     public class Storage : ContentCollection
32     {
33         private IntPtr _storageHandle = IntPtr.Zero;
34         private IntPtr Handle
35         {
36             get
37             {
38                 if (_storageHandle == IntPtr.Zero)
39                 {
40                     throw new ObjectDisposedException(nameof(Storage));
41                 }
42
43                 return _storageHandle;
44             }
45         }
46         /// <summary>
47         /// The storage id of the media storage
48         /// </summary>
49         /// <since_tizen> 3 </since_tizen>
50         public string Id
51         {
52             get
53             {
54                 IntPtr val = IntPtr.Zero;
55                 try
56                 {
57                     MediaContentValidator.ThrowIfError(
58                         Interop.Storage.GetId(Handle, out val), "Failed to get value");
59
60                     return Marshal.PtrToStringAnsi(val);
61                 }
62                 finally
63                 {
64                     Interop.Libc.Free(val);
65                 }
66             }
67         }
68
69         /// <summary>
70         /// The storage path of the media storage
71         /// </summary>
72         /// <since_tizen> 3 </since_tizen>
73         public string StoragePath
74         {
75             get
76             {
77                 IntPtr val = IntPtr.Zero;
78                 try
79                 {
80                     MediaContentValidator.ThrowIfError(
81                         Interop.Storage.GetPath(Handle, out val), "Failed to get value");
82
83                     return Marshal.PtrToStringAnsi(val);
84                 }
85                 finally
86                 {
87                     Interop.Libc.Free(val);
88                 }
89             }
90         }
91
92         /// <summary>
93         /// The storage name of the media storage
94         /// </summary>
95         /// <since_tizen> 3 </since_tizen>
96         public string Name
97         {
98             get
99             {
100                 IntPtr val = IntPtr.Zero;
101                 try
102                 {
103                     MediaContentValidator.ThrowIfError(
104                         Interop.Storage.GetName(Handle, out val), "Failed to get value");
105
106                     return Marshal.PtrToStringAnsi(val);
107                 }
108                 finally
109                 {
110                     Interop.Libc.Free(val);
111                 }
112             }
113         }
114
115         /// <summary>
116         /// The storage type of the media storage
117         /// </summary>
118         /// <since_tizen> 3 </since_tizen>
119         public ContentStorageType StorageType
120         {
121             get
122             {
123                 ContentStorageType storageType;
124                 MediaContentValidator.ThrowIfError(
125                     Interop.Storage.GetType(Handle, out storageType), "Failed to get value");
126
127                 return storageType;
128             }
129         }
130
131         internal Storage(IntPtr handle)
132         {
133             _storageHandle = handle;
134         }
135
136         /// <summary>
137         /// Gets the count of media files for the passed filter in the given storage from the media database.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
141         /// <returns>The number of media contents matching the filter passed</returns>
142         public override int GetMediaInformationCount(ContentFilter filter)
143         {
144             int mediaCount;
145             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
146             MediaContentValidator.ThrowIfError(
147                 Interop.Storage.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
148
149             return mediaCount;
150         }
151
152         public override void Dispose()
153         {
154             if (_storageHandle != IntPtr.Zero)
155             {
156                 Interop.Storage.Destroy(_storageHandle);
157                 _storageHandle = IntPtr.Zero;
158             }
159         }
160
161         /// <summary>
162         /// Iterates through the media files with an optional filter in the given storage from the media database.
163         /// This function gets all media files associated with the given storage and meeting desired filter option.
164         /// If NULL is passed to the filter, no filtering is applied.
165         /// </summary>
166         /// <since_tizen> 3 </since_tizen>
167         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
168         /// <returns>List of content media items matching the passed filter</returns>
169         public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
170         {
171             List<MediaInformation> mediaContents = new List<MediaInformation>();
172
173             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
174             Interop.Storage.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
175             {
176                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
177                 MediaContentValidator.ThrowIfError(
178                     Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
179
180                 MediaContentType type;
181                 Interop.MediaInformation.GetMediaType(newHandle, out type);
182                 if (type == MediaContentType.Image)
183                 {
184                     Interop.ImageInformation.SafeImageInformationHandle imageInfo;
185                     MediaContentValidator.ThrowIfError(
186                         Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
187
188                     mediaContents.Add(new ImageInformation(imageInfo, newHandle));
189                 }
190                 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
191                 {
192                     Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
193                     MediaContentValidator.ThrowIfError(
194                         Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
195
196                     mediaContents.Add(new AudioInformation(audioInfo, newHandle));
197                 }
198                 else if (type == MediaContentType.Video)
199                 {
200                     Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
201                     MediaContentValidator.ThrowIfError(
202                         Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
203
204                     mediaContents.Add(new VideoInformation(videoInfo, newHandle));
205                 }
206                 else if (type == MediaContentType.Others)
207                 {
208                     mediaContents.Add(new MediaInformation(newHandle));
209                 }
210
211                 return true;
212             };
213             MediaContentValidator.ThrowIfError(
214                 Interop.Storage.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
215
216             return mediaContents;
217         }
218     }
219 }