Release 4.0.0-preview1-00051
[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 using System.Collections;
19 using System.Collections.Generic;
20
21 namespace Tizen.Pims.Calendar
22 {
23     /// <summary>
24     /// A list of records with the same type.
25     /// </summary>
26     public class CalendarList:IDisposable
27     {
28         private Int64 _memoryPressure = 20;
29         internal int _count = -1;
30         internal IntPtr _listHandle;
31
32         internal CalendarList(IntPtr handle)
33         {
34             _listHandle = handle;
35
36             _memoryPressure += this.Count * CalendarViews.AverageSizeOfRecord;
37             GC.AddMemoryPressure(_memoryPressure);
38         }
39
40         /// <summary>
41         /// Creates a calendar list.
42         /// </summary>
43         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
44         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
45         public CalendarList()
46         {
47             int error = Interop.Calendar.List.Create(out _listHandle);
48             if (CalendarError.None != (CalendarError)error)
49             {
50                 Log.Error(Globals.LogTag, "CalendarList Failed with error " + error);
51                 throw CalendarErrorFactory.GetException(error);
52             }
53             GC.AddMemoryPressure(_memoryPressure);
54         }
55
56         ~CalendarList()
57         {
58             Dispose(false);
59         }
60
61         /// <summary>
62         /// The count of the calendar entity.
63         /// </summary>
64         public int Count
65         {
66             get
67             {
68                 if (_count == -1)
69                 {
70                     int count = -1;
71                     int error = Interop.Calendar.List.GetCount(_listHandle, out count);
72                     if (CalendarError.None != (CalendarError)error)
73                     {
74                         Log.Error(Globals.LogTag, "GetCount Failed with error " + error);
75                     }
76                     _count = count;
77                 }
78                 return _count;
79             }
80         }
81
82 #region IDisposable Support
83         private bool disposedValue = false; // To detect redundant calls
84
85         protected virtual void Dispose(bool disposing)
86         {
87             if (!disposedValue)
88             {
89                 Log.Debug(Globals.LogTag, "Dispose :" + disposing);
90
91                 int error = Interop.Calendar.List.Destroy(_listHandle, true);
92                 if (CalendarError.None != (CalendarError)error)
93                 {
94                     Log.Error(Globals.LogTag, "Destroy Failed with error " + error);
95                     throw CalendarErrorFactory.GetException(error);
96                 }
97                 disposedValue = true;
98                 GC.RemoveMemoryPressure(_memoryPressure);
99             }
100         }
101
102         /// <summary>
103         /// Releases all resources used by the CalendarList.
104         /// It should be called after finished using of the object.
105         /// </summary>
106         public void Dispose()
107         {
108             Dispose(true);
109         }
110 #endregion
111
112         /// <summary>
113         /// Adds a record to the calendar list.
114         /// </summary>
115         /// <param name="record">The record to be added</param>
116         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
117         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
118         public void AddRecord(CalendarRecord record)
119         {
120             int error = Interop.Calendar.List.Add(_listHandle, record._recordHandle);
121             if (CalendarError.None != (CalendarError)error)
122             {
123                 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
124                 throw CalendarErrorFactory.GetException(error);
125             }
126             record._disposedValue = true;
127             _count = -1;
128             _memoryPressure += CalendarViews.AverageSizeOfRecord;
129         }
130
131         /// <summary>
132         /// Removes a record from the calendar list.
133         /// </summary>
134         /// <param name="record">The record to be removed</param>
135         /// <exception cref="NotSupportedException">Thrown when an invoked method 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 RemoveRecord(CalendarRecord record)
138         {
139             int error = Interop.Calendar.List.Remove(_listHandle, record._recordHandle);
140             if (CalendarError.None != (CalendarError)error)
141             {
142                 Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error);
143                 throw CalendarErrorFactory.GetException(error);
144             }
145             record._disposedValue = false;
146             _count = -1;
147             _memoryPressure -= CalendarViews.AverageSizeOfRecord;
148         }
149
150         /// <summary>
151         /// Retrieves a record from the calendar list.
152         /// </summary>
153         /// <returns>
154         /// calendar record
155         /// </returns>
156         public CalendarRecord GetCurrentRecord()
157         {
158             IntPtr handle;
159             int error = Interop.Calendar.List.GetCurrentRecordP(_listHandle, out handle);
160             if (CalendarError.None != (CalendarError)error)
161             {
162                 Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error);
163                 throw CalendarErrorFactory.GetException(error);
164             }
165             return new CalendarRecord(handle, true);
166         }
167
168         /// <summary>
169         /// Moves a calendar list to the previous position.
170         /// </summary>
171         /// <returns>
172         /// if cursor is moved to the end, it returns false.
173         /// </returns>
174         public bool MovePrevious()
175         {
176             int error = Interop.Calendar.List.Prev(_listHandle);
177             if (CalendarError.None == (CalendarError)error)
178             {
179                 return true;
180             }
181             else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error)
182             {
183                 Log.Debug(Globals.LogTag, "Nodata MovePrevious " + error);
184                 return false;
185             }
186             else
187             {
188                 Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error);
189                 throw CalendarErrorFactory.GetException(error);
190             }
191         }
192
193         /// <summary>
194         /// Moves a calendar list to the next position.
195         /// </summary>
196         /// <returns>
197         /// if cursor is moved to the end, it returns false.
198         /// </returns>
199         public bool MoveNext()
200         {
201             int error = Interop.Calendar.List.Next(_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 MoveNext" + error);
209                 return false;
210             }
211             else
212             {
213                 Log.Error(Globals.LogTag, "MoveNext Failed with error " + error);
214                 throw CalendarErrorFactory.GetException(error);
215             }
216         }
217
218         /// <summary>
219         /// Moves a calendar list to the first position.
220         /// </summary>
221         public void MoveFirst()
222         {
223             int error = Interop.Calendar.List.First(_listHandle);
224             if (CalendarError.None != (CalendarError)error)
225             {
226                 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
227                 throw CalendarErrorFactory.GetException(error);
228             }
229         }
230
231         /// <summary>
232         /// Moves a calendar list to the last position.
233         /// </summary>
234         public void MoveLast()
235         {
236             int error = Interop.Calendar.List.Last(_listHandle);
237             if (CalendarError.None != (CalendarError)error)
238             {
239                 Log.Error(Globals.LogTag, "MoveLast Failed with error " + error);
240                 throw CalendarErrorFactory.GetException(error);
241             }
242         }
243
244     }
245 }