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.
19 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
21 using System.Threading.Tasks;
23 namespace Tizen.Content.MediaContent
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.
29 public class Tag : ContentCollection
31 private IntPtr _tagHandle = IntPtr.Zero;
32 private string _tagName = "";
34 internal IntPtr Handle
38 if (_tagHandle == IntPtr.Zero)
40 throw new ObjectDisposedException(nameof(Tag));
52 /// The ID of the media tag
54 /// <since_tizen> 3 </since_tizen>
60 MediaContentValidator.ThrowIfError(
61 Interop.Tag.GetTagId(Handle, out id), "Failed to get value");
67 /// The name of the tag
69 /// <since_tizen> 3 </since_tizen>
79 MediaContentValidator.ThrowIfError(
80 Interop.Tag.SetName(Handle, value), "Failed to set value");
85 internal Tag(IntPtr tagHandle)
87 _tagHandle = tagHandle;
88 IntPtr val = IntPtr.Zero;
91 MediaContentValidator.ThrowIfError(
92 Interop.Tag.GetName(Handle, out val), "Failed to get value");
93 _tagName = Marshal.PtrToStringAnsi(val);
97 Interop.Libc.Free(val);
102 /// Creates a Tag object which can be inserted to the media database using ContentManager:InsertToDatabaseAsync(ContentCollection)
104 /// <since_tizen> 3 </since_tizen>
105 /// <param name="tagName">The name of the media tag</param>
106 public Tag(string tagName)
112 /// Adds a new media info to the tag.
114 /// <since_tizen> 3 </since_tizen>
115 /// <param name="mediaContent">The media info which is added</param>
116 public void AddItem(MediaInformation mediaContent)
118 MediaContentValidator.ThrowIfError(
119 Interop.Tag.AddMedia(Handle, mediaContent.MediaId), "Failed to add item");
123 /// Removes the media info from the given tag.
125 /// <since_tizen> 3 </since_tizen>
126 /// <param name="mediaContent">The media info which is removed</param>
127 public void RemoveItem(MediaInformation mediaContent)
129 MediaContentValidator.ThrowIfError(
130 Interop.Tag.RemoveMedia(Handle, mediaContent.MediaId), "Failed to remove item");
134 /// Gets the number of media files for the passed filter in the given tag from the media database.
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)
142 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
143 MediaContentValidator.ThrowIfError(
144 Interop.Tag.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get count");
149 public override void Dispose()
151 if (_tagHandle != IntPtr.Zero)
153 Interop.Tag.Destroy(_tagHandle);
154 _tagHandle = IntPtr.Zero;
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.
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)
168 List<MediaInformation> mediaContents = new List<MediaInformation>();
169 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
171 Interop.Tag.MediaInfoCallback callback = (IntPtr mediaHandle, IntPtr data) =>
173 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
174 MediaContentValidator.ThrowIfError(
175 Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
177 MediaContentType type;
178 Interop.MediaInformation.GetMediaType(newHandle, out type);
179 if (type == MediaContentType.Image)
181 Interop.ImageInformation.SafeImageInformationHandle imageInfo;
182 MediaContentValidator.ThrowIfError(
183 Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
185 mediaContents.Add(new ImageInformation(imageInfo, newHandle));
187 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
189 Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
190 MediaContentValidator.ThrowIfError(
191 Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
193 mediaContents.Add(new AudioInformation(audioInfo, newHandle));
195 else if (type == MediaContentType.Video)
197 Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
198 MediaContentValidator.ThrowIfError(
199 Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
201 mediaContents.Add(new VideoInformation(videoInfo, newHandle));
203 else if (type == MediaContentType.Others)
205 mediaContents.Add(new MediaInformation(newHandle));
211 MediaContentValidator.ThrowIfError(
212 Interop.Tag.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get information");
214 return mediaContents;