Release 4.0.0-preview1-00253
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarQuery.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.Calendar
21 {
22     /// <summary>
23     /// A query is used to retrieve data which satisfies given criteria.
24     /// </summary>
25     /// <since_tizen> 4 </since_tizen>
26     /// <remarks>
27     /// A query is used to retrieve calendar data which satisfies a given criteria,
28     /// such as an integer property being greater than a given value,
29     /// or a string property containing a given substring.
30     /// A query needs a filter which can set the conditions for the search.
31     /// </remarks>
32     public class CalendarQuery:IDisposable
33     {
34         internal IntPtr _queryHandle;
35
36         /// <summary>
37         /// Creates a query.
38         /// </summary>
39         /// <since_tizen> 4 </since_tizen>
40         /// <feature>http://tizen.org/feature/calendar</feature>
41         /// <param name="viewUri">The view URI of a query</param>
42         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
43         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
44         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
45         [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
46         public CalendarQuery(string viewUri)
47         {
48             int error = Interop.Query.Create(viewUri, out _queryHandle);
49             if (CalendarError.None != (CalendarError)error)
50             {
51                 Log.Error(Globals.LogTag, "CalendarQuery Failed with error " + error);
52                 throw CalendarErrorFactory.GetException(error);
53             }
54         }
55
56         internal CalendarQuery(IntPtr handle)
57         {
58             _queryHandle = handle;
59         }
60
61         /// <summary>
62         /// Destructor
63         /// </summary>
64         ~CalendarQuery()
65         {
66             Dispose(false);
67         }
68
69 #region IDisposable Support
70         private bool disposedValue = false;
71
72         /// <summary>
73         /// Disposes of the resources (other than memory) used by the CalendarQuery.
74         /// </summary>
75         /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
76         protected virtual void Dispose(bool disposing)
77         {
78             if (!disposedValue)
79             {
80                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
81
82                 int error = Interop.Query.Destroy(_queryHandle);
83                 if (CalendarError.None != (CalendarError)error)
84                 {
85                     Log.Error(Globals.LogTag, "CalendarQueryDestroy Failed with error " + error);
86                     throw CalendarErrorFactory.GetException(error);
87                 }
88                 disposedValue = true;
89             }
90         }
91
92         /// <summary>
93         /// Releases all resources used by the CalendarQuery.
94         /// It should be called after having finished using of the object.
95         /// </summary>
96         public void Dispose()
97         {
98             Dispose(true);
99             GC.SuppressFinalize(this);
100         }
101 #endregion
102
103         /// <summary>
104         /// Adds property IDs for projection.
105         /// </summary>
106         /// <since_tizen> 4 </since_tizen>
107         /// <param name="propertyIdArray">The property ID array </param>
108         /// <feature>http://tizen.org/feature/calendar</feature>
109         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
110         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
111         public void SetProjection(uint[] propertyIdArray)
112         {
113             int error = Interop.Query.SetProjection(_queryHandle, propertyIdArray, propertyIdArray.Length);
114             if (CalendarError.None != (CalendarError)error)
115             {
116                 Log.Error(Globals.LogTag, "SetProjection Failed with error " + error);
117                 throw CalendarErrorFactory.GetException(error);
118             }
119         }
120
121         /// <summary>
122         /// Sets the "distinct" option for projection.
123         /// </summary>
124         /// <since_tizen> 4 </since_tizen>
125         /// <param name="set">If true it is set, otherwise if false it is unset</param>
126         /// <feature>http://tizen.org/feature/calendar</feature>
127         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
128         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
129         public void SetDistinct(bool set)
130         {
131             int error = Interop.Query.SetDistinct(_queryHandle, set);
132             if (CalendarError.None != (CalendarError)error)
133             {
134                 Log.Error(Globals.LogTag, "SetDistinct Failed with error " + error);
135                 throw CalendarErrorFactory.GetException(error);
136             }
137         }
138
139         /// <summary>
140         /// Sets the filter for a query.
141         /// </summary>
142         /// <since_tizen> 4 </since_tizen>
143         /// <param name="filter">The filter</param>
144         /// <feature>http://tizen.org/feature/calendar</feature>
145         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
146         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
147         public void SetFilter(CalendarFilter filter)
148         {
149             int error = Interop.Query.SetFilter(_queryHandle, filter._filterHandle);
150             if (CalendarError.None != (CalendarError)error)
151             {
152                 Log.Error(Globals.LogTag, "SetFilter Failed with error " + error);
153                 throw CalendarErrorFactory.GetException(error);
154             }
155         }
156
157         /// <summary>
158         /// Sets the sort mode for a query.
159         /// </summary>
160         /// <since_tizen> 4 </since_tizen>
161         /// <param name="propertyId">The property ID to sort</param>
162         /// <param name="isAscending">If true it sorts in the ascending order, otherwise if false it sorts in the descending order</param>
163         /// <feature>http://tizen.org/feature/calendar</feature>
164         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
165         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
166         public void SetSort(uint propertyId, bool isAscending)
167         {
168             int error = Interop.Query.SetSort(_queryHandle, propertyId, isAscending);
169             if (CalendarError.None != (CalendarError)error)
170             {
171                 Log.Error(Globals.LogTag, "SetSort Failed with error " + error);
172                 throw CalendarErrorFactory.GetException(error);
173             }
174         }
175     }
176 }