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