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