Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / Group.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 using System;
19 using System.Collections.Generic;
20 using System.Threading.Tasks;
21
22 namespace Tizen.Content.MediaContent
23 {
24     /// <summary>
25     /// A Media Group represents logical grouping of media files with respect to their group name.
26     /// It is also used for filtering media items.
27     /// </summary>
28     public class Group : ContentCollection
29     {
30         private readonly string _groupName;
31         private readonly MediaGroupType _groupType;
32         private bool _disposedValue = false;
33
34         /// <summary>
35         /// The name of the media group
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         public string Name
39         {
40             get { return _groupName; }
41         }
42
43         internal Group(string name, MediaGroupType groupType)
44         {
45             _groupName = name;
46             _groupType = groupType;
47         }
48
49         public override void Dispose()
50         {
51             Dispose(true);
52             GC.SuppressFinalize(this);
53         }
54
55         protected virtual void Dispose(bool disposing)
56         {
57             if (!_disposedValue)
58             {
59                 _disposedValue = true;
60             }
61         }
62
63         /// <summary>
64         /// Gets the count of the media info for the given media group present in the media database.
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
68         /// <returns>The number of media contents matching the filter passed</returns>
69         public override int GetMediaInformationCount(ContentFilter filter)
70         {
71             int mediaCount = 0;
72             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
73             MediaContentValidator.ThrowIfError(
74                 Interop.Group.GetMediaCountFromDb(Name, _groupType, handle, out mediaCount), "Failed to GetMediaCountFromDb");
75
76             return mediaCount;
77         }
78
79
80         /// <summary>
81         /// Iterates through the media files with an optional filter in the given group from the media database.
82         /// This function gets all media files associated with the given group and meeting desired filter option.
83         /// If NULL is passed to the filter, no filtering is applied.
84         /// </summary>
85         /// <since_tizen> 3 </since_tizen>
86         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
87         /// <returns>List of content media items matching the passed filter</returns>
88         public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
89         {
90             List<MediaInformation> mediaContents = new List<MediaInformation>();
91             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
92             Interop.Group.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
93             {
94                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
95                 MediaContentValidator.ThrowIfError(
96                     Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone MediaInformation instance");
97
98                 MediaContentType type;
99                 Interop.MediaInformation.GetMediaType(newHandle, out type);
100                 if (type == MediaContentType.Image)
101                 {
102                     Interop.ImageInformation.SafeImageInformationHandle imageInfo;
103                     MediaContentValidator.ThrowIfError(
104                         Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
105
106                     mediaContents.Add(new ImageInformation(imageInfo, newHandle));
107                 }
108                 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
109                 {
110                     Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
111                     MediaContentValidator.ThrowIfError(
112                         Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
113
114                     mediaContents.Add(new AudioInformation(audioInfo, newHandle));
115                 }
116                 else if (type == MediaContentType.Video)
117                 {
118                     Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
119                     MediaContentValidator.ThrowIfError(
120                         Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
121
122                     mediaContents.Add(new VideoInformation(videoInfo, newHandle));
123                 }
124                 else if (type == MediaContentType.Others)
125                 {
126                     mediaContents.Add(new MediaInformation(newHandle));
127                 }
128
129                 return true;
130             };
131             MediaContentValidator.ThrowIfError(
132                 Interop.Group.ForeachMediaFromDb(Name, _groupType, handle, callback, IntPtr.Zero), "Failed to get media information for the group");
133
134             return mediaContents;
135         }
136     }
137 }