[Media Content]Implementation. Review Comments added.
[platform/core/csapi/media-content.git] / Tizen.Content / Tizen.Content.MediaContent / Group.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Threading.Tasks;
4 /// Copyright 2016 by Samsung Electronics, Inc.,
5 ///
6 /// This software is the confidential and proprietary information
7 /// of Samsung Electronics, Inc.("Confidential Information"). You
8 /// shall not disclose such Confidential Information and shall use
9 /// it only in accordance with the terms of the license agreement
10 /// you entered into with Samsung.
11
12
13 namespace Tizen.Content.MediaContent
14 {
15     /// <summary>
16     /// A Media Group represents logical grouping of media files with respect to their group name.
17     /// It is also used for filtering media items.
18     /// </summary>
19     public class Group : ContentCollection
20     {
21         private readonly string _groupName;
22         private bool _disposedValue = false;
23
24         /// <summary>
25         /// The name of the media group
26         /// </summary>
27         public string Name
28         {
29             get { return _groupName; }
30         }
31
32         internal Group(string name)
33         {
34             _groupName = name;
35         }
36
37         /// <summary>
38         /// Dispose API for closing the internal resources.
39         /// This function can be used to stop all effects started by Vibrate().
40         /// </summary>
41         public override void Dispose()
42         {
43             Dispose(true);
44             GC.SuppressFinalize(this);
45         }
46
47         protected virtual void Dispose(bool disposing)
48         {
49             if (!_disposedValue)
50             {
51                 _disposedValue = true;
52             }
53         }
54
55         /// <summary>
56         /// Gets the count of the media info for the given media group present in the media database.
57         /// </summary>
58         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
59         /// <returns>The number of media contents matching the filter passed</returns>
60         public override int GetMediaInformationCount(ContentFilter filter)
61         {
62             int mediaCount;
63             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
64             MediaContentError res = (MediaContentError)Interop.Group.GetMediaCountFromDb(Name, filter.GroupType, handle, out mediaCount);
65             if (res != MediaContentError.None)
66             {
67                 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media count for the group");
68             }
69             return mediaCount;
70         }
71
72
73         /// <summary>
74         /// Iterates through the media files with an optional filter in the given group from the media database.
75         /// This function gets all media files associated with the given group and meeting desired filter option.
76         /// If NULL is passed to the filter, no filtering is applied.
77         /// </summary>
78         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
79         /// <returns>List of content media items matching the passed filter</returns>
80         public override Task<IEnumerable<MediaInformation>> GetMediaInformationsAsync(ContentFilter filter)
81         {
82             var tcs = new TaskCompletionSource<IEnumerable<MediaInformation>>();
83             List<MediaInformation> mediaContents = new List<MediaInformation>();
84             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
85             MediaContentError res;
86             Interop.Group.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
87             {
88                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
89                 res = (MediaContentError)Interop.MediaInformation.Clone(out newHandle, mediaHandle);
90                 MediaInformation info = new MediaInformation(newHandle);
91                 mediaContents.Add(info);
92                 return true;
93             };
94             res = (MediaContentError)Interop.Group.ForeachMediaFromDb(Name, filter.GroupType, handle, callback, IntPtr.Zero);
95             if (res != MediaContentError.None)
96             {
97                 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media information for the group");
98             }
99             tcs.TrySetResult(mediaContents);
100             return tcs.Task;
101         }
102     }
103 }