Release 4.0.0-preview1-00279
[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         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
137         public void AddRecord(CalendarRecord record)
138         {
139             int error = Interop.List.Add(_listHandle, record._recordHandle);
140             if (CalendarError.None != (CalendarError)error)
141             {
142                 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
143                 throw CalendarErrorFactory.GetException(error);
144             }
145             record._disposedValue = true;
146             _count = -1;
147             _memoryPressure += CalendarViews.Record.AverageSize;
148         }
149
150         /// <summary>
151         /// Removes a record from the calendar list.
152         /// </summary>
153         /// <since_tizen> 4 </since_tizen>
154         /// <param name="record">The record to be removed</param>
155         /// <feature>http://tizen.org/feature/calendar</feature>
156         /// <exception cref="NotSupportedException">Thrown when feature is not supported</exception>
157         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</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         /// calendar record
177         /// </returns>
178         /// <feature>http://tizen.org/feature/calendar</feature>
179         /// <exception cref="NotSupportedException">Thrown when 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 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 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 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 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 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 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 }