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