update tizen source
[framework/messaging/msg-service.git] / utils / MsgContact.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 extern "C"
32 {
33         #include <contacts-svc.h>
34 }
35
36 #include "MsgDebug.h"
37 #include "MsgUtilStorage.h"
38 #include "MsgGconfWrapper.h"
39 #include "MsgContact.h"
40
41
42 /*==================================================================================================
43                                      VARIABLES
44 ==================================================================================================*/
45 static bool isContactSvcOpened = false;
46
47 MsgDbHandler ContactDbHandle;
48
49 MsgContactChangeCB cbFunction = NULL;
50 /*==================================================================================================
51                                      FUNCTION IMPLEMENTATION
52 ==================================================================================================*/
53 static void MsgContactSvcCallback(void *pData)
54 {
55         MSG_DEBUG("Contact Data is Changed!!!");
56
57         MsgSyncContact();
58
59         if (ContactDbHandle.disconnect() != MSG_SUCCESS)
60         {
61                 MSG_DEBUG("DB Disconnect Fail");
62         }
63 }
64
65
66 MSG_ERROR_T MsgOpenContactSvc()
67 {
68         int errCode = CTS_SUCCESS;
69
70         if(!isContactSvcOpened)
71         {
72                 errCode = contacts_svc_connect();
73
74                 if (errCode == CTS_SUCCESS)
75                 {
76                         MSG_DEBUG("Connect to Contact Service Success");
77                         isContactSvcOpened = true;
78                 }
79                 else
80                 {
81                         MSG_DEBUG("Connect to Contact Service Fail [%d]", errCode);
82                         isContactSvcOpened = false;
83                         return MSG_ERR_DB_CONNECT;
84                 }
85         }
86         else
87         {
88                 MSG_DEBUG("Already connected to Contact Service.");
89         }
90
91         return MSG_SUCCESS;
92 }
93
94
95 MSG_ERROR_T MsgCloseContactSvc()
96 {
97         int errCode = CTS_SUCCESS;
98
99         if(isContactSvcOpened)
100         {
101                 errCode = contacts_svc_disconnect();
102
103                 if (errCode == CTS_SUCCESS)
104                 {
105                         MSG_DEBUG("Disconnect to Contact Service Success");
106                 }
107                 else
108                 {
109                         MSG_DEBUG("Disconnect to Contact Service Fail [%d]", errCode);
110                         return MSG_ERR_DB_DISCONNECT;
111                 }
112         }
113
114         return MSG_SUCCESS;
115 }
116
117
118 MSG_ERROR_T MsgInitContactSvc(MsgContactChangeCB cb)
119 {
120         int errCode = CTS_SUCCESS;
121
122         if (cb != NULL)
123                 cbFunction = cb;
124
125         // Register callback function
126         errCode = contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, MsgContactSvcCallback, NULL);
127
128         if (errCode == CTS_SUCCESS)
129         {
130                 MSG_DEBUG("Register Contact Service Callback");
131         }
132         else
133         {
134                 MSG_DEBUG("Fail to Register Contact Service Callback [%d]", errCode);
135         }
136
137         return MSG_SUCCESS;
138 }
139
140
141 MSG_ERROR_T MsgGetContactInfo(const MSG_ADDRESS_INFO_S *pAddrInfo, MSG_CONTACT_INFO_S *pContactInfo)
142 {
143         MSG_BEGIN();
144
145         MSG_DEBUG("Address Type [%d], Address Value [%s]", pAddrInfo->addressType, pAddrInfo->addressVal);
146
147         memset(pContactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
148
149         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_PLMN && strlen(pAddrInfo->addressVal) > (MAX_PHONE_NUMBER_LEN+1))
150         {
151                 MSG_DEBUG("Phone Number is too long [%s]", pAddrInfo->addressVal);
152                 return MSG_ERR_INVALID_PARAMETER;
153         }
154
155         int index, ret = -1;
156
157         CTSstruct* contact = NULL;
158
159         cts_find_op recordType;
160
161         if (pAddrInfo->addressType == MSG_ADDRESS_TYPE_EMAIL)
162                 recordType = CTS_FIND_BY_EMAIL;
163         else
164                 recordType = CTS_FIND_BY_NUMBER;
165
166         index = contacts_svc_find_contact_by(recordType, (char*)pAddrInfo->addressVal);
167
168         if (index > CTS_SUCCESS)
169         {
170                 MSG_DEBUG("Index : [%d]", index);
171                 ret = contacts_svc_get_contact(index, &contact);
172         }
173
174         if (ret < 0)
175         {
176                 MSG_DEBUG("No Contact Info");
177                 return MSG_SUCCESS;
178         }
179
180         CTSvalue* name = NULL;
181
182         ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
183
184         if (ret != CTS_SUCCESS)
185         {
186                 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
187                 return MSG_SUCCESS;
188         }
189
190         const char* strDisplayName = contacts_svc_value_get_str(name, CTS_NAME_VAL_DISPLAY_STR);
191         const char* strFirstName = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
192         const char* strLastName = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
193
194         MSG_DEBUG("Display Name : [%s], First Name : [%s], Last Name : [%s]", strDisplayName, strFirstName, strLastName);
195
196         if (strDisplayName != NULL)
197         {
198                 strncpy(pContactInfo->displayName, strDisplayName, MAX_DISPLAY_NAME_LEN);
199         }
200
201         if (strFirstName != NULL)
202         {
203                 strncpy(pContactInfo->firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
204         }
205
206         if (strLastName != NULL)
207         {
208                 strncpy(pContactInfo->lastName, strLastName, MAX_DISPLAY_NAME_LEN);
209         }
210
211         CTSvalue* base = NULL;
212
213         ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &base);
214
215         if (ret != CTS_SUCCESS)
216         {
217                 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
218                 return MSG_SUCCESS;
219         }
220
221         pContactInfo->contactId = contacts_svc_value_get_int(base, CTS_BASE_VAL_ID_INT);
222
223         MSG_DEBUG("Contact ID [%d]", pContactInfo->contactId);
224
225         const char* strImagePath = contacts_svc_value_get_str(base, CTS_BASE_VAL_IMG_PATH_STR);
226
227         if (strImagePath != NULL)
228         {
229                 strncpy(pContactInfo->imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
230         }
231
232         MSG_DEBUG("Image Path [%s]", pContactInfo->imagePath);
233
234         contacts_svc_value_free(base);
235         contacts_svc_value_free(name);
236
237         contacts_svc_struct_free(contact);
238
239         MSG_END();
240
241         return MSG_SUCCESS;
242 }
243
244
245 void MsgSyncContact()
246 {
247         int ret = -1;
248         int index_num = 0;
249         int changed_count = 0;
250         int lastSyncTime = 0;
251
252         /* get contact sync time */
253         lastSyncTime = MsgSettingGetInt(CONTACT_SYNC_TIME);
254
255         if (lastSyncTime < 0) {
256                 MSG_DEBUG("Fail to get CONTACT_SYNC_TIME.");
257                 lastSyncTime = 0;
258         }
259
260         CTSiter* pIter;
261
262         ret = contacts_svc_get_updated_contacts(0, lastSyncTime, &pIter);
263
264         if (ret != CTS_SUCCESS)
265         {
266                 MSG_DEBUG("contacts_svc_get_updated_contacts() Error [%d]", ret);
267                 return;
268         }
269
270         while (contacts_svc_iter_next(pIter) == CTS_SUCCESS)
271         {
272                 CTSvalue *row_info = NULL;
273
274                 row_info = contacts_svc_iter_get_info(pIter);
275
276                 index_num = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
277
278                 MSG_DEBUG("index (%d)", index_num);
279
280                 int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
281
282                 if (type == CTS_OPERATION_UPDATED || type == CTS_OPERATION_INSERTED)
283                 {
284                         MsgUpdateContact(index_num, type);
285                 }
286                 else // Delete
287                 {
288                         MSG_DEBUG("Delete Contact");
289
290                         MsgDeleteContact(index_num);
291                 }
292
293                 if(lastSyncTime < contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT))
294                         lastSyncTime = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT);
295
296                 contacts_svc_value_free(row_info);
297
298                 changed_count++;
299
300         }
301
302         MsgSettingSetInt(CONTACT_SYNC_TIME, lastSyncTime);
303         MSG_DEBUG("lastSyncTime : %d", lastSyncTime);
304
305         contacts_svc_iter_remove(pIter);
306
307         if(changed_count > 0)
308                 cbFunction();
309 }
310
311
312 bool MsgInsertContact(MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
313 {
314         if (!pNumber || strlen(pNumber) <= 0)
315                 return false;
316
317         if (MsgStoAddContactInfo(&ContactDbHandle, pContactInfo, pNumber) != MSG_SUCCESS)
318         {
319                 MSG_DEBUG("Fail to add contact info.");
320                 return false;
321         }
322
323         return true;
324 }
325
326
327 bool MsgUpdateContact(int index, int type)
328 {
329         int ret = -1;
330
331         CTSstruct *contact = NULL;
332
333         ret = contacts_svc_get_contact(index, &contact);
334
335         if (ret != CTS_SUCCESS)
336         {
337                 MSG_DEBUG("contacts_svc_get_contact() Error [%d]", ret);
338                 return false;
339         }
340
341         // Base Info
342         CTSvalue *base = NULL;
343
344         ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &base);
345
346         if (ret != CTS_SUCCESS)
347         {
348                 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
349                 return false;
350         }
351
352         MSG_CONTACT_INFO_S contactInfo = {0};
353
354         contactInfo.contactId = contacts_svc_value_get_int(base, CTS_BASE_VAL_ID_INT);
355
356         MSG_DEBUG("Contact ID [%d]", contactInfo.contactId);
357
358         const char* strImagePath = contacts_svc_value_get_str(base, CTS_BASE_VAL_IMG_PATH_STR);
359
360         if (strImagePath != NULL)
361         {
362                 strncpy(contactInfo.imagePath , strImagePath, MAX_IMAGE_PATH_LEN);
363         }
364
365         MSG_DEBUG("Image Path [%s]", contactInfo.imagePath);
366
367         // Name Info
368         CTSvalue* name = NULL;
369
370         ret = contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &name);
371
372         if (ret != CTS_SUCCESS)
373         {
374                 MSG_DEBUG("contacts_svc_struct_get_value() Error [%d]", ret);
375                 return MSG_SUCCESS;
376         }
377
378         const char* strDisplayName = contacts_svc_value_get_str(name, CTS_NAME_VAL_DISPLAY_STR);
379         const char* strFirstName = contacts_svc_value_get_str(name, CTS_NAME_VAL_FIRST_STR);
380         const char* strLastName = contacts_svc_value_get_str(name, CTS_NAME_VAL_LAST_STR);
381
382         MSG_DEBUG("Display Name : [%s], First Name : [%s], Last Name : [%s]", strDisplayName, strFirstName, strLastName);
383
384         if (strDisplayName != NULL)
385         {
386                 strncpy(contactInfo.displayName, strDisplayName, MAX_DISPLAY_NAME_LEN);
387         }
388
389         if (strFirstName != NULL)
390         {
391                 strncpy(contactInfo.firstName, strFirstName, MAX_DISPLAY_NAME_LEN);
392         }
393
394         if (strLastName != NULL)
395         {
396                 strncpy(contactInfo.lastName, strLastName, MAX_DISPLAY_NAME_LEN);
397         }
398
399         MsgStoClearContactInfo(&ContactDbHandle, index);
400
401         GSList *get_list, *cursor;
402
403         get_list = NULL;
404
405         contacts_svc_struct_get_list(contact, CTS_CF_NUMBER_LIST, &get_list);
406
407         cursor = get_list;
408
409         // No phone number in contact
410         if (cursor == NULL)
411         {
412                  contacts_svc_struct_free(contact);
413
414                 return true;
415         }
416
417         for(; cursor; cursor = g_slist_next(cursor))
418         {
419                 MSG_DEBUG("Add Contact Data");
420
421                 const char* strNumber = contacts_svc_value_get_str((CTSvalue*)cursor->data, CTS_NUM_VAL_NUMBER_STR);
422
423                 MSG_DEBUG("Number = %s", strNumber);
424
425                 if (MsgInsertContact(&contactInfo, strNumber) == false)
426                 {
427                         continue;
428                 }
429         }
430
431         contacts_svc_struct_free(contact);
432
433         return true;
434 }
435
436
437 bool MsgDeleteContact(int index)
438 {
439         if (MsgStoClearContactInfo(&ContactDbHandle, index) != MSG_SUCCESS)
440         {
441                 return false;
442         }
443
444         return true;
445 }
446
447
448 int MsgGetContactNameOrder()
449 {
450         int ret = 0;
451
452         cts_order_type order = CTS_ORDER_NAME_FIRSTLAST;
453
454         order = contacts_svc_get_order(CTS_ORDER_OF_DISPLAY);
455
456         if (order == CTS_ORDER_NAME_FIRSTLAST)
457                 ret = 0;
458         else if (order == CTS_ORDER_NAME_LASTFIRST)
459                 ret = 1;
460
461         return ret;
462 }
463
464
465 int MsgContactSVCBeginTrans()
466 {
467         return contacts_svc_begin_trans();
468 }
469
470
471 int MsgContactSVCEndTrans(bool bSuccess)
472 {
473         return contacts_svc_end_trans(bSuccess);
474 }