summaryrefslogtreecommitdiff
path: root/src/cts-phonelog.c
blob: 42e041268aaf86d231525ef091f0d13f42f442a9 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Contacts Service
 *
 * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Youngjae Shin <yj99.shin@samsung.com>
 *
 * 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 <sys/types.h>
#include <regex.h>

#include "cts-internal.h"
#include "cts-schema.h"
#include "cts-sqlite.h"
#include "cts-contact.h"
#include "cts-utils.h"
#include "cts-types.h"
#include "cts-normalize.h"
#include "cts-phonelog.h"

#define CTS_NAME_LEN_MAX 128

static int cts_phonelog_accumulation_handle(cts_plog *plog)
{
	int ret, cnt, duration, total_cnt, total_duration, deal_cnt=0;
	cts_stmt stmt = NULL;
	char query[CTS_SQL_MAX_LEN] = {0};

	snprintf(query, sizeof(query), "SELECT * FROM %s WHERE id <= 2",
			CTS_TABLE_PHONELOG_ACC);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	while (CTS_TRUE == cts_stmt_step(stmt))
	{
		ret = cts_stmt_get_int(stmt, 0);
		if (1 == ret) {
			cnt = cts_stmt_get_int(stmt, 1);
			duration = cts_stmt_get_int(stmt, 3);
			deal_cnt++;
		}else if (2 == ret) {
			total_cnt = cts_stmt_get_int(stmt, 1);
			total_duration = cts_stmt_get_int(stmt, 3);
			deal_cnt++;
		}
	}
	cts_stmt_finalize(stmt);

	if (deal_cnt != 2) {
		ERR("Getting plog accumulation data is Failed");
		return CTS_ERR_DB_FAILED;
	}

	snprintf(query, sizeof(query), "INSERT OR REPLACE INTO %s VALUES(?, ?, NULL, ?)",
			CTS_TABLE_PHONELOG_ACC);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	cts_stmt_bind_int(stmt, 1, 1);
	cts_stmt_bind_int(stmt, 2, cnt+1);
	cts_stmt_bind_int(stmt, 3, duration + plog->extra_data1);

	ret = cts_stmt_step(stmt);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return ret;
	}
	cts_stmt_reset(stmt);

	cts_stmt_bind_int(stmt, 1, 2);
	cts_stmt_bind_int(stmt, 2, total_cnt+1);
	cts_stmt_bind_int(stmt, 3, total_duration + plog->extra_data1);

	ret = cts_stmt_step(stmt);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return ret;
	}

	cts_stmt_finalize(stmt);

	return CTS_SUCCESS;
}

//extra_data1 : duration, message_id, email_id
//extra_data2 : short message, email subject
static inline int cts_insert_phonelog(cts_plog *plog)
{
	int ret;
	cts_stmt stmt = NULL;
	char clean_num[CTS_NUMBER_MAX_LEN] = {0};
	char query[CTS_SQL_MAX_LEN] = {0};
	const char *normal_num;

	retvm_if(plog->log_type <= CTS_PLOG_TYPE_NONE
			|| CTS_PLOG_TYPE_MAX <= plog->log_type,
			CTS_ERR_ARG_INVALID, "phonelog type(%d) is invaid", plog->log_type);

	snprintf(query, sizeof(query), "INSERT INTO %s("
			"number, normal_num, related_id, log_type, log_time, data1, data2) "
			"VALUES(?, ?, ?, %d, %d, %d, ?)",
			CTS_TABLE_PHONELOGS, plog->log_type,
			plog->log_time, plog->extra_data1);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	if (plog->number) {
		cts_stmt_bind_text(stmt, 1, plog->number);
		if (plog->log_type < CTS_PLOG_TYPE_EMAIL_RECEIVED) {
			ret = cts_clean_number(plog->number, clean_num, sizeof(clean_num));
			if (0 < ret) {
				normal_num = cts_normalize_number(clean_num);
				cts_stmt_bind_text(stmt, 2, normal_num);
			}
		}
	}

	if (0 < plog->related_id)
		cts_stmt_bind_int(stmt, 3, plog->related_id);

	if (plog->extra_data2)
		cts_stmt_bind_text(stmt, 4, plog->extra_data2);

	ret = cts_stmt_step(stmt);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return ret;
	}
	cts_stmt_finalize(stmt);

	if (CTS_PLOG_TYPE_VOICE_OUTGOING == plog->log_type
			|| CTS_PLOG_TYPE_VIDEO_OUTGOING == plog->log_type)
	{
		ret = cts_phonelog_accumulation_handle(plog);
		retvm_if(CTS_SUCCESS != ret, ret, "cts_phonelog_accumulation_handle() Failed");
	}

	if (CTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN == plog->log_type ||
			CTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN == plog->log_type)
		cts_set_missed_call_noti();

	cts_set_plog_noti();
	return CTS_SUCCESS;
}

API int contacts_svc_insert_phonelog(CTSvalue* phone_log)
{
	int ret;
	cts_plog *plog = (cts_plog *)phone_log;

	retv_if(NULL == phone_log, CTS_ERR_ARG_NULL);
	retvm_if(plog->id, CTS_ERR_ARG_INVALID, "The phone_log has ID(%d)", plog->id);

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	ret = cts_insert_phonelog(plog);
	if (CTS_SUCCESS != ret)
	{
		ERR("cts_insert_phonelog() Failed(%d)", ret);
		contacts_svc_end_trans(false);
		return ret;
	}

	if (0 < plog->related_id) {
		ret = cts_increase_outgoing_count(plog->related_id);
		warn_if(CTS_SUCCESS != ret, "cts_increase_outgoing_count() Failed(%d)", ret);
	}

	ret = contacts_svc_end_trans(true);
	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_delete_phonelog(cts_del_plog_op op_code, ...)
{
	int id, ret;
	char *number;
	char query[CTS_SQL_MAX_LEN];
	va_list args;

	switch (op_code)
	{
	case CTS_PLOG_DEL_BY_ID:
		va_start(args, op_code);
		id = va_arg(args, int);
		va_end(args);
		snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d",
				CTS_TABLE_PHONELOGS, id);
		break;
	case CTS_PLOG_DEL_BY_NUMBER:
		va_start(args, op_code);
		number = va_arg(args, char *);
		va_end(args);
		retv_if(NULL == number, CTS_ERR_ARG_NULL);
		snprintf(query, sizeof(query), "DELETE FROM %s WHERE number = '%s'",
				CTS_TABLE_PHONELOGS, number);
		break;
	case CTS_PLOG_DEL_BY_MSGID:
		va_start(args, op_code);
		id = va_arg(args, int);
		va_end(args);
		snprintf(query, sizeof(query), "DELETE FROM %s "
				"WHERE data1 = %d AND %d <= log_type AND log_type <= %d",
				CTS_TABLE_PHONELOGS,
				id, CTS_PLOG_TYPE_MMS_INCOMMING, CTS_PLOG_TYPE_MMS_BLOCKED);
		break;
	case CTS_PLOG_DEL_NO_NUMBER:
		snprintf(query, sizeof(query), "DELETE FROM %s WHERE number ISNULL",
				CTS_TABLE_PHONELOGS);
		break;
	default:
		ERR("Invalid op_code. Your op_code(%d) is not supported.", op_code);
		return CTS_ERR_ARG_INVALID;
	}

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	ret = cts_query_exec(query);
	if (CTS_SUCCESS != ret) {
		ERR("cts_query_exec() Failed(%d)", ret);
		contacts_svc_end_trans(false);
		return ret;
	}
	cts_set_plog_noti();
	if (CTS_PLOG_DEL_BY_MSGID != op_code)
		cts_set_missed_call_noti();

	ret = contacts_svc_end_trans(true);
	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

API int contacts_svc_get_phonelog(int plog_id, CTSvalue **phonelog)
{
	int ret;
	cts_stmt stmt;
	cts_plog *plog;
	char query[CTS_SQL_MAX_LEN] = {0};

	snprintf(query, sizeof(query),
			"SELECT id, number, related_id, log_type, log_time, data1, data2 "
			"FROM %s WHERE id = %d",
			CTS_TABLE_PHONELOGS, plog_id);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	ret = cts_stmt_step(stmt);
	if (CTS_TRUE != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return CTS_ERR_DB_RECORD_NOT_FOUND;
	}

	plog = (cts_plog *)contacts_svc_value_new(CTS_VALUE_PHONELOG);
	if (plog) {
		ret = CTS_SUCCESS;
		plog->v_type = CTS_VALUE_RDONLY_PLOG;
		plog->id = cts_stmt_get_int(stmt, 0);
		plog->number = SAFE_STRDUP(cts_stmt_get_text(stmt, 1));
		plog->related_id = cts_stmt_get_int(stmt, 2);
		plog->log_type = cts_stmt_get_int(stmt, 3);
		plog->log_time = cts_stmt_get_int(stmt, 4);
		plog->extra_data1 = cts_stmt_get_int(stmt, 5);
		plog->extra_data2 = SAFE_STRDUP(cts_stmt_get_text(stmt, 6));

		*phonelog = (CTSvalue*)plog;

		cts_stmt_finalize(stmt);
		return CTS_SUCCESS;
	}
	else {
		ERR("contacts_svc_value_new() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return CTS_ERR_OUT_OF_MEMORY;
	}
}


API int contacts_svc_phonelog_set_seen(int index, int type)
{
	int ret;
	char query[CTS_SQL_MAX_LEN] = {0};

	retvm_if(CTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN != type &&
			CTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN != type &&
			CTS_PLOG_TYPE_NONE != type,
			CTS_ERR_ARG_INVALID,
			"The type is invalid. It must be CTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN"
			" or CTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN");

	if (0 == index) {
		if (CTS_PLOG_TYPE_NONE == type)
			snprintf(query, sizeof(query), "UPDATE %s SET log_type = log_type + 1 WHERE log_type = %d OR log_type = %d",
					CTS_TABLE_PHONELOGS, CTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN,
					CTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN);
		else
			snprintf(query, sizeof(query), "UPDATE %s SET log_type = %d WHERE log_type = %d",
					CTS_TABLE_PHONELOGS, type+1, type);
	}
	else {
		snprintf(query, sizeof(query), "UPDATE %s SET log_type = %d WHERE id = %d",
				CTS_TABLE_PHONELOGS, type+1, index);
	}

	ret = contacts_svc_begin_trans();
	retvm_if(ret, ret, "contacts_svc_begin_trans() Failed(%d)", ret);

	ret = cts_query_exec(query);
	if (CTS_SUCCESS != ret) {
		ERR("cts_query_exec() Failed(%d)", ret);
		contacts_svc_end_trans(false);
		return ret;
	}

	if (cts_db_change()) {
		cts_set_plog_noti();
		cts_set_missed_call_noti();
	}

	ret = contacts_svc_end_trans(true);
	if (ret < CTS_SUCCESS)
		return ret;
	else
		return CTS_SUCCESS;
}

/**
 * This is the signature of a callback function added with contacts_svc_phonelog_get_all_number(),
 * \n This function is invoked in the above functions.
 * \n If this function doesn't return #CTS_SUCCESS, foreach function is terminated.
 *
 * @param[in] number number.
 * @param[in] user_data The data which is set by contacts_svc_phonelog_get_all_number(),
 * @return #CTS_SUCCESS on success, other value on error
 */
typedef int (*cts_plog_foreach_fn)(const char *number, void *user_data);

/**
 * This function calls #cts_plog_foreach_fn for each number of all number list.
 * The all number list doesn't have duplicated numbers.
 *
 * @param[in] cb callback function pointer(#cts_plog_foreach_fn)
 * @param[in] user_data data which is passed to callback function
 * @return #CTS_SUCCESS on success, Negative value(#cts_error) on error
 */
API int contacts_svc_phonelog_get_all_number(cts_plog_foreach_fn cb,
		void *user_data)
{
	cts_stmt stmt = NULL;
	char query[CTS_SQL_MAX_LEN] = {0};

	retv_if(NULL == cb, CTS_ERR_ARG_NULL);

	snprintf(query, sizeof(query),
			"SELECT DISTINCT number FROM %s", CTS_TABLE_PHONELOGS);

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, CTS_ERR_DB_FAILED, "cts_query_prepare() Failed");

	while (CTS_TRUE == cts_stmt_step(stmt)) {
		if (cb(cts_stmt_get_text(stmt, 0), user_data))
			break;
	}
	cts_stmt_finalize(stmt);

	return CTS_SUCCESS;
}

API char* contacts_svc_phonelog_get_last_number(cts_plog_get_last_op op)
{
	int ret;
	cts_stmt stmt;
	char *number;
	char query[CTS_SQL_MAX_LEN] = {0};

	switch (op) {
	case CTS_PLOG_LAST_ALL:
		snprintf(query, sizeof(query),
				"SELECT id, number FROM %s "
				"WHERE id = (SELECT MAX(id) FROM %s WHERE log_type = %d OR log_type = %d)",
				CTS_TABLE_PHONELOGS, CTS_TABLE_PHONELOGS,
				CTS_PLOG_TYPE_VOICE_OUTGOING, CTS_PLOG_TYPE_VIDEO_OUTGOING);
		break;
	case CTS_PLOG_LAST_CALL_ONLY:
		snprintf(query, sizeof(query),
				"SELECT id, number FROM %s "
				"WHERE id = (SELECT MAX(id) FROM %s WHERE log_type = %d)",
				CTS_TABLE_PHONELOGS, CTS_TABLE_PHONELOGS, CTS_PLOG_TYPE_VOICE_OUTGOING);
		break;
	case CTS_PLOG_LAST_VIDEO_CALL_ONLY:
		snprintf(query, sizeof(query),
				"SELECT id, number FROM %s "
				"WHERE id = (SELECT MAX(id) FROM %s WHERE log_type = %d)",
				CTS_TABLE_PHONELOGS, CTS_TABLE_PHONELOGS, CTS_PLOG_TYPE_VIDEO_OUTGOING);
		break;
	default:
		ERR("Invalid op(%d)", op);
		return NULL;
	}

	stmt = cts_query_prepare(query);
	retvm_if(NULL == stmt, NULL, "cts_query_prepare() Failed");

	ret = cts_stmt_step(stmt);
	if (CTS_TRUE != ret)
	{
		ERR("cts_stmt_step() Failed(%d)", ret);
		cts_stmt_finalize(stmt);
		return NULL;
	}
	number = SAFE_STRDUP(cts_stmt_get_text(stmt, 1));

	cts_stmt_finalize(stmt);

	return number;
}