Relase Tizen 2.0 beta
[framework/api/call-log.git] / src / calllog.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17  
18 #include <tizen.h>
19 #include <calllog.h>
20 #include <contacts-svc.h>
21 #include <calllog_private.h>
22 #include <dlog.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "TIZEN_N_CALLLOG"
31 #define LOG_MODE (1)
32
33 int calllog_connect(void)
34 {
35         if(contacts_svc_connect() == CTS_SUCCESS) {   
36                 return CALLLOG_ERROR_NONE;
37     }
38     LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
39         return CALLLOG_ERROR_DB_FAILED;
40 }
41
42 int calllog_disconnect(void)
43 {
44         if(contacts_svc_disconnect() == CTS_SUCCESS) {
45                 return CALLLOG_ERROR_NONE;
46     }
47     LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
48         return CALLLOG_ERROR_DB_FAILED;
49 }
50
51 int calllog_create(calllog_h* log)
52 {
53         if(log == NULL) {
54         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
55                 return CALLLOG_ERROR_INVALID_PARAMETER;
56     }
57         CTSvalue* value = contacts_svc_value_new(CTS_VALUE_PHONELOG);
58         if(value == NULL) {
59         LOGE("[%s] CALLLOG_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CALLLOG_ERROR_OUT_OF_MEMORY);
60                 return CALLLOG_ERROR_OUT_OF_MEMORY;
61         }
62         *log = (calllog_h)value;
63         return CALLLOG_ERROR_NONE;
64 }
65
66 int calllog_destroy(calllog_h log)
67 {
68         if(log == NULL) {
69         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
70                 return CALLLOG_ERROR_INVALID_PARAMETER;
71     }
72         if(contacts_svc_value_free((CTSvalue*)log) == CTS_SUCCESS) {
73                 return CALLLOG_ERROR_NONE;
74     }
75     LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
76         return CALLLOG_ERROR_INVALID_PARAMETER;
77 }
78
79 int calllog_insert_to_db(calllog_h log)
80 {
81         if(log == NULL) {
82         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
83                 return CALLLOG_ERROR_INVALID_PARAMETER;
84     }
85         if(contacts_svc_insert_phonelog((CTSvalue*)log) == CTS_SUCCESS) {
86                 return CALLLOG_ERROR_NONE;
87     }
88     LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
89         return CALLLOG_ERROR_DB_FAILED;
90 }
91
92 int calllog_delete_from_db(int calllog_db_id)
93 {
94         if(calllog_db_id < 0) {
95         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
96                 return CALLLOG_ERROR_INVALID_PARAMETER;
97     }
98         if(contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, calllog_db_id) == CTS_SUCCESS) {
99                 return CALLLOG_ERROR_NONE;
100     }
101     LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
102         return CALLLOG_ERROR_DB_FAILED;
103 }
104
105 int calllog_delete_all_from_db()
106 {
107         CTSiter *iter = NULL;
108         if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) {
109         LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
110                 return CALLLOG_ERROR_DB_FAILED;
111         }
112
113         // get count
114         int count = 0;
115         while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
116                 CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
117                 if(foreach_data == NULL)
118                         break;
119                 count++;
120                 contacts_svc_value_free(foreach_data);
121         }
122         contacts_svc_iter_remove(iter);
123
124         if(count == 0) {
125                 return CALLLOG_ERROR_NONE;
126         }
127         
128         // create id list
129         iter = NULL;
130         if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) {
131         LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
132                 return CALLLOG_ERROR_DB_FAILED;
133         }
134         int *id_list = NULL;
135         int i = 0;
136         id_list = calloc(count, sizeof(int));
137         if(id_list == NULL) {
138         LOGE("[%s] CALLLOG_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CALLLOG_ERROR_OUT_OF_MEMORY);
139                 return CALLLOG_ERROR_OUT_OF_MEMORY;
140         }
141         while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
142                 CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
143                 if(foreach_data == NULL)
144                         break;
145                 id_list[i] = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT);
146                 contacts_svc_value_free(foreach_data);
147                 i++;
148         }
149         contacts_svc_iter_remove(iter);
150         
151         // delete all call log
152         for(i=0; i<count; i++)
153         {
154                 contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, id_list[i]);
155         }
156
157         _calllog_safe_free(id_list);
158         
159         return CALLLOG_ERROR_NONE;
160 }
161
162 int calllog_update_missed_unchecked_to_checked_to_db(int calllog_db_id)
163 {
164         if(calllog_db_id < 0) {
165         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
166                 return CALLLOG_ERROR_INVALID_PARAMETER;
167     }
168         if(contacts_svc_connect() != CTS_SUCCESS) {
169         LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
170                 return CALLLOG_ERROR_DB_FAILED;
171         }
172
173         CTSvalue *phonelog = NULL;
174         if(contacts_svc_get_phonelog(calllog_db_id, &phonelog) != CTS_SUCCESS) {
175                 contacts_svc_disconnect();
176         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
177                 return CALLLOG_ERROR_INVALID_PARAMETER;
178         }
179
180         if(contacts_svc_phonelog_set_seen(calllog_db_id, ((_calllog_s*)phonelog)->calllog_type) < 0) {
181                 contacts_svc_value_free(phonelog);
182                 contacts_svc_disconnect();
183         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
184                 return CALLLOG_ERROR_INVALID_PARAMETER;
185         }
186
187         contacts_svc_value_free(phonelog);
188         contacts_svc_disconnect();
189         return CALLLOG_ERROR_NONE;
190 }
191
192 int calllog_get_time(calllog_h log, time_t* log_time)
193 {
194         if(log == NULL || log_time == NULL) {
195         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
196                 return CALLLOG_ERROR_INVALID_PARAMETER;
197     }
198         CTSvalue * CTSlog = (CTSvalue *)log;
199         *log_time=(time_t)contacts_svc_value_get_int(CTSlog, CTS_PLOG_VAL_LOG_TIME_INT);
200         return CALLLOG_ERROR_NONE;
201 }
202
203 int calllog_set_time(calllog_h log, time_t log_time)
204 {
205         if(log == NULL) {
206         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
207                 return CALLLOG_ERROR_INVALID_PARAMETER;
208     }
209         CTSvalue * CTSlog = (CTSvalue *)log;
210         if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_LOG_TIME_INT, (int)log_time) == CTS_SUCCESS) {
211                 return CALLLOG_ERROR_NONE;
212         }
213     LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
214         return CALLLOG_ERROR_INVALID_PARAMETER;
215 }
216
217
218 int calllog_get_type(calllog_h log, calllog_type_e* type)
219 {
220         if(log == NULL || type == NULL) {
221         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
222                 return CALLLOG_ERROR_INVALID_PARAMETER;
223     }
224         CTSvalue * CTSlog = (CTSvalue *)log;
225         * type=contacts_svc_value_get_int(CTSlog,CTS_PLOG_VAL_LOG_TYPE_INT);
226         return CALLLOG_ERROR_NONE;
227 }
228
229 int calllog_set_type(calllog_h log, calllog_type_e type)
230 {
231         if(log == NULL) {
232         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
233                 return CALLLOG_ERROR_INVALID_PARAMETER;
234     }
235         CTSvalue * CTSlog = (CTSvalue *)log;
236
237         if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_LOG_TYPE_INT,type) == CTS_SUCCESS) {
238                 return CALLLOG_ERROR_NONE;
239     }
240     LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
241         return CALLLOG_ERROR_INVALID_PARAMETER;
242 }
243
244
245 int calllog_get_duration(calllog_h log, int* duration_sec)
246 {
247         if(log == NULL || duration_sec == NULL) {
248         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
249                 return CALLLOG_ERROR_INVALID_PARAMETER;
250     }
251         CTSvalue * CTSlog = (CTSvalue *)log;
252         *duration_sec=contacts_svc_value_get_int(CTSlog,CTS_PLOG_VAL_DURATION_INT);
253         return CALLLOG_ERROR_NONE;
254 }
255
256 int calllog_set_duration(calllog_h log, int duration_sec)
257 {
258         if(log == NULL) {
259         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
260                 return CALLLOG_ERROR_INVALID_PARAMETER;
261     }
262         CTSvalue * CTSlog = (CTSvalue *)log;
263         if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_DURATION_INT,duration_sec) == CTS_SUCCESS) {
264                 return CALLLOG_ERROR_NONE;
265         }
266     LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
267         return CALLLOG_ERROR_INVALID_PARAMETER;
268 }
269
270 int calllog_get_number(calllog_h log, char** number)
271 {
272         if(log == NULL || number == NULL) {
273         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
274                 return CALLLOG_ERROR_INVALID_PARAMETER;
275     }
276         *number = NULL;
277         *number = _calllog_safe_strdup(((_calllog_s*)log)->number);
278         
279         return CALLLOG_ERROR_NONE;
280 }
281
282 int calllog_set_number(calllog_h log, const char* number)
283 {
284         if(log == NULL || number == NULL) {
285         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
286                 return CALLLOG_ERROR_INVALID_PARAMETER;
287     }
288         CTSvalue * CTSlog = (CTSvalue *)log;
289
290         if(contacts_svc_value_set_str(CTSlog,CTS_PLOG_VAL_NUMBER_STR,number) == CTS_SUCCESS) {
291                 return CALLLOG_ERROR_NONE;
292     }
293     LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
294         return CALLLOG_ERROR_INVALID_PARAMETER;
295 }
296
297 int calllog_foreach_calllog_from_db(calllog_foreach_cb cb, void *user_data)
298 {
299         if(cb == NULL) {
300         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
301                 return CALLLOG_ERROR_INVALID_PARAMETER;
302     }
303         CTSiter *iter = NULL;
304         int             func_ret = 0;
305         if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) {
306         LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
307                 return CALLLOG_ERROR_DB_FAILED;
308         }
309
310         while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
311                 CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
312                 if(foreach_data == NULL)
313                         break;
314
315                 calllog_query_detail_s query_data;
316                 query_data.calllog_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT);
317                 query_data.calllog_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TYPE_INT);
318                 query_data.contact_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_RELATED_ID_INT);
319                 query_data.first_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_FIRST_NAME_STR));
320                 query_data.last_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_LAST_NAME_STR));
321                 query_data.display_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_DISPLAY_NAME_STR));
322                 query_data.contact_image_path = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_IMG_PATH_STR));
323                 query_data.phone_number_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_NUM_TYPE_INT);
324                 query_data.phone_number = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_NUMBER_STR));
325                 query_data.short_message = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_SHORTMSG_STR));
326                 query_data.timestamp = (time_t)contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TIME_INT);
327                 query_data.duration_sec = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_DURATION_INT);
328                 
329                 func_ret = cb(&query_data, user_data);
330                 
331                 contacts_svc_value_free(foreach_data);
332
333                 _calllog_safe_free(query_data.first_name);
334                 _calllog_safe_free(query_data.last_name);
335                 _calllog_safe_free(query_data.display_name);
336                 _calllog_safe_free(query_data.contact_image_path);
337                 _calllog_safe_free(query_data.phone_number);
338                 _calllog_safe_free(query_data.short_message);
339         }
340         
341         contacts_svc_iter_remove(iter);
342         return CALLLOG_ERROR_NONE;
343 }
344
345 int calllog_query_calllog_by_number(calllog_foreach_cb cb, const char* number_to_find, void* user_data)
346 {
347         if(cb == NULL || number_to_find == NULL) {
348         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
349                 return CALLLOG_ERROR_INVALID_PARAMETER;
350     }
351         CTSiter *iter = NULL;
352         int             func_ret = 0;
353         if(CTS_SUCCESS != contacts_svc_get_list_with_str(CTS_LIST_PLOGS_OF_NUMBER, number_to_find, &iter)) {
354         LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
355                 return CALLLOG_ERROR_DB_FAILED;
356         }
357
358         while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
359                 CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
360                 if(foreach_data == NULL)
361                         break;
362
363                 calllog_query_detail_s query_data;
364                 query_data.calllog_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT);
365                 query_data.calllog_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TYPE_INT);
366                 query_data.contact_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_RELATED_ID_INT);
367                 query_data.first_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_FIRST_NAME_STR));
368                 query_data.last_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_LAST_NAME_STR));
369                 query_data.display_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_DISPLAY_NAME_STR));
370                 query_data.contact_image_path = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_IMG_PATH_STR));
371                 query_data.phone_number_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_NUM_TYPE_INT);
372                 query_data.phone_number = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_NUMBER_STR));
373                 query_data.short_message = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_SHORTMSG_STR));
374                 query_data.timestamp = (time_t)contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TIME_INT);
375                 query_data.duration_sec = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_DURATION_INT);
376                 
377                 func_ret = cb(&query_data, user_data);
378                 
379                 contacts_svc_value_free(foreach_data);
380
381                 _calllog_safe_free(query_data.first_name);
382                 _calllog_safe_free(query_data.last_name);
383                 _calllog_safe_free(query_data.display_name);
384                 _calllog_safe_free(query_data.contact_image_path);
385                 _calllog_safe_free(query_data.phone_number);
386                 _calllog_safe_free(query_data.short_message);
387                 if (!func_ret) {
388                         break;
389                 }
390         }
391         
392         contacts_svc_iter_remove(iter);
393         return CALLLOG_ERROR_NONE;
394 }
395
396 int calllog_add_calllog_db_changed_cb(calllog_db_changed_cb callback, void *user_data)
397 {
398         if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_PLOG_CHANGE, callback, user_data) == CTS_SUCCESS)
399                 return CALLLOG_ERROR_NONE;
400
401         LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
402         return CALLLOG_ERROR_INVALID_PARAMETER;
403 }
404
405 int calllog_remove_calllog_db_changed_cb(calllog_db_changed_cb callback)
406 {
407         if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_PLOG_CHANGE, callback) == CTS_SUCCESS)
408                 return CALLLOG_ERROR_NONE;
409
410         LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
411         return CALLLOG_ERROR_INVALID_PARAMETER;
412 }
413