4 * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "ctsvc_internal.h"
23 #include "ctsvc_db_schema.h"
24 #include "ctsvc_db_sqlite.h"
25 #include "ctsvc_normalize.h"
26 #include "ctsvc_number_utils.h"
27 #include "ctsvc_db_utils.h"
28 #include "ctsvc_list.h"
29 #include "ctsvc_record.h"
30 #include "ctsvc_db_query.h"
31 #include "ctsvc_db_init.h"
32 #include "ctsvc_notification.h"
33 #include "ctsvc_server_setting.h"
34 #include "ctsvc_db_access_control.h"
35 #include "ctsvc_localize_utils.h"
36 #include "ctsvc_server_phonelog.h"
38 #ifdef _CONTACTS_IPC_SERVER
39 #include "ctsvc_server_change_subject.h"
40 #include "ctsvc_server_sim.h"
41 #endif /* _CONTACTS_IPC_SERVER */
44 static int __ctsvc_db_phonelog_value_set(cts_stmt stmt, contacts_record_h *record)
49 ctsvc_phonelog_s *phonelog;
51 ret = contacts_record_create(_contacts_phone_log._uri, record);
52 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
53 phonelog = (ctsvc_phonelog_s*)*record;
56 phonelog->id = ctsvc_stmt_get_int(stmt, i++);
57 temp = ctsvc_stmt_get_text(stmt, i++);
58 phonelog->address = SAFE_STRDUP(temp);
59 phonelog->person_id = ctsvc_stmt_get_int(stmt, i++);
60 phonelog->log_type = ctsvc_stmt_get_int(stmt, i++);
61 phonelog->log_time = ctsvc_stmt_get_int(stmt, i++);
62 phonelog->extra_data1 = ctsvc_stmt_get_int(stmt, i++);
63 temp = ctsvc_stmt_get_text(stmt, i++);
64 phonelog->extra_data2 = SAFE_STRDUP(temp);
65 #ifdef _CONTACTS_IPC_SERVER
66 phonelog->sim_slot_no = ctsvc_server_sim_get_sim_slot_no_by_info_id(ctsvc_stmt_get_int(stmt, i++));
67 #endif /* _CONTACTS_IPC_SERVER */
68 return CONTACTS_ERROR_NONE;
71 static int __ctsvc_db_phonelog_get_record(int id, contacts_record_h *out_record)
75 char query[CTS_SQL_MAX_LEN] = {0};
76 contacts_record_h record;
78 RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
81 snprintf(query, sizeof(query),
82 "SELECT id, number, person_id, log_type, log_time, data1, data2, sim_id "
83 "FROM "CTS_TABLE_PHONELOGS" WHERE id = %d", id);
85 ret = ctsvc_query_prepare(query, &stmt);
86 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
88 ret = ctsvc_stmt_step(stmt);
89 if (1 /*CTS_TRUE*/ != ret) {
90 ERR("ctsvc_stmt_step() Fail(%d)", ret);
91 ctsvc_stmt_finalize(stmt);
92 if (CONTACTS_ERROR_NONE == ret)
93 return CONTACTS_ERROR_NO_DATA;
98 ret = __ctsvc_db_phonelog_value_set(stmt, &record);
100 ctsvc_stmt_finalize(stmt);
101 if (CONTACTS_ERROR_NONE != ret) {
102 ERR("__ctsvc_db_phonelog_value_set(ALL) Fail(%d)", ret);
106 *out_record = record;
108 return CONTACTS_ERROR_NONE;
111 static int __ctsvc_db_phonelog_update_record(contacts_record_h record)
114 char query[CTS_SQL_MIN_LEN] = {0};
115 ctsvc_phonelog_s *phonelog = (ctsvc_phonelog_s*)record;
116 int ret = CONTACTS_ERROR_NONE;
118 GSList *bind_text = NULL;
119 GSList *cursor = NULL;
121 RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
122 RETVM_IF(phonelog->id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
123 "The phone_log has ID(%d)", phonelog->id);
124 RETVM_IF(phonelog->log_type != CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN &&
125 phonelog->log_type != CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN, CONTACTS_ERROR_INVALID_PARAMETER,
126 "the type is can not updated(%d)", phonelog->log_type);
127 RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (phonelog->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
129 ret = ctsvc_begin_trans();
130 RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
132 snprintf(query, sizeof(query),
133 "SELECT id FROM "CTS_TABLE_PHONELOGS" WHERE id = %d", phonelog->id);
134 ret = ctsvc_query_get_first_int_result(query, &phonelog_id);
135 if (ret != CONTACTS_ERROR_NONE) {
136 ERR("ctsvc_query_get_first_int_result() Fail(%d)", ret);
137 ctsvc_end_trans(false);
142 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
143 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_PHONELOGS, phonelog->id))) break;
145 if (ctsvc_db_change()) {
146 ctsvc_set_phonelog_noti();
148 #ifdef _CONTACTS_IPC_SERVER
149 ctsvc_change_subject_add_changed_phone_log_id(CONTACTS_CHANGE_UPDATED, phonelog->id);
154 CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
158 for (cursor = bind_text; cursor; cursor = cursor->next) {
162 g_slist_free(bind_text);
165 ret = ctsvc_end_trans(true);
166 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
168 return CONTACTS_ERROR_NONE;
171 static int __ctsvc_db_phonelog_delete_record(int id)
175 char query[CTS_SQL_MAX_LEN] = {0};
177 ret = ctsvc_begin_trans();
178 RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
180 snprintf(query, sizeof(query),
181 "SELECT id FROM "CTS_TABLE_PHONELOGS" WHERE id = %d", id);
182 ret = ctsvc_query_get_first_int_result(query, &phonelog_id);
183 if (ret != CONTACTS_ERROR_NONE) {
184 ERR("ctsvc_query_get_first_int_result() Fail(%d)", ret);
185 ctsvc_end_trans(false);
189 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d",
190 CTS_TABLE_PHONELOGS, id);
192 ret = ctsvc_query_exec(query);
193 if (CONTACTS_ERROR_NONE != ret) {
194 ERR("ctsvc_query_exec() Fail(%d)", ret);
195 ctsvc_end_trans(false);
199 ctsvc_set_phonelog_noti();
201 ret = ctsvc_end_trans(true);
202 if (ret < CONTACTS_ERROR_NONE) {
203 ERR("ctsvc_end_trans() Fail(%d)", ret);
206 return CONTACTS_ERROR_NONE;
210 static int __ctsvc_db_phonelog_get_all_records(int offset, int limit,
211 contacts_list_h *out_list)
216 char query[CTS_SQL_MAX_LEN] = {0};
217 contacts_list_h list;
219 len = snprintf(query, sizeof(query),
220 "SELECT id, number, person_id, log_type, log_time, data1, data2, sim_id "
221 "FROM "CTS_TABLE_PHONELOGS);
224 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
226 len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
229 ret = ctsvc_query_prepare(query, &stmt);
230 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
232 contacts_list_create(&list);
233 while ((ret = ctsvc_stmt_step(stmt))) {
234 contacts_record_h record;
236 ERR("ctsvc_stmt_step() Fail(%d)", ret);
237 ctsvc_stmt_finalize(stmt);
238 contacts_list_destroy(list, true);
241 __ctsvc_db_phonelog_value_set(stmt, &record);
243 ctsvc_list_prepend(list, record);
245 ctsvc_stmt_finalize(stmt);
246 ctsvc_list_reverse(list);
248 *out_list = (contacts_list_h)list;
249 return CONTACTS_ERROR_NONE;
252 static int __ctsvc_db_phonelog_get_records_with_query(contacts_query_h query, int offset,
253 int limit, contacts_list_h *out_list)
258 ctsvc_query_s *s_query;
260 contacts_list_h list;
261 ctsvc_phonelog_s *phonelog;
263 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
264 s_query = (ctsvc_query_s*)query;
266 ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
267 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
269 contacts_list_create(&list);
270 while ((ret = ctsvc_stmt_step(stmt))) {
271 contacts_record_h record;
273 ERR("ctsvc_stmt_step() Fail(%d)", ret);
274 ctsvc_stmt_finalize(stmt);
275 contacts_list_destroy(list, true);
279 contacts_record_create(_contacts_phone_log._uri, &record);
280 phonelog = (ctsvc_phonelog_s*)record;
281 if (0 == s_query->projection_count) {
282 field_count = s_query->property_count;
284 field_count = s_query->projection_count;
286 int err = ctsvc_record_set_projection_flags(record, s_query->projection,
287 s_query->projection_count, s_query->property_count);
288 if (CONTACTS_ERROR_NONE != err)
289 ASSERT_NOT_REACHED("To set projection is Fail.\n");
292 for (i = 0; i < field_count; i++) {
295 if (0 == s_query->projection_count)
296 property_id = s_query->properties[i].property_id;
298 property_id = s_query->projection[i];
300 switch (property_id) {
301 case CTSVC_PROPERTY_PHONELOG_ID:
302 phonelog->id = ctsvc_stmt_get_int(stmt, i);
304 case CTSVC_PROPERTY_PHONELOG_PERSON_ID:
305 phonelog->person_id = ctsvc_stmt_get_int(stmt, i);
307 case CTSVC_PROPERTY_PHONELOG_ADDRESS:
308 temp = ctsvc_stmt_get_text(stmt, i);
309 free(phonelog->address);
310 phonelog->address = SAFE_STRDUP(temp);
312 case CTSVC_PROPERTY_PHONELOG_LOG_TIME:
313 phonelog->log_time = ctsvc_stmt_get_int(stmt, i);
315 case CTSVC_PROPERTY_PHONELOG_LOG_TYPE:
316 phonelog->log_type = ctsvc_stmt_get_int(stmt, i);
318 case CTSVC_PROPERTY_PHONELOG_EXTRA_DATA1:
319 phonelog->extra_data1 = ctsvc_stmt_get_int(stmt, i);
321 case CTSVC_PROPERTY_PHONELOG_EXTRA_DATA2:
322 temp = ctsvc_stmt_get_text(stmt, i);
323 free(phonelog->extra_data2);
324 phonelog->extra_data2 = SAFE_STRDUP(temp);
326 case CTSVC_PROPERTY_PHONELOG_SIM_SLOT_NO:
327 phonelog->sim_slot_no = ctsvc_server_sim_get_sim_slot_no_by_info_id(ctsvc_stmt_get_int(stmt, i));
333 ctsvc_list_prepend(list, record);
336 ctsvc_stmt_finalize(stmt);
337 ctsvc_list_reverse(list);
341 return CONTACTS_ERROR_NONE;
344 static int __ctsvc_db_phonelog_increase_used_count(ctsvc_phonelog_s *phonelog)
348 int type = CONTACTS_USAGE_STAT_TYPE_NONE;
349 char query[CTS_SQL_MIN_LEN] = {0};
351 switch (phonelog->log_type) {
352 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
353 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
354 type = CONTACTS_USAGE_STAT_TYPE_OUTGOING_CALL;
356 case CONTACTS_PLOG_TYPE_MMS_OUTGOING:
357 case CONTACTS_PLOG_TYPE_SMS_OUTGOING:
358 type = CONTACTS_USAGE_STAT_TYPE_OUTGOING_MSG;
360 case CONTACTS_PLOG_TYPE_EMAIL_SENT:
361 type = CONTACTS_USAGE_STAT_TYPE_OUTGOING_EMAIL;
363 case CONTACTS_PLOG_TYPE_VOICE_INCOMING:
364 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING:
365 type = CONTACTS_USAGE_STAT_TYPE_INCOMING_CALL;
367 case CONTACTS_PLOG_TYPE_MMS_INCOMING:
368 case CONTACTS_PLOG_TYPE_SMS_INCOMING:
369 type = CONTACTS_USAGE_STAT_TYPE_INCOMING_MSG;
371 case CONTACTS_PLOG_TYPE_EMAIL_RECEIVED:
372 type = CONTACTS_USAGE_STAT_TYPE_INCOMING_EMAIL;
374 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN:
375 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN:
376 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN:
377 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN:
378 type = CONTACTS_USAGE_STAT_TYPE_MISSED_CALL;
380 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
381 case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
382 type = CONTACTS_USAGE_STAT_TYPE_REJECTED_CALL;
384 case CONTACTS_PLOG_TYPE_VOICE_BLOCKED:
385 case CONTACTS_PLOG_TYPE_VIDEO_BLOCKED:
386 type = CONTACTS_USAGE_STAT_TYPE_BLOCKED_CALL;
389 case CONTACTS_PLOG_TYPE_SMS_BLOCKED:
390 case CONTACTS_PLOG_TYPE_MMS_BLOCKED:
391 type = CONTACTS_USAGE_STAT_TYPE_BLOCKED_MSG;
395 ERR("unknown log type (%d)", phonelog->log_type);
396 return CONTACTS_ERROR_INVALID_PARAMETER;
399 snprintf(query, sizeof(query),
400 "SELECT person_id FROM %s WHERE person_id = %d and usage_type = %d ",
401 CTS_TABLE_CONTACT_STAT, phonelog->person_id, type);
403 ret = ctsvc_query_get_first_int_result(query, &id);
404 if (CONTACTS_ERROR_NO_DATA == ret) {
405 snprintf(query, sizeof(query),
406 "INSERT INTO %s(person_id, usage_type, times_used) VALUES(%d, %d, 1)",
407 CTS_TABLE_CONTACT_STAT, phonelog->person_id, type);
409 snprintf(query, sizeof(query),
410 "UPDATE %s SET times_used = times_used + 1 WHERE person_id = %d and usage_type = %d",
411 CTS_TABLE_CONTACT_STAT, phonelog->person_id, type);
414 ret = ctsvc_query_exec(query);
415 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
417 return CONTACTS_ERROR_NONE;
420 static int __ctsvc_db_phonelog_insert(ctsvc_phonelog_s *phonelog, int *id)
423 cts_stmt stmt = NULL;
424 char query[CTS_SQL_MAX_LEN] = {0};
426 RETVM_IF((phonelog->log_type < CONTACTS_PLOG_TYPE_NONE
427 || CONTACTS_PLOG_TYPE_EMAIL_SENT < phonelog->log_type)
428 , CONTACTS_ERROR_INVALID_PARAMETER, "phonelog type(%d) is invaid", phonelog->log_type);
430 snprintf(query, sizeof(query), "INSERT INTO "CTS_TABLE_PHONELOGS"("
431 "number, normal_num, minmatch, clean_num, person_id, log_type,log_time, data1, data2, sim_id) "
432 "VALUES(?, ?, ?, ?, ?, %d, %d, %d, ?, ?)",
433 phonelog->log_type, phonelog->log_time, phonelog->extra_data1);
435 ret = ctsvc_query_prepare(query, &stmt);
436 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
438 if (phonelog->address) {
439 ctsvc_stmt_bind_text(stmt, 1, phonelog->address);
440 if (phonelog->log_type < CONTACTS_PLOG_TYPE_EMAIL_RECEIVED) {
441 char clean_num[strlen(phonelog->address) + 1];
442 ret = ctsvc_clean_number(phonelog->address, clean_num, sizeof(clean_num), true);
444 char normal_num[sizeof(clean_num) + 20];
445 ctsvc_stmt_bind_copy_text(stmt, 4, clean_num, strlen(clean_num));
446 ret = ctsvc_normalize_number(clean_num, normal_num, sizeof(normal_num), true);
448 char minmatch[sizeof(normal_num) + 1];
449 ctsvc_stmt_bind_copy_text(stmt, 2, normal_num, strlen(normal_num));
450 ret = ctsvc_get_minmatch_number(normal_num, minmatch, sizeof(minmatch), ctsvc_get_phonenumber_min_match_digit());
451 ctsvc_stmt_bind_copy_text(stmt, 3, minmatch, strlen(minmatch));
457 if (0 < phonelog->person_id)
458 ctsvc_stmt_bind_int(stmt, 5, phonelog->person_id);
460 if (phonelog->extra_data2)
461 ctsvc_stmt_bind_text(stmt, 6, phonelog->extra_data2);
463 if (0 <= phonelog->sim_slot_no) {
465 sim_info_id = ctsvc_server_sim_get_info_id_by_sim_slot_no(phonelog->sim_slot_no);
467 ctsvc_stmt_bind_int(stmt, 7, sim_info_id);
469 ctsvc_stmt_bind_int(stmt, 7, -1);
472 ret = ctsvc_stmt_step(stmt);
473 if (CONTACTS_ERROR_NONE != ret) {
474 ERR("ctsvc_stmt_step() Fail(%d)", ret);
475 ctsvc_stmt_finalize(stmt);
479 *id = ctsvc_db_get_last_insert_id();
480 ctsvc_stmt_finalize(stmt);
482 /* update phonelog */
483 ctsvc_db_phone_log_update_person_id(phonelog->address, phonelog->person_id, -1, false, &phonelog->person_id);
485 ctsvc_set_phonelog_noti();
486 return CONTACTS_ERROR_NONE;
489 static int __ctsvc_db_phonelog_insert_record(contacts_record_h record, int *id)
492 ctsvc_phonelog_s *phonelog = (ctsvc_phonelog_s*)record;
494 RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
495 RETVM_IF(phonelog->id, CONTACTS_ERROR_INVALID_PARAMETER,
496 "The phone_log has ID(%d)", phonelog->id);
498 ret = ctsvc_begin_trans();
499 RETVM_IF(ret, ret, "ctsvc_begin_trans() Fail(%d)", ret);
501 ret = __ctsvc_db_phonelog_insert(phonelog, id);
502 if (CONTACTS_ERROR_NONE != ret) {
503 ERR("__ctsvc_db_phonelog_insert() Fail(%d)", ret);
504 ctsvc_end_trans(false);
508 if (0 < phonelog->person_id) {
509 ret = __ctsvc_db_phonelog_increase_used_count(phonelog);
510 WARN_IF(CONTACTS_ERROR_NONE != ret, "__ctsvc_db_phonelog_increase_used_count() Fail(%d)", ret);
513 #ifdef _CONTACTS_IPC_SERVER
514 /* add id for subscribe */
515 ctsvc_change_subject_add_changed_phone_log_id(CONTACTS_CHANGE_INSERTED, *id);
518 ret = ctsvc_end_trans(true);
519 if (ret < CONTACTS_ERROR_NONE) {
520 ERR("ctsvc_end_trans() Fail(%d)", ret);
523 return CONTACTS_ERROR_NONE;
526 ctsvc_db_plugin_info_s ctsvc_db_plugin_phonelog = {
527 .is_query_only = false,
528 .insert_record = __ctsvc_db_phonelog_insert_record,
529 .get_record = __ctsvc_db_phonelog_get_record,
530 .update_record = __ctsvc_db_phonelog_update_record,
531 .delete_record = __ctsvc_db_phonelog_delete_record,
532 .get_all_records = __ctsvc_db_phonelog_get_all_records,
533 .get_records_with_query = __ctsvc_db_phonelog_get_records_with_query,
534 .insert_records = NULL,
535 .update_records = NULL,
536 .delete_records = NULL,
538 .get_count_with_query = NULL,
539 .replace_record = NULL,
540 .replace_records = NULL,