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