summaryrefslogtreecommitdiff
path: root/src/calllog.c
blob: 943c1f1590675276980f0f343c37286d2457efb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

 
#include <tizen.h>
#include <calllog.h>
#include <contacts-svc.h>
#include <calllog_private.h>
#include <dlog.h>
#include <stdlib.h>
#include <string.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "TIZEN_N_CALLLOG"
#define LOG_MODE (1)

int calllog_connect(void)
{
	if(contacts_svc_connect() == CTS_SUCCESS) {   
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
	return CALLLOG_ERROR_DB_FAILED;
}

int calllog_disconnect(void)
{
	if(contacts_svc_disconnect() == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
	return CALLLOG_ERROR_DB_FAILED;
}

int calllog_create(calllog_h* log)
{
	if(log == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue* value = contacts_svc_value_new(CTS_VALUE_PHONELOG);
	if(value == NULL) {
        LOGE("[%s] CALLLOG_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CALLLOG_ERROR_OUT_OF_MEMORY);
		return CALLLOG_ERROR_OUT_OF_MEMORY;
	}
	*log = (calllog_h)value;
	return CALLLOG_ERROR_NONE;
}

int calllog_destroy(calllog_h log)
{
	if(log == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	if(contacts_svc_value_free((CTSvalue*)log) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}

int calllog_insert_to_db(calllog_h log)
{
	if(log == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	if(contacts_svc_insert_phonelog((CTSvalue*)log) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
	return CALLLOG_ERROR_DB_FAILED;
}

int calllog_delete_from_db(int calllog_db_id)
{
	if(calllog_db_id < 0) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	if(contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, calllog_db_id) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
	return CALLLOG_ERROR_DB_FAILED;
}

int calllog_delete_all_from_db()
{
	CTSiter *iter = NULL;
	if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) {
        LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
		return CALLLOG_ERROR_DB_FAILED;
	}

	// get count
	int count = 0;
	while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
		CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
		if(foreach_data == NULL)
			break;
		count++;
		contacts_svc_value_free(foreach_data);
	}
	contacts_svc_iter_remove(iter);

	if(count == 0) {
		return CALLLOG_ERROR_NONE;
	}
	
	// create id list
	iter = NULL;
	if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) {
        LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
		return CALLLOG_ERROR_DB_FAILED;
	}
	int *id_list = NULL;
	int i = 0;
	id_list = calloc(count, sizeof(int));
	if(id_list == NULL) {
        LOGE("[%s] CALLLOG_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CALLLOG_ERROR_OUT_OF_MEMORY);
		return CALLLOG_ERROR_OUT_OF_MEMORY;
	}
	while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
		CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
		if(foreach_data == NULL)
			break;
		id_list[i] = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT);
		contacts_svc_value_free(foreach_data);
		i++;
	}
	contacts_svc_iter_remove(iter);
	
	// delete all call log
	for(i=0; i<count; i++)
	{
		contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, id_list[i]);
	}

	_calllog_safe_free(id_list);
	
	return CALLLOG_ERROR_NONE;
}

int calllog_update_missed_unchecked_to_checked_to_db(int calllog_db_id)
{
	if(calllog_db_id < 0) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	if(contacts_svc_connect() != CTS_SUCCESS) {
        LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
		return CALLLOG_ERROR_DB_FAILED;
	}

	CTSvalue *phonelog = NULL;
	if(contacts_svc_get_phonelog(calllog_db_id, &phonelog) != CTS_SUCCESS) {
		contacts_svc_disconnect();
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
	}

	if(contacts_svc_phonelog_set_seen(calllog_db_id, ((_calllog_s*)phonelog)->calllog_type) < 0) {
		contacts_svc_value_free(phonelog);
		contacts_svc_disconnect();
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
	}

	contacts_svc_value_free(phonelog);
	contacts_svc_disconnect();
	return CALLLOG_ERROR_NONE;
}

int calllog_get_time(calllog_h log, time_t* log_time)
{
	if(log == NULL || log_time == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;
	*log_time=(time_t)contacts_svc_value_get_int(CTSlog, CTS_PLOG_VAL_LOG_TIME_INT);
	return CALLLOG_ERROR_NONE;
}

int calllog_set_time(calllog_h log, time_t log_time)
{
	if(log == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;
	if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_LOG_TIME_INT, (int)log_time) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
	}
    LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}


int calllog_get_type(calllog_h log, calllog_type_e* type)
{
	if(log == NULL || type == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;
	* type=contacts_svc_value_get_int(CTSlog,CTS_PLOG_VAL_LOG_TYPE_INT);
	return CALLLOG_ERROR_NONE;
}

int calllog_set_type(calllog_h log, calllog_type_e type)
{
	if(log == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;

	if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_LOG_TYPE_INT,type) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}


int calllog_get_duration(calllog_h log, int* duration_sec)
{
	if(log == NULL || duration_sec == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;
	*duration_sec=contacts_svc_value_get_int(CTSlog,CTS_PLOG_VAL_DURATION_INT);
	return CALLLOG_ERROR_NONE;
}

int calllog_set_duration(calllog_h log, int duration_sec)
{
	if(log == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;
	if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_DURATION_INT,duration_sec) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
	}
    LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}

int calllog_get_number(calllog_h log, char** number)
{
	if(log == NULL || number == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	*number = NULL;
	*number = _calllog_safe_strdup(((_calllog_s*)log)->number);
	
	return CALLLOG_ERROR_NONE;
}

int calllog_set_number(calllog_h log, const char* number)
{
	if(log == NULL || number == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSvalue * CTSlog = (CTSvalue *)log;

	if(contacts_svc_value_set_str(CTSlog,CTS_PLOG_VAL_NUMBER_STR,number) == CTS_SUCCESS) {
		return CALLLOG_ERROR_NONE;
    }
    LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}

int calllog_foreach_calllog_from_db(calllog_foreach_cb cb, void *user_data)
{
	if(cb == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSiter *iter = NULL;
	int		func_ret = 0;
	if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) {
        LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
		return CALLLOG_ERROR_DB_FAILED;
	}

	while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
		CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
		if(foreach_data == NULL)
			break;

		calllog_query_detail_s query_data;
		query_data.calllog_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT);
		query_data.calllog_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TYPE_INT);
		query_data.contact_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_RELATED_ID_INT);
		query_data.first_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_FIRST_NAME_STR));
		query_data.last_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_LAST_NAME_STR));
		query_data.display_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_DISPLAY_NAME_STR));
		query_data.contact_image_path = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_IMG_PATH_STR));
		query_data.phone_number_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_NUM_TYPE_INT);
		query_data.phone_number = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_NUMBER_STR));
		query_data.short_message = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_SHORTMSG_STR));
		query_data.timestamp = (time_t)contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TIME_INT);
		query_data.duration_sec = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_DURATION_INT);
		
		func_ret = cb(&query_data, user_data);
		
		contacts_svc_value_free(foreach_data);

		_calllog_safe_free(query_data.first_name);
		_calllog_safe_free(query_data.last_name);
		_calllog_safe_free(query_data.display_name);
		_calllog_safe_free(query_data.contact_image_path);
		_calllog_safe_free(query_data.phone_number);
		_calllog_safe_free(query_data.short_message);
	}
	
	contacts_svc_iter_remove(iter);
	return CALLLOG_ERROR_NONE;
}

int calllog_query_calllog_by_number(calllog_foreach_cb cb, const char* number_to_find, void* user_data)
{
	if(cb == NULL || number_to_find == NULL) {
        LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
		return CALLLOG_ERROR_INVALID_PARAMETER;
    }
	CTSiter *iter = NULL;
	int		func_ret = 0;
	if(CTS_SUCCESS != contacts_svc_get_list_with_str(CTS_LIST_PLOGS_OF_NUMBER, number_to_find, &iter)) {
        LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED);
		return CALLLOG_ERROR_DB_FAILED;
	}

	while(CTS_SUCCESS == contacts_svc_iter_next(iter)) {
		CTSvalue* foreach_data = contacts_svc_iter_get_info(iter);
		if(foreach_data == NULL)
			break;

		calllog_query_detail_s query_data;
		query_data.calllog_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT);
		query_data.calllog_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TYPE_INT);
		query_data.contact_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_RELATED_ID_INT);
		query_data.first_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_FIRST_NAME_STR));
		query_data.last_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_LAST_NAME_STR));
		query_data.display_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_DISPLAY_NAME_STR));
		query_data.contact_image_path = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_IMG_PATH_STR));
		query_data.phone_number_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_NUM_TYPE_INT);
		query_data.phone_number = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_NUMBER_STR));
		query_data.short_message = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_SHORTMSG_STR));
		query_data.timestamp = (time_t)contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TIME_INT);
		query_data.duration_sec = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_DURATION_INT);
		
		func_ret = cb(&query_data, user_data);
		
		contacts_svc_value_free(foreach_data);

		_calllog_safe_free(query_data.first_name);
		_calllog_safe_free(query_data.last_name);
		_calllog_safe_free(query_data.display_name);
		_calllog_safe_free(query_data.contact_image_path);
		_calllog_safe_free(query_data.phone_number);
		_calllog_safe_free(query_data.short_message);
		if (!func_ret) {
			break;
		}
	}
	
	contacts_svc_iter_remove(iter);
	return CALLLOG_ERROR_NONE;
}

int calllog_add_calllog_db_changed_cb(calllog_db_changed_cb callback, void *user_data)
{
	if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_PLOG_CHANGE, callback, user_data) == CTS_SUCCESS)
		return CALLLOG_ERROR_NONE;

	LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}

int calllog_remove_calllog_db_changed_cb(calllog_db_changed_cb callback)
{
	if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_PLOG_CHANGE, callback) == CTS_SUCCESS)
		return CALLLOG_ERROR_NONE;

	LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER);
	return CALLLOG_ERROR_INVALID_PARAMETER;
}