PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/AddressBook/ABRecord.cs

https://github.com/kjpou1/maccore
C# | 216 lines | 156 code | 29 blank | 31 comment | 18 complexity | fedf2e9f5604e6152073d14b9ca76e06 MD5 | raw file
Possible License(s): Apache-2.0
  1. //
  2. // ABRecord.cs: Implements the managed ABRecord
  3. //
  4. // Authors: Mono Team
  5. //
  6. // Copyright (C) 2009 Novell, Inc
  7. // Copyright 2011, 2012 Xamarin Inc.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //
  29. using System;
  30. using System.Runtime.InteropServices;
  31. using MonoMac.CoreFoundation;
  32. using MonoMac.Foundation;
  33. using MonoMac.ObjCRuntime;
  34. namespace MonoMac.AddressBook {
  35. public enum ABRecordType : uint {
  36. Person = 0,
  37. Group = 1,
  38. Source = 2
  39. }
  40. public enum ABPropertyType : uint {
  41. Invalid = 0,
  42. String = 0x1,
  43. Integer = 0x2,
  44. Real = 0x3,
  45. DateTime = 0x4,
  46. Dictionary = 0x5,
  47. MultiString = ABMultiValue.Mask | String,
  48. MultiInteger = ABMultiValue.Mask | Integer,
  49. MultiReal = ABMultiValue.Mask | Real,
  50. MultiDateTime = ABMultiValue.Mask | DateTime,
  51. MultiDictionary = ABMultiValue.Mask | Dictionary,
  52. }
  53. public abstract class ABRecord : INativeObject, IDisposable {
  54. public const int InvalidRecordId = -1;
  55. public const int InvalidPropertyId = -1;
  56. IntPtr handle;
  57. internal ABRecord (IntPtr handle, bool owns)
  58. {
  59. if (!owns)
  60. CFObject.CFRetain (handle);
  61. this.handle = handle;
  62. }
  63. internal static ABRecord FromHandle (IntPtr handle, ABAddressBook addressbook, bool owns = true)
  64. {
  65. if (handle == IntPtr.Zero)
  66. throw new ArgumentNullException ("handle");
  67. // TODO: does ABGroupCopyArrayOfAllMembers() have Create or Get
  68. // semantics for the array elements?
  69. var type = ABRecordGetRecordType (handle);
  70. ABRecord rec;
  71. switch (type) {
  72. case ABRecordType.Person:
  73. rec = new ABPerson (handle, owns);
  74. break;
  75. case ABRecordType.Group:
  76. rec = new ABGroup (handle, owns);
  77. break;
  78. case ABRecordType.Source:
  79. rec = new ABSource (handle, owns);
  80. break;
  81. default:
  82. throw new NotSupportedException ("Could not determine record type.");
  83. }
  84. rec.AddressBook = addressbook;
  85. return rec;
  86. }
  87. ~ABRecord ()
  88. {
  89. Dispose (false);
  90. }
  91. public void Dispose ()
  92. {
  93. Dispose (true);
  94. GC.SuppressFinalize (this);
  95. }
  96. protected virtual void Dispose (bool disposing)
  97. {
  98. if (handle != IntPtr.Zero)
  99. CFObject.CFRelease (handle);
  100. handle = IntPtr.Zero;
  101. AddressBook = null;
  102. }
  103. void AssertValid ()
  104. {
  105. if (handle == IntPtr.Zero)
  106. throw new ObjectDisposedException ("");
  107. }
  108. internal ABAddressBook AddressBook {
  109. get; set;
  110. }
  111. public IntPtr Handle {
  112. get {
  113. AssertValid ();
  114. return handle;
  115. }
  116. internal set {
  117. handle = value;
  118. }
  119. }
  120. [DllImport (Constants.AddressBookLibrary)]
  121. extern static int ABRecordGetRecordID (IntPtr record);
  122. public int Id {
  123. get {return ABRecordGetRecordID (Handle);}
  124. }
  125. [DllImport (Constants.AddressBookLibrary)]
  126. extern static ABRecordType ABRecordGetRecordType (IntPtr record);
  127. public ABRecordType Type {
  128. get {return ABRecordGetRecordType (Handle);}
  129. }
  130. [DllImport (Constants.AddressBookLibrary)]
  131. extern static IntPtr ABRecordCopyCompositeName (IntPtr record);
  132. public override string ToString ()
  133. {
  134. using (var s = new NSString (ABRecordCopyCompositeName (Handle)))
  135. return s.ToString ();
  136. }
  137. // TODO: Should SetValue/CopyValue/RemoveValue be public?
  138. [DllImport (Constants.AddressBookLibrary)]
  139. extern static bool ABRecordSetValue (IntPtr record, int property, IntPtr value, out IntPtr error);
  140. internal void SetValue (int property, IntPtr value)
  141. {
  142. IntPtr error;
  143. if (!ABRecordSetValue (Handle, property, value, out error))
  144. throw CFException.FromCFError (error);
  145. }
  146. internal void SetValue (int property, NSObject value)
  147. {
  148. SetValue (property, value == null ? IntPtr.Zero : value.Handle);
  149. }
  150. internal void SetValue (int property, string value)
  151. {
  152. using (NSObject v = value == null ? null : new NSString (value))
  153. SetValue (property, v);
  154. }
  155. [DllImport (Constants.AddressBookLibrary)]
  156. extern static IntPtr ABRecordCopyValue (IntPtr record, int property);
  157. internal IntPtr CopyValue (int property)
  158. {
  159. return ABRecordCopyValue (Handle, property);
  160. }
  161. [DllImport (Constants.AddressBookLibrary)]
  162. extern static bool ABRecordRemoveValue (IntPtr record, int property, out IntPtr error);
  163. internal void RemoveValue (int property)
  164. {
  165. IntPtr error;
  166. bool r = ABRecordRemoveValue (Handle, property, out error);
  167. if (!r && error != IntPtr.Zero)
  168. throw CFException.FromCFError (error);
  169. }
  170. internal T PropertyTo<T> (int id)
  171. where T : NSObject
  172. {
  173. IntPtr value = CopyValue (id);
  174. if (value == IntPtr.Zero)
  175. return null;
  176. return (T) Runtime.GetNSObject (value);
  177. }
  178. internal string PropertyToString (int id)
  179. {
  180. IntPtr value = CopyValue (id);
  181. if (value == IntPtr.Zero)
  182. return null;
  183. using (var s = new NSString (value))
  184. return s.ToString ();
  185. }
  186. }
  187. }