Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / Album.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     /// An album is a logical collection or grouping of related audio files. It is also used for filtering media items.
28     /// The Media Album API allows to manage media albums which contains all video and audio items from the same album.
29     /// </summary>
30     public class Album : ContentCollection
31     {
32         private IntPtr _albumHandle = IntPtr.Zero;
33
34         private IntPtr Handle
35         {
36             get
37             {
38                 if (_albumHandle == IntPtr.Zero)
39                 {
40                     throw new ObjectDisposedException(nameof(Album));
41                 }
42
43                 return _albumHandle;
44             }
45         }
46
47         /// <summary>
48         /// The media album ID
49         /// </summary>
50         /// <since_tizen> 3 </since_tizen>
51         public int Id
52         {
53             get
54             {
55                 int id = 0;
56                 MediaContentValidator.ThrowIfError(
57                     Interop.Group.MediaAlbumGetAlbumId(Handle, out id), "Failed to get value");
58
59                 return id;
60             }
61         }
62
63         /// <summary>
64         /// The name of the media artist
65         /// If the media content has no album info, the property returns empty string.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         public string Artist
69         {
70             get
71             {
72                 IntPtr val = IntPtr.Zero;
73                 try
74                 {
75                     MediaContentValidator.ThrowIfError(
76                         Interop.Group.MediaAlbumGetArtist(Handle, out val), "Failed to get value");
77
78                     return Marshal.PtrToStringAnsi(val);
79                 }
80                 finally
81                 {
82                     Interop.Libc.Free(val);
83                 }
84             }
85         }
86
87         /// <summary>
88         /// The path of the media album art
89         /// </summary>
90         /// <since_tizen> 3 </since_tizen>
91         public string Art
92         {
93             get
94             {
95                 IntPtr val = IntPtr.Zero;
96                 try
97                 {
98                     MediaContentValidator.ThrowIfError(
99                         Interop.Group.MediaAlbumGetAlbumArt(Handle, out val), "Failed to get value");
100
101                     return Marshal.PtrToStringAnsi(val);
102                 }
103                 finally
104                 {
105                     Interop.Libc.Free(val);
106                 }
107             }
108         }
109
110         /// <summary>
111         /// The name of the media album
112         /// If the media content has no album info, the property returns empty string.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         public string Name
116         {
117             get
118             {
119                 IntPtr val = IntPtr.Zero;
120                 try
121                 {
122                     MediaContentValidator.ThrowIfError(
123                         Interop.Group.MediaAlbumGetName(Handle, out val), "Failed to get value");
124
125                     return Marshal.PtrToStringAnsi(val);
126                 }
127                 finally
128                 {
129                     Interop.Libc.Free(val);
130                 }
131             }
132         }
133
134         internal Album(IntPtr handle)
135         {
136             _albumHandle = handle;
137         }
138
139         /// <summary>
140         /// Gets the number of MediaInformation Items for the given album present in the media database.
141         /// If NULL is passed to the filter, no filtering is applied.
142         /// </summary>
143         /// <since_tizen> 3 </since_tizen>
144         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
145         /// <returns>The number of media contents matching the filter passed</returns>
146         public override int GetMediaInformationCount(ContentFilter filter)
147         {
148             int mediaCount = 0;
149             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
150
151             MediaContentValidator.ThrowIfError(
152                 Interop.Group.MediaAlbumGetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
153
154             return mediaCount;
155         }
156
157         public override void Dispose()
158         {
159             if (_albumHandle != IntPtr.Zero)
160             {
161                 Interop.Group.MediaAlbumDestroy(_albumHandle);
162                 _albumHandle = IntPtr.Zero;
163             }
164         }
165
166         /// <summary>
167         /// Iterates through the media files with a filter in the given media album from the media database.
168         /// This function gets all media files associated with the given media album and meeting desired filter option.
169         /// If NULL is passed to the filter, no filtering is applied.
170         /// </summary>
171         /// <since_tizen> 3 </since_tizen>
172         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
173         /// <returns>List of content media items matching the passed filter</returns>
174         public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
175         {
176             List<MediaInformation> mediaContents = new List<MediaInformation>();
177
178             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
179             Interop.Group.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
180             {
181                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
182                 MediaContentValidator.ThrowIfError(
183                     Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone");
184
185                 MediaContentType type;
186                 Interop.MediaInformation.GetMediaType(newHandle, out type);
187                 if (type == MediaContentType.Image)
188                 {
189                     Interop.ImageInformation.SafeImageInformationHandle imageInfo;
190                     MediaContentValidator.ThrowIfError(
191                         Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
192
193                     mediaContents.Add(new ImageInformation(imageInfo, newHandle));
194                 }
195                 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
196                 {
197                     Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
198                     MediaContentValidator.ThrowIfError(
199                         Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
200
201                     mediaContents.Add(new AudioInformation(audioInfo, newHandle));
202                 }
203                 else if (type == MediaContentType.Video)
204                 {
205                     Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
206                     MediaContentValidator.ThrowIfError(
207                         Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
208
209                     mediaContents.Add(new VideoInformation(videoInfo, newHandle));
210                 }
211                 else if (type == MediaContentType.Others)
212                 {
213                     mediaContents.Add(new MediaInformation(newHandle));
214                 }
215
216                 return true;
217             };
218             MediaContentValidator.ThrowIfError(
219                 Interop.Group.MediaAlbumForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
220
221             return mediaContents;
222         }
223     }
224 }