Release 4.0.0-preview1-00097
[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.Record.AverageSize;
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         /// <summary>
60         /// Destructor
61         /// </summary>
62         ~ContactsList()
63         {
64             Dispose(false);
65         }
66
67         /// <summary>
68         /// The count of contact entity.
69         /// </summary>
70         /// <value>The count of contact entity.</value>
71         public int Count
72         {
73             get
74             {
75                 int count = -1;
76                 int error = Interop.List.ContactsListGetCount(_listHandle, out count);
77                 if ((int)ContactsError.None != error)
78                 {
79                     Log.Error(Globals.LogTag, "ContactsList Count Failed with error " + error);
80                 }
81                 return count;
82             }
83         }
84
85         #region IDisposable Support
86         private bool disposedValue = false; // To detect redundant calls
87
88         /// <summary>
89         /// Releases all resources used by the ContactsList.
90         /// </summary>
91         /// <param name="disposing">Disposing by User</param>
92         protected virtual void Dispose(bool disposing)
93         {
94             if (disposing)
95             {
96                 //Called by User
97                 //Release your own managed resources here.
98                 //You should release all of your own disposable objects here
99             }
100
101             if (!disposedValue)
102             {
103                 int error = Interop.List.ContactsListDestroy(_listHandle, true);
104                 if ((int)ContactsError.None != error)
105                 {
106                     Log.Error(Globals.LogTag, "ContactsListDestroy Failed with error " + error);
107                 }
108
109                 disposedValue = true;
110                 GC.RemoveMemoryPressure(_memoryPressure);
111             }
112         }
113
114         /// <summary>
115         /// Releases all resources used by the ContactsList.
116         /// It should be called after finished using of the object.
117         /// </summary>
118         public void Dispose()
119         {
120             Dispose(true);
121             GC.SuppressFinalize(this);
122         }
123         #endregion
124
125         /// <summary>
126         /// Adds a record to the contacts list.
127         /// </summary>
128         /// <param name="record">The record to add</param>
129         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
130         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
131         public void AddRecord(ContactsRecord record)
132         {
133             int error = Interop.List.ContactsListAdd(_listHandle, record._recordHandle);
134             if ((int)ContactsError.None != error)
135             {
136                 Log.Error(Globals.LogTag, "AddRecord Failed with error " + error);
137                 throw ContactsErrorFactory.CheckAndCreateException(error);
138             }
139             record._disposedValue = true;
140             _memoryPressure += ContactsViews.Record.AverageSize;
141         }
142
143         /// <summary>
144         /// Removes a record from the contacts list.
145         /// </summary>
146         /// <param name="record">The record to remove</param>
147         /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
148         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
149         public void RemoveRecord(ContactsRecord record)
150         {
151             int error = Interop.List.ContactsListRemove(_listHandle, record._recordHandle);
152             if ((int)ContactsError.None != error)
153             {
154                 Log.Error(Globals.LogTag, "RemoveRecord Failed with error " + error);
155                 throw ContactsErrorFactory.CheckAndCreateException(error);
156             }
157             record._disposedValue = false;
158             _memoryPressure -= ContactsViews.Record.AverageSize;
159         }
160
161         /// <summary>
162         /// Retrieves a record from the contacts list.
163         /// </summary>
164         /// <returns>
165         /// contacts record
166         /// </returns>
167         public ContactsRecord GetCurrentRecord()
168         {
169             IntPtr handle;
170             int error = Interop.List.ContactsListGetCurrentRecordP(_listHandle, out handle);
171             if ((int)ContactsError.None != error)
172             {
173                 Log.Error(Globals.LogTag, "GetCurrentRecord Failed with error " + error);
174                 throw ContactsErrorFactory.CheckAndCreateException(error);
175             }
176             return new ContactsRecord(handle, true);
177         }
178
179         /// <summary>
180         /// Moves a contacts list to the previous position.
181         /// </summary>
182         /// <returns>
183         /// When the cursor is already at the first position, it returns false.
184         /// </returns>
185         public bool MovePrevious()
186         {
187             int error = Interop.List.ContactsListPrev(_listHandle);
188
189             if ((int)ContactsError.None == error)
190             {
191                 return true;
192             }
193             else if (Count > 0 && (int)ContactsError.NoData == error)
194             {
195                 Log.Debug(Globals.LogTag, "Nodata MovePrevious" + error);
196                 return false;
197             }
198             else
199             {
200                 Log.Error(Globals.LogTag, "MovePrevious Failed with error " + error);
201                 throw ContactsErrorFactory.CheckAndCreateException(error);
202             }
203         }
204
205         /// <summary>
206         /// Moves a contacts list to the next position.
207         /// </summary>
208         /// <returns>
209         /// When the cursor is already at the last position, it returns false.
210         /// </returns>
211         public bool MoveNext()
212         {
213             int error = Interop.List.ContactsListNext(_listHandle);
214
215             if ((int)ContactsError.None == error)
216             {
217                 return true;
218             }
219             else if (Count > 0 && (int)ContactsError.NoData == error)
220             {
221                 Log.Debug(Globals.LogTag, "Nodata MoveNext" + error);
222                 return false;
223             }
224             else
225             {
226                 Log.Error(Globals.LogTag, "MoveNext Failed with error " + error);
227                 throw ContactsErrorFactory.CheckAndCreateException(error);
228             }
229         }
230
231         /// <summary>
232         /// Moves a contacts list to the first position.
233         /// </summary>
234         public void MoveFirst()
235         {
236             int error = Interop.List.ContactsListFirst(_listHandle);
237             if ((int)ContactsError.None != error)
238             {
239                 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
240                 throw ContactsErrorFactory.CheckAndCreateException(error);
241             }
242         }
243
244         /// <summary>
245         /// Moves a contacts list to the last position.
246         /// </summary>
247         public void MoveLast()
248         {
249             int error = Interop.List.ContactsListLast(_listHandle);
250             if ((int)ContactsError.None != error)
251             {
252                 Log.Error(Globals.LogTag, "MoveFirst Failed with error " + error);
253                 throw ContactsErrorFactory.CheckAndCreateException(error);
254             }
255         }
256     }
257 }