2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Pims.Contacts
24 /// A record represents an actual record in the database
27 /// A record represents an actual record in the database, but you can also consider it a piece of information, such as an address, a phone number, or a group of contacts.
28 /// A record can be a complex set of data, containing other data. For example, a contact record contains the address property, which is a reference to an address record.
29 /// An address record belongs to a contact record, and its ContactId property is set to the identifier of the corresponding contact. In this case, the address is the child record of the contact and the contact is the parent record.
31 public class ContactsRecord : IDisposable
33 private string _uri = null;
35 private Int64 _memoryPressure = ContactsViews.AverageSizeOfRecord;
36 internal IntPtr _recordHandle;
38 internal ContactsRecord(IntPtr handle)
40 _recordHandle = handle;
42 int error = Interop.Record.GetUriP(handle, out viewUri);
43 if ((int)ContactsError.None != error)
45 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
46 throw ContactsErrorFactory.CheckAndCreateException(error);
48 GC.AddMemoryPressure(_memoryPressure);
49 _uri = Marshal.PtrToStringAnsi(viewUri);
52 internal ContactsRecord(IntPtr handle, bool disposedValue)
54 _recordHandle = handle;
55 _disposedValue = disposedValue;
57 int error = Interop.Record.GetUriP(handle, out viewUri);
58 if ((int)ContactsError.None != error)
60 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
61 throw ContactsErrorFactory.CheckAndCreateException(error);
65 GC.AddMemoryPressure(_memoryPressure);
66 _uri = Marshal.PtrToStringAnsi(viewUri);
69 internal ContactsRecord(IntPtr handle, int id)
71 _recordHandle = handle;
74 int error = Interop.Record.GetUriP(handle, out viewUri);
75 if ((int)ContactsError.None != error)
77 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
78 throw ContactsErrorFactory.CheckAndCreateException(error);
80 _uri = Marshal.PtrToStringAnsi(viewUri);
81 GC.AddMemoryPressure(_memoryPressure);
87 /// <param name="viewUri">The view URI</param>
88 /// <exception cref="NotSupportedException">Thrown when an invoked method is not supported</exception>
89 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
90 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
91 public ContactsRecord(string viewUri)
93 int error = Interop.Record.Create(viewUri, out _recordHandle);
94 if ((int)ContactsError.None != error)
96 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
97 throw ContactsErrorFactory.CheckAndCreateException(error);
100 GC.AddMemoryPressure(_memoryPressure);
109 /// The URI of the record
119 #region IDisposable Support
120 internal bool _disposedValue = false; // To detect redundant calls
122 protected virtual void Dispose(bool disposing)
126 int error = Interop.Record.Destroy(_recordHandle, true);
127 if ((int)ContactsError.None != error)
129 Log.Error(Globals.LogTag, "Dispose Failed with error " + error);
131 _disposedValue = true;
132 GC.RemoveMemoryPressure(_memoryPressure);
137 /// Releases all resources used by the ContactsRecord.
138 /// It should be called after finished using of the object.
140 public void Dispose()
147 /// Makes a clone of a record.
149 /// <returns>A cloned record</returns>
150 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
151 public ContactsRecord Clone()
153 IntPtr _clonedRecordHandle;
154 int error = Interop.Record.Clone(_recordHandle, out _clonedRecordHandle);
155 if ((int)ContactsError.None != error)
157 Log.Error(Globals.LogTag, "Clone Failed with error " + error);
158 throw ContactsErrorFactory.CheckAndCreateException(error);
160 return new ContactsRecord(_clonedRecordHandle, (int)_id);
164 /// Gets a value of the property from a record.
166 /// <param name="propertyId">The property ID</param>
168 /// The value of the property corresponding to property id.
170 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
171 public T Get<T>(uint propertyId)
173 object parsedValue = null;
174 if (typeof(T) == typeof(string))
177 int error = Interop.Record.GetStr(_recordHandle, propertyId, out val);
178 if ((int)ContactsError.None != error)
180 Log.Error(Globals.LogTag, "Get String Failed with error " + error);
181 throw ContactsErrorFactory.CheckAndCreateException(error);
183 parsedValue = Convert.ChangeType(val, typeof(T));
185 else if (typeof(T) == typeof(int))
188 int error = Interop.Record.GetInt(_recordHandle, propertyId, out val);
189 if ((int)ContactsError.None != error)
191 Log.Error(Globals.LogTag, "Get Int Failed with error " + error);
192 throw ContactsErrorFactory.CheckAndCreateException(error);
194 parsedValue = Convert.ChangeType(val, typeof(T));
196 else if (typeof(T) == typeof(bool))
199 int error = Interop.Record.GetBool(_recordHandle, propertyId, out val);
200 if ((int)ContactsError.None != error)
202 Log.Error(Globals.LogTag, "Get Bool Failed with error " + error);
203 throw ContactsErrorFactory.CheckAndCreateException(error);
205 parsedValue = Convert.ChangeType(val, typeof(T));
207 else if (typeof(T) == typeof(long))
210 int error = Interop.Record.GetLli(_recordHandle, propertyId, out val);
211 if ((int)ContactsError.None != error)
213 Log.Error(Globals.LogTag, "Get Long Failed with error " + error);
214 throw ContactsErrorFactory.CheckAndCreateException(error);
216 parsedValue = Convert.ChangeType(val, typeof(T));
218 else if (typeof(T) == typeof(double))
221 int error = Interop.Record.GetDouble(_recordHandle, propertyId, out val);
222 if ((int)ContactsError.None != error)
224 Log.Error(Globals.LogTag, "Get Long Failed with error " + error);
225 throw ContactsErrorFactory.CheckAndCreateException(error);
227 parsedValue = Convert.ChangeType(val, typeof(T));
231 Log.Error(Globals.LogTag, "Not Supported Data Type");
232 throw ContactsErrorFactory.CheckAndCreateException((int)ContactsError.NotSupported);
234 return (T)parsedValue;
238 /// Sets a value of the property to a record.
240 /// <param name="propertyId">The property ID</param>
241 /// <param name="value">The value to set</param>
242 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
243 public void Set<T>(uint propertyId, T value)
245 if (typeof(T) == typeof(string))
247 string val = Convert.ToString(value);
248 int error = Interop.Record.SetStr(_recordHandle, propertyId, val);
249 if ((int)ContactsError.None != error)
251 Log.Error(Globals.LogTag, "Set String Failed with error " + error);
252 throw ContactsErrorFactory.CheckAndCreateException(error);
255 else if (typeof(T) == typeof(int))
257 int val = Convert.ToInt32(value);
258 int error = Interop.Record.SetInt(_recordHandle, propertyId, val);
259 if ((int)ContactsError.None != error)
261 Log.Error(Globals.LogTag, "Set Int Failed with error " + error);
262 throw ContactsErrorFactory.CheckAndCreateException(error);
265 else if (typeof(T) == typeof(bool))
267 bool val = Convert.ToBoolean(value);
268 int error = Interop.Record.SetBool(_recordHandle, propertyId, val);
269 if ((int)ContactsError.None != error)
271 Log.Error(Globals.LogTag, "Set Bool Failed with error " + error);
272 throw ContactsErrorFactory.CheckAndCreateException(error);
275 else if (typeof(T) == typeof(long))
277 long val = Convert.ToInt64(value);
278 int error = Interop.Record.SetLli(_recordHandle, propertyId, val);
279 if ((int)ContactsError.None != error)
281 Log.Error(Globals.LogTag, "Set Long Failed with error " + error);
282 throw ContactsErrorFactory.CheckAndCreateException(error);
285 else if (typeof(T) == typeof(double))
287 double val = Convert.ToDouble(value);
288 int error = Interop.Record.SetDouble(_recordHandle, propertyId, val);
289 if ((int)ContactsError.None != error)
291 Log.Error(Globals.LogTag, "Get Long Failed with error " + error);
292 throw ContactsErrorFactory.CheckAndCreateException(error);
297 Log.Error(Globals.LogTag, "Not Supported Data Type");
298 throw ContactsErrorFactory.CheckAndCreateException((int)ContactsError.NotSupported);
303 /// Adds a child record to the parent record.
305 /// <param name="propertyId">The property ID</param>
306 /// <param name="childRecord">The child record to add to parent record</param>
307 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
308 public void AddChildRecord(uint propertyId, ContactsRecord childRecord)
310 int error = Interop.Record.AddChildRecord(_recordHandle, propertyId, childRecord._recordHandle);
311 if ((int)ContactsError.None != error)
313 Log.Error(Globals.LogTag, "AddChildRecord Failed with error " + error);
314 throw ContactsErrorFactory.CheckAndCreateException(error);
316 childRecord._disposedValue = true;
320 /// Removes a child record from the parent record.
322 /// <param name="propertyId">The property ID</param>
323 /// <param name="childRecord">The child record to remove from parent record</param>
324 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
325 public void RemoveChildRecord(uint propertyId, ContactsRecord childRecord)
327 int error = Interop.Record.RemoveChildRecord(_recordHandle, propertyId, childRecord._recordHandle);
328 if ((int)ContactsError.None != error)
330 Log.Error(Globals.LogTag, "RemoveChildRecord Failed with error " + error);
331 throw ContactsErrorFactory.CheckAndCreateException(error);
333 childRecord._disposedValue = false;
337 /// Gets the number of child records of a parent record.
339 /// <param name="propertyId">The property ID</param>
340 /// <returns>The number of child records corresponding to property ID</returns>
341 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
342 public int GetChildRecordCount(uint propertyId)
345 int error = Interop.Record.GetChildRecordCount(_recordHandle, propertyId, out count);
346 if ((int)ContactsError.None != error)
348 Log.Error(Globals.LogTag, "GetChildRecordCount Failed with error " + error);
349 throw ContactsErrorFactory.CheckAndCreateException(error);
355 /// Gets a child record from the parent record
357 /// <param name="propertyId">The property ID</param>
358 /// <param name="index">The index of child record</param>
359 /// <returns>The record </returns>
360 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
361 public ContactsRecord GetChildRecord(uint propertyId, int index)
365 int error = Interop.Record.GetChildRecordAtP(_recordHandle, propertyId, index, out handle);
366 if ((int)ContactsError.None != error)
368 Log.Error(Globals.LogTag, "GetChildRecord Failed with error " + error);
369 throw ContactsErrorFactory.CheckAndCreateException(error);
371 return new ContactsRecord(handle, true);
375 /// Clones a child record list corresponding to property ID
377 /// <param name="propertyId">The property ID</param>
381 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
382 public ContactsList CloneChildRecordList(uint propertyId)
386 int error = Interop.Record.CloneChildRecordList(_recordHandle, propertyId, out listHandle);
387 if ((int)ContactsError.None != error)
389 Log.Error(Globals.LogTag, "CloneChildRecordList Failed with error " + error);
390 throw ContactsErrorFactory.CheckAndCreateException(error);
392 ContactsList list = new ContactsList(listHandle);