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.
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
24 namespace Tizen.Content.MediaContent
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.
31 public class MediaFolder : ContentCollection
33 private IntPtr _folderHandle = IntPtr.Zero;
34 private bool _disposedValue = false;
35 internal IntPtr Handle
39 if (_folderHandle == IntPtr.Zero)
41 throw new ObjectDisposedException(nameof(MediaFolder));
48 /// The ID of the media folder. For each MediaFolder this id is unique.
50 /// <since_tizen> 3 </since_tizen>
55 IntPtr val = IntPtr.Zero;
58 MediaContentValidator.ThrowIfError(
59 Interop.Folder.GetFolderId(Handle, out val), "Failed to get value");
61 return Marshal.PtrToStringAnsi(val);
65 Interop.Libc.Free(val);
71 /// ParentId of the MediaFolder that is the ID of the upper media folder (parent folder).
73 /// <since_tizen> 3 </since_tizen>
74 public string ParentId
78 IntPtr val = IntPtr.Zero;
81 MediaContentValidator.ThrowIfError(
82 Interop.Folder.GetParentFolderId(Handle, out val), "Failed to get value");
84 return Marshal.PtrToStringAnsi(val);
88 Interop.Libc.Free(val);
94 /// The path of the media folder
96 /// <since_tizen> 3 </since_tizen>
97 public string FolderPath
101 IntPtr val = IntPtr.Zero;
104 MediaContentValidator.ThrowIfError(
105 Interop.Folder.GetPath(Handle, out val), "Failed to get value");
107 return Marshal.PtrToStringAnsi(val);
111 Interop.Libc.Free(val);
117 /// The name of the media folder
119 /// <since_tizen> 3 </since_tizen>
124 IntPtr val = IntPtr.Zero;
127 MediaContentValidator.ThrowIfError(
128 Interop.Folder.GetName(Handle, out val), "Failed to get value");
130 return Marshal.PtrToStringAnsi(val);
134 Interop.Libc.Free(val);
140 MediaContentValidator.ThrowIfError(
141 Interop.Folder.SetName(Handle, value), "Failed to set value");
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...
149 /// <since_tizen> 3 </since_tizen>
150 public ContentStorageType StorageType
154 ContentStorageType type;
155 MediaContentValidator.ThrowIfError(
156 Interop.Folder.GetStorageType(Handle, out type), "Failed to get value");
163 /// The storage id of the media folder
165 /// <since_tizen> 3 </since_tizen>
166 public string StorageId
170 IntPtr val = IntPtr.Zero;
173 MediaContentValidator.ThrowIfError(
174 Interop.Folder.GetStorageId(Handle, out val), "Failed to get value");
176 return Marshal.PtrToStringAnsi(val);
180 Interop.Libc.Free(val);
186 /// The modified date of the media folder
188 /// <since_tizen> 3 </since_tizen>
189 public DateTime ModifiedTime
194 MediaContentValidator.ThrowIfError(
195 Interop.Folder.GetModifiedTime(Handle, out date), "Failed to get value");
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.
206 /// <since_tizen> 3 </since_tizen>
212 MediaContentValidator.ThrowIfError(
213 Interop.Folder.GetOrder(Handle, out order), "Failed to get value");
220 MediaContentValidator.ThrowIfError(
221 Interop.Folder.SetOrder(Handle, value), "Failed to set value");
225 internal MediaFolder(IntPtr handle)
227 _folderHandle = handle;
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.
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)
240 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
241 MediaContentValidator.ThrowIfError(
242 Interop.Folder.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
252 public override void Dispose()
255 GC.SuppressFinalize(this);
258 protected virtual void Dispose(bool disposing)
262 if (_folderHandle != IntPtr.Zero)
264 Interop.Folder.Destroy(_folderHandle);
265 _folderHandle = IntPtr.Zero;
268 _disposedValue = true;
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.
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)
282 List<MediaInformation> mediaContents = new List<MediaInformation>();
283 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
285 Interop.Folder.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
287 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
288 MediaContentValidator.ThrowIfError(
289 Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone");
291 MediaContentType type;
292 Interop.MediaInformation.GetMediaType(newHandle, out type);
293 if (type == MediaContentType.Image)
295 Interop.ImageInformation.SafeImageInformationHandle imageInfo;
296 MediaContentValidator.ThrowIfError(
297 Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
299 mediaContents.Add(new ImageInformation(imageInfo, newHandle));
301 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
303 Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
304 MediaContentValidator.ThrowIfError(
305 Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
307 mediaContents.Add(new AudioInformation(audioInfo, newHandle));
309 else if (type == MediaContentType.Video)
311 Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
312 MediaContentValidator.ThrowIfError(
313 Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
315 mediaContents.Add(new VideoInformation(videoInfo, newHandle));
317 else if (type == MediaContentType.Others)
319 mediaContents.Add(new MediaInformation(newHandle));
324 MediaContentValidator.ThrowIfError(
325 Interop.Folder.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
327 return mediaContents;