Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Contacts / Tizen.Pims.Contacts / ContactsFilter.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.Contacts;
20
21 namespace Tizen.Pims.Contacts
22 {
23     /// <summary>
24     /// A filter includes the conditions for the search
25     /// </summary>
26     public class ContactsFilter:IDisposable
27     {
28         internal IntPtr _filterHandle;
29
30         /// <summary>
31         /// Creates a filter with a condition for a string type property.
32         /// </summary>
33         /// <param name="viewUri">The view URI of a filter</param>
34         /// <param name="propertyId">The property ID to add a condition</param>
35         /// <param name="matchType">The match flag</param>
36         /// <param name="matchValue">The match value</param>
37         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
38         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
39         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
40         public ContactsFilter(string viewUri, uint propertyId, StringMatchType matchType, string matchValue)
41         {
42             int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
43             if ((int)ContactsError.None != error)
44             {
45                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
46                 throw ContactsErrorFactory.CheckAndCreateException(error);
47             }
48
49             error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue);
50             if ((int)ContactsError.None != error)
51             {
52                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
53                 throw ContactsErrorFactory.CheckAndCreateException(error);
54             }
55         }
56
57         /// <summary>
58         /// Creates a filter with a condition for an integer type property.
59         /// </summary>
60         /// <param name="viewUri">The view URI of a filter</param>
61         /// <param name="propertyId">The property ID to add a condition</param>
62         /// <param name="matchType">The match flag</param>
63         /// <param name="matchValue">The match value</param>
64         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
65         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
66         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
67         public ContactsFilter(string viewUri, uint propertyId, IntegerMatchType matchType, int matchValue)
68         {
69             int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
70             if ((int)ContactsError.None != error)
71             {
72                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
73                 throw ContactsErrorFactory.CheckAndCreateException(error);
74             }
75
76             error = Interop.Filter.ContactsFilterAddInt(_filterHandle, propertyId, matchType, matchValue);
77             if ((int)ContactsError.None != error)
78             {
79                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
80                 throw ContactsErrorFactory.CheckAndCreateException(error);
81             }
82         }
83
84         /// <summary>
85         /// Creates a filter with a condition for a long type property.
86         /// </summary>
87         /// <param name="viewUri">The view URI of a filter</param>
88         /// <param name="propertyId">The property ID to add a condition</param>
89         /// <param name="matchType">The match flag</param>
90         /// <param name="matchValue">The match value</param>
91         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
92         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
93         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
94         public ContactsFilter(string viewUri, uint propertyId, IntegerMatchType matchType, long matchValue)
95         {
96             int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
97             if ((int)ContactsError.None != error)
98             {
99                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
100                 throw ContactsErrorFactory.CheckAndCreateException(error);
101             }
102
103             error = Interop.Filter.ContactsFilterAddLli(_filterHandle, propertyId, matchType, matchValue);
104             if ((int)ContactsError.None != error)
105             {
106                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
107                 throw ContactsErrorFactory.CheckAndCreateException(error);
108             }
109         }
110
111         /// <summary>
112         /// Creates a filter with a condition for a double type property.
113         /// </summary>
114         /// <param name="viewUri">The view URI of a filter</param>
115         /// <param name="propertyId">The property ID to add a condition</param>
116         /// <param name="matchType">The match flag</param>
117         /// <param name="matchValue">The match value</param>
118         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
119         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
120         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
121         public ContactsFilter(string viewUri, uint propertyId, IntegerMatchType matchType, double matchValue)
122         {
123             int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
124             if ((int)ContactsError.None != error)
125             {
126                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
127                 throw ContactsErrorFactory.CheckAndCreateException(error);
128             }
129
130             error = Interop.Filter.ContactsFilterAddDouble(_filterHandle, propertyId, matchType, matchValue);
131             if ((int)ContactsError.None != error)
132             {
133                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
134                 throw ContactsErrorFactory.CheckAndCreateException(error);
135             }
136         }
137
138         /// <summary>
139         /// Creates a filter with a condition for a boolean type property.
140         /// </summary>
141         /// <param name="viewUri">The view URI of a filter</param>
142         /// <param name="propertyId">The property ID to add a condition</param>
143         /// <param name="matchValue">The match value</param>
144         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
145         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
146         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
147         public ContactsFilter(string viewUri, uint propertyId, bool matchValue)
148         {
149             int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
150             if ((int)ContactsError.None != error)
151             {
152                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
153                 throw ContactsErrorFactory.CheckAndCreateException(error);
154             }
155
156             error = Interop.Filter.ContactsFilterAddBool(_filterHandle, propertyId, matchValue);
157             if ((int)ContactsError.None != error)
158             {
159                 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
160                 throw ContactsErrorFactory.CheckAndCreateException(error);
161             }
162         }
163
164         ~ContactsFilter()
165         {
166             Dispose(false);
167         }
168
169         /// <summary>
170         /// Enumeration for the filter match type of a string.
171         /// </summary>
172         public enum StringMatchType
173         {
174             /// <summary>
175             /// Full string, case-sensitive
176             /// </summary>
177             Exactly,
178             /// <summary>
179             /// Full string, case-insensitive
180             /// </summary>
181             FullString,
182             /// <summary>
183             /// Sub string, case-insensitive
184             /// </summary>
185             Contains,
186             /// <summary>
187             /// Start with, case-insensitive
188             /// </summary>
189             StartsWith,
190             /// <summary>
191             /// End with, case-insensitive
192             /// </summary>
193             EndsWith,
194             /// <summary>
195             /// IS NOT NUL
196             /// </summary>
197             Exists,
198         }
199
200         /// <summary>
201         /// Enumeration for the filter match type of an integer.
202         /// </summary>
203         public enum IntegerMatchType
204         {
205             /// <summary>
206             /// =
207             /// </summary>
208             Equal,
209             /// <summary>
210             /// &gt;
211             /// </summary>
212             GreaterThan,
213             /// <summary>
214             /// &gt;=
215             /// </summary>
216             GreaterThanOrEqual,
217             /// <summary>
218             /// &lt;
219             /// </summary>
220             LessThan,
221             /// <summary>
222             /// &lt;=
223             /// </summary>
224             LessThanOrEqual,
225             /// <summary>
226             /// &lt;&gt;, this flag can yield poor performance
227             /// </summary>
228             NotEqual,
229             /// <summary>
230             /// IS NULL
231             /// </summary>
232             None,
233         }
234
235         /// <summary>
236         /// Enumeration for a filter operator.
237         /// </summary>
238         public enum LogicalOperator
239         {
240             And,
241             Or,
242         }
243
244         #region IDisposable Support
245         private bool disposedValue = false; // To detect redundant calls
246
247         protected virtual void Dispose(bool disposing)
248         {
249             if (!disposedValue)
250             {
251                 int error = Interop.Filter.ContactsFilterDestroy(_filterHandle);
252                 if ((int)ContactsError.None != error)
253                 {
254                     Log.Error(Globals.LogTag, "ContactsFilterDestroy Failed with error " + error);
255                 }
256
257                 disposedValue = true;
258             }
259         }
260
261         /// <summary>
262         /// Releases all resources used by the ContactsFilter.
263         /// It should be called after finished using of the object.
264         /// </summary>
265         public void Dispose()
266         {
267             Dispose(true);
268         }
269         #endregion
270
271         /// <summary>
272         /// Adds a condition for a string type property.
273         /// </summary>
274         /// <param name="logicalOperator">The operator type</param>
275         /// <param name="propertyId">The property ID to add a condition</param>
276         /// <param name="matchType">The match flag</param>
277         /// <param name="matchValue">The match value</param>
278         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
279         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
280         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue)
281         {
282             int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
283             if ((int)ContactsError.None != error)
284             {
285                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
286                 throw ContactsErrorFactory.CheckAndCreateException(error);
287             }
288
289             error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue);
290             if ((int)ContactsError.None != error)
291             {
292                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
293                 throw ContactsErrorFactory.CheckAndCreateException(error);
294             }
295         }
296
297         /// <summary>
298         /// Adds a condition for a integer type property.
299         /// </summary>
300         /// <param name="logicalOperator">The operator type</param>
301         /// <param name="propertyId">The property ID to add a condition</param>
302         /// <param name="matchType">The match flag</param>
303         /// <param name="matchValue">The match value</param>
304         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
305         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
306         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, int matchValue)
307         {
308             int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
309             if ((int)ContactsError.None != error)
310             {
311                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
312                 throw ContactsErrorFactory.CheckAndCreateException(error);
313             }
314
315             error = Interop.Filter.ContactsFilterAddInt(_filterHandle, propertyId, matchType, matchValue);
316             if ((int)ContactsError.None != error)
317             {
318                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
319                 throw ContactsErrorFactory.CheckAndCreateException(error);
320             }
321         }
322
323         /// <summary>
324         /// Adds a condition for a long type property.
325         /// </summary>
326         /// <param name="logicalOperator">The operator type</param>
327         /// <param name="propertyId">The property ID to add a condition</param>
328         /// <param name="matchType">The match flag</param>
329         /// <param name="matchValue">The match value</param>
330         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
331         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
332         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, long matchValue)
333         {
334             int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
335             if ((int)ContactsError.None != error)
336             {
337                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
338                 throw ContactsErrorFactory.CheckAndCreateException(error);
339             }
340
341             error = Interop.Filter.ContactsFilterAddLli(_filterHandle, propertyId, matchType, matchValue);
342             if ((int)ContactsError.None != error)
343             {
344                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
345                 throw ContactsErrorFactory.CheckAndCreateException(error);
346             }
347         }
348
349         /// <summary>
350         /// Adds a condition for a double type property.
351         /// </summary>
352         /// <param name="logicalOperator">The operator type</param>
353         /// <param name="propertyId">The property ID to add a condition</param>
354         /// <param name="matchType">The match flag</param>
355         /// <param name="matchValue">The match value</param>
356         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
357         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
358         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, double matchValue)
359         {
360             int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
361             if ((int)ContactsError.None != error)
362             {
363                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
364                 throw ContactsErrorFactory.CheckAndCreateException(error);
365             }
366
367             error = Interop.Filter.ContactsFilterAddDouble(_filterHandle, propertyId, matchType, matchValue);
368             if ((int)ContactsError.None != error)
369             {
370                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
371                 throw ContactsErrorFactory.CheckAndCreateException(error);
372             }
373         }
374
375         /// <summary>
376         /// Adds a condition for a boolean type property.
377         /// </summary>
378         /// <param name="logicalOperator">The operator type</param>
379         /// <param name="propertyId">The property ID to add a condition</param>
380         /// <param name="matchValue">The match value</param>
381         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
382         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
383         public void AddCondition(LogicalOperator logicalOperator, uint propertyId, bool matchValue)
384         {
385             int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
386             if ((int)ContactsError.None != error)
387             {
388                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
389                 throw ContactsErrorFactory.CheckAndCreateException(error);
390             }
391
392             error = Interop.Filter.ContactsFilterAddBool(_filterHandle, propertyId, matchValue);
393             if ((int)ContactsError.None != error)
394             {
395                 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
396                 throw ContactsErrorFactory.CheckAndCreateException(error);
397             }
398         }
399
400         /// <summary>
401         /// Adds a child filter to a parent filter.
402         /// </summary>
403         /// <param name="logicalOperator">The operator type</param>
404         /// <param name="filter">The child filter</param>
405         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
406         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
407         public void AddFilter(LogicalOperator logicalOperator, ContactsFilter filter)
408         {
409             int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
410             if ((int)ContactsError.None != error)
411             {
412                 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
413                 throw ContactsErrorFactory.CheckAndCreateException(error);
414             }
415
416             error = Interop.Filter.ContactsFilterAddFilter(_filterHandle, filter._filterHandle);
417             if ((int)ContactsError.None != error)
418             {
419                 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
420                 throw ContactsErrorFactory.CheckAndCreateException(error);
421             }
422         }
423     }
424 }