[VoiceControl] Base Code
[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         /// <exception cref="InvalidOperationException">
37         /// This Exception can be due to the following reaons
38         /// 1. Invalid parameter
39         /// 2. Permission Denied
40         /// 3. Not supported
41         /// 4. Out of memory
42         /// </exception>
43         public VoiceCommandList()
44         {
45             SafeCommandListHandle handle = new SafeCommandListHandle();
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         }
54
55         internal VoiceCommandList(SafeCommandListHandle handle)
56         {
57             _handle = handle;
58         }
59
60         /// <summary>
61         /// Gets command count of list.
62         /// -1 is returned in case of internal failure.
63         /// </summary>
64         public int GetCount
65         {
66             get
67             {
68                 int count;
69                 ErrorCode error = VcCmdListGetCount(_handle, out count);
70                 if (error != ErrorCode.None)
71                 {
72                     Log.Error(LogTag, "Count Failed with error " + error);
73                     return -1;
74                 }
75
76                 return count;
77             }
78         }
79
80         /// <summary>
81         /// Get current command from command list by index.
82         /// null will be returned in case of Empty List
83         /// </summary>
84         public VoiceCommand GetCurrent
85         {
86             get
87             {
88                 SafeCommandHandle current;
89                 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
90                 if (error != ErrorCode.None)
91                 {
92                     Log.Error(LogTag, "Current Failed with error " + error);
93                     return null;
94                 }
95                 current._ownership = false;
96                 return new VoiceCommand(current);
97             }
98         }
99
100         /// <summary>
101         /// Adds command to command list.
102         /// </summary>
103         /// <param name="command">The command</param>
104         /// <exception cref="InvalidOperationException">
105         /// This Exception can be due to the following reaons
106         /// 1. Invalid parameter
107         /// 2. Permission Denied
108         /// 3. Not supported
109         /// </exception>
110         /// <exception cref="NullReferenceException">This will occur if the provide parameter is null</exception>
111         public void Add(VoiceCommand command)
112         {
113             ErrorCode error = VcCmdListAdd(_handle, command._handle);
114             if (error != ErrorCode.None)
115             {
116                 Log.Error(LogTag, "Add Failed with error " + error);
117                 throw ExceptionFactory.CreateException(error);
118             }
119         }
120
121         /// <summary>
122         /// Removes command from command list.
123         /// </summary>
124         /// <param name="command">The command</param>
125         /// <exception cref="InvalidOperationException">
126         /// This Exception can be due to the following reaons
127         /// 1. Invalid parameter
128         /// 2. Permission Denied
129         /// 3. Not supported
130         /// </exception>
131         /// <exception cref="NullReferenceException">This will occur if the provide parameter is null</exception>
132         public void Remove(VoiceCommand command)
133         {
134             ErrorCode error = VcCmdListRemove(_handle, command._handle);
135             if (error != ErrorCode.None)
136             {
137                 Log.Error(LogTag, "Remove Failed with error " + error);
138                 throw ExceptionFactory.CreateException(error);
139             }
140         }
141
142         /// <summary>
143         /// Retrieves all commands of command list.
144         /// </summary>
145         /// <exception cref="InvalidOperationException">
146         /// This Exception can be due to the following reaons
147         /// 1. Permission Denied
148         /// 2. Not supported
149         /// </exception>
150         public IEnumerable<VoiceCommand> GetAllCommands()
151         {
152             _list = new List<VoiceCommand>();
153             _callback = (IntPtr vcCommand, IntPtr userData) =>
154             {
155                 SafeCommandHandle handle = new SafeCommandHandle(vcCommand);
156                 handle._ownership = false;
157                 _list.Add(new VoiceCommand(handle));
158                 return true;
159             };
160             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
161             if (error != ErrorCode.None)
162             {
163                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
164                 throw ExceptionFactory.CreateException(error);
165             }
166
167             return _list;
168         }
169
170         /// <summary>
171         /// Moves index to first command.
172         /// </summary>
173         /// <exception cref="InvalidOperationException">
174         /// This Exception can be due to the following reaons
175         /// 1. List Empty
176         /// 2. Permission Denied
177         /// 3. Not supported
178         /// </exception>
179         public void First()
180         {
181             ErrorCode error = VcCmdListFirst(_handle);
182             if (error != ErrorCode.None)
183             {
184                 Log.Error(LogTag, "First Failed with error " + error);
185                 throw ExceptionFactory.CreateException(error);
186             }
187         }
188
189         /// <summary>
190         /// Moves index to last command.
191         /// </summary>
192         /// <exception cref="InvalidOperationException">
193         /// This Exception can be due to the following reaons
194         /// 1. List Empty
195         /// 2. Permission Denied
196         /// 3. Not supported
197         /// </exception>
198         public void Last()
199         {
200             ErrorCode error = VcCmdListLast(_handle);
201             if (error != ErrorCode.None)
202             {
203                 Log.Error(LogTag, "Last Failed with error " + error);
204                 throw ExceptionFactory.CreateException(error);
205             }
206         }
207
208         /// <summary>
209         /// Moves index to next command.
210         /// </summary>
211         /// <exception cref="InvalidOperationException">
212         /// This Exception can be due to the following reaons
213         /// 1. List Empty
214         /// 2. List reached end
215         /// 3. Permission Denied
216         /// 4. Not supported
217         /// </exception>
218         public void Next()
219         {
220             ErrorCode error = VcCmdListNext(_handle);
221             if (error != ErrorCode.None)
222             {
223                 Log.Error(LogTag, "Next Failed with error " + error);
224                 throw ExceptionFactory.CreateException(error);
225             }
226         }
227
228         /// <summary>
229         /// Moves index to previous command.
230         /// </summary>
231         /// <exception cref="InvalidOperationException">
232         /// This Exception can be due to the following reaons
233         /// 1. List Empty
234         /// 2. List reached end
235         /// 3. Permission Denied
236         /// 4. Not supported
237         /// </exception>
238         public void Previous()
239         {
240             ErrorCode error = VcCmdListPrev(_handle);
241             if (error != ErrorCode.None)
242             {
243                 Log.Error(LogTag, "Previous Failed with error " + error);
244                 throw ExceptionFactory.CreateException(error);
245             }
246         }
247     }
248 }