Fix code for TDIS-5396
[framework/osp/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 <FSclUserProfile.h>
33 #include <FSclAddressbookManager.h>
34 #include <FSec_AccessController.h>
35 #include <FScl_AddressbookManagerImpl.h>
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Security;
40
41 namespace Tizen { namespace Social
42 {
43
44 AddressbookManager* AddressbookManager::__pInstance = null;
45
46 AddressbookManager::AddressbookManager(void)
47         : __pAddressbookManagerImpl(null)
48 {
49         //empty body
50 }
51
52 AddressbookManager::~AddressbookManager(void)
53 {
54         delete __pAddressbookManagerImpl;
55 }
56
57 result
58 AddressbookManager::Construct(void)
59 {
60         result r = E_SUCCESS;
61
62         SysAssertf(__pAddressbookManagerImpl == null,
63                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
64
65         _AddressbookManagerImpl* pAddressbookManagerImpl = new (std::nothrow) _AddressbookManagerImpl();
66         SysTryReturnResult(NID_SCL, pAddressbookManagerImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
67
68         r = pAddressbookManagerImpl->Construct();
69         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
70
71         __pAddressbookManagerImpl = pAddressbookManagerImpl;
72
73         return E_SUCCESS;
74
75 CATCH:
76         delete pAddressbookManagerImpl;
77
78         return r;
79 }
80
81 result
82 AddressbookManager::SetEventListener(IAddressbookEventListener* pListener)
83 {
84         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
85         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
86         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
87         SysAssertf(__pAddressbookManagerImpl != null,
88                         "Not yet constructed. Construct() should be called before use.");
89
90         r = __pAddressbookManagerImpl->SetEventListener(pListener);
91         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
92
93         return E_SUCCESS;
94 }
95
96 result
97 AddressbookManager::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
98 {
99         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
100         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
101         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
102         SysAssertf(__pAddressbookManagerImpl != null,
103                         "Not yet constructed. Construct() should be called before use.");
104
105         r = __pAddressbookManagerImpl->SetAddressbookChangeEventListener(pListener);
106         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
107
108         return E_SUCCESS;
109 }
110
111 Addressbook*
112 AddressbookManager::CreateAddressbookN(AccountId accountId, const String& name)
113 {
114         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
115         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
116         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
117         SysAssertf(__pAddressbookManagerImpl != null,
118                         "Not yet constructed. Construct() should be called before use.");
119
120         Addressbook* pAddressbook = __pAddressbookManagerImpl->CreateAddressbookN(accountId, name);
121         SysTryReturn(NID_SCL, pAddressbook != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
122
123         return pAddressbook;
124 }
125
126 result
127 AddressbookManager::DeleteAddressbook(AddressbookId addressbookId)
128 {
129         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
130         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
131         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
132         SysAssertf(__pAddressbookManagerImpl != null,
133                         "Not yet constructed. Construct() should be called before use.");
134
135         r = __pAddressbookManagerImpl->DeleteAddressbook(addressbookId);
136         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
137
138         return E_SUCCESS;
139 }
140
141 IList*
142 AddressbookManager::GetAddressbooksByAccountN(AccountId accountId) const
143 {
144         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
145         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
146         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
147         SysAssertf(__pAddressbookManagerImpl != null,
148                         "Not yet constructed. Construct() should be called before use.");
149
150         IList*  pList = __pAddressbookManagerImpl->GetAddressbooksByAccountN(accountId);
151         SysTryReturn(NID_SCL, pList !=null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
152
153         return pList;
154 }
155
156 IList*
157 AddressbookManager::GetAllAddressbooksN(void) const
158 {
159         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
160         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
161         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
162         SysAssertf(__pAddressbookManagerImpl != null,
163                         "Not yet constructed. Construct() should be called before use.");
164
165         IList* pList = __pAddressbookManagerImpl->GetAllAddressbooksN();
166         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
167
168         return pList;
169 }
170
171 Addressbook*
172 AddressbookManager::GetAddressbookN(AddressbookId addressbookId) const
173 {
174         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
175         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
176         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
177         SysAssertf(__pAddressbookManagerImpl != null,
178                         "Not yet constructed. Construct() should be called before use.");
179
180         Addressbook* pAddressbook = __pAddressbookManagerImpl->GetAddressbookN(addressbookId);
181         SysTryReturn(NID_SCL, pAddressbook != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
182
183         return pAddressbook;
184 }
185
186 result
187 AddressbookManager::AddContact(Contact& contact, AddressbookId addressbookId)
188 {
189         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
190         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
191         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
192         SysAssertf(__pAddressbookManagerImpl != null,
193                         "Not yet constructed. Construct() should be called before use.");
194
195         r = __pAddressbookManagerImpl->AddContact(contact, addressbookId);
196         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
197
198         return E_SUCCESS;
199 }
200
201 result
202 AddressbookManager::AddCategory(Category& category, AddressbookId addressbookId)
203 {
204         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
205         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
206         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
207         SysAssertf(__pAddressbookManagerImpl != null,
208                         "Not yet constructed. Construct() should be called before use.");
209
210         r = __pAddressbookManagerImpl->AddCategory(category, addressbookId);
211         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
212
213         return E_SUCCESS;
214 }
215
216 result
217 AddressbookManager::RemoveContact(RecordId contactId)
218 {
219         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
220         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
221         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
222         SysAssertf(__pAddressbookManagerImpl != null,
223                         "Not yet constructed. Construct() should be called before use.");
224
225         r = __pAddressbookManagerImpl->RemoveContact(contactId);
226         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
227
228         return E_SUCCESS;
229 }
230
231 result
232 AddressbookManager::RemoveCategory(RecordId categoryId)
233 {
234         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
235         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
236         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
237         SysAssertf(__pAddressbookManagerImpl != null,
238                         "Not yet constructed. Construct() should be called before use.");
239
240         r = __pAddressbookManagerImpl->RemoveCategory(categoryId);
241         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
242
243         return E_SUCCESS;
244 }
245
246 result
247 AddressbookManager::UpdateContact(const Contact& contact)
248 {
249         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
250         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
251         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
252         SysAssertf(__pAddressbookManagerImpl != null,
253                         "Not yet constructed. Construct() should be called before use.");
254
255         r = __pAddressbookManagerImpl->UpdateContact(contact);
256         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
257
258         return E_SUCCESS;
259 }
260
261 result
262 AddressbookManager::UpdateCategory(const Category& category)
263 {
264         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
265         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
266         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
267         SysAssertf(__pAddressbookManagerImpl != null,
268                         "Not yet constructed. Construct() should be called before use.");
269
270         r = __pAddressbookManagerImpl->UpdateCategory(category);
271         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
272
273         return E_SUCCESS;
274 }
275
276 IList*
277 AddressbookManager::GetAllContactsN(void) const
278 {
279         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
280         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
281         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
282         SysAssertf(__pAddressbookManagerImpl != null,
283                         "Not yet constructed. Construct() should be called before use.");
284
285         IList* pList = __pAddressbookManagerImpl->GetAllContactsN();
286         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
287
288         return pList;
289 }
290
291 IList*
292 AddressbookManager::GetContactsByCategoryN(RecordId categoryId) const
293 {
294         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
295         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
296         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
297         SysAssertf(__pAddressbookManagerImpl != null,
298                         "Not yet constructed. Construct() should be called before use.");
299
300         IList* pList = __pAddressbookManagerImpl->GetContactsByCategoryN(categoryId);
301         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
302
303         return pList;
304 }
305
306 IList*
307 AddressbookManager::GetContactsByPersonN(PersonId personId) const
308 {
309         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
310         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
311         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
312         SysAssertf(__pAddressbookManagerImpl != null,
313                         "Not yet constructed. Construct() should be called before use.");
314
315         IList* pList = __pAddressbookManagerImpl->GetContactsByPersonN(personId);
316         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
317
318         return pList;
319 }
320
321 result
322 AddressbookManager::AddMemberToCategory(RecordId categoryId, RecordId contactId)
323 {
324         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
325         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
326         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
327         SysAssertf(__pAddressbookManagerImpl != null,
328                         "Not yet constructed. Construct() should be called before use.");
329
330         r = __pAddressbookManagerImpl->AddMemberToCategory(categoryId, contactId);
331         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
332
333         return E_SUCCESS;
334 }
335
336 result
337 AddressbookManager::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
338 {
339         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
340         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
341         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
342         SysAssertf(__pAddressbookManagerImpl != null,
343                         "Not yet constructed. Construct() should be called before use.");
344
345         r = __pAddressbookManagerImpl->RemoveMemberFromCategory(categoryId, contactId);
346         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
347
348         return E_SUCCESS;
349 }
350
351 IList*
352 AddressbookManager::GetAllCategoriesN(void) const
353 {
354         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
355         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
356         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
357         SysAssertf(__pAddressbookManagerImpl != null,
358                         "Not yet constructed. Construct() should be called before use.");
359
360         IList* pList = __pAddressbookManagerImpl->GetAllCategoriesN();
361         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
362
363         return pList;
364 }
365
366 IList*
367 AddressbookManager::GetCategoriesByContactN(RecordId contactId) const
368 {
369         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
370         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
371         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
372         SysAssertf(__pAddressbookManagerImpl != null,
373                         "Not yet constructed. Construct() should be called before use.");
374
375         IList* pList = __pAddressbookManagerImpl->GetCategoriesByContactN(contactId);
376         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
377
378         return pList;
379 }
380
381 IList*
382 AddressbookManager::GetCategoriesByPersonN(PersonId personId) const
383 {
384         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
385         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
386         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
387         SysAssertf(__pAddressbookManagerImpl != null,
388                         "Not yet constructed. Construct() should be called before use.");
389
390         IList* pList = __pAddressbookManagerImpl->GetCategoriesByPersonN(personId);
391         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
392
393         return pList;
394 }
395
396 IList*
397 AddressbookManager::SearchContactsByEmailN(const String& email) const
398 {
399         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
400         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
401         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
402         SysAssertf(__pAddressbookManagerImpl != null,
403                         "Not yet constructed. Construct() should be called before use.");
404
405         IList* pList = __pAddressbookManagerImpl->SearchContactsByEmailN(email);
406         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
407
408         return pList;
409 }
410
411 IList*
412 AddressbookManager::SearchContactsByNameN(const String& name) const
413 {
414         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
415         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
416         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
417         SysAssertf(__pAddressbookManagerImpl != null,
418                         "Not yet constructed. Construct() should be called before use.");
419
420         IList* pList = __pAddressbookManagerImpl->SearchContactsByNameN(name);
421         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
422
423         return pList;
424 }
425
426 IList*
427 AddressbookManager::SearchContactsByPhoneNumberN(const String& phoneNumber) const
428 {
429         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
430         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
431         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
432         SysAssertf(__pAddressbookManagerImpl != null,
433                         "Not yet constructed. Construct() should be called before use.");
434
435         IList* pList = __pAddressbookManagerImpl->SearchContactsByPhoneNumberN(phoneNumber);
436         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
437
438         return pList;
439 }
440
441 int
442 AddressbookManager::GetCategoryCount(void) const
443 {
444         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
445         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
446         SysTryReturn(NID_SCL, r == E_SUCCESS, -1, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
447         SysAssertf(__pAddressbookManagerImpl != null,
448                         "Not yet constructed. Construct() should be called before use.");
449
450         int count = __pAddressbookManagerImpl->GetCategoryCount();
451         SysTryReturn(NID_SCL, count != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
452
453         return count;
454 }
455
456 int
457 AddressbookManager::GetContactCount(void) const
458 {
459         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
460         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
461         SysTryReturn(NID_SCL, r == E_SUCCESS, -1, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
462         SysAssertf(__pAddressbookManagerImpl != null,
463                         "Not yet constructed. Construct() should be called before use.");
464
465         int count = __pAddressbookManagerImpl->GetContactCount();
466         SysTryReturn(NID_SCL, count != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
467
468         return count;
469 }
470
471 Contact*
472 AddressbookManager::GetContactN(RecordId contactId) const
473 {
474         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
475         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
476         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
477         SysAssertf(__pAddressbookManagerImpl != null,
478                         "Not yet constructed. Construct() should be called before use.");
479
480         Contact* pContact = __pAddressbookManagerImpl->GetContactN(contactId);
481         SysTryReturn(NID_SCL, pContact != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
482
483         return pContact;
484 }
485
486 Person*
487 AddressbookManager::GetPersonN(PersonId personId) const
488 {
489         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
490         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
491         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
492         SysAssertf(__pAddressbookManagerImpl != null,
493                         "Not yet constructed. Construct() should be called before use.");
494
495         Person* pPerson = __pAddressbookManagerImpl->GetPersonN(personId);
496         SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
497
498         return pPerson;
499 }
500 Category*
501 AddressbookManager::GetCategoryN(RecordId categoryId) const
502 {
503         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
504         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
505         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
506         SysAssertf(__pAddressbookManagerImpl != null,
507                         "Not yet constructed. Construct() should be called before use.");
508
509         Category* pCategory = __pAddressbookManagerImpl->GetCategoryN(categoryId);
510         SysTryReturn(NID_SCL, pCategory != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
511
512         return pCategory;
513 }
514
515 int
516 AddressbookManager::GetLatestVersion(void) const
517 {
518         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
519         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
520         SysTryReturn(NID_SCL, r == E_SUCCESS, -1, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
521         SysAssertf(__pAddressbookManagerImpl != null,
522                         "Not yet constructed. Construct() should be called before use.");
523
524         int latestVersion = __pAddressbookManagerImpl->GetLatestVersion();
525         SysTryReturn(NID_SCL, latestVersion != -1, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
526
527         return latestVersion;
528 }
529
530 IList*
531 AddressbookManager::GetChangedContactsAfterN(int version, int& latestVersion) const
532 {
533         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
534         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
535         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
536         SysAssertf(__pAddressbookManagerImpl != null,
537                         "Not yet constructed. Construct() should be called before use.");
538
539         IList* pList = __pAddressbookManagerImpl->GetChangedContactsAfterN(version, latestVersion);
540         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
541
542         return pList;
543 }
544
545 IList*
546 AddressbookManager::GetChangedCategoriesAfterN(int version, int& latestVersion) const
547 {
548         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
549         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
550         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
551         SysAssertf(__pAddressbookManagerImpl != null,
552                         "Not yet constructed. Construct() should be called before use.");
553
554         IList* pList = __pAddressbookManagerImpl->GetChangedCategoriesAfterN(version, latestVersion);
555         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
556
557         return pList;
558 }
559
560 result
561 AddressbookManager::RemovePerson(PersonId personId)
562 {
563         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
564         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
565         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
566         SysAssertf(__pAddressbookManagerImpl != null,
567                         "Not yet constructed. Construct() should be called before use.");
568
569         return __pAddressbookManagerImpl->RemovePerson(personId);
570 }
571
572 result
573 AddressbookManager::SetPersonAsFavorite(PersonId personId, bool isFavorite)
574 {
575         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
576         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
577         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
578         SysAssertf(__pAddressbookManagerImpl != null,
579                         "Not yet constructed. Construct() should be called before use.");
580
581         return __pAddressbookManagerImpl->SetPersonAsFavorite(personId, isFavorite);
582 }
583
584 IList*
585 AddressbookManager::GetAllPersonsN(void) const
586 {       
587         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
588         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
589         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
590         SysAssertf(__pAddressbookManagerImpl != null,
591                         "Not yet constructed. Construct() should be called before use.");
592
593         return __pAddressbookManagerImpl->GetAllPersonsN();
594 }
595
596 IList*
597 AddressbookManager::GetPersonsByCategoryN(RecordId categoryId) const
598 {
599         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
600         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
601         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
602         SysAssertf(__pAddressbookManagerImpl != null,
603                         "Not yet constructed. Construct() should be called before use.");
604
605         return __pAddressbookManagerImpl->GetPersonsByCategoryN(categoryId);
606 }
607
608 IList*
609 AddressbookManager::GetFavoritePersonsN() const
610 {
611         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
612         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
613         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
614         SysAssertf(__pAddressbookManagerImpl != null,
615                         "Not yet constructed. Construct() should be called before use.");
616
617         return __pAddressbookManagerImpl->GetFavoritePersonsN();
618 }
619
620 IList*
621 AddressbookManager::SearchPersonsN(const Tizen::Base::String& keyword) const
622 {       
623         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
624         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
625         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
626         SysAssertf(__pAddressbookManagerImpl != null,
627                         "Not yet constructed. Construct() should be called before use.");
628
629         return __pAddressbookManagerImpl->SearchPersonsN(keyword);
630 }
631
632 result
633 AddressbookManager::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
634 {
635         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
636         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
637         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
638         SysAssertf(__pAddressbookManagerImpl != null,
639                         "Not yet constructed. Construct() should be called before use.");
640
641         r = __pAddressbookManagerImpl->MergePersons(sourcePersonId, targetPersonId);
642         SysTryReturn(NID_SCL, r == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
643
644         return E_SUCCESS;
645 }
646
647 result
648 AddressbookManager::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
649 {
650         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_WRITE);
651         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
652         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
653         SysAssertf(__pAddressbookManagerImpl != null,
654                         "Not yet constructed. Construct() should be called before use.");
655
656         r = __pAddressbookManagerImpl->UnlinkContact(personId, contactId, newPersonId);
657         SysTryReturn(NID_SCL, r == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
658
659         return E_SUCCESS;
660 }
661
662 IList*
663 AddressbookManager::SearchN(const AddressbookFilter& filter, 
664                         unsigned long propertyToSort, Tizen::Base::SortOrder sortOrder, int offset, int maxCount)
665 {
666         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
667         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
668         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
669         SysAssertf(__pAddressbookManagerImpl != null,
670                         "Not yet constructed. Construct() should be called before use.");
671
672         return __pAddressbookManagerImpl->SearchN(filter, propertyToSort, sortOrder, offset, maxCount);
673 }
674
675 int
676 AddressbookManager::GetMatchedItemCount(const AddressbookFilter& filter)
677 {
678         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
679         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
680         SysTryReturn(NID_SCL, r == E_SUCCESS, -1, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
681         SysAssertf(__pAddressbookManagerImpl != null,
682                         "Not yet constructed. Construct() should be called before use.");
683
684         return __pAddressbookManagerImpl->GetMatchedItemCount(filter);
685 }
686
687 IList*
688 AddressbookManager::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
689 {
690         return __pAddressbookManagerImpl->ParseContactsFromVcardN(vcardPath);
691 }
692
693 result
694 AddressbookManager::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
695 {       
696         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
697         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
698         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
699         SysAssertf(__pAddressbookManagerImpl != null,
700                         "Not yet constructed. Construct() should be called before use.");
701
702         return __pAddressbookManagerImpl->ExportPersonToVcard(person, vcardPath);
703 }
704
705 result
706 AddressbookManager::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
707 {
708         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
709         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
710         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
711         SysAssertf(__pAddressbookManagerImpl != null,
712                         "Not yet constructed. Construct() should be called before use.");
713
714         return __pAddressbookManagerImpl->ExportPersonsToVcard(personList, vcardPath);
715 }
716
717 result
718 AddressbookManager::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
719 {
720         return __pAddressbookManagerImpl->ExportContactToVcard(contact, vcardPath);
721 }
722
723 result
724 AddressbookManager::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
725 {
726         return __pAddressbookManagerImpl->ExportContactsToVcard(contactList, vcardPath);
727 }
728
729 ByteBuffer*
730 AddressbookManager::ExportContactToVcardStreamN(const Contact& contact)
731 {
732         return __pAddressbookManagerImpl->ExportContactToVcardStreamN(contact);
733 }
734
735 ByteBuffer*
736 AddressbookManager::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
737 {
738         return __pAddressbookManagerImpl->ExportContactsToVcardStreamN(contactList);
739 }
740
741 ByteBuffer*
742 AddressbookManager::ExportPersonToVcardStreamN(const Person& person)
743 {
744         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
745         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
746         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
747         SysAssertf(__pAddressbookManagerImpl != null,
748                         "Not yet constructed. Construct() should be called before use.");
749
750         return __pAddressbookManagerImpl->ExportPersonToVcardStreamN(person);
751 }
752
753 ByteBuffer*
754 AddressbookManager::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
755 {
756         result r = _AccessController::CheckUserPrivilege(_PRV_CONTACT_READ);
757         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
758         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
759         SysAssertf(__pAddressbookManagerImpl != null,
760                         "Not yet constructed. Construct() should be called before use.");
761
762         return __pAddressbookManagerImpl->ExportPersonsToVcardStreamN(personList);
763 }
764
765 IList*
766 AddressbookManager::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
767 {
768         return __pAddressbookManagerImpl->ParseVcardStreamN(vcardStream);
769 }
770
771 Tizen::Base::ByteBuffer*
772 AddressbookManager::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
773 {
774         SysAssertf(__pAddressbookManagerImpl != null,
775                         "Not yet constructed. Construct() should be called before use.");
776
777         return __pAddressbookManagerImpl->ExportUserProfileToVcardStreamN(userProfile);
778 }
779
780 Tizen::Base::ByteBuffer*
781 AddressbookManager::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
782 {
783         SysAssertf(__pAddressbookManagerImpl != null,
784                         "Not yet constructed. Construct() should be called before use.");
785
786         return __pAddressbookManagerImpl->ExportUserProfilesToVcardStreamN(userProfileList);
787 }
788
789 result
790 AddressbookManager::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
791 {
792         SysAssertf(__pAddressbookManagerImpl != null,
793                         "Not yet constructed. Construct() should be called before use.");
794
795         return __pAddressbookManagerImpl->ExportUserProfileToVcard(userProfile, vcardPath);
796 }
797
798 result
799 AddressbookManager::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
800 {
801         SysAssertf(__pAddressbookManagerImpl != null,
802                         "Not yet constructed. Construct() should be called before use.");
803
804         return __pAddressbookManagerImpl->ExportUserProfilesToVcard(userProfileList, vcardPath);
805 }
806
807 Tizen::Base::Collection::IList*
808 AddressbookManager::GetAllUserProfilesN(void) const
809 {
810         result r = _AccessController::CheckUserPrivilege(_PRV_USERPROFILE_READ);
811         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
812         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
813         SysAssertf(__pAddressbookManagerImpl != null,
814                         "Not yet constructed. Construct() should be called before use.");
815
816         return __pAddressbookManagerImpl->GetAllUserProfilesN();
817 }
818
819 UserProfile*
820 AddressbookManager::GetUserProfileN(AddressbookId addressbookId) const
821 {
822         result r = _AccessController::CheckUserPrivilege(_PRV_USERPROFILE_READ);
823         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_USER_NOT_CONSENTED);
824         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] The application is not permitted to call this method.", GetErrorMessage(r));
825         SysAssertf(__pAddressbookManagerImpl != null,
826                         "Not yet constructed. Construct() should be called before use.");
827
828         return __pAddressbookManagerImpl->GetUserProfileN(addressbookId);
829 }
830
831 AddressbookManager*
832 AddressbookManager::GetInstance(void)
833 {
834         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
835
836         if (__pInstance == null)
837         {
838                 ClearLastResult();
839
840                 pthread_once(&onceBlock, InitAddressbookManager);
841
842                 result r = GetLastResult();
843
844                 if (IsFailed(r))
845                 {
846                         onceBlock = PTHREAD_ONCE_INIT;
847                 }
848         }
849
850         return __pInstance;
851 }
852
853 void
854 AddressbookManager::InitAddressbookManager(void)
855 {
856         std::unique_ptr<AddressbookManager> pInstance(new (std::nothrow) AddressbookManager());
857         SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
858
859         result r = pInstance->Construct();
860         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
861
862         __pInstance = pInstance.release();
863
864         std::atexit(DestroyAddressbookManager);
865 }
866
867 void
868 AddressbookManager::DestroyAddressbookManager(void)
869 {
870         delete __pInstance;
871         __pInstance = null;
872 }
873
874 }}  // Tizen::Social