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 static Interop.Contacts;
21 namespace Tizen.Pims.Contacts
24 /// Enumeration for name display order.
26 public enum ContactDisplayOrder
29 /// First name comes at the first
33 /// First name comes at the last
39 /// Enumeration for name sorting order.
41 public enum ContactSortingOrder
44 /// Contacts are first sorted based on the first name
48 /// Contacts are first sorted based on the last name
54 /// A class for managing contact information. It allows applications to access contacts database.
56 public class ContactsManager : IDisposable
58 private ContactsDatabase _db = null;
59 private Object thisLock = new Object();
60 private Interop.Setting.DisplayOrderChangedCallback _displayOrderDelegate;
61 private Interop.Setting.SortingOrderChangedCallback _sortingOrderDelegate;
64 /// Creates a ContactsManager.
66 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
67 public ContactsManager()
69 int error = Interop.Contacts.Connect();
70 if ((int)ContactsError.None != error)
72 Log.Error(Globals.LogTag, "Connect Failed with error " + error);
73 throw ContactsErrorFactory.CheckAndCreateException(error);
75 _db = new ContactsDatabase();
83 #region IDisposable Support
84 private bool disposedValue = false; // To detect redundant calls
86 protected virtual void Dispose(bool disposing)
90 int error = Interop.Contacts.Disconnect();
91 if ((int)ContactsError.None != error)
93 Log.Error(Globals.LogTag, "Disconnect Failed with error " + error);
101 /// Releases all resources used by the ContactsManager.
102 /// It should be called after finished using of the object.
104 public void Dispose()
110 private event EventHandler<NameDisplayOrderChangedEventArgs> _nameDisplayOrderChanged;
111 private event EventHandler<NameSortingOrderChangedEventArgs> _nameSortingOrderChanged;
114 /// (event) NameDisplayOrderChanged is raised when changing setting value of contacts name display order
116 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
117 public event EventHandler<NameDisplayOrderChangedEventArgs> NameDisplayOrderChanged
123 if (_displayOrderDelegate == null)
125 _displayOrderDelegate = (ContactDisplayOrder nameDisplayOrder, IntPtr userData) =>
127 NameDisplayOrderChangedEventArgs args = new NameDisplayOrderChangedEventArgs(nameDisplayOrder);
128 _nameDisplayOrderChanged?.Invoke(this, args);
132 if (_nameDisplayOrderChanged == null)
134 int error = Interop.Setting.AddNameDisplayOrderChangedCB(_displayOrderDelegate, IntPtr.Zero);
135 if ((int)ContactsError.None != error)
137 Log.Error(Globals.LogTag, "Add NameDisplayOrderChangedCB Failed with error " + error);
141 _nameDisplayOrderChanged += value;
149 _nameDisplayOrderChanged -= value;
151 if (_nameDisplayOrderChanged == null)
153 int error = Interop.Setting.RemoveNameDisplayOrderChangedCB(_displayOrderDelegate, IntPtr.Zero);
154 if ((int)ContactsError.None != error)
156 Log.Error(Globals.LogTag, "Remove StateChanged Failed with error " + error);
164 /// (event) NameSortingOrderChanged is raised when changing setting value of contacts name sorting order
166 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
167 public event EventHandler<NameSortingOrderChangedEventArgs> NameSortingOrderChanged
173 if (_sortingOrderDelegate == null)
175 _sortingOrderDelegate = (ContactSortingOrder nameSortingOrder, IntPtr userData) =>
177 NameSortingOrderChangedEventArgs args = new NameSortingOrderChangedEventArgs(nameSortingOrder);
178 _nameSortingOrderChanged?.Invoke(this, args);
182 if (_nameSortingOrderChanged == null)
184 int error = Interop.Setting.AddNameSortingOrderChangedCB(_sortingOrderDelegate, IntPtr.Zero);
185 if ((int)ContactsError.None != error)
187 Log.Error(Globals.LogTag, "Add NameSortingOrderChangedCB Failed with error " + error);
191 _nameSortingOrderChanged += value;
199 _nameSortingOrderChanged -= value;
201 if (_nameSortingOrderChanged == null)
203 int error = Interop.Setting.RemoveNameSortingOrderChangedCB(_sortingOrderDelegate, IntPtr.Zero);
204 if ((int)ContactsError.None != error)
206 Log.Error(Globals.LogTag, "Remove StateChanged Failed with error " + error);
214 /// A ContactsDatabase
216 public ContactsDatabase Database
225 /// A setting value of contacts name display order
228 /// DisplayName of contacts returned from database are determined by this property
230 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
231 /// <privilege>http://tizen.org/privilege/contact.write</privilege>
232 public ContactDisplayOrder NameDisplayOrder
236 ContactDisplayOrder contactDisplayOrder;
237 int error = Interop.Setting.GetNameDisplayOrder(out contactDisplayOrder);
238 if ((int)ContactsError.None != error)
240 Log.Error(Globals.LogTag, "Get NameDisplayOrder Failed with error " + error);
242 return contactDisplayOrder;
246 int error = Interop.Setting.SetNameDisplayOrder(value);
247 if ((int)ContactsError.None != error)
249 Log.Error(Globals.LogTag, "Set NameDisplayOrder Failed with error " + error);
255 /// A setting value of contacts name sorting order
258 /// Contacts returned from database are first sorted based on the first name or last name by this property
260 /// <privilege>http://tizen.org/privilege/contact.read</privilege>
261 /// <privilege>http://tizen.org/privilege/contact.write</privilege>
262 public ContactSortingOrder NameSortingOrder
266 ContactSortingOrder contactsSortingOrder;
267 int error = Interop.Setting.GetNameSortingOrder(out contactsSortingOrder);
268 if ((int)ContactsError.None != error)
270 Log.Error(Globals.LogTag, "Get NameSortingOrder Failed with error " + error);
272 return contactsSortingOrder;
276 int error = Interop.Setting.SetNameSortingOrder(value);
277 if ((int)ContactsError.None != error)
279 Log.Error(Globals.LogTag, "Set NameSortingOrder Failed with error " + error);