[Calendar]Remove invalid exception
[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 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 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         /// Destroy 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 of 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         protected virtual void Dispose(bool disposing)
103         {
104             if (!disposedValue)
105             {
106                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
107
108                 int error = Interop.List.Destroy(_listHandle, true);
109                 if (CalendarError.None != (CalendarError)error)
110                 {
111                     Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
112                 }
113                 disposedValue = true;
114                 GC.RemoveMemoryPressure(_memoryPressure);
115             }
116         }
117
118         /// <summary>
119         /// Releases all resources used by the CalendarList.
120         /// It should be called after having finished using of the object.
121         /// </summary>
122         public void Dispose()
123         {
124             Dispose(true);
125             GC.SuppressFinalize(this);
126         }
127 #endregion
128
129         /// <summary>
130         /// Adds a record to the calendar list.
131         /// </summary>
132         /// <since_tizen> 4 </since_tizen>
133         /// <param name="record">The record to be added</param>
134         /// <feature>http://tizen.org/feature/calendar</feature>
135         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
136         public void AddRecord(CalendarRecord record)
137         {
138             int error = Interop.List.Add(_listHandle, record._recordHandle);
139             if (CalendarError.None != (CalendarError)error)
140             {
141                 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
142                 throw CalendarErrorFactory.GetException(error);
143             }
144             record._disposedValue = true;
145             _count = -1;
146             _memoryPressure += CalendarViews.Record.AverageSize;
147         }
148
149         /// <summary>
150         /// Removes a record from the calendar list.
151         /// </summary>
152         /// <since_tizen> 4 </since_tizen>
153         /// <param name="record">The record to be removed</param>
154         /// <feature>http://tizen.org/feature/calendar</feature>
155         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
156         public void RemoveRecord(CalendarRecord record)
157         {
158             int error = Interop.List.Remove(_listHandle, record._recordHandle);
159             if (CalendarError.None != (CalendarError)error)
160             {
161                 Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error);
162                 throw CalendarErrorFactory.GetException(error);
163             }
164             record._disposedValue = false;
165             _count = -1;
166             _memoryPressure -= CalendarViews.Record.AverageSize;
167         }
168
169         /// <summary>
170         /// Retrieves a record from the calendar list.
171         /// </summary>
172         /// <since_tizen> 4 </since_tizen>
173         /// <returns>
174         /// calendar record
175         /// </returns>
176         /// <feature>http://tizen.org/feature/calendar</feature>
177         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
178         public CalendarRecord GetCurrentRecord()
179         {
180             IntPtr handle;
181             int error = Interop.List.GetCurrentRecordP(_listHandle, out handle);
182             if (CalendarError.None != (CalendarError)error)
183             {
184                 Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error);
185                 throw CalendarErrorFactory.GetException(error);
186             }
187             return new CalendarRecord(handle, true);
188         }
189
190         /// <summary>
191         /// Moves a calendar list to the previous position.
192         /// </summary>
193         /// <since_tizen> 4 </since_tizen>
194         /// <returns>
195         /// if cursor is moved to the end, it returns false.
196         /// </returns>
197         /// <feature>http://tizen.org/feature/calendar</feature>
198         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
199         public bool MovePrevious()
200         {
201             int error = Interop.List.Prev(_listHandle);
202             if (CalendarError.None == (CalendarError)error)
203             {
204                 return true;
205             }
206             else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error)
207             {
208                 Log.Debug(Globals.LogTag, "Nodata MovePrevious " + error);
209                 return false;
210             }
211             else
212             {
213                 Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error);
214                 throw CalendarErrorFactory.GetException(error);
215             }
216         }
217
218         /// <summary>
219         /// Moves a calendar list to the next position.
220         /// </summary>
221         /// <since_tizen> 4 </since_tizen>
222         /// <returns>
223         /// if cursor is moved to the end, it returns false.
224         /// </returns>
225         /// <feature>http://tizen.org/feature/calendar</feature>
226         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
227         public bool MoveNext()
228         {
229             int error = Interop.List.Next(_listHandle);
230             if (CalendarError.None == (CalendarError)error)
231             {
232                 return true;
233             }
234             else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error)
235             {
236                 Log.Debug(Globals.LogTag, "Nodata MoveNext" + error);
237                 return false;
238             }
239             else
240             {
241                 Log.Error(Globals.LogTag, "MoveNext Failed with error " + error);
242                 throw CalendarErrorFactory.GetException(error);
243             }
244         }
245
246         /// <summary>
247         /// Moves a calendar list to the first position.
248         /// </summary>
249         /// <since_tizen> 4 </since_tizen>
250         /// <feature>http://tizen.org/feature/calendar</feature>
251         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
252         public void MoveFirst()
253         {
254             int error = Interop.List.First(_listHandle);
255             if (CalendarError.None != (CalendarError)error)
256             {
257                 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
258                 throw CalendarErrorFactory.GetException(error);
259             }
260         }
261
262         /// <summary>
263         /// Moves a calendar list to the last position.
264         /// </summary>
265         /// <since_tizen> 4 </since_tizen>
266         /// <feature>http://tizen.org/feature/calendar</feature>
267         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
268         public void MoveLast()
269         {
270             int error = Interop.List.Last(_listHandle);
271             if (CalendarError.None != (CalendarError)error)
272             {
273                 Log.Error(Globals.LogTag, "MoveLast Failed with error " + error);
274                 throw CalendarErrorFactory.GetException(error);
275             }
276         }
277
278     }
279 }