2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using static Interop.Contacts;
21 namespace Tizen.Pims.Contacts
24 /// A filter includes the conditions for the search
26 public class ContactsFilter:IDisposable
28 internal IntPtr _filterHandle;
31 /// Creates a filter with a condition for a string type property.
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)
42 int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
43 if ((int)ContactsError.None != error)
45 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
46 throw ContactsErrorFactory.CheckAndCreateException(error);
49 error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue);
50 if ((int)ContactsError.None != error)
52 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
53 throw ContactsErrorFactory.CheckAndCreateException(error);
58 /// Creates a filter with a condition for an integer type property.
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)
69 int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
70 if ((int)ContactsError.None != error)
72 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
73 throw ContactsErrorFactory.CheckAndCreateException(error);
76 error = Interop.Filter.ContactsFilterAddInt(_filterHandle, propertyId, matchType, matchValue);
77 if ((int)ContactsError.None != error)
79 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
80 throw ContactsErrorFactory.CheckAndCreateException(error);
85 /// Creates a filter with a condition for a long type property.
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)
96 int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
97 if ((int)ContactsError.None != error)
99 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
100 throw ContactsErrorFactory.CheckAndCreateException(error);
103 error = Interop.Filter.ContactsFilterAddLli(_filterHandle, propertyId, matchType, matchValue);
104 if ((int)ContactsError.None != error)
106 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
107 throw ContactsErrorFactory.CheckAndCreateException(error);
112 /// Creates a filter with a condition for a double type property.
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)
123 int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
124 if ((int)ContactsError.None != error)
126 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
127 throw ContactsErrorFactory.CheckAndCreateException(error);
130 error = Interop.Filter.ContactsFilterAddDouble(_filterHandle, propertyId, matchType, matchValue);
131 if ((int)ContactsError.None != error)
133 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
134 throw ContactsErrorFactory.CheckAndCreateException(error);
139 /// Creates a filter with a condition for a boolean type property.
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)
149 int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);
150 if ((int)ContactsError.None != error)
152 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
153 throw ContactsErrorFactory.CheckAndCreateException(error);
156 error = Interop.Filter.ContactsFilterAddBool(_filterHandle, propertyId, matchValue);
157 if ((int)ContactsError.None != error)
159 Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
160 throw ContactsErrorFactory.CheckAndCreateException(error);
170 /// Enumeration for the filter match type of a string.
172 public enum StringMatchType
175 /// Full string, case-sensitive
179 /// Full string, case-insensitive
183 /// Sub string, case-insensitive
187 /// Start with, case-insensitive
191 /// End with, case-insensitive
201 /// Enumeration for the filter match type of an integer.
203 public enum IntegerMatchType
226 /// <>, this flag can yield poor performance
236 /// Enumeration for a filter operator.
238 public enum LogicalOperator
244 #region IDisposable Support
245 private bool disposedValue = false; // To detect redundant calls
247 protected virtual void Dispose(bool disposing)
251 int error = Interop.Filter.ContactsFilterDestroy(_filterHandle);
252 if ((int)ContactsError.None != error)
254 Log.Error(Globals.LogTag, "ContactsFilterDestroy Failed with error " + error);
257 disposedValue = true;
262 /// Releases all resources used by the ContactsFilter.
263 /// It should be called after finished using of the object.
265 public void Dispose()
272 /// Adds a condition for a string type property.
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)
282 int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
283 if ((int)ContactsError.None != error)
285 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
286 throw ContactsErrorFactory.CheckAndCreateException(error);
289 error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue);
290 if ((int)ContactsError.None != error)
292 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
293 throw ContactsErrorFactory.CheckAndCreateException(error);
298 /// Adds a condition for a integer type property.
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)
308 int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
309 if ((int)ContactsError.None != error)
311 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
312 throw ContactsErrorFactory.CheckAndCreateException(error);
315 error = Interop.Filter.ContactsFilterAddInt(_filterHandle, propertyId, matchType, matchValue);
316 if ((int)ContactsError.None != error)
318 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
319 throw ContactsErrorFactory.CheckAndCreateException(error);
324 /// Adds a condition for a long type property.
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)
334 int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
335 if ((int)ContactsError.None != error)
337 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
338 throw ContactsErrorFactory.CheckAndCreateException(error);
341 error = Interop.Filter.ContactsFilterAddLli(_filterHandle, propertyId, matchType, matchValue);
342 if ((int)ContactsError.None != error)
344 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
345 throw ContactsErrorFactory.CheckAndCreateException(error);
350 /// Adds a condition for a double type property.
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)
360 int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
361 if ((int)ContactsError.None != error)
363 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
364 throw ContactsErrorFactory.CheckAndCreateException(error);
367 error = Interop.Filter.ContactsFilterAddDouble(_filterHandle, propertyId, matchType, matchValue);
368 if ((int)ContactsError.None != error)
370 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
371 throw ContactsErrorFactory.CheckAndCreateException(error);
376 /// Adds a condition for a boolean type property.
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)
385 int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
386 if ((int)ContactsError.None != error)
388 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
389 throw ContactsErrorFactory.CheckAndCreateException(error);
392 error = Interop.Filter.ContactsFilterAddBool(_filterHandle, propertyId, matchValue);
393 if ((int)ContactsError.None != error)
395 Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
396 throw ContactsErrorFactory.CheckAndCreateException(error);
401 /// Adds a child filter to a parent filter.
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)
409 int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);
410 if ((int)ContactsError.None != error)
412 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
413 throw ContactsErrorFactory.CheckAndCreateException(error);
416 error = Interop.Filter.ContactsFilterAddFilter(_filterHandle, filter._filterHandle);
417 if ((int)ContactsError.None != error)
419 Log.Error(Globals.LogTag, "AddFilter Failed with error " + error);
420 throw ContactsErrorFactory.CheckAndCreateException(error);