Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Contacts / Tizen.Pims.Contacts / ContactsManager.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 using System.Collections.Generic;
19 using static Interop.Contacts;
20
21 namespace Tizen.Pims.Contacts
22 {
23     /// <summary>
24     /// Enumeration for name display order.
25     /// </summary>
26     public enum ContactDisplayOrder
27     {
28         /// <summary>
29         /// First name comes at the first
30         /// </summary>
31         FirstLast,
32         /// <summary>
33         /// First name comes at the last
34         /// </summary>
35         LastFirst
36     };
37
38     /// <summary>
39     /// Enumeration for name sorting order.
40     /// </summary>
41     public enum ContactSortingOrder
42     {
43         /// <summary>
44         /// Contacts are first sorted based on the first name
45         /// </summary>
46         FirstLast,
47         /// <summary>
48         /// Contacts are first sorted based on the last name
49         /// </summary>
50         LastFirst
51     };
52
53     /// <summary>
54     /// A class for managing contact information. It allows applications to access contacts database.
55     /// </summary>
56     public class ContactsManager : IDisposable
57     {
58         private ContactsDatabase _db = null;
59         private Object thisLock = new Object();
60         private Interop.Setting.DisplayOrderChangedCallback _displayOrderDelegate;
61         private Interop.Setting.SortingOrderChangedCallback _sortingOrderDelegate;
62
63         /// <summary>
64         /// Creates a ContactsManager.
65         /// </summary>
66         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
67         public ContactsManager()
68         {
69             int error = Interop.Contacts.Connect();
70             if ((int)ContactsError.None != error)
71             {
72                 Log.Error(Globals.LogTag, "Connect Failed with error " + error);
73                 throw ContactsErrorFactory.CheckAndCreateException(error);
74             }
75             _db = new ContactsDatabase();
76         }
77
78         ~ContactsManager()
79         {
80             Dispose(false);
81         }
82
83         #region IDisposable Support
84         private bool disposedValue = false; // To detect redundant calls
85
86         protected virtual void Dispose(bool disposing)
87         {
88             if (!disposedValue)
89             {
90                 int error = Interop.Contacts.Disconnect();
91                 if ((int)ContactsError.None != error)
92                 {
93                     Log.Error(Globals.LogTag, "Disconnect Failed with error " + error);
94                 }
95
96                 disposedValue = true;
97             }
98         }
99
100         /// <summary>
101         /// Releases all resources used by the ContactsManager.
102         /// It should be called after finished using of the object.
103         /// </summary>
104         public void Dispose()
105         {
106             Dispose(true);
107         }
108         #endregion
109
110         private event EventHandler<NameDisplayOrderChangedEventArgs> _nameDisplayOrderChanged;
111         private event EventHandler<NameSortingOrderChangedEventArgs> _nameSortingOrderChanged;
112
113         /// <summary>
114         /// (event) NameDisplayOrderChanged is raised when changing setting value of contacts name display order
115         /// </summary>
116         /// <privilege>http://tizen.org/privilege/contact.read</privilege>
117         public event EventHandler<NameDisplayOrderChangedEventArgs> NameDisplayOrderChanged
118         {
119             add
120             {
121                 lock (thisLock)
122                 {
123                     if (_displayOrderDelegate == null)
124                     {
125                         _displayOrderDelegate = (ContactDisplayOrder nameDisplayOrder, IntPtr userData) =>
126                         {
127                             NameDisplayOrderChangedEventArgs args = new NameDisplayOrderChangedEventArgs(nameDisplayOrder);
128                             _nameDisplayOrderChanged?.Invoke(this, args);
129                         };
130                     }
131
132                     if (_nameDisplayOrderChanged == null)
133                     {
134                         int error = Interop.Setting.AddNameDisplayOrderChangedCB(_displayOrderDelegate, IntPtr.Zero);
135                         if ((int)ContactsError.None != error)
136                         {
137                             Log.Error(Globals.LogTag, "Add NameDisplayOrderChangedCB Failed with error " + error);
138                         }
139                     }
140
141                     _nameDisplayOrderChanged += value;
142                 }
143             }
144
145             remove
146             {
147                 lock (thisLock)
148                 {
149                     _nameDisplayOrderChanged -= value;
150
151                     if (_nameDisplayOrderChanged == null)
152                     {
153                         int error = Interop.Setting.RemoveNameDisplayOrderChangedCB(_displayOrderDelegate, IntPtr.Zero);
154                         if ((int)ContactsError.None != error)
155                         {
156                             Log.Error(Globals.LogTag, "Remove StateChanged Failed with error " + error);
157                         }
158                     }
159                 }
160             }
161         }
162
163         /// <summary>
164         /// (event) NameSortingOrderChanged is raised when changing setting value of contacts name sorting order
165         /// </summary>
166         /// <privilege>http://tizen.org/privilege/contact.read</privilege>
167         public event EventHandler<NameSortingOrderChangedEventArgs> NameSortingOrderChanged
168         {
169             add
170             {
171                 lock (thisLock)
172                 {
173                     if (_sortingOrderDelegate == null)
174                     {
175                         _sortingOrderDelegate = (ContactSortingOrder nameSortingOrder, IntPtr userData) =>
176                         {
177                             NameSortingOrderChangedEventArgs args = new NameSortingOrderChangedEventArgs(nameSortingOrder);
178                             _nameSortingOrderChanged?.Invoke(this, args);
179                         };
180                     }
181
182                     if (_nameSortingOrderChanged == null)
183                     {
184                         int error = Interop.Setting.AddNameSortingOrderChangedCB(_sortingOrderDelegate, IntPtr.Zero);
185                         if ((int)ContactsError.None != error)
186                         {
187                             Log.Error(Globals.LogTag, "Add NameSortingOrderChangedCB Failed with error " + error);
188                         }
189                     }
190
191                     _nameSortingOrderChanged += value;
192                 }
193             }
194
195             remove
196             {
197                 lock (thisLock)
198                 {
199                     _nameSortingOrderChanged -= value;
200
201                     if (_nameSortingOrderChanged == null)
202                     {
203                         int error = Interop.Setting.RemoveNameSortingOrderChangedCB(_sortingOrderDelegate, IntPtr.Zero);
204                         if ((int)ContactsError.None != error)
205                         {
206                             Log.Error(Globals.LogTag, "Remove StateChanged Failed with error " + error);
207                         }
208                     }
209                 }
210             }
211         }
212
213         /// <summary>
214         /// A ContactsDatabase
215         /// </summary>
216         public ContactsDatabase Database
217         {
218             get
219             {
220                 return _db;
221             }
222         }
223
224         /// <summary>
225         /// A setting value of contacts name display order
226         /// </summary>
227         /// <remarks>
228         /// DisplayName of contacts returned from database are determined by this property
229         /// </remarks>
230         /// <privilege>http://tizen.org/privilege/contact.read</privilege>
231         /// <privilege>http://tizen.org/privilege/contact.write</privilege>
232         public ContactDisplayOrder NameDisplayOrder
233         {
234             get
235             {
236                 ContactDisplayOrder contactDisplayOrder;
237                 int error = Interop.Setting.GetNameDisplayOrder(out contactDisplayOrder);
238                 if ((int)ContactsError.None != error)
239                 {
240                     Log.Error(Globals.LogTag, "Get NameDisplayOrder Failed with error " + error);
241                 }
242                 return contactDisplayOrder;
243             }
244             set
245             {
246                 int error = Interop.Setting.SetNameDisplayOrder(value);
247                 if ((int)ContactsError.None != error)
248                 {
249                     Log.Error(Globals.LogTag, "Set NameDisplayOrder Failed with error " + error);
250                 }
251             }
252         }
253
254         /// <summary>
255         /// A setting value of contacts name sorting order
256         /// </summary>
257         /// <remarks>
258         /// Contacts returned from database are first sorted based on the first name or last name by this property
259         /// </remarks>
260         /// <privilege>http://tizen.org/privilege/contact.read</privilege>
261         /// <privilege>http://tizen.org/privilege/contact.write</privilege>
262         public ContactSortingOrder NameSortingOrder
263         {
264             get
265             {
266                 ContactSortingOrder contactsSortingOrder;
267                 int error = Interop.Setting.GetNameSortingOrder(out contactsSortingOrder);
268                 if ((int)ContactsError.None != error)
269                 {
270                     Log.Error(Globals.LogTag, "Get NameSortingOrder Failed with error " + error);
271                 }
272                 return contactsSortingOrder;
273             }
274             set
275             {
276                 int error = Interop.Setting.SetNameSortingOrder(value);
277                 if ((int)ContactsError.None != error)
278                 {
279                     Log.Error(Globals.LogTag, "Set NameSortingOrder Failed with error " + error);
280                 }
281             }
282         }
283     }
284 }