Fix unintended memory free 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 a list of the voice commands.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class VoiceCommandList
29     {
30         internal SafeCommandListHandle _handle;
31         private List<VoiceCommand> _list;
32         private VcCmdListCb _callback;
33         private int _index;
34
35         /// <summary>
36         /// The public constructor.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <privilege>
40         /// http://tizen.org/privilege/recorder
41         /// </privilege>
42         /// <privlevel>
43         /// public
44         /// </privlevel>
45         /// <feature>
46         /// http://tizen.org/feature/speech.control
47         /// http://tizen.org/feature/microphone
48         /// </feature>
49         /// <exception cref="OutOfMemoryException">This exception can be due to out of memory.</exception>
50         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
51         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
52         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
53         public VoiceCommandList()
54         {
55             SafeCommandListHandle handle;
56             ErrorCode error = VcCmdListCreate(out handle);
57             if (error != ErrorCode.None)
58             {
59                 Log.Error(LogTag, "Create Failed with error " + error);
60                 throw ExceptionFactory.CreateException(error);
61             }
62             _handle = handle;
63             _list = new List<VoiceCommand>();
64             _index = 0;
65         }
66
67         internal VoiceCommandList(SafeCommandListHandle handle)
68         {
69             _handle = handle;
70             _index = 0;
71
72             _list = new List<VoiceCommand>();
73             _callback = (IntPtr vcCommand, IntPtr userData) =>
74             {
75                 SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
76                 cmdHandle._ownership = false;
77                 _list.Add(new VoiceCommand(cmdHandle));
78                 return true;
79             };
80             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
81             if (error != ErrorCode.None)
82             {
83                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
84                 throw ExceptionFactory.CreateException(error);
85             }
86         }
87
88         /// <summary>
89         /// Gets a command count of the list.
90         /// -1 is returned in case of an internal failure.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <value>
94         /// Command count of the list.
95         /// </value>
96         /// <privilege>
97         /// http://tizen.org/privilege/recorder
98         /// </privilege>
99         /// <privlevel>
100         /// public
101         /// </privlevel>
102         public int Count
103         {
104             get
105             {
106                 int count;
107                 ErrorCode error = VcCmdListGetCount(_handle, out count);
108                 if (error != ErrorCode.None)
109                 {
110                     Log.Error(LogTag, "Count Failed with error " + error);
111                     return -1;
112                 }
113
114                 return count;
115             }
116         }
117
118         /// <summary>
119         /// Gets the current command from the command list by index.
120         /// Null will be returned in case of an empty list.
121         /// </summary>
122         /// <since_tizen> 3 </since_tizen>
123         /// <value>
124         /// Current command from the command list.
125         /// </value>
126         /// <privilege>
127         /// http://tizen.org/privilege/recorder
128         /// </privilege>
129         /// <privlevel>
130         /// public
131         /// </privlevel>
132         public VoiceCommand Current
133         {
134             get
135             {
136                 SafeCommandHandle current;
137                 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
138                 if (ErrorCode.None != error)
139                 {
140                     Log.Error(LogTag, "Current Failed with error " + error);
141                     return null;
142                 }
143
144                 return _list[_index];
145             }
146         }
147
148         /// <summary>
149         /// Adds a command to the command list.
150         /// </summary>
151         /// <since_tizen> 3 </since_tizen>
152         /// <privilege>
153         /// http://tizen.org/privilege/recorder
154         /// </privilege>
155         /// <privlevel>
156         /// public
157         /// </privlevel>
158         /// <feature>
159         /// http://tizen.org/feature/speech.control
160         /// http://tizen.org/feature/microphone
161         /// </feature>
162         /// <param name="command">The command</param>
163         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
164         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
165         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
166         public void Add(VoiceCommand command)
167         {
168             ErrorCode error = VcCmdListAdd(_handle, command._handle);
169             if (error != ErrorCode.None)
170             {
171                 Log.Error(LogTag, "Add Failed with error " + error);
172                 throw ExceptionFactory.CreateException(error);
173             }
174
175             _list.Add(command);
176         }
177
178         /// <summary>
179         /// Removes a command from the command list.
180         /// </summary>
181         /// <since_tizen> 3 </since_tizen>
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         /// <param name="command">The command</param>
193         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
194         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
195         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
196         public void Remove(VoiceCommand command)
197         {
198             ErrorCode error = VcCmdListRemove(_handle, command._handle);
199             if (error != ErrorCode.None)
200             {
201                 Log.Error(LogTag, "Remove Failed with error " + error);
202                 throw ExceptionFactory.CreateException(error);
203             }
204
205             _list.Remove(command);
206         }
207
208         /// <summary>
209         /// Retrieves all commands from the command list.
210         /// </summary>
211         /// <since_tizen> 3 </since_tizen>
212         /// <privilege>
213         /// http://tizen.org/privilege/recorder
214         /// </privilege>
215         /// <privlevel>
216         /// public
217         /// </privlevel>
218         /// <feature>
219         /// http://tizen.org/feature/speech.control
220         /// http://tizen.org/feature/microphone
221         /// </feature>
222         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
223         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
224         public IEnumerable<VoiceCommand> GetAllCommands()
225         {
226             List<VoiceCommand> commandList = new List<VoiceCommand>();
227             _callback = (IntPtr vcCommand, IntPtr userData) =>
228             {
229                 SafeCommandHandle handle = new SafeCommandHandle(vcCommand);
230                 handle._ownership = false;
231                 commandList.Add(new VoiceCommand(handle));
232                 return true;
233             };
234             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
235             if (error != ErrorCode.None)
236             {
237                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
238                 throw ExceptionFactory.CreateException(error);
239             }
240
241             return _list;
242         }
243
244         /// <summary>
245         /// Moves an index to the first command.
246         /// </summary>
247         /// <since_tizen> 3 </since_tizen>
248         /// <privilege>
249         /// http://tizen.org/privilege/recorder
250         /// </privilege>
251         /// <privlevel>
252         /// public
253         /// </privlevel>
254         /// <feature>
255         /// http://tizen.org/feature/speech.control
256         /// http://tizen.org/feature/microphone
257         /// </feature>
258         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
259         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
260         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
261         public void First()
262         {
263             ErrorCode error = VcCmdListFirst(_handle);
264             if (ErrorCode.None != error)
265             {
266                 Log.Error(LogTag, "First Failed with error " + error);
267                 throw ExceptionFactory.CreateException(error);
268             }
269             _index = 0;
270         }
271
272         /// <summary>
273         /// Moves an index to the last command.
274         /// </summary>
275         /// <since_tizen> 3 </since_tizen>
276         /// <privilege>
277         /// http://tizen.org/privilege/recorder
278         /// </privilege>
279         /// <privlevel>
280         /// public
281         /// </privlevel>
282         /// <feature>
283         /// http://tizen.org/feature/speech.control
284         /// http://tizen.org/feature/microphone
285         /// </feature>
286         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
287         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
288         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
289         public void Last()
290         {
291             ErrorCode error = VcCmdListLast(_handle);
292             if (ErrorCode.None != error)
293             {
294                 Log.Error(LogTag, "Last Failed with error " + error);
295                 throw ExceptionFactory.CreateException(error);
296             }
297             _index = Count - 1;
298         }
299
300         /// <summary>
301         /// Moves an index to the next command.
302         /// </summary>
303         /// <since_tizen> 3 </since_tizen>
304         /// <privilege>
305         /// http://tizen.org/privilege/recorder
306         /// </privilege>
307         /// <privlevel>
308         /// public
309         /// </privlevel>
310         /// <feature>
311         /// http://tizen.org/feature/speech.control
312         /// http://tizen.org/feature/microphone
313         /// </feature>
314         /// <exception cref="InvalidOperationException">
315         /// This exception can be due to the following reasons:
316         /// 1. List empty
317         /// 2. List reached end
318         /// </exception>
319         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
320         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
321         public void Next()
322         {
323             ErrorCode error = VcCmdListNext(_handle);
324             if (ErrorCode.None != error)
325             {
326                 Log.Error(LogTag, "Next Failed with error " + error);
327                 throw ExceptionFactory.CreateException(error);
328             }
329            _index++;
330         }
331
332         /// <summary>
333         /// Moves an index to the previous command.
334         /// </summary>
335         /// <since_tizen> 3 </since_tizen>
336         /// <privilege>
337         /// http://tizen.org/privilege/recorder
338         /// </privilege>
339         /// <privlevel>
340         /// public
341         /// </privlevel>
342         /// <feature>
343         /// http://tizen.org/feature/speech.control
344         /// http://tizen.org/feature/microphone
345         /// </feature>
346         /// <exception cref="InvalidOperationException">
347         /// This exception can be due to the following reasons:
348         /// 1. List empty
349         /// 2. List reached end
350         /// </exception>
351         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
352         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
353         public void Previous()
354         {
355             ErrorCode error = VcCmdListPrev(_handle);
356             if (ErrorCode.None != error)
357             {
358                 Log.Error(LogTag, "Previous Failed with error " + error);
359                 throw ExceptionFactory.CreateException(error);
360             }
361             _index--;
362         }
363     }
364 }