Tizen 2.1 base
[external/enchant.git] / src / bindings / Enchant.Net / Dictionary.cs
1 /* Copyright (c) 2007 Eric Scott Albright\r
2  * \r
3  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  * of this software and associated documentation files (the "Software"), to deal\r
5  * in the Software without restriction, including without limitation the rights\r
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  * copies of the Software, and to permit persons to whom the Software is\r
8  * furnished to do so, subject to the following conditions:\r
9  * \r
10  * The above copyright notice and this permission notice shall be included in\r
11  * all copies or substantial portions of the Software.\r
12  * \r
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19  * THE SOFTWARE.\r
20  */\r
21 \r
22 using System;\r
23 using System.Collections.Generic;\r
24 \r
25 namespace Enchant\r
26 {\r
27         public sealed class Dictionary : IDisposable\r
28         {\r
29                 private readonly SafeDictionaryHandle _handle;\r
30                 private bool _disposed = false;\r
31                 private DictionaryInfo _info;\r
32 \r
33                 internal Dictionary(SafeDictionaryHandle handle)\r
34                 {\r
35                         if (handle == null)\r
36                         {\r
37                                 throw new ArgumentNullException("handle");\r
38                         }\r
39                         if (handle.IsInvalid)\r
40                         {\r
41                                 throw new ArgumentException("handle is invalid");\r
42                         }\r
43                         _handle = handle;\r
44                 }\r
45 \r
46                 public DictionaryInfo Information\r
47                 {\r
48                         get\r
49                         {\r
50                                 VerifyNotDisposed();\r
51                                 if (_info == null)\r
52                                 {\r
53                                         InitializeDictionaryInformation();\r
54                                 }\r
55                                 return _info;\r
56                         }\r
57                 }\r
58 \r
59     private void InitializeDictionaryInformation()\r
60     {\r
61       Bindings.enchant_dict_describe(_handle,\r
62                                      delegate(DictionaryInfo dictionary)\r
63                                      {\r
64                                        _info = dictionary;\r
65                                      });\r
66     }\r
67 \r
68     #region IDisposable Members\r
69 \r
70                 public void Dispose()\r
71                 {\r
72                         Dispose(true);\r
73                         GC.SuppressFinalize(this);\r
74                 }\r
75 \r
76                 #endregion\r
77 \r
78     private void Dispose(bool disposing)\r
79     {\r
80       if (!_disposed)\r
81       {\r
82         if (disposing)\r
83         {\r
84           // dispose-only, i.e. non-finalizable logic\r
85           _handle.Dispose();\r
86         }\r
87 \r
88         // shared (dispose and finalizable) cleanup logic\r
89         _disposed = true;\r
90         if (disposing)\r
91         {\r
92             OnDisposed(); // call it here so will throw if someone uses\r
93             // it because _disposed has been set to true;\r
94         }\r
95       }\r
96     }\r
97 \r
98     private void VerifyNotDisposed()\r
99     {\r
100       if (_disposed)\r
101       {\r
102         throw new ObjectDisposedException("Dictionary");\r
103       }\r
104     }\r
105 \r
106     public bool Check(string word)\r
107                 {\r
108                         VerifyNotDisposed();\r
109                         if (word == null)\r
110                         {\r
111                                 throw new ArgumentNullException("word");\r
112                         }\r
113                         // result is 0 if correctly spelled, positive if not, negative if error\r
114                         int result = Bindings.enchant_dict_check(_handle, word);\r
115                         if (result < 0)\r
116                         {\r
117                                 string message = Bindings.enchant_dict_get_error(_handle);\r
118                                 throw new ApplicationException(message);\r
119                         }\r
120                         return result == 0;\r
121                 }\r
122 \r
123                 public ICollection<string> Suggest(string word)\r
124                 {\r
125                         VerifyNotDisposed();\r
126                         if (word == null)\r
127                         {\r
128                                 throw new ArgumentNullException("word");\r
129                         }\r
130                         return Bindings.enchant_dict_suggest(_handle, word);\r
131                 }\r
132 \r
133                 public void Add(string word)\r
134                 {\r
135                         VerifyNotDisposed();\r
136                         if (word == null)\r
137                         {\r
138                                 throw new ArgumentNullException("word");\r
139                         }\r
140                         Bindings.enchant_dict_add(_handle, word);\r
141                 }\r
142 \r
143                 public void AddToSession(string word)\r
144                 {\r
145                         VerifyNotDisposed();\r
146                         if (word == null)\r
147                         {\r
148                                 throw new ArgumentNullException("word");\r
149                         }\r
150                         Bindings.enchant_dict_add_to_session(_handle, word);\r
151                 }\r
152 \r
153                 public bool IsAdded(string word)\r
154                 {\r
155                         VerifyNotDisposed();\r
156                         if (word == null)\r
157                         {\r
158                                 throw new ArgumentNullException("word");\r
159                         }\r
160                         return Bindings.enchant_dict_is_added(_handle, word) == 1;\r
161                 }\r
162 \r
163                 public void Remove(string word)\r
164                 {\r
165                         VerifyNotDisposed();\r
166                         if (word == null)\r
167                         {\r
168                                 throw new ArgumentNullException("word");\r
169                         }\r
170                         Bindings.enchant_dict_remove(_handle, word);\r
171                 }\r
172 \r
173                 public void RemoveFromSession(string word)\r
174                 {\r
175                         VerifyNotDisposed();\r
176                         if (word == null)\r
177                         {\r
178                                 throw new ArgumentNullException("word");\r
179                         }\r
180                         Bindings.enchant_dict_remove_from_session(_handle, word);\r
181                 }\r
182 \r
183                 public bool IsRemoved(string word)\r
184                 {\r
185                         VerifyNotDisposed();\r
186                         if (word == null)\r
187                         {\r
188                                 throw new ArgumentNullException("word");\r
189                         }\r
190                         return Bindings.enchant_dict_is_removed(_handle, word) == 1;\r
191                 }\r
192 \r
193                 public void StoreReplacement(string misspelling, string correction)\r
194                 {\r
195                         VerifyNotDisposed();\r
196                         if (misspelling == null)\r
197                         {\r
198                                 throw new ArgumentNullException("misspelling");\r
199                         }\r
200                         if (correction == null)\r
201                         {\r
202                                 throw new ArgumentNullException("correction");\r
203                         }\r
204                         Bindings.enchant_dict_store_replacement(_handle, misspelling, correction);\r
205                 }\r
206 \r
207         /// <summary>\r
208         /// Occurs when a dictionary is disposed by a call to the Dispose method\r
209         /// </summary>\r
210         public event EventHandler Disposed = delegate { };\r
211 \r
212         private void OnDisposed()\r
213         {\r
214             Disposed.Invoke(this, new EventArgs());\r
215         }\r
216         }\r
217 }