[VoiceControlWidget] Add VoiceControlWidget internal API
[platform/core/csapi/tizenfx.git] / internal / src / Tizen.Uix.VoiceControlWidget / Tizen.Uix.VoiceControlWidget / 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.VoiceControlWidget;
20 using static Interop.VoiceControlCommand;
21
22 namespace Tizen.Uix.VoiceControlWidget
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         /// <exception cref="OutOfMemoryException">This exception can be due to out of memory.</exception>
40         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
41         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
42         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
43         public VoiceCommandList()
44         {
45             SafeCommandListHandle handle;
46             ErrorCode error = VcCmdListCreate(out handle);
47             if (error != ErrorCode.None)
48             {
49                 Log.Error(LogTag, "Create Failed with error " + error);
50                 throw ExceptionFactory.CreateException(error);
51             }
52             _handle = handle;
53             _list = new List<VoiceCommand>();
54             _index = 0;
55         }
56
57         internal VoiceCommandList(SafeCommandListHandle handle)
58         {
59             _handle = handle;
60             _index = 0;
61
62             _list = new List<VoiceCommand>();
63             _callback = (IntPtr vcCommand, IntPtr userData) =>
64             {
65                 SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
66                 cmdHandle._ownership = false;
67                 _list.Add(new VoiceCommand(cmdHandle));
68                 return true;
69             };
70             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
71             if (error != ErrorCode.None)
72             {
73                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
74                 throw ExceptionFactory.CreateException(error);
75             }
76         }
77
78         /// <summary>
79         /// Gets a command count of the list.
80         /// -1 is returned in case of an internal failure.
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         /// <value>
84         /// Command count of the list.
85         /// </value>
86         public int Count
87         {
88             get
89             {
90                 int count;
91                 ErrorCode error = VcCmdListGetCount(_handle, out count);
92                 if (error != ErrorCode.None)
93                 {
94                     Log.Error(LogTag, "Count Failed with error " + error);
95                     return -1;
96                 }
97
98                 return count;
99             }
100         }
101
102         /// <summary>
103         /// Gets the current command from the command list by index.
104         /// Null will be returned in case of an empty list.
105         /// </summary>
106         /// <since_tizen> 3 </since_tizen>
107         /// <value>
108         /// Current command from the command list.
109         /// </value>
110         public VoiceCommand Current
111         {
112             get
113             {
114                 SafeCommandHandle current;
115                 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
116                 if (ErrorCode.None != error)
117                 {
118                     Log.Error(LogTag, "Current Failed with error " + error);
119                     return null;
120                 }
121
122                 current._ownership = false;
123                 return _list[_index];
124             }
125         }
126
127         /// <summary>
128         /// Adds a command to the command list.
129         /// </summary>
130         /// <since_tizen> 3 </since_tizen>
131         /// <param name="command">The command</param>
132         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
133         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
134         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
135         public void Add(VoiceCommand command)
136         {
137             ErrorCode error = VcCmdListAdd(_handle, command._handle);
138             if (error != ErrorCode.None)
139             {
140                 Log.Error(LogTag, "Add Failed with error " + error);
141                 throw ExceptionFactory.CreateException(error);
142             }
143
144             _list.Add(command);
145         }
146
147         /// <summary>
148         /// Removes a command from the command list.
149         /// </summary>
150         /// <since_tizen> 3 </since_tizen>
151         /// <param name="command">The command</param>
152         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
153         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
154         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
155         public void Remove(VoiceCommand command)
156         {
157             ErrorCode error = VcCmdListRemove(_handle, command._handle);
158             if (error != ErrorCode.None)
159             {
160                 Log.Error(LogTag, "Remove Failed with error " + error);
161                 throw ExceptionFactory.CreateException(error);
162             }
163
164             _list.Remove(command);
165         }
166
167         /// <summary>
168         /// Retrieves all commands from the command list.
169         /// </summary>
170         /// <since_tizen> 3 </since_tizen>
171         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
172         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
173         public IEnumerable<VoiceCommand> GetAllCommands()
174         {
175             _callback = (IntPtr vcCommand, IntPtr userData) =>
176             {
177                 if (IntPtr.Zero == vcCommand) {
178                     Log.Error(LogTag, "Invalid command pointer");
179                     return false;
180                 }
181                 return true;
182             };
183             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
184             if (error != ErrorCode.None)
185             {
186                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
187                 throw ExceptionFactory.CreateException(error);
188             }
189
190             return _list;
191         }
192
193         /// <summary>
194         /// Moves an index to the first command.
195         /// </summary>
196         /// <since_tizen> 3 </since_tizen>
197         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
198         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
199         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
200         public void First()
201         {
202             ErrorCode error = VcCmdListFirst(_handle);
203             if (ErrorCode.None != error)
204             {
205                 Log.Error(LogTag, "First Failed with error " + error);
206                 throw ExceptionFactory.CreateException(error);
207             }
208             _index = 0;
209         }
210
211         /// <summary>
212         /// Moves an index to the last command.
213         /// </summary>
214         /// <since_tizen> 3 </since_tizen>
215         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
216         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
217         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
218         public void Last()
219         {
220             ErrorCode error = VcCmdListLast(_handle);
221             if (ErrorCode.None != error)
222             {
223                 Log.Error(LogTag, "Last Failed with error " + error);
224                 throw ExceptionFactory.CreateException(error);
225             }
226             _index = Count - 1;
227         }
228
229         /// <summary>
230         /// Moves an index to the next command.
231         /// </summary>
232         /// <since_tizen> 3 </since_tizen>
233         /// <exception cref="InvalidOperationException">This exception can be due to list empty or list end.</exception>
234         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
235         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
236         public void Next()
237         {
238             ErrorCode error = VcCmdListNext(_handle);
239             if (ErrorCode.None != error)
240             {
241                 Log.Error(LogTag, "Next Failed with error " + error);
242                 throw ExceptionFactory.CreateException(error);
243             }
244            _index++;
245         }
246
247         /// <summary>
248         /// Moves an index to the previous command.
249         /// </summary>
250         /// <since_tizen> 3 </since_tizen>
251         /// <exception cref="InvalidOperationException">This exception can be due to list empty or list end.</exception>
252         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
253         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
254         public void Previous()
255         {
256             ErrorCode error = VcCmdListPrev(_handle);
257             if (ErrorCode.None != error)
258             {
259                 Log.Error(LogTag, "Previous Failed with error " + error);
260                 throw ExceptionFactory.CreateException(error);
261             }
262             _index--;
263         }
264     }
265 }