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.Diagnostics.CodeAnalysis;
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 /// <since_tizen> 4 </since_tizen>
32 public class ContactsRecord : IDisposable
34 private string _uri = null;
36 private Int64 _memoryPressure = ContactsViews.Record.AverageSize;
37 internal IntPtr _recordHandle;
39 internal ContactsRecord(IntPtr handle)
41 _recordHandle = handle;
43 int error = Interop.Record.GetUriP(handle, out viewUri);
44 if ((int)ContactsError.None != error)
46 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
47 throw ContactsErrorFactory.CheckAndCreateException(error);
49 GC.AddMemoryPressure(_memoryPressure);
50 _uri = Marshal.PtrToStringAnsi(viewUri);
53 internal ContactsRecord(IntPtr handle, bool disposedValue)
55 _recordHandle = handle;
56 _disposedValue = disposedValue;
58 int error = Interop.Record.GetUriP(handle, out viewUri);
59 if ((int)ContactsError.None != error)
61 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
62 throw ContactsErrorFactory.CheckAndCreateException(error);
66 GC.AddMemoryPressure(_memoryPressure);
67 _uri = Marshal.PtrToStringAnsi(viewUri);
70 internal ContactsRecord(IntPtr handle, int id)
72 _recordHandle = handle;
75 int error = Interop.Record.GetUriP(handle, out viewUri);
76 if ((int)ContactsError.None != error)
78 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
79 throw ContactsErrorFactory.CheckAndCreateException(error);
81 _uri = Marshal.PtrToStringAnsi(viewUri);
82 GC.AddMemoryPressure(_memoryPressure);
88 /// <param name="viewUri">The view URI.</param>
89 /// <feature>http://tizen.org/feature/contact</feature>
90 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
91 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
92 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
93 /// <since_tizen> 4 </since_tizen>
94 [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
95 public ContactsRecord(string viewUri)
97 int error = Interop.Record.Create(viewUri, out _recordHandle);
98 if ((int)ContactsError.None != error)
100 Log.Error(Globals.LogTag, "ContactsRecord Failed with error " + error);
101 throw ContactsErrorFactory.CheckAndCreateException(error);
104 GC.AddMemoryPressure(_memoryPressure);
110 /// <since_tizen> 4 </since_tizen>
117 /// The URI of the record.
119 /// <value>The URI of the record.</value>
120 /// <since_tizen> 4 </since_tizen>
121 [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
130 #region IDisposable Support
131 internal bool _disposedValue = false; // To detect redundant calls
134 /// Releases all the resources used by the ContactsRecord.
136 /// <param name="disposing">Disposing by the user.</param>
137 /// <since_tizen> 4 </since_tizen>
138 protected virtual void Dispose(bool disposing)
143 //Release your own managed resources here.
144 //You should release all of your own disposable objects here
149 int error = Interop.Record.Destroy(_recordHandle, true);
150 if ((int)ContactsError.None != error)
152 Log.Error(Globals.LogTag, "Dispose Failed with error " + error);
154 _disposedValue = true;
155 GC.RemoveMemoryPressure(_memoryPressure);
160 /// Releases all the resources used by the ContactsRecord.
161 /// It should be called after it has finished using the object.
163 /// <since_tizen> 4 </since_tizen>
164 public void Dispose()
167 GC.SuppressFinalize(this);
172 /// Makes a clone of a record.
174 /// <returns>A cloned record.</returns>
175 /// <feature>http://tizen.org/feature/contact</feature>
176 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
177 /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
178 /// <since_tizen> 4 </since_tizen>
179 public ContactsRecord Clone()
181 IntPtr _clonedRecordHandle;
182 int error = Interop.Record.Clone(_recordHandle, out _clonedRecordHandle);
183 if ((int)ContactsError.None != error)
185 Log.Error(Globals.LogTag, "Clone Failed with error " + error);
186 throw ContactsErrorFactory.CheckAndCreateException(error);
188 return new ContactsRecord(_clonedRecordHandle, (int)_id);
192 /// Gets a value of the property from a record.
194 /// <param name="propertyId">The property ID.</param>
196 /// The value of the property corresponding to a property ID.
198 /// <feature>http://tizen.org/feature/contact</feature>
199 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
200 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
201 /// <since_tizen> 4 </since_tizen>
202 public T Get<T>(uint propertyId)
204 object parsedValue = null;
205 if (typeof(T) == typeof(string))
208 int error = Interop.Record.GetStr(_recordHandle, propertyId, out val);
209 if ((int)ContactsError.None != error)
211 Log.Error(Globals.LogTag, "Get String Failed with error " + error);
212 throw ContactsErrorFactory.CheckAndCreateException(error);
214 parsedValue = Convert.ChangeType(val, typeof(T));
216 else if (typeof(T) == typeof(int))
219 int error = Interop.Record.GetInt(_recordHandle, propertyId, out val);
220 if ((int)ContactsError.None != error)
222 Log.Error(Globals.LogTag, "Get Int Failed with error " + error);
223 throw ContactsErrorFactory.CheckAndCreateException(error);
225 parsedValue = Convert.ChangeType(val, typeof(T));
227 else if (typeof(T) == typeof(bool))
230 int error = Interop.Record.GetBool(_recordHandle, propertyId, out val);
231 if ((int)ContactsError.None != error)
233 Log.Error(Globals.LogTag, "Get Bool Failed with error " + error);
234 throw ContactsErrorFactory.CheckAndCreateException(error);
236 parsedValue = Convert.ChangeType(val, typeof(T));
238 else if (typeof(T) == typeof(long))
241 int error = Interop.Record.GetLli(_recordHandle, propertyId, out val);
242 if ((int)ContactsError.None != error)
244 Log.Error(Globals.LogTag, "Get Long Failed with error " + error);
245 throw ContactsErrorFactory.CheckAndCreateException(error);
247 parsedValue = Convert.ChangeType(val, typeof(T));
249 else if (typeof(T) == typeof(double))
252 int error = Interop.Record.GetDouble(_recordHandle, propertyId, out val);
253 if ((int)ContactsError.None != error)
255 Log.Error(Globals.LogTag, "Get Long Failed with error " + error);
256 throw ContactsErrorFactory.CheckAndCreateException(error);
258 parsedValue = Convert.ChangeType(val, typeof(T));
262 Log.Error(Globals.LogTag, "Not Supported Data Type");
263 throw ContactsErrorFactory.CheckAndCreateException((int)ContactsError.NotSupported);
265 return (T)parsedValue;
269 /// Sets a value of the property to a record.
271 /// <feature>http://tizen.org/feature/contact</feature>
272 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
273 /// <param name="propertyId">The property ID.</param>
274 /// <param name="value">The value to set.</param>
275 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
276 /// <since_tizen> 4 </since_tizen>
277 public void Set<T>(uint propertyId, T value)
279 if (typeof(T) == typeof(string))
281 string val = Convert.ToString(value);
282 int error = Interop.Record.SetStr(_recordHandle, propertyId, val);
283 if ((int)ContactsError.None != error)
285 Log.Error(Globals.LogTag, "Set String Failed with error " + error);
286 throw ContactsErrorFactory.CheckAndCreateException(error);
289 else if (typeof(T) == typeof(int))
291 int val = Convert.ToInt32(value);
292 int error = Interop.Record.SetInt(_recordHandle, propertyId, val);
293 if ((int)ContactsError.None != error)
295 Log.Error(Globals.LogTag, "Set Int Failed with error " + error);
296 throw ContactsErrorFactory.CheckAndCreateException(error);
299 else if (typeof(T) == typeof(bool))
301 bool val = Convert.ToBoolean(value);
302 int error = Interop.Record.SetBool(_recordHandle, propertyId, val);
303 if ((int)ContactsError.None != error)
305 Log.Error(Globals.LogTag, "Set Bool Failed with error " + error);
306 throw ContactsErrorFactory.CheckAndCreateException(error);
309 else if (typeof(T) == typeof(long))
311 long val = Convert.ToInt64(value);
312 int error = Interop.Record.SetLli(_recordHandle, propertyId, val);
313 if ((int)ContactsError.None != error)
315 Log.Error(Globals.LogTag, "Set Long Failed with error " + error);
316 throw ContactsErrorFactory.CheckAndCreateException(error);
319 else if (typeof(T) == typeof(double))
321 double val = Convert.ToDouble(value);
322 int error = Interop.Record.SetDouble(_recordHandle, propertyId, val);
323 if ((int)ContactsError.None != error)
325 Log.Error(Globals.LogTag, "Get Long Failed with error " + error);
326 throw ContactsErrorFactory.CheckAndCreateException(error);
331 Log.Error(Globals.LogTag, "Not Supported Data Type");
332 throw ContactsErrorFactory.CheckAndCreateException((int)ContactsError.NotSupported);
337 /// Adds a child record to the parent record.
339 /// <param name="propertyId">The property ID.</param>
340 /// <param name="childRecord">The child record to add to the parent record<./param>
341 /// <feature>http://tizen.org/feature/contact</feature>
342 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
343 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
344 /// <since_tizen> 4 </since_tizen>
345 public void AddChildRecord(uint propertyId, ContactsRecord childRecord)
347 int error = Interop.Record.AddChildRecord(_recordHandle, propertyId, childRecord._recordHandle);
348 if ((int)ContactsError.None != error)
350 Log.Error(Globals.LogTag, "AddChildRecord Failed with error " + error);
351 throw ContactsErrorFactory.CheckAndCreateException(error);
353 childRecord._disposedValue = true;
357 /// Removes a child record from the parent record.
359 /// <param name="propertyId">The property ID.</param>
360 /// <param name="childRecord">The child record to remove from the parent record.</param>
361 /// <feature>http://tizen.org/feature/contact</feature>
362 /// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
363 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
364 /// <since_tizen> 4 </since_tizen>
365 public void RemoveChildRecord(uint propertyId, ContactsRecord childRecord)
367 int error = Interop.Record.RemoveChildRecord(_recordHandle, propertyId, childRecord._recordHandle);
368 if ((int)ContactsError.None != error)
370 Log.Error(Globals.LogTag, "RemoveChildRecord Failed with error " + error);
371 throw ContactsErrorFactory.CheckAndCreateException(error);
373 childRecord._disposedValue = false;
377 /// Gets the number of child records of a parent record.
379 /// <param name="propertyId">The property ID.</param>
380 /// <returns>The number of child records corresponding to the property ID.</returns>
381 /// <feature>http://tizen.org/feature/contact</feature>
382 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
383 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
384 /// <since_tizen> 4 </since_tizen>
385 public int GetChildRecordCount(uint propertyId)
388 int error = Interop.Record.GetChildRecordCount(_recordHandle, propertyId, out count);
389 if ((int)ContactsError.None != error)
391 Log.Error(Globals.LogTag, "GetChildRecordCount Failed with error " + error);
392 throw ContactsErrorFactory.CheckAndCreateException(error);
398 /// Gets a child record from the parent record.
400 /// <param name="propertyId">The property ID.</param>
401 /// <param name="index">The index of the child record.</param>
402 /// <returns>The record </returns>
403 /// <feature>http://tizen.org/feature/contact</feature>
404 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
405 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
406 /// <since_tizen> 4 </since_tizen>
407 public ContactsRecord GetChildRecord(uint propertyId, int index)
411 int error = Interop.Record.GetChildRecordAtP(_recordHandle, propertyId, index, out handle);
412 if ((int)ContactsError.None != error)
414 Log.Error(Globals.LogTag, "GetChildRecord Failed with error " + error);
415 throw ContactsErrorFactory.CheckAndCreateException(error);
417 return new ContactsRecord(handle, true);
421 /// Clones a child record list corresponding to the property ID.
423 /// <param name="propertyId">The property ID.</param>
427 /// <feature>http://tizen.org/feature/contact</feature>
428 /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
429 /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
430 /// <since_tizen> 4 </since_tizen>
431 public ContactsList CloneChildRecordList(uint propertyId)
435 int error = Interop.Record.CloneChildRecordList(_recordHandle, propertyId, out listHandle);
436 if ((int)ContactsError.None != error)
438 Log.Error(Globals.LogTag, "CloneChildRecordList Failed with error " + error);
439 throw ContactsErrorFactory.CheckAndCreateException(error);
441 ContactsList list = new ContactsList(listHandle);