05b8e84476aa0ad8acd716b8c70e28901f9e1307
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / FaceInfoCommand.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 using System;
18
19 namespace Tizen.Content.MediaContent
20 {
21     /// <summary>
22     /// Provides commands to manage face information in the database.
23     /// </summary>
24     /// <seealso cref="Album"/>
25     /// <since_tizen> 4 </since_tizen>
26     public class FaceInfoCommand : MediaCommand
27     {
28         /// <summary>
29         /// Initializes a new instance of the <see cref="FaceInfoCommand"/> class with the specified <see cref="MediaDatabase"/>.
30         /// </summary>
31         /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
32         /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
33         /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed.</exception>
34         /// <since_tizen> 4 </since_tizen>
35         public FaceInfoCommand(MediaDatabase database) : base(database)
36         {
37         }
38
39         /// <summary>
40         /// Deletes the face information from the database.
41         /// </summary>
42         /// <privilege>http://tizen.org/privilege/content.write</privilege>
43         /// <param name="faceInfoId">The face information ID to delete.</param>
44         /// <returns>true if the matched record was found and deleted, otherwise false.</returns>
45         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
46         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed.</exception>
47         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
48         /// <exception cref="ArgumentNullException"><paramref name="faceInfoId"/> is null.</exception>
49         /// <exception cref="ArgumentException"><paramref name="faceInfoId"/> is a zero-length string, contains only white space.</exception>
50         /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
51         /// <since_tizen> 4 </since_tizen>
52         public bool Delete(string faceInfoId)
53         {
54             ValidateDatabase();
55
56             ValidationUtil.ValidateNotNullOrEmpty(faceInfoId, nameof(faceInfoId));
57
58             var reader = Select(new SelectArguments { FilterExpression = $"{FaceInfoColumns.Id}='{faceInfoId}'" });
59
60             if (reader.Read() == false)
61             {
62                 return false;
63             }
64
65             CommandHelper.Delete(Interop.Face.DeleteFromDb, faceInfoId);
66             return true;
67         }
68
69         /// <summary>
70         /// Retrieves the face information.
71         /// </summary>
72         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
73         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
74         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed.</exception>
75         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
76         /// <since_tizen> 4 </since_tizen>
77         public MediaDataReader<FaceInfo> Select()
78         {
79             return Select(null);
80         }
81
82         /// <summary>
83         /// Retrieves the face information with the <see cref="SelectArguments"/>.
84         /// </summary>
85         /// <param name="filter">The criteria to use to filter. This value can be null.</param>
86         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
87         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
88         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed.</exception>
89         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
90         /// <since_tizen> 4 </since_tizen>
91         public MediaDataReader<FaceInfo> Select(SelectArguments filter)
92         {
93             ValidateDatabase();
94
95             return CommandHelper.Select(filter, Interop.Face.ForeachFromDb, FaceInfo.FromHandle);
96         }
97
98         /// <summary>
99         /// Updates a tag with the specified tag.
100         /// </summary>
101         /// <privilege>http://tizen.org/privilege/content.write</privilege>
102         /// <param name="faceInfoId">The face information ID to update.</param>
103         /// <param name="tag">The tag value for update.</param>
104         /// <returns>true if the matched record was found and updated, otherwise false.</returns>
105         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
106         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed.</exception>
107         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
108         /// <exception cref="ArgumentNullException"><paramref name="faceInfoId"/> is null.</exception>
109         /// <exception cref="ArgumentException"><paramref name="faceInfoId"/> is a zero-length string, contains only white space.</exception>
110         /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
111         /// <since_tizen> 4 </since_tizen>
112         public bool UpdateTag(string faceInfoId, string tag)
113         {
114             ValidateDatabase();
115
116             ValidationUtil.ValidateNotNullOrEmpty(faceInfoId, nameof(faceInfoId));
117
118             if (tag == null)
119             {
120                 throw new ArgumentNullException(nameof(tag));
121             }
122
123             var handle = CommandHelper.SelectScalar(Interop.Face.ForeachFromDb, $"{FaceInfoColumns.Id}='{faceInfoId}'",
124                 Interop.Face.Clone);
125
126             if (handle == IntPtr.Zero)
127             {
128                 return false;
129             }
130
131             try
132             {
133                 Interop.Face.SetTag(handle, tag).ThrowIfError("Failed to update(setting tag)");
134
135                 Interop.Face.Update(handle).ThrowIfError("Failed to update(executing query)");
136                 return true;
137             }
138             finally
139             {
140                 Interop.Face.Destroy(handle);
141             }
142         }
143
144         /// <summary>
145         /// Inserts new face information to the database with the specified media ID, area, orientation.
146         /// </summary>
147         /// <privilege>http://tizen.org/privilege/content.write</privilege>
148         /// <param name="mediaId">The media ID to be associated with the face.</param>
149         /// <param name="area">The region of face in the media.</param>
150         /// <param name="orientation">The orientation of the specified media.</param>
151         /// <returns>The <see cref="FaceInfo"/> containing the results.</returns>
152         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
153         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed.</exception>
154         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
155         /// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null. </exception>
156         /// <exception cref="ArgumentException">
157         ///     <paramref name="mediaId"/> is a zero-length string, contains only white space.<br/>
158         ///     -or-<br/>
159         ///     <paramref name="orientation"/> is not valid enumeration.
160         /// </exception>
161         /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
162         /// <since_tizen> 6 </since_tizen>
163         public FaceInfo Insert(string mediaId, Rectangle area, Orientation orientation)
164         {
165             return Insert(mediaId, area, orientation, null);
166         }
167
168         /// <summary>
169         /// Inserts new face information to the database with the specified media ID, area, orientation, and tag.
170         /// </summary>
171         /// <privilege>http://tizen.org/privilege/content.write</privilege>
172         /// <param name="mediaId">The media ID to be associated with the face.</param>
173         /// <param name="area">The region of face in the media.</param>
174         /// <param name="orientation">The orientation of specified media.</param>
175         /// <param name="tag">The tag value.</param>
176         /// <returns>The <see cref="FaceInfo"/> containing the results.</returns>
177         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
178         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed.</exception>
179         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
180         /// <exception cref="ArgumentNullException"><paramref name="mediaId"/> is null. </exception>
181         /// <exception cref="ArgumentException">
182         ///     <paramref name="mediaId"/> is a zero-length string, contains only white space.<br/>
183         ///     -or-<br/>
184         ///     <paramref name="orientation"/> is not valid enumeration.
185         /// </exception>
186         /// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
187         /// <since_tizen> 6 </since_tizen>
188         public FaceInfo Insert(string mediaId, Rectangle area, Orientation orientation, string tag)
189         {
190             ValidateDatabase();
191
192             ValidationUtil.ValidateNotNullOrEmpty(mediaId, nameof(mediaId));
193             ValidationUtil.ValidateEnum(typeof(Orientation), orientation, nameof(orientation));
194
195             Interop.Face.Create(mediaId, out IntPtr handle).ThrowIfError("Failed to create face handle");
196
197             try
198             {
199                 Interop.Face.SetFaceRect(handle, area.X, area.Y, area.Width, area.Height).
200                     ThrowIfError("Failed to set face area");
201
202                 Interop.Face.SetOrientation(handle, orientation).ThrowIfError("Failed to set face orientation");
203
204                 if (tag != null)
205                 {
206                     Interop.Face.SetTag(handle, tag).ThrowIfError("Failed to set face tag");
207                 }
208
209                 Interop.Face.Insert(handle).ThrowIfError("Failed to insert face information");
210
211                 return new FaceInfo(handle);
212             }
213             finally
214             {
215                 Interop.Face.Destroy(handle).ThrowIfError("Failed to destroy face handle");
216             }
217         }
218     }
219 }