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