Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / Tag.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.Runtime.InteropServices;
21 using System.Threading.Tasks;
22
23 namespace Tizen.Content.MediaContent
24 {
25     /// <summary>
26     /// A Tag is a special piece of information that may be associated with media content items.
27     /// Tagging allows a user to organize large number of items into logical groups providing a simplified and faster way of accessing media content items.
28     /// </summary>
29     public class Tag : ContentCollection
30     {
31         private IntPtr _tagHandle = IntPtr.Zero;
32         private string _tagName = "";
33
34         internal IntPtr Handle
35         {
36             get
37             {
38                 if (_tagHandle == IntPtr.Zero)
39                 {
40                     throw new ObjectDisposedException(nameof(Tag));
41                 }
42
43                 return _tagHandle;
44             }
45
46             set
47             {
48                 _tagHandle = value;
49             }
50         }
51         /// <summary>
52         /// The ID of the media tag
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         public int Id
56         {
57             get
58             {
59                 int id;
60                 MediaContentValidator.ThrowIfError(
61                     Interop.Tag.GetTagId(Handle, out id), "Failed to get value");
62                 return id;
63             }
64         }
65
66         /// <summary>
67         /// The name of the tag
68         /// </summary>
69         /// <since_tizen> 3 </since_tizen>
70         public string Name
71         {
72             get
73             {
74                 return _tagName;
75             }
76
77             set
78             {
79                 MediaContentValidator.ThrowIfError(
80                     Interop.Tag.SetName(Handle, value), "Failed to set value");
81                 _tagName = value;
82             }
83         }
84
85         internal Tag(IntPtr tagHandle)
86         {
87             _tagHandle = tagHandle;
88             IntPtr val = IntPtr.Zero;
89             try
90             {
91                 MediaContentValidator.ThrowIfError(
92                     Interop.Tag.GetName(Handle, out val), "Failed to get value");
93                 _tagName = Marshal.PtrToStringAnsi(val);
94             }
95             finally
96             {
97                 Interop.Libc.Free(val);
98             }
99         }
100
101         /// <summary>
102         /// Creates a Tag object which can be inserted to the media database using ContentManager:InsertToDatabaseAsync(ContentCollection)
103         /// </summary>
104         /// <since_tizen> 3 </since_tizen>
105         /// <param name="tagName">The name of the media tag</param>
106         public Tag(string tagName)
107         {
108             _tagName = tagName;
109         }
110
111         /// <summary>
112         /// Adds a new media info to the tag.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         /// <param name="mediaContent">The media info which is added</param>
116         public void AddItem(MediaInformation mediaContent)
117         {
118             MediaContentValidator.ThrowIfError(
119                 Interop.Tag.AddMedia(Handle, mediaContent.MediaId), "Failed to add item");
120         }
121
122         /// <summary>
123         /// Removes the media info from the given tag.
124         /// </summary>
125         /// <since_tizen> 3 </since_tizen>
126         /// <param name="mediaContent">The media info which is removed</param>
127         public void RemoveItem(MediaInformation mediaContent)
128         {
129             MediaContentValidator.ThrowIfError(
130                 Interop.Tag.RemoveMedia(Handle, mediaContent.MediaId), "Failed to remove item");
131         }
132
133         /// <summary>
134         /// Gets the number of media files for the passed filter in the given tag from the media database.
135         /// </summary>
136         /// <since_tizen> 3 </since_tizen>
137         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
138         /// <returns>The number of media contents matching the filter passed</returns>
139         public override int GetMediaInformationCount(ContentFilter filter)
140         {
141             int mediaCount;
142             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
143             MediaContentValidator.ThrowIfError(
144                 Interop.Tag.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
145
146             return mediaCount;
147         }
148
149         public override void Dispose()
150         {
151             if (_tagHandle != IntPtr.Zero)
152             {
153                 Interop.Tag.Destroy(_tagHandle);
154                 _tagHandle = IntPtr.Zero;
155             }
156         }
157
158         /// <summary>
159         /// Iterates through media items for a given tag from the media database.
160         /// This function gets all media items associated with a given tag and meeting a desired filter.
161         /// If NULL is passed to the filter, no filtering is applied.
162         /// </summary>
163         /// <since_tizen> 3 </since_tizen>
164         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
165         /// <returns>List of content media items matching the passed filter</returns>
166         public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
167         {
168             List<MediaInformation> mediaContents = new List<MediaInformation>();
169             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
170
171             Interop.Tag.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
172             {
173                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
174                 MediaContentValidator.ThrowIfError(
175                     Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
176
177                 MediaContentType type;
178                 Interop.MediaInformation.GetMediaType(newHandle, out type);
179                 if (type == MediaContentType.Image)
180                 {
181                     Interop.ImageInformation.SafeImageInformationHandle imageInfo;
182                     MediaContentValidator.ThrowIfError(
183                         Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
184
185                     mediaContents.Add(new ImageInformation(imageInfo, newHandle));
186                 }
187                 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
188                 {
189                     Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
190                     MediaContentValidator.ThrowIfError(
191                         Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
192
193                     mediaContents.Add(new AudioInformation(audioInfo, newHandle));
194                 }
195                 else if (type == MediaContentType.Video)
196                 {
197                     Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
198                     MediaContentValidator.ThrowIfError(
199                         Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
200
201                     mediaContents.Add(new VideoInformation(videoInfo, newHandle));
202                 }
203                 else if (type == MediaContentType.Others)
204                 {
205                     mediaContents.Add(new MediaInformation(newHandle));
206                 }
207
208                 return true;
209             };
210
211             MediaContentValidator.ThrowIfError(
212                 Interop.Tag.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
213
214             return mediaContents;
215         }
216     }
217 }