/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Diagnostics.CodeAnalysis; namespace Tizen.Pims.Contacts { /// /// A filter includes the conditions for the search. /// /// 4 public class ContactsFilter:IDisposable { internal IntPtr _filterHandle; /// /// Creates a filter with a condition for a string type property. /// /// The view URI of a filter. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// Thrown when failed due to out of memory. /// 4 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public ContactsFilter(string viewUri, uint propertyId, StringMatchType matchType, string matchValue) { int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Interop.Filter.ContactsFilterDestroy(_filterHandle); Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Creates a filter with a condition for an integer type property. /// /// The view URI of a filter. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// Thrown when failed due to out of memory. /// 4 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public ContactsFilter(string viewUri, uint propertyId, IntegerMatchType matchType, int matchValue) { int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddInt(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Interop.Filter.ContactsFilterDestroy(_filterHandle); Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Creates a filter with a condition for a long type property. /// /// The view URI of a filter. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// Thrown when failed due to out of memory. /// 4 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public ContactsFilter(string viewUri, uint propertyId, IntegerMatchType matchType, long matchValue) { int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddLli(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Interop.Filter.ContactsFilterDestroy(_filterHandle); Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Creates a filter with a condition for a double type property. /// /// The view URI of a filter. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// Thrown when failed due to out of memory. /// 4 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public ContactsFilter(string viewUri, uint propertyId, IntegerMatchType matchType, double matchValue) { int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddDouble(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Interop.Filter.ContactsFilterDestroy(_filterHandle); Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Creates a filter with a condition for a boolean type property. /// /// The view URI of a filter. /// The property ID to add a condition. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// Thrown when failed due to out of memory. /// 4 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")] public ContactsFilter(string viewUri, uint propertyId, bool matchValue) { int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddBool(_filterHandle, propertyId, matchValue); if ((int)ContactsError.None != error) { Interop.Filter.ContactsFilterDestroy(_filterHandle); Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// The destructor. /// /// 4 ~ContactsFilter() { Dispose(false); } /// /// Enumeration for the filter match types of a string. /// /// 4 public enum StringMatchType { /// /// Full string, case-sensitive. /// /// 4 Exactly, /// /// Full string, case-insensitive. /// /// 4 FullString, /// /// Sub string, case-insensitive. /// /// 4 Contains, /// /// Start with, case-insensitive. /// /// 4 StartsWith, /// /// End with, case-insensitive. /// /// 4 EndsWith, /// /// Is not null. /// /// 4 Exists, } /// /// Enumeration for the filter match types of an integer. /// /// 4 public enum IntegerMatchType { /// /// = /// /// 4 Equal, /// /// > /// /// 4 GreaterThan, /// /// >= /// /// 4 GreaterThanOrEqual, /// /// < /// /// 4 LessThan, /// /// <= /// /// 4 LessThanOrEqual, /// /// <>, this flag can yield poor performance. /// /// 4 NotEqual, /// /// Is null. /// /// 4 None, } /// /// Enumeration for a filter operator. /// /// 4 public enum LogicalOperator { /// /// And. /// /// 4 And, /// /// Or. /// /// 4 Or, } #region IDisposable Support private bool disposedValue = false; // To detect redundant calls /// /// Releases all the resources used by the ContactsFilter. /// /// Disposing by the user. /// 4 protected virtual void Dispose(bool disposing) { if (disposing) { //Called by User //Release your own managed resources here. //You should release all of your own disposable objects here } if (!disposedValue) { int error = Interop.Filter.ContactsFilterDestroy(_filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "ContactsFilterDestroy Failed with error " + error); } disposedValue = true; } } /// /// Releases all the resources used by the ContactsFilter. /// It should be called after it has finished using the object. /// /// 4 public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion /// /// Adds a condition for a string type property. /// /// The operator type. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// 4 public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue) { int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Adds a condition for an integer type property. /// /// The operator type. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// 4 public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, int matchValue) { int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddInt(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Adds a condition for a long type property. /// /// The operator type. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// 4 public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, long matchValue) { int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddLli(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Adds a condition for a double type property. /// /// The operator type. /// The property ID to add a condition. /// The match flag. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// 4 public void AddCondition(LogicalOperator logicalOperator, uint propertyId, IntegerMatchType matchType, double matchValue) { int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddDouble(_filterHandle, propertyId, matchType, matchValue); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Adds a condition for a boolean type property. /// /// The operator type. /// The property ID to add a condition. /// The match value. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// 4 public void AddCondition(LogicalOperator logicalOperator, uint propertyId, bool matchValue) { int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddBool(_filterHandle, propertyId, matchValue); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddCondition Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } /// /// Adds a child filter to a parent filter. /// /// The operator type. /// The child filter. /// http://tizen.org/feature/contact /// Thrown when the feature is not supported. /// Thrown when one of the arguments provided to a method is not valid. /// 4 public void AddFilter(LogicalOperator logicalOperator, ContactsFilter filter) { int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } error = Interop.Filter.ContactsFilterAddFilter(_filterHandle, filter._filterHandle); if ((int)ContactsError.None != error) { Log.Error(Globals.LogTag, "AddFilter Failed with error " + error); throw ContactsErrorFactory.CheckAndCreateException(error); } } } }