Tizen 2.0 Release
[platform/framework/native/social.git] / src / FSclAddressbookManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FSclAddressbookManager.cpp
19  * @brief               This is the implementation for AddressbookManager class.
20  *
21  * This file contains definitions of @e AddressbookManager class.
22  */
23
24 #include <unique_ptr.h>
25 #include <pthread.h>
26 #include <FBaseResult.h>
27 #include <FBaseString.h>
28 #include <FBaseColIList.h>
29 #include <FBaseSysLog.h>
30 #include <FSclContact.h>
31 #include <FSclCategory.h>
32 #include <FSclAddressbookManager.h>
33 #include <FSec_AccessController.h>
34 #include <FScl_AddressbookManagerImpl.h>
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Security;
39
40 namespace Tizen { namespace Social
41 {
42
43 AddressbookManager* AddressbookManager::__pInstance = null;
44
45 AddressbookManager::AddressbookManager(void)
46         : __pAddressbookManagerImpl(null)
47 {
48         //empty body
49 }
50
51 AddressbookManager::~AddressbookManager(void)
52 {
53         delete __pAddressbookManagerImpl;
54 }
55
56 result
57 AddressbookManager::Construct(void)
58 {
59         result r = E_SUCCESS;
60
61         SysAssertf(__pAddressbookManagerImpl == null,
62                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
63
64         _AddressbookManagerImpl* pAddressbookManagerImpl = new (std::nothrow) _AddressbookManagerImpl();
65         SysTryReturnResult(NID_SCL, pAddressbookManagerImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
66
67         r = pAddressbookManagerImpl->Construct();
68         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
69
70         __pAddressbookManagerImpl = pAddressbookManagerImpl;
71
72         return E_SUCCESS;
73
74 CATCH:
75         delete pAddressbookManagerImpl;
76
77         return r;
78 }
79
80 result
81 AddressbookManager::SetEventListener(IAddressbookEventListener* pListener)
82 {
83         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
84                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
85         SysAssertf(__pAddressbookManagerImpl != null,
86                         "Not yet constructed. Construct() should be called before use.");
87
88         result r = __pAddressbookManagerImpl->SetEventListener(pListener);
89         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
90
91         return E_SUCCESS;
92 }
93
94 Addressbook*
95 AddressbookManager::CreateAddressbookN(AccountId accountId, const String& name)
96 {
97         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
98                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
99         SysAssertf(__pAddressbookManagerImpl != null,
100                         "Not yet constructed. Construct() should be called before use.");
101
102         Addressbook* pAddressbook = __pAddressbookManagerImpl->CreateAddressbookN(accountId, name);
103         SysTryReturn(NID_SCL, pAddressbook != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
104
105         return pAddressbook;
106 }
107
108 result
109 AddressbookManager::DeleteAddressbook(AddressbookId addressbookId)
110 {
111         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
112                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
113         SysAssertf(__pAddressbookManagerImpl != null,
114                         "Not yet constructed. Construct() should be called before use.");
115
116         result r = __pAddressbookManagerImpl->DeleteAddressbook(addressbookId);
117         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
118
119         return E_SUCCESS;
120 }
121
122 IList*
123 AddressbookManager::GetAddressbooksByAccountN(AccountId accountId) const
124 {
125         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
126                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
127         SysAssertf(__pAddressbookManagerImpl != null,
128                         "Not yet constructed. Construct() should be called before use.");
129
130         IList*  pList = __pAddressbookManagerImpl->GetAddressbooksByAccountN(accountId);
131         SysTryReturn(NID_SCL, pList !=null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
132
133         return pList;
134 }
135
136 IList*
137 AddressbookManager::GetAllAddressbooksN(void) const
138 {
139         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
140                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
141         SysAssertf(__pAddressbookManagerImpl != null,
142                         "Not yet constructed. Construct() should be called before use.");
143
144         IList* pList = __pAddressbookManagerImpl->GetAllAddressbooksN();
145         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
146
147         return pList;
148 }
149
150 Addressbook*
151 AddressbookManager::GetAddressbookN(AddressbookId addressbookId) const
152 {
153         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
154                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
155         SysAssertf(__pAddressbookManagerImpl != null,
156                         "Not yet constructed. Construct() should be called before use.");
157
158         Addressbook* pAddressbook = __pAddressbookManagerImpl->GetAddressbookN(addressbookId);
159         SysTryReturn(NID_SCL, pAddressbook != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
160
161         return pAddressbook;
162 }
163
164 result
165 AddressbookManager::AddContact(Contact& contact, AddressbookId addressbookId)
166 {
167         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
168                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
169         SysAssertf(__pAddressbookManagerImpl != null,
170                         "Not yet constructed. Construct() should be called before use.");
171
172         result r = __pAddressbookManagerImpl->AddContact(contact, addressbookId);
173         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
174
175         return E_SUCCESS;
176 }
177
178 result
179 AddressbookManager::AddCategory(Category& category, AddressbookId addressbookId)
180 {
181         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
182                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
183         SysAssertf(__pAddressbookManagerImpl != null,
184                         "Not yet constructed. Construct() should be called before use.");
185
186         result r = __pAddressbookManagerImpl->AddCategory(category, addressbookId);
187         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
188
189         return E_SUCCESS;
190 }
191
192 result
193 AddressbookManager::RemoveContact(RecordId contactId)
194 {
195         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
196                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
197         SysAssertf(__pAddressbookManagerImpl != null,
198                         "Not yet constructed. Construct() should be called before use.");
199
200         result r = __pAddressbookManagerImpl->RemoveContact(contactId);
201         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
202
203         return E_SUCCESS;
204 }
205
206 result
207 AddressbookManager::RemoveCategory(RecordId categoryId)
208 {
209         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
210                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
211         SysAssertf(__pAddressbookManagerImpl != null,
212                         "Not yet constructed. Construct() should be called before use.");
213
214         result r = __pAddressbookManagerImpl->RemoveCategory(categoryId);
215         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
216
217         return E_SUCCESS;
218 }
219
220 result
221 AddressbookManager::UpdateContact(const Contact& contact)
222 {
223         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
224                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
225         SysAssertf(__pAddressbookManagerImpl != null,
226                         "Not yet constructed. Construct() should be called before use.");
227
228         result r = __pAddressbookManagerImpl->UpdateContact(contact);
229         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
230
231         return E_SUCCESS;
232 }
233
234 result
235 AddressbookManager::UpdateCategory(const Category& category)
236 {
237         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
238                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
239         SysAssertf(__pAddressbookManagerImpl != null,
240                         "Not yet constructed. Construct() should be called before use.");
241
242         result r = __pAddressbookManagerImpl->UpdateCategory(category);
243         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
244
245         return E_SUCCESS;
246 }
247
248 IList*
249 AddressbookManager::GetAllContactsN(void) const
250 {
251         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
252                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
253         SysAssertf(__pAddressbookManagerImpl != null,
254                         "Not yet constructed. Construct() should be called before use.");
255
256         IList* pList = __pAddressbookManagerImpl->GetAllContactsN();
257         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
258
259         return pList;
260 }
261
262 IList*
263 AddressbookManager::GetContactsByCategoryN(RecordId categoryId) const
264 {
265         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
266                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
267         SysAssertf(__pAddressbookManagerImpl != null,
268                         "Not yet constructed. Construct() should be called before use.");
269
270         IList* pList = __pAddressbookManagerImpl->GetContactsByCategoryN(categoryId);
271         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
272
273         return pList;
274 }
275
276 IList*
277 AddressbookManager::GetContactsByPersonN(PersonId personId) const
278 {
279         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
280                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
281         SysAssertf(__pAddressbookManagerImpl != null,
282                         "Not yet constructed. Construct() should be called before use.");
283
284         IList* pList = __pAddressbookManagerImpl->GetContactsByPersonN(personId);
285         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
286
287         return pList;
288 }
289
290 result
291 AddressbookManager::AddMemberToCategory(RecordId categoryId, RecordId contactId)
292 {
293         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
294                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
295         SysAssertf(__pAddressbookManagerImpl != null,
296                         "Not yet constructed. Construct() should be called before use.");
297
298         result r = __pAddressbookManagerImpl->AddMemberToCategory(categoryId, contactId);
299         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
300
301         return E_SUCCESS;
302 }
303
304 result
305 AddressbookManager::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
306 {
307         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
308                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
309         SysAssertf(__pAddressbookManagerImpl != null,
310                         "Not yet constructed. Construct() should be called before use.");
311
312         result r = __pAddressbookManagerImpl->RemoveMemberFromCategory(categoryId, contactId);
313         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
314
315         return E_SUCCESS;
316 }
317
318 IList*
319 AddressbookManager::GetAllCategoriesN(void) const
320 {
321         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
322                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
323         SysAssertf(__pAddressbookManagerImpl != null,
324                         "Not yet constructed. Construct() should be called before use.");
325
326         IList* pList = __pAddressbookManagerImpl->GetAllCategoriesN();
327         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
328
329         return pList;
330 }
331
332 IList*
333 AddressbookManager::GetCategoriesByContactN(RecordId contactId) const
334 {
335         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
336                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
337         SysAssertf(__pAddressbookManagerImpl != null,
338                         "Not yet constructed. Construct() should be called before use.");
339
340         IList* pList = __pAddressbookManagerImpl->GetCategoriesByContactN(contactId);
341         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
342
343         return pList;
344 }
345
346 IList*
347 AddressbookManager::GetCategoriesByPersonN(PersonId personId) const
348 {
349         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
350                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
351         SysAssertf(__pAddressbookManagerImpl != null,
352                         "Not yet constructed. Construct() should be called before use.");
353
354         IList* pList = __pAddressbookManagerImpl->GetCategoriesByPersonN(personId);
355         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
356
357         return pList;
358 }
359
360 IList*
361 AddressbookManager::SearchContactsByEmailN(const String& email) const
362 {
363         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
364                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
365         SysAssertf(__pAddressbookManagerImpl != null,
366                         "Not yet constructed. Construct() should be called before use.");
367
368         IList* pList = __pAddressbookManagerImpl->SearchContactsByEmailN(email);
369         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
370
371         return pList;
372 }
373
374 IList*
375 AddressbookManager::SearchContactsByNameN(const String& name) const
376 {
377         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
378                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
379         SysAssertf(__pAddressbookManagerImpl != null,
380                         "Not yet constructed. Construct() should be called before use.");
381
382         IList* pList = __pAddressbookManagerImpl->SearchContactsByNameN(name);
383         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
384
385         return pList;
386 }
387
388 IList*
389 AddressbookManager::SearchContactsByPhoneNumberN(const String& phoneNumber) const
390 {
391         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
392                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
393         SysAssertf(__pAddressbookManagerImpl != null,
394                         "Not yet constructed. Construct() should be called before use.");
395
396         IList* pList = __pAddressbookManagerImpl->SearchContactsByPhoneNumberN(phoneNumber);
397         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
398
399         return pList;
400 }
401
402 int
403 AddressbookManager::GetCategoryCount(void) const
404 {
405         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, -1, E_PRIVILEGE_DENIED,
406                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
407         SysAssertf(__pAddressbookManagerImpl != null,
408                         "Not yet constructed. Construct() should be called before use.");
409
410         int count = __pAddressbookManagerImpl->GetCategoryCount();
411         SysTryReturn(NID_SCL, count != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
412
413         return count;
414 }
415
416 int
417 AddressbookManager::GetContactCount(void) const
418 {
419         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, -1, E_PRIVILEGE_DENIED,
420                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
421         SysAssertf(__pAddressbookManagerImpl != null,
422                         "Not yet constructed. Construct() should be called before use.");
423
424         int count = __pAddressbookManagerImpl->GetContactCount();
425         SysTryReturn(NID_SCL, count != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
426
427         return count;
428 }
429
430 Contact*
431 AddressbookManager::GetContactN(RecordId contactId) const
432 {
433         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
434                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
435         SysAssertf(__pAddressbookManagerImpl != null,
436                         "Not yet constructed. Construct() should be called before use.");
437
438         Contact* pContact = __pAddressbookManagerImpl->GetContactN(contactId);
439         SysTryReturn(NID_SCL, pContact != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
440
441         return pContact;
442 }
443
444 Person*
445 AddressbookManager::GetPersonN(PersonId personId) const
446 {
447         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
448                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
449         SysAssertf(__pAddressbookManagerImpl != null,
450                         "Not yet constructed. Construct() should be called before use.");
451
452         Person* pPerson = __pAddressbookManagerImpl->GetPersonN(personId);
453         SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
454
455         return pPerson;
456 }
457 Category*
458 AddressbookManager::GetCategoryN(RecordId categoryId) const
459 {
460         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
461                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
462         SysAssertf(__pAddressbookManagerImpl != null,
463                         "Not yet constructed. Construct() should be called before use.");
464
465         Category* pCategory = __pAddressbookManagerImpl->GetCategoryN(categoryId);
466         SysTryReturn(NID_SCL, pCategory != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
467
468         return pCategory;
469 }
470
471 int
472 AddressbookManager::GetLatestVersion(void) const
473 {
474         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, -1, E_PRIVILEGE_DENIED,
475                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
476         SysAssertf(__pAddressbookManagerImpl != null,
477                         "Not yet constructed. Construct() should be called before use.");
478
479         int latestVersion = __pAddressbookManagerImpl->GetLatestVersion();
480         SysTryReturn(NID_SCL, latestVersion != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
481
482         return latestVersion;
483 }
484
485 IList*
486 AddressbookManager::GetChangedContactsAfterN(int version, int& latestVersion) const
487 {
488         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
489                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
490         SysAssertf(__pAddressbookManagerImpl != null,
491                         "Not yet constructed. Construct() should be called before use.");
492
493         IList* pList = __pAddressbookManagerImpl->GetChangedContactsAfterN(version, latestVersion);
494         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
495
496         return pList;
497 }
498
499 IList*
500 AddressbookManager::GetChangedCategoriesAfterN(int version, int& latestVersion) const
501 {
502         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
503                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
504         SysAssertf(__pAddressbookManagerImpl != null,
505                         "Not yet constructed. Construct() should be called before use.");
506
507         IList* pList = __pAddressbookManagerImpl->GetChangedCategoriesAfterN(version, latestVersion);
508         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
509
510         return pList;
511 }
512
513 result
514 AddressbookManager::RemovePerson(PersonId personId)
515 {
516         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
517                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
518         SysAssertf(__pAddressbookManagerImpl != null,
519                         "Not yet constructed. Construct() should be called before use.");
520
521         return __pAddressbookManagerImpl->RemovePerson(personId);
522 }
523
524 result
525 AddressbookManager::SetPersonAsFavorite(PersonId personId, bool isFavorite)
526 {
527         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
528                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
529         SysAssertf(__pAddressbookManagerImpl != null,
530                         "Not yet constructed. Construct() should be called before use.");
531
532         return __pAddressbookManagerImpl->SetPersonAsFavorite(personId, isFavorite);
533 }
534
535 IList*
536 AddressbookManager::GetAllPersonsN(void) const
537 {       
538         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
539                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
540         SysAssertf(__pAddressbookManagerImpl != null,
541                         "Not yet constructed. Construct() should be called before use.");
542
543         return __pAddressbookManagerImpl->GetAllPersonsN();
544 }
545
546 IList*
547 AddressbookManager::GetPersonsByCategoryN(RecordId categoryId) const
548 {
549         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
550                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
551         SysAssertf(__pAddressbookManagerImpl != null,
552                         "Not yet constructed. Construct() should be called before use.");
553
554         return __pAddressbookManagerImpl->GetPersonsByCategoryN(categoryId);
555 }
556
557 IList*
558 AddressbookManager::GetFavoritePersonsN() const
559 {
560         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
561                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
562         SysAssertf(__pAddressbookManagerImpl != null,
563                         "Not yet constructed. Construct() should be called before use.");
564
565         return __pAddressbookManagerImpl->GetFavoritePersonsN();
566 }
567
568 IList*
569 AddressbookManager::SearchPersonsN(const Tizen::Base::String& keyword) const
570 {       
571         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
572                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
573         SysAssertf(__pAddressbookManagerImpl != null,
574                         "Not yet constructed. Construct() should be called before use.");
575
576         return __pAddressbookManagerImpl->SearchPersonsN(keyword);
577 }
578
579 result
580 AddressbookManager::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
581 {
582         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
583                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
584         SysAssertf(__pAddressbookManagerImpl != null,
585                         "Not yet constructed. Construct() should be called before use.");
586
587         result r = __pAddressbookManagerImpl->MergePersons(sourcePersonId, targetPersonId);
588         SysTryReturn(NID_SCL, r == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
589
590         return E_SUCCESS;
591 }
592
593 result
594 AddressbookManager::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
595 {
596         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
597                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
598         SysAssertf(__pAddressbookManagerImpl != null,
599                         "Not yet constructed. Construct() should be called before use.");
600
601         result r = __pAddressbookManagerImpl->UnlinkContact(personId, contactId, newPersonId);
602         SysTryReturn(NID_SCL, r == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
603
604         return E_SUCCESS;
605 }
606
607 IList*
608 AddressbookManager::SearchN(const AddressbookFilter& filter, 
609                         unsigned long propertyToSort, Tizen::Base::SortOrder sortOrder, int offset, int maxCount)
610 {
611         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED,
612                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
613         SysAssertf(__pAddressbookManagerImpl != null,
614                         "Not yet constructed. Construct() should be called before use.");
615
616         return __pAddressbookManagerImpl->SearchN(filter, propertyToSort, sortOrder, offset, maxCount);
617 }
618
619 int
620 AddressbookManager::GetMatchedItemCount(const AddressbookFilter& filter)
621 {
622         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, -1, E_PRIVILEGE_DENIED,
623                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
624         SysAssertf(__pAddressbookManagerImpl != null,
625                         "Not yet constructed. Construct() should be called before use.");
626
627         return __pAddressbookManagerImpl->GetMatchedItemCount(filter);
628 }
629
630 IList*
631 AddressbookManager::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
632 {
633         return __pAddressbookManagerImpl->ParseContactsFromVcardN(vcardPath);
634 }
635
636 result
637 AddressbookManager::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
638 {       
639         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
640                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
641         SysAssertf(__pAddressbookManagerImpl != null,
642                         "Not yet constructed. Construct() should be called before use.");
643
644         return __pAddressbookManagerImpl->ExportPersonToVcard(person, vcardPath);
645 }
646
647 result
648 AddressbookManager::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
649 {
650         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ) == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
651                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
652         SysAssertf(__pAddressbookManagerImpl != null,
653                         "Not yet constructed. Construct() should be called before use.");
654
655         return __pAddressbookManagerImpl->ExportPersonsToVcard(personList, vcardPath);
656 }
657
658 result
659 AddressbookManager::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
660 {
661         return __pAddressbookManagerImpl->ExportContactToVcard(contact, vcardPath);
662 }
663
664 result
665 AddressbookManager::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
666 {
667         return __pAddressbookManagerImpl->ExportContactsToVcard(contactList, vcardPath);
668 }
669
670 AddressbookManager*
671 AddressbookManager::GetInstance(void)
672 {
673         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
674
675         if (__pInstance == null)
676         {
677                 ClearLastResult();
678
679                 pthread_once(&onceBlock, InitAddressbookManager);
680
681                 result r = GetLastResult();
682
683                 if (IsFailed(r))
684                 {
685                         onceBlock = PTHREAD_ONCE_INIT;
686                 }
687         }
688
689         return __pInstance;
690 }
691
692 void
693 AddressbookManager::InitAddressbookManager(void)
694 {
695         std::unique_ptr<AddressbookManager> pInstance(new (std::nothrow) AddressbookManager());
696         SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
697
698         result r = pInstance->Construct();
699         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
700
701         __pInstance = pInstance.release();
702
703         std::atexit(DestroyAddressbookManager);
704 }
705
706 void
707 AddressbookManager::DestroyAddressbookManager(void)
708 {
709         delete __pInstance;
710         __pInstance = null;
711 }
712
713 }}  // Tizen::Social