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