Fix handle leak issue
[platform/core/csapi/uix-voice-control.git] / Tizen.Uix.VoiceControl / Tizen.Uix.VoiceControl / VoiceCommandList.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 using System.Collections.Generic;
19 using static Interop.VoiceControl;
20 using static Interop.VoiceControlCommand;
21
22 namespace Tizen.Uix.VoiceControl
23 {
24     /// <summary>
25     /// this class represents list of Voice Commands
26     /// </summary>
27     public class VoiceCommandList
28     {
29         internal SafeCommandListHandle _handle;
30         private List<VoiceCommand> _list;
31         private VcCmdListCb _callback;
32
33         /// <summary>
34         /// Public Constructor
35         /// </summary>
36         /// <privilege>
37         /// http://tizen.org/privilege/recorder
38         /// </privilege>
39         /// <privlevel>
40         /// public
41         /// </privlevel>
42         /// <feature>
43         /// http://tizen.org/feature/speech.control
44         /// http://tizen.org/feature/microphone
45         /// </feature>
46         /// <exception cref="OutOfMemoryException"> This Exception can be due to Out of memory. </exception>
47         /// <exception cref="ArgumentException"> This Exception can be due to Invalid Parameter. </exception>
48         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
49         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
50         public VoiceCommandList()
51         {
52             SafeCommandListHandle handle;
53             ErrorCode error = VcCmdListCreate(out handle);
54             if (error != ErrorCode.None)
55             {
56                 Log.Error(LogTag, "Create Failed with error " + error);
57                 throw ExceptionFactory.CreateException(error);
58             }
59             _handle = handle;
60         }
61
62         internal VoiceCommandList(SafeCommandListHandle handle)
63         {
64             _handle = handle;
65         }
66
67         /// <summary>
68         /// Gets command count of list.
69         /// -1 is returned in case of internal failure.
70         /// </summary>
71         /// <value>
72         /// Command counts of the list.
73         /// </value>
74         /// <privilege>
75         /// http://tizen.org/privilege/recorder
76         /// </privilege>
77         /// <privlevel>
78         /// public
79         /// </privlevel>
80         public int Count
81         {
82             get
83             {
84                 int count;
85                 ErrorCode error = VcCmdListGetCount(_handle, out count);
86                 if (error != ErrorCode.None)
87                 {
88                     Log.Error(LogTag, "Count Failed with error " + error);
89                     return -1;
90                 }
91
92                 return count;
93             }
94         }
95
96         /// <summary>
97         /// Get current command from command list by index.
98         /// null will be returned in case of Empty List
99         /// </summary>
100         /// <value>
101         /// Current command from the command list.
102         /// </value>
103         /// <privilege>
104         /// http://tizen.org/privilege/recorder
105         /// </privilege>
106         /// <privlevel>
107         /// public
108         /// </privlevel>
109         public VoiceCommand Current
110         {
111             get
112             {
113                 SafeCommandHandle current;
114                 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
115                 if (error != ErrorCode.None)
116                 {
117                     Log.Error(LogTag, "Current Failed with error " + error);
118                     return null;
119                 }
120                 current._ownership = false;
121                 return new VoiceCommand(current);
122             }
123         }
124
125         /// <summary>
126         /// Adds command to command list.
127         /// </summary>
128         /// <privilege>
129         /// http://tizen.org/privilege/recorder
130         /// </privilege>
131         /// <privlevel>
132         /// public
133         /// </privlevel>
134         /// <feature>
135         /// http://tizen.org/feature/speech.control
136         /// http://tizen.org/feature/microphone
137         /// </feature>
138         /// <param name="command">The command</param>
139         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
140         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
141         /// <exception cref="NullReferenceException"> This will occur if the provide parameter is null. </exception>
142         public void Add(VoiceCommand command)
143         {
144             ErrorCode error = VcCmdListAdd(_handle, command._handle);
145             if (error != ErrorCode.None)
146             {
147                 Log.Error(LogTag, "Add Failed with error " + error);
148                 throw ExceptionFactory.CreateException(error);
149             }
150         }
151
152         /// <summary>
153         /// Removes command from command list.
154         /// </summary>
155         /// <privilege>
156         /// http://tizen.org/privilege/recorder
157         /// </privilege>
158         /// <privlevel>
159         /// public
160         /// </privlevel>
161         /// <feature>
162         /// http://tizen.org/feature/speech.control
163         /// http://tizen.org/feature/microphone
164         /// </feature>
165         /// <param name="command">The command</param>
166         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
167         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
168         /// <exception cref="NullReferenceException"> This will occur if the provide parameter is null. </exception>
169         public void Remove(VoiceCommand command)
170         {
171             ErrorCode error = VcCmdListRemove(_handle, command._handle);
172             if (error != ErrorCode.None)
173             {
174                 Log.Error(LogTag, "Remove Failed with error " + error);
175                 throw ExceptionFactory.CreateException(error);
176             }
177         }
178
179         /// <summary>
180         /// Retrieves all commands of command list.
181         /// </summary>
182         /// <privilege>
183         /// http://tizen.org/privilege/recorder
184         /// </privilege>
185         /// <privlevel>
186         /// public
187         /// </privlevel>
188         /// <feature>
189         /// http://tizen.org/feature/speech.control
190         /// http://tizen.org/feature/microphone
191         /// </feature>
192         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
193         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
194         public IEnumerable<VoiceCommand> GetAllCommands()
195         {
196             _list = new List<VoiceCommand>();
197             _callback = (IntPtr vcCommand, IntPtr userData) =>
198             {
199                 SafeCommandHandle handle = new SafeCommandHandle(vcCommand);
200                 handle._ownership = false;
201                 _list.Add(new VoiceCommand(handle));
202                 return true;
203             };
204             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
205             if (error != ErrorCode.None)
206             {
207                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
208                 throw ExceptionFactory.CreateException(error);
209             }
210
211             return _list;
212         }
213
214         /// <summary>
215         /// Moves index to first command.
216         /// </summary>
217         /// <privilege>
218         /// http://tizen.org/privilege/recorder
219         /// </privilege>
220         /// <privlevel>
221         /// public
222         /// </privlevel>
223         /// <feature>
224         /// http://tizen.org/feature/speech.control
225         /// http://tizen.org/feature/microphone
226         /// </feature>
227         /// <exception cref="InvalidOperationException"> This Exception can be due to List Empty. </exception>
228         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
229         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
230         public void First()
231         {
232             ErrorCode error = VcCmdListFirst(_handle);
233             if (error != ErrorCode.None)
234             {
235                 Log.Error(LogTag, "First Failed with error " + error);
236                 throw ExceptionFactory.CreateException(error);
237             }
238         }
239
240         /// <summary>
241         /// Moves index to last command.
242         /// </summary>
243         /// <privilege>
244         /// http://tizen.org/privilege/recorder
245         /// </privilege>
246         /// <privlevel>
247         /// public
248         /// </privlevel>
249         /// <feature>
250         /// http://tizen.org/feature/speech.control
251         /// http://tizen.org/feature/microphone
252         /// </feature>
253         /// <exception cref="InvalidOperationException"> This Exception can be due to List Empty. </exception>
254         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
255         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
256         public void Last()
257         {
258             ErrorCode error = VcCmdListLast(_handle);
259             if (error != ErrorCode.None)
260             {
261                 Log.Error(LogTag, "Last Failed with error " + error);
262                 throw ExceptionFactory.CreateException(error);
263             }
264         }
265
266         /// <summary>
267         /// Moves index to next command.
268         /// </summary>
269         /// <privilege>
270         /// http://tizen.org/privilege/recorder
271         /// </privilege>
272         /// <privlevel>
273         /// public
274         /// </privlevel>
275         /// <feature>
276         /// http://tizen.org/feature/speech.control
277         /// http://tizen.org/feature/microphone
278         /// </feature>
279         /// <exception cref="InvalidOperationException">
280         /// This Exception can be due to the following reaons
281         /// 1. List Empty
282         /// 2. List reached end
283         /// </exception>
284         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
285         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
286         public void Next()
287         {
288             ErrorCode error = VcCmdListNext(_handle);
289             if (error != ErrorCode.None)
290             {
291                 Log.Error(LogTag, "Next Failed with error " + error);
292                 throw ExceptionFactory.CreateException(error);
293             }
294         }
295
296         /// <summary>
297         /// Moves index to previous command.
298         /// </summary>
299         /// <privilege>
300         /// http://tizen.org/privilege/recorder
301         /// </privilege>
302         /// <privlevel>
303         /// public
304         /// </privlevel>
305         /// <feature>
306         /// http://tizen.org/feature/speech.control
307         /// http://tizen.org/feature/microphone
308         /// </feature>
309         /// <exception cref="InvalidOperationException">
310         /// This Exception can be due to the following reaons
311         /// 1. List Empty
312         /// 2. List reached end
313         /// </exception>
314         /// <exception cref="UnauthorizedAccessException"> This Exception can be due to Permission Denied. </exception>
315         /// <exception cref="NotSupportedException"> This Exception can be due to Not Supported. </exception>
316         public void Previous()
317         {
318             ErrorCode error = VcCmdListPrev(_handle);
319             if (error != ErrorCode.None)
320             {
321                 Log.Error(LogTag, "Previous Failed with error " + error);
322                 throw ExceptionFactory.CreateException(error);
323             }
324         }
325     }
326 }