/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace Tizen.Pims.Calendar { /// /// A list of records with the same type. /// public class CalendarList:IDisposable { private Int64 _memoryPressure = 20; internal int _count = -1; internal IntPtr _listHandle; internal CalendarList(IntPtr handle) { _listHandle = handle; _memoryPressure += this.Count * CalendarViews.Record.AverageSize; GC.AddMemoryPressure(_memoryPressure); } /// /// Creates a calendar list. /// /// Thrown when an invoked method is not supported /// Thrown when failed due to out of memory public CalendarList() { int error = Interop.List.Create(out _listHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "CalendarList Failed with error " + error); throw CalendarErrorFactory.GetException(error); } GC.AddMemoryPressure(_memoryPressure); } /// /// The count of the calendar entity. /// /// The count of calendar entity. public int Count { get { if (_count == -1) { int count = -1; int error = Interop.List.GetCount(_listHandle, out count); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetCount Failed with error " + error); } _count = count; } return _count; } } /// /// Destory CalendarList resource. /// ~CalendarList() { Dispose(false); } #region IDisposable Support private bool disposedValue = false; // To detect redundant calls /// /// Disposes of the resources (other than memory) used by the CalendarList. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (!disposedValue) { Log.Debug(Globals.LogTag, "Dispose :" + disposing); int error = Interop.List.Destroy(_listHandle, true); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "Destroy Failed with error " + error); throw CalendarErrorFactory.GetException(error); } disposedValue = true; GC.RemoveMemoryPressure(_memoryPressure); } } /// /// Releases all resources used by the CalendarList. /// It should be called after having finished using of the object. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion /// /// Adds a record to the calendar list. /// /// The record to be added /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void AddRecord(CalendarRecord record) { int error = Interop.List.Add(_listHandle, record._recordHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "AddRecord Failed with error " + error); throw CalendarErrorFactory.GetException(error); } record._disposedValue = true; _count = -1; _memoryPressure += CalendarViews.Record.AverageSize; } /// /// Removes a record from the calendar list. /// /// The record to be removed /// Thrown when an invoked method is not supported /// Thrown when one of the arguments provided to a method is not valid public void RemoveRecord(CalendarRecord record) { int error = Interop.List.Remove(_listHandle, record._recordHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error); throw CalendarErrorFactory.GetException(error); } record._disposedValue = false; _count = -1; _memoryPressure -= CalendarViews.Record.AverageSize; } /// /// Retrieves a record from the calendar list. /// /// /// calendar record /// public CalendarRecord GetCurrentRecord() { IntPtr handle; int error = Interop.List.GetCurrentRecordP(_listHandle, out handle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error); throw CalendarErrorFactory.GetException(error); } return new CalendarRecord(handle, true); } /// /// Moves a calendar list to the previous position. /// /// /// if cursor is moved to the end, it returns false. /// public bool MovePrevious() { int error = Interop.List.Prev(_listHandle); if (CalendarError.None == (CalendarError)error) { return true; } else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error) { Log.Debug(Globals.LogTag, "Nodata MovePrevious " + error); return false; } else { Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Moves a calendar list to the next position. /// /// /// if cursor is moved to the end, it returns false. /// public bool MoveNext() { int error = Interop.List.Next(_listHandle); if (CalendarError.None == (CalendarError)error) { return true; } else if (this.Count > 0 && CalendarError.NoData == (CalendarError)error) { Log.Debug(Globals.LogTag, "Nodata MoveNext" + error); return false; } else { Log.Error(Globals.LogTag, "MoveNext Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Moves a calendar list to the first position. /// public void MoveFirst() { int error = Interop.List.First(_listHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } /// /// Moves a calendar list to the last position. /// public void MoveLast() { int error = Interop.List.Last(_listHandle); if (CalendarError.None != (CalendarError)error) { Log.Error(Globals.LogTag, "MoveLast Failed with error " + error); throw CalendarErrorFactory.GetException(error); } } } }