Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / MediaFolder.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 Folder is used to organize media content files i.e. image, audio, video files, in the physical storage of the device.
28     /// The Media Folder API provides functions to get basic information about existing folders e.g. folder name, path and storage type.
29     /// It also provides functions to get information related to media items present in the folder.
30     /// </summary>
31     public class MediaFolder : ContentCollection
32     {
33         private IntPtr _folderHandle = IntPtr.Zero;
34         private bool _disposedValue = false;
35         internal IntPtr Handle
36         {
37             get
38             {
39                 if (_folderHandle == IntPtr.Zero)
40                 {
41                     throw new ObjectDisposedException(nameof(MediaFolder));
42                 }
43
44                 return _folderHandle;
45             }
46         }
47         /// <summary>
48         /// The ID of the media folder. For each MediaFolder this id is unique.
49         /// </summary>
50         /// <since_tizen> 3 </since_tizen>
51         public string Id
52         {
53             get
54             {
55                 IntPtr val = IntPtr.Zero;
56                 try
57                 {
58                     MediaContentValidator.ThrowIfError(
59                         Interop.Folder.GetFolderId(Handle, out val), "Failed to get value");
60
61                     return Marshal.PtrToStringAnsi(val);
62                 }
63                 finally
64                 {
65                     Interop.Libc.Free(val);
66                 }
67             }
68         }
69
70         /// <summary>
71         /// ParentId of the MediaFolder that is the ID of the upper media folder (parent folder).
72         /// </summary>
73         /// <since_tizen> 3 </since_tizen>
74         public string ParentId
75         {
76             get
77             {
78                 IntPtr val = IntPtr.Zero;
79                 try
80                 {
81                     MediaContentValidator.ThrowIfError(
82                         Interop.Folder.GetParentFolderId(Handle, out val), "Failed to get value");
83
84                     return Marshal.PtrToStringAnsi(val);
85                 }
86                 finally
87                 {
88                     Interop.Libc.Free(val);
89                 }
90             }
91         }
92
93         /// <summary>
94         /// The path of the media folder
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         public string FolderPath
98         {
99             get
100             {
101                 IntPtr val = IntPtr.Zero;
102                 try
103                 {
104                     MediaContentValidator.ThrowIfError(
105                         Interop.Folder.GetPath(Handle, out val), "Failed to get value");
106
107                     return Marshal.PtrToStringAnsi(val);
108                 }
109                 finally
110                 {
111                     Interop.Libc.Free(val);
112                 }
113             }
114         }
115
116         /// <summary>
117         /// The name of the media folder
118         /// </summary>
119         /// <since_tizen> 3 </since_tizen>
120         public string Name
121         {
122             get
123             {
124                 IntPtr val = IntPtr.Zero;
125                 try
126                 {
127                     MediaContentValidator.ThrowIfError(
128                         Interop.Folder.GetName(Handle, out val), "Failed to get value");
129
130                     return Marshal.PtrToStringAnsi(val);
131                 }
132                 finally
133                 {
134                     Interop.Libc.Free(val);
135                 }
136             }
137
138             set
139             {
140                 MediaContentValidator.ThrowIfError(
141                     Interop.Folder.SetName(Handle, value), "Failed to set value");
142             }
143         }
144
145         /// <summary>
146         /// The storage type of the media folder.
147         /// Storage types give information about the location of storage like Internal memory, USB or External Storage etc...
148         /// </summary>
149         /// <since_tizen> 3 </since_tizen>
150         public ContentStorageType StorageType
151         {
152             get
153             {
154                 ContentStorageType type;
155                 MediaContentValidator.ThrowIfError(
156                     Interop.Folder.GetStorageType(Handle, out type), "Failed to get value");
157
158                 return type;
159             }
160         }
161
162         /// <summary>
163         /// The storage id of the media folder
164         /// </summary>
165         /// <since_tizen> 3 </since_tizen>
166         public string StorageId
167         {
168             get
169             {
170                 IntPtr val = IntPtr.Zero;
171                 try
172                 {
173                     MediaContentValidator.ThrowIfError(
174                         Interop.Folder.GetStorageId(Handle, out val), "Failed to get value");
175
176                     return Marshal.PtrToStringAnsi(val);
177                 }
178                 finally
179                 {
180                     Interop.Libc.Free(val);
181                 }
182             }
183         }
184
185         /// <summary>
186         /// The modified date of the media folder
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         public DateTime ModifiedTime
190         {
191             get
192             {
193                 DateTime date;
194                 MediaContentValidator.ThrowIfError(
195                     Interop.Folder.GetModifiedTime(Handle, out date), "Failed to get value");
196
197                 return date;
198             }
199         }
200
201         /// <summary>
202         /// The folder order value. Get/Set the folder viewing order.
203         /// Default Order value is zero.
204         /// If you set the order value for each folder, you can sort in ascending or descending order as the set order values using the filter.
205         /// </summary>
206         /// <since_tizen> 3 </since_tizen>
207         public int Order
208         {
209             get
210             {
211                 int order;
212                 MediaContentValidator.ThrowIfError(
213                     Interop.Folder.GetOrder(Handle, out order), "Failed to get value");
214
215                 return order;
216             }
217
218             set
219             {
220                 MediaContentValidator.ThrowIfError(
221                     Interop.Folder.SetOrder(Handle, value), "Failed to set value");
222             }
223         }
224
225         internal MediaFolder(IntPtr handle)
226         {
227             _folderHandle = handle;
228         }
229
230         /// <summary>
231         /// Gets the count of media files for the passed filter in the given folder from the media database.
232         /// If NULL is passed to the filter, no filtering is applied.
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         /// <param name="filter">ContentFilter used to match media content from teh media database.</param>
236         /// <returns>The number of media contents matching the filter passed</returns>
237         public override int GetMediaInformationCount(ContentFilter filter)
238         {
239             int mediaCount;
240             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
241             MediaContentValidator.ThrowIfError(
242                 Interop.Folder.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
243
244             return mediaCount;
245         }
246
247         ~MediaFolder()
248         {
249             Dispose(false);
250         }
251
252         public override void Dispose()
253         {
254             Dispose(true);
255             GC.SuppressFinalize(this);
256         }
257
258         protected virtual void Dispose(bool disposing)
259         {
260             if (!_disposedValue)
261             {
262                 if (_folderHandle != IntPtr.Zero)
263                 {
264                     Interop.Folder.Destroy(_folderHandle);
265                     _folderHandle = IntPtr.Zero;
266                 }
267
268                 _disposedValue = true;
269             }
270         }
271
272         /// <summary>
273         /// Iterates through the media files with an filter in the given folder from the media database.
274         /// This function gets all media files associated with the given folder and meeting desired filter option.
275         /// If NULL is passed to the filter, no filtering is applied.
276         /// </summary>
277         /// <since_tizen> 3 </since_tizen>
278         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
279         /// <returns>List of content media items matching the passed filter</returns>
280         public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
281         {
282             List<MediaInformation> mediaContents = new List<MediaInformation>();
283             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
284
285             Interop.Folder.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
286             {
287                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
288                 MediaContentValidator.ThrowIfError(
289                     Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone");
290
291                 MediaContentType type;
292                 Interop.MediaInformation.GetMediaType(newHandle, out type);
293                 if (type == MediaContentType.Image)
294                 {
295                     Interop.ImageInformation.SafeImageInformationHandle imageInfo;
296                     MediaContentValidator.ThrowIfError(
297                         Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
298
299                     mediaContents.Add(new ImageInformation(imageInfo, newHandle));
300                 }
301                 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
302                 {
303                     Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
304                     MediaContentValidator.ThrowIfError(
305                         Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
306
307                     mediaContents.Add(new AudioInformation(audioInfo, newHandle));
308                 }
309                 else if (type == MediaContentType.Video)
310                 {
311                     Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
312                     MediaContentValidator.ThrowIfError(
313                         Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
314
315                     mediaContents.Add(new VideoInformation(videoInfo, newHandle));
316                 }
317                 else if (type == MediaContentType.Others)
318                 {
319                     mediaContents.Add(new MediaInformation(newHandle));
320                 }
321
322                 return true;
323             };
324             MediaContentValidator.ThrowIfError(
325                 Interop.Folder.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
326
327             return mediaContents;
328         }
329     }
330 }