[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarList.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
19 namespace Tizen.Pims.Calendar
20 {
21     /// <summary>
22     /// A list of records with the same type.
23     /// </summary>
24     /// <since_tizen> 4 </since_tizen>
25     public class CalendarList:IDisposable
26     {
27         private Int64 _memoryPressure = 20;
28         internal int _count = -1;
29         internal IntPtr _listHandle;
30
31         internal CalendarList(IntPtr handle)
32         {
33             _listHandle = handle;
34
35             int error = Interop.List.First(_listHandle);
36             if (CalendarError.None != (CalendarError)error)
37             {
38                 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
39                 throw CalendarErrorFactory.GetException(error);
40             }
41
42             _memoryPressure += this.Count * CalendarViews.Record.AverageSize;
43             GC.AddMemoryPressure(_memoryPressure);
44         }
45
46         /// <summary>
47         /// Creates a calendar list.
48         /// </summary>
49         /// <since_tizen> 4 </since_tizen>
50         /// <feature>http://tizen.org/feature/calendar</feature>
51         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
52         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
53         public CalendarList()
54         {
55             int error = Interop.List.Create(out _listHandle);
56             if (CalendarError.None != (CalendarError)error)
57             {
58                 Log.Error(Globals.LogTag, "CalendarList Failed with error " + error);
59                 throw CalendarErrorFactory.GetException(error);
60             }
61             GC.AddMemoryPressure(_memoryPressure);
62         }
63
64         /// <summary>
65         /// The count of the calendar entity.
66         /// </summary>
67         /// <since_tizen> 4 </since_tizen>
68         /// <value>The count of the calendar entity.</value>
69         public int Count
70         {
71             get
72             {
73                 if (_count == -1)
74                 {
75                     int count = -1;
76                     int error = Interop.List.GetCount(_listHandle, out count);
77                     if (CalendarError.None != (CalendarError)error)
78                     {
79                         Log.Error(Globals.LogTag, "GetCount Failed with error " + error);
80                     }
81                     _count = count;
82                 }
83                 return _count;
84             }
85         }
86
87         /// <summary>
88         /// Destroys the CalendarList resource.
89         /// </summary>
90         ~CalendarList()
91         {
92             Dispose(false);
93         }
94
95 #region IDisposable Support
96         private bool disposedValue = false; // To detect redundant calls
97
98         /// <summary>
99         /// Disposes off the resources (other than memory) used by the CalendarList.
100         /// </summary>
101         /// <param name="disposing">true to release both managed and unmanaged resources, false to release only unmanaged resources.</param>
102         /// <since_tizen> 4 </since_tizen>
103         protected virtual void Dispose(bool disposing)
104         {
105             if (!disposedValue)
106             {
107                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
108
109                 int error = Interop.List.Destroy(_listHandle, true);
110                 if (CalendarError.None != (CalendarError)error)
111                 {
112                     Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
113                 }
114                 disposedValue = true;
115                 GC.RemoveMemoryPressure(_memoryPressure);
116             }
117         }
118
119         /// <summary>
120         /// Releases all resources used by the CalendarList.
121         /// It should be called after it has finished using the object.
122         /// </summary>
123         /// <since_tizen> 4 </since_tizen>
124         public void Dispose()
125         {
126             Dispose(true);
127             GC.SuppressFinalize(this);
128         }
129 #endregion
130
131         /// <summary>
132         /// Adds a record to the calendar list.
133         /// </summary>
134         /// <since_tizen> 4 </since_tizen>
135         /// <param name="record">The record to be added.</param>
136         /// <feature>http://tizen.org/feature/calendar</feature>
137         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
138         public void AddRecord(CalendarRecord record)
139         {
140             int error = Interop.List.Add(_listHandle, record._recordHandle);
141             if (CalendarError.None != (CalendarError)error)
142             {
143                 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
144                 throw CalendarErrorFactory.GetException(error);
145             }
146             record._disposedValue = true;
147             _count = -1;
148             _memoryPressure += CalendarViews.Record.AverageSize;
149         }
150
151         /// <summary>
152         /// Removes a record from the calendar list.
153         /// </summary>
154         /// <since_tizen> 4 </since_tizen>
155         /// <param name="record">The record to be removed.</param>
156         /// <feature>http://tizen.org/feature/calendar</feature>
157         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
158         public void RemoveRecord(CalendarRecord record)
159         {
160             int error = Interop.List.Remove(_listHandle, record._recordHandle);
161             if (CalendarError.None != (CalendarError)error)
162             {
163                 Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error);
164                 throw CalendarErrorFactory.GetException(error);
165             }
166             record._disposedValue = false;
167             _count = -1;
168             _memoryPressure -= CalendarViews.Record.AverageSize;
169         }
170
171         /// <summary>
172         /// Retrieves a record from the calendar list.
173         /// </summary>
174         /// <since_tizen> 4 </since_tizen>
175         /// <returns>
176         /// The calendar record.
177         /// </returns>
178         /// <feature>http://tizen.org/feature/calendar</feature>
179         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
180         public CalendarRecord GetCurrentRecord()
181         {
182             IntPtr handle;
183             int error = Interop.List.GetCurrentRecordP(_listHandle, out handle);
184             if (CalendarError.None != (CalendarError)error)
185             {
186                 Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error);
187                 throw CalendarErrorFactory.GetException(error);
188             }
189             return new CalendarRecord(handle, true);
190         }
191
192         /// <summary>
193         /// Moves a calendar list to the previous position.
194         /// </summary>
195         /// <since_tizen> 4 </since_tizen>
196         /// <returns>
197         /// If the cursor is moved to the end, it returns false.
198         /// </returns>
199         /// <feature>http://tizen.org/feature/calendar</feature>
200         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
201         public bool MovePrevious()
202         {
203             int error = Interop.List.Prev(_listHandle);
204             if (CalendarError.None == (CalendarError)error)
205             {
206                 return true;
207             }
208             else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error)
209             {
210                 Log.Debug(Globals.LogTag, "Nodata MovePrevious " + error);
211                 return false;
212             }
213             else
214             {
215                 Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error);
216                 throw CalendarErrorFactory.GetException(error);
217             }
218         }
219
220         /// <summary>
221         /// Moves a calendar list to the next position.
222         /// </summary>
223         /// <since_tizen> 4 </since_tizen>
224         /// <returns>
225         /// If the cursor is moved to the end, it returns false.
226         /// </returns>
227         /// <feature>http://tizen.org/feature/calendar</feature>
228         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
229         public bool MoveNext()
230         {
231             int error = Interop.List.Next(_listHandle);
232             if (CalendarError.None == (CalendarError)error)
233             {
234                 return true;
235             }
236             else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error)
237             {
238                 Log.Debug(Globals.LogTag, "Nodata MoveNext" + error);
239                 return false;
240             }
241             else
242             {
243                 Log.Error(Globals.LogTag, "MoveNext Failed with error " + error);
244                 throw CalendarErrorFactory.GetException(error);
245             }
246         }
247
248         /// <summary>
249         /// Moves a calendar list to the first position.
250         /// </summary>
251         /// <since_tizen> 4 </since_tizen>
252         /// <feature>http://tizen.org/feature/calendar</feature>
253         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
254         public void MoveFirst()
255         {
256             int error = Interop.List.First(_listHandle);
257             if (CalendarError.None != (CalendarError)error)
258             {
259                 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
260                 throw CalendarErrorFactory.GetException(error);
261             }
262         }
263
264         /// <summary>
265         /// Moves a calendar list to the last position.
266         /// </summary>
267         /// <since_tizen> 4 </since_tizen>
268         /// <feature>http://tizen.org/feature/calendar</feature>
269         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
270         public void MoveLast()
271         {
272             int error = Interop.List.Last(_listHandle);
273             if (CalendarError.None != (CalendarError)error)
274             {
275                 Log.Error(Globals.LogTag, "MoveLast Failed with error " + error);
276                 throw CalendarErrorFactory.GetException(error);
277             }
278         }
279
280     }
281 }