4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include <notification.h>
29 #include <notification_db.h>
30 #include <notification_noti.h>
31 #include <notification_debug.h>
32 #include <notification_internal.h>
34 static int _notification_noti_bind_query_text(sqlite3_stmt * stmt, const char *name,
40 index = sqlite3_bind_parameter_index(stmt, name);
42 NOTIFICATION_ERR("Insert : invalid column name");
43 return NOTIFICATION_ERROR_FROM_DB;
47 sqlite3_bind_text(stmt, index, NOTIFICATION_CHECK_STR(str), -1,
49 if (ret != SQLITE_OK) {
50 NOTIFICATION_ERR("Insert text : %s",
51 NOTIFICATION_CHECK_STR(str));
52 return NOTIFICATION_ERROR_FROM_DB;
55 return NOTIFICATION_ERROR_NONE;
58 static int _notification_noti_bind_query_double(sqlite3_stmt * stmt, const char *name,
64 index = sqlite3_bind_parameter_index(stmt, name);
66 NOTIFICATION_ERR("Insert : invalid column name");
67 return NOTIFICATION_ERROR_FROM_DB;
70 ret = sqlite3_bind_double(stmt, index, val);
71 if (ret != SQLITE_OK) {
72 NOTIFICATION_ERR("Insert double : %f", val);
73 return NOTIFICATION_ERROR_FROM_DB;
76 return NOTIFICATION_ERROR_NONE;
79 static int _notification_noti_check_priv_id(notification_h noti, sqlite3 * db)
81 sqlite3_stmt *stmt = NULL;
82 char query[NOTIFICATION_QUERY_MAX] = { 0, };
83 int ret = NOTIFICATION_ERROR_NONE, result = 0;
85 /* Make query to check priv_id exist */
86 snprintf(query, sizeof(query),
87 "select count(*) from noti_list where caller_pkgname = '%s' and priv_id = %d",
88 noti->caller_pkgname, noti->priv_id);
90 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
91 if (ret != SQLITE_OK) {
92 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
94 return NOTIFICATION_ERROR_FROM_DB;
97 ret = sqlite3_step(stmt);
98 if (ret == SQLITE_ROW) {
99 result = sqlite3_column_int(stmt, 0);
104 sqlite3_finalize(stmt);
106 /* If result > 0, there is priv_id in DB */
108 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
111 return NOTIFICATION_ERROR_NONE;
114 static int _notification_noti_get_priv_id(notification_h noti, sqlite3 * db)
116 sqlite3_stmt *stmt = NULL;
117 char query[NOTIFICATION_QUERY_MAX] = { 0, };
118 int ret = NOTIFICATION_ERROR_NONE, result = 0;
120 /* Make query to get max priv_id */
121 snprintf(query, sizeof(query),
122 "select max(priv_id) from noti_list where caller_pkgname = '%s'",
123 noti->caller_pkgname);
125 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
126 if (ret != SQLITE_OK) {
127 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
129 return NOTIFICATION_ERROR_FROM_DB;
132 ret = sqlite3_step(stmt);
133 if (ret == SQLITE_ROW) {
134 result = sqlite3_column_int(stmt, 0);
139 sqlite3_finalize(stmt);
142 return NOTIFICATION_ERROR_FROM_DB;
145 /* Increase result(max priv_id value) for next priv_id */
146 noti->priv_id = result + 1;
148 return NOTIFICATION_ERROR_NONE;
151 static int _notification_noti_get_internal_group_id_by_priv_id(const char *pkgname,
155 sqlite3_stmt *stmt = NULL;
156 char query[NOTIFICATION_QUERY_MAX] = { 0, };
157 int ret = NOTIFICATION_ERROR_NONE, result = 0;
159 snprintf(query, sizeof(query),
160 "select internal_group_id from noti_list where caller_pkgname = '%s' and priv_id = %d",
163 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
164 if (ret != SQLITE_OK) {
165 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
167 return NOTIFICATION_ERROR_FROM_DB;
170 ret = sqlite3_step(stmt);
171 if (ret == SQLITE_ROW) {
172 result = sqlite3_column_int(stmt, 0);
177 sqlite3_finalize(stmt);
182 static int _notification_noti_get_max_internal_group_id(notification_h noti,
185 sqlite3_stmt *stmt = NULL;
186 char query[NOTIFICATION_QUERY_MAX] = { 0, };
187 int ret = NOTIFICATION_ERROR_NONE, result = 0;
189 /* Get max internal group id */
190 snprintf(query, sizeof(query),
191 "select max(internal_group_id) from noti_list");
193 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
194 if (ret != SQLITE_OK) {
195 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
197 return NOTIFICATION_ERROR_FROM_DB;
200 ret = sqlite3_step(stmt);
201 if (ret == SQLITE_ROW) {
202 result = sqlite3_column_int(stmt, 0);
207 sqlite3_finalize(stmt);
212 static int _notification_noti_get_internal_group_id(notification_h noti,
215 sqlite3_stmt *stmt = NULL;
216 char query[NOTIFICATION_QUERY_MAX] = { 0, };
217 int ret = NOTIFICATION_ERROR_NONE, result = 0;
218 const char *ret_title = NULL;
219 char buf_key[32] = { 0, };
221 if (noti->group_id == NOTIFICATION_GROUP_ID_NONE) {
222 /* If Group ID is NONE Get max internal group ID */
223 result = _notification_noti_get_max_internal_group_id(noti, db);
225 return NOTIFICATION_ERROR_FROM_DB;
228 /* Internal Group ID is max internal group ID + 1 */
229 noti->internal_group_id = result + 1;
231 return NOTIFICATION_ERROR_NONE;
232 } else if (noti->group_id == NOTIFICATION_GROUP_ID_DEFAULT) {
233 /* If Group ID is DEFAULT, Get internal group id if it exist */
234 if (noti->b_key != NULL) {
235 snprintf(buf_key, sizeof(buf_key), "%d",
236 NOTIFICATION_TEXT_TYPE_TITLE);
238 ret_title = bundle_get_val(noti->b_key, buf_key);
241 if (ret_title == NULL && noti->b_text != NULL) {
242 snprintf(buf_key, sizeof(buf_key), "%d",
243 NOTIFICATION_TEXT_TYPE_TITLE);
245 ret_title = bundle_get_val(noti->b_text, buf_key);
248 if (ret_title == NULL) {
249 ret_title = noti->caller_pkgname;
252 snprintf(query, sizeof(query),
253 "select internal_group_id from noti_list where title_key = $title_key and group_id = %d",
254 NOTIFICATION_GROUP_ID_DEFAULT);
256 /* If Group ID is > DEFAULT, Get internal group id if it exit */
257 snprintf(query, sizeof(query),
258 "select internal_group_id from noti_list where caller_pkgname = '%s' and group_id = %d",
259 NOTIFICATION_CHECK_STR(noti->caller_pkgname),
263 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
264 if (ret != SQLITE_OK) {
265 NOTIFICATION_ERR("Select Query : %s", query);
266 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
269 sqlite3_finalize(stmt);
271 return NOTIFICATION_ERROR_FROM_DB;
275 if (ret_title != NULL) {
277 _notification_noti_bind_query_text(stmt, "$title_key",
278 NOTIFICATION_CHECK_STR
280 if (ret != NOTIFICATION_ERROR_NONE) {
281 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
283 sqlite3_finalize(stmt);
289 ret = sqlite3_step(stmt);
290 if (ret == SQLITE_ROW) {
291 result = sqlite3_column_int(stmt, 0);
293 /* If there is not internal_group_id, create new one */
294 result = _notification_noti_get_max_internal_group_id(noti, db);
298 sqlite3_finalize(stmt);
300 noti->internal_group_id = result;
302 return NOTIFICATION_ERROR_NONE;
305 static int _notification_noti_make_query(notification_h noti, char *query,
309 char *group_args = NULL;
310 char *b_image_path = NULL;
311 char *b_execute_option = NULL;
312 char *b_service_responding = NULL;
313 char *b_service_single_launch = NULL;
314 char *b_service_multi_launch = NULL;
317 char *b_format_args = NULL;
318 int flag_simmode = 0;
320 /* Decode bundle to insert DB */
322 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
324 if (noti->group_args) {
325 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
329 if (noti->b_execute_option) {
330 bundle_encode(noti->b_execute_option,
331 (bundle_raw **) & b_execute_option, NULL);
333 if (noti->b_service_responding) {
334 bundle_encode(noti->b_service_responding,
335 (bundle_raw **) & b_service_responding, NULL);
337 if (noti->b_service_single_launch) {
338 bundle_encode(noti->b_service_single_launch,
339 (bundle_raw **) & b_service_single_launch, NULL);
341 if (noti->b_service_multi_launch) {
342 bundle_encode(noti->b_service_multi_launch,
343 (bundle_raw **) & b_service_multi_launch, NULL);
347 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
350 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
352 if (noti->b_format_args) {
353 bundle_encode(noti->b_format_args,
354 (bundle_raw **) & b_format_args, NULL);
357 if (noti->b_image_path) {
358 bundle_encode(noti->b_image_path,
359 (bundle_raw **) & b_image_path, NULL);
362 /* Check only simmode property is enable */
363 if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
368 snprintf(query, query_size, "insert into noti_list ("
371 "caller_pkgname, launch_pkgname, "
373 "group_id, internal_group_id, priv_id, "
375 "b_text, b_key, b_format_args, num_format_args, "
376 "text_domain, text_dir, "
377 "time, insert_time, "
380 "b_service_responding, b_service_single_launch, b_service_multi_launch, "
381 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
382 "flags_for_property, flag_simmode, display_applist, "
383 "progress_size, progress_percentage) values ("
390 "'%s', '%s', '%s', %d, "
396 "%d, '%s', %d, '%s', %d, %d,"
398 "$progress_size, $progress_percentage)",
401 NOTIFICATION_CHECK_STR(noti->caller_pkgname),
402 NOTIFICATION_CHECK_STR(noti->launch_pkgname),
403 NOTIFICATION_CHECK_STR(b_image_path), noti->group_id,
404 noti->internal_group_id, noti->priv_id,
405 NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
406 NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
407 NOTIFICATION_CHECK_STR(noti->domain),
408 NOTIFICATION_CHECK_STR(noti->dir), (int)noti->time,
409 (int)noti->insert_time, NOTIFICATION_CHECK_STR(args),
410 NOTIFICATION_CHECK_STR(group_args),
411 NOTIFICATION_CHECK_STR(b_execute_option),
412 NOTIFICATION_CHECK_STR(b_service_responding),
413 NOTIFICATION_CHECK_STR(b_service_single_launch),
414 NOTIFICATION_CHECK_STR(b_service_multi_launch),
415 noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
416 noti->vibration_type,
417 NOTIFICATION_CHECK_STR(noti->vibration_path),
420 noti->flags_for_property, flag_simmode, noti->display_applist);
422 /* Free decoded data */
430 if (b_execute_option) {
431 free(b_execute_option);
433 if (b_service_responding) {
434 free(b_service_responding);
436 if (b_service_single_launch) {
437 free(b_service_single_launch);
439 if (b_service_multi_launch) {
440 free(b_service_multi_launch);
457 return NOTIFICATION_ERROR_NONE;
461 static int _notification_noti_make_update_query(notification_h noti, char *query,
465 char *group_args = NULL;
466 char *b_image_path = NULL;
467 char *b_execute_option = NULL;
468 char *b_service_responding = NULL;
469 char *b_service_single_launch = NULL;
470 char *b_service_multi_launch = NULL;
473 char *b_format_args = NULL;
474 int flag_simmode = 0;
476 /* Decode bundle to update DB */
478 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
480 if (noti->group_args) {
481 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
485 if (noti->b_execute_option) {
486 bundle_encode(noti->b_execute_option,
487 (bundle_raw **) & b_execute_option, NULL);
489 if (noti->b_service_responding) {
490 bundle_encode(noti->b_service_responding,
491 (bundle_raw **) & b_service_responding, NULL);
493 if (noti->b_service_single_launch) {
494 bundle_encode(noti->b_service_single_launch,
495 (bundle_raw **) & b_service_single_launch, NULL);
497 if (noti->b_service_multi_launch) {
498 bundle_encode(noti->b_service_multi_launch,
499 (bundle_raw **) & b_service_multi_launch, NULL);
503 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
506 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
508 if (noti->b_format_args) {
509 bundle_encode(noti->b_format_args,
510 (bundle_raw **) & b_format_args, NULL);
513 if (noti->b_image_path) {
514 bundle_encode(noti->b_image_path,
515 (bundle_raw **) & b_image_path, NULL);
518 /* Check only simmode property is enable */
519 if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
524 snprintf(query, query_size, "update noti_list set "
527 "launch_pkgname = '%s', "
528 "image_path = '%s', "
529 "b_text = '%s', b_key = '%s', "
530 "b_format_args = '%s', num_format_args = %d, "
531 "text_domain = '%s', text_dir = '%s', "
532 "time = %d, insert_time = %d, "
533 "args = '%s', group_args = '%s', "
534 "b_execute_option = '%s', "
535 "b_service_responding = '%s', "
536 "b_service_single_launch = '%s', "
537 "b_service_multi_launch = '%s', "
538 "sound_type = %d, sound_path = '%s', "
539 "vibration_type = %d, vibration_path = '%s', "
540 "led_operation = %d, led_argb = %d, "
541 "flags_for_property = %d, flag_simmode = %d, "
542 "display_applist = %d, "
543 "progress_size = $progress_size, progress_percentage = $progress_percentage "
544 "where priv_id = %d ",
547 NOTIFICATION_CHECK_STR(noti->launch_pkgname),
548 NOTIFICATION_CHECK_STR(b_image_path),
549 NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
550 NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
551 NOTIFICATION_CHECK_STR(noti->domain),
552 NOTIFICATION_CHECK_STR(noti->dir),
553 (int)noti->time, (int)noti->insert_time,
554 NOTIFICATION_CHECK_STR(args), NOTIFICATION_CHECK_STR(group_args),
555 NOTIFICATION_CHECK_STR(b_execute_option),
556 NOTIFICATION_CHECK_STR(b_service_responding),
557 NOTIFICATION_CHECK_STR(b_service_single_launch),
558 NOTIFICATION_CHECK_STR(b_service_multi_launch),
559 noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
560 noti->vibration_type,
561 NOTIFICATION_CHECK_STR(noti->vibration_path),
564 noti->flags_for_property, flag_simmode, noti->display_applist,
567 /* Free decoded data */
575 if (b_execute_option) {
576 free(b_execute_option);
578 if (b_service_responding) {
579 free(b_service_responding);
581 if (b_service_single_launch) {
582 free(b_service_single_launch);
584 if (b_service_multi_launch) {
585 free(b_service_multi_launch);
602 return NOTIFICATION_ERROR_NONE;
605 static void _notification_noti_populate_from_stmt(sqlite3_stmt * stmt, notification_h noti) {
608 if (stmt == NULL || noti == NULL) {
612 noti->type = sqlite3_column_int(stmt, col++);
613 noti->layout = sqlite3_column_int(stmt, col++);
614 noti->caller_pkgname = notification_db_column_text(stmt, col++);
615 noti->launch_pkgname = notification_db_column_text(stmt, col++);
616 noti->b_image_path = notification_db_column_bundle(stmt, col++);
617 noti->group_id = sqlite3_column_int(stmt, col++);
618 noti->internal_group_id = 0;
619 noti->priv_id = sqlite3_column_int(stmt, col++);
621 noti->b_text = notification_db_column_bundle(stmt, col++);
622 noti->b_key = notification_db_column_bundle(stmt, col++);
623 noti->b_format_args = notification_db_column_bundle(stmt, col++);
624 noti->num_format_args = sqlite3_column_int(stmt, col++);
626 noti->domain = notification_db_column_text(stmt, col++);
627 noti->dir = notification_db_column_text(stmt, col++);
628 noti->time = sqlite3_column_int(stmt, col++);
629 noti->insert_time = sqlite3_column_int(stmt, col++);
630 noti->args = notification_db_column_bundle(stmt, col++);
631 noti->group_args = notification_db_column_bundle(stmt, col++);
633 noti->b_execute_option = notification_db_column_bundle(stmt, col++);
634 noti->b_service_responding = notification_db_column_bundle(stmt, col++);
635 noti->b_service_single_launch =
636 notification_db_column_bundle(stmt, col++);
637 noti->b_service_multi_launch =
638 notification_db_column_bundle(stmt, col++);
640 noti->sound_type = sqlite3_column_int(stmt, col++);
641 noti->sound_path = notification_db_column_text(stmt, col++);
642 noti->vibration_type = sqlite3_column_int(stmt, col++);
643 noti->vibration_path = notification_db_column_text(stmt, col++);
644 noti->led_operation = sqlite3_column_int(stmt, col++);
645 noti->led_argb = sqlite3_column_int(stmt, col++);
647 noti->flags_for_property = sqlite3_column_int(stmt, col++);
648 noti->display_applist = sqlite3_column_int(stmt, col++);
649 noti->progress_size = sqlite3_column_double(stmt, col++);
650 noti->progress_percentage = sqlite3_column_double(stmt, col++);
652 noti->app_icon_path = NULL;
653 noti->app_name = NULL;
654 noti->temp_title = NULL;
655 noti->temp_content = NULL;
658 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
660 notification_h noti = NULL;
662 noti = malloc(sizeof(struct _notification));
667 _notification_noti_populate_from_stmt(stmt, noti);
672 int notification_noti_set_tag(const char *tag, char *value, char *buf, int buf_len)
676 len_total += (strlen(tag) * 2) + 5 + strlen(value) + 1;
678 if (buf_len <= len_total)
679 return NOTIFICATION_ERROR_INVALID_DATA;
681 snprintf(buf, buf_len, "<%s>%s</%s>", tag, value, tag);
683 return NOTIFICATION_ERROR_NONE;
686 char *notification_noti_strip_tag(const char *tagged_str)
688 if (tagged_str == NULL)
691 int len_total = strlen(tagged_str);
696 char *b_f_e = strstr(tagged_str, ">");
697 char *b_e_s = strstr(tagged_str, "</");
699 if (b_f_e == NULL || b_e_s == NULL || (b_e_s - b_f_e - 1) <= 0)
702 return strndup(b_f_e + 1, b_e_s - b_f_e - 1);
705 int notification_noti_get_tag_type(const char *tagged_str)
707 if (tagged_str == NULL)
708 return TAG_TYPE_INVALID;
710 if (strlen(tagged_str)== 0)
711 return TAG_TYPE_INVALID;
713 char *b_f_s = strstr(tagged_str, "<");
714 char *b_f_e = strstr(tagged_str, ">");
716 if (b_f_s == NULL || b_f_e == NULL || (b_f_e - b_f_s - 1) <= 0)
717 return TAG_TYPE_INVALID;
719 char *start = b_f_s + 1;
720 int len_tag = b_f_e - b_f_s - 1;
722 if (strncmp(start,TAG_TIME,len_tag) == 0) {
723 return TAG_TYPE_TIME;
726 return TAG_TYPE_INVALID;
729 static int _notification_noti_update_priv_id(sqlite3 * db, int rowid)
731 char query[128] = { 0, };
734 return NOTIFICATION_ERROR_INVALID_DATA;
737 snprintf(query, sizeof(query), "update noti_list set "
738 "priv_id = %d, internal_group_id = %d where rowid = %d",
739 rowid, rowid, rowid);
741 return notification_db_exec(db, query);
744 EXPORT_API int notification_noti_insert(notification_h noti)
747 sqlite3_stmt *stmt = NULL;
748 char query[NOTIFICATION_QUERY_MAX] = { 0, };
750 char buf_key[32] = { 0, };
751 const char *title_key = NULL;
754 db = notification_db_open(DBPATH);
756 return NOTIFICATION_ERROR_FROM_DB;
759 /* Initialize private ID */
760 noti->priv_id = NOTIFICATION_PRIV_ID_NONE;
761 noti->group_id = NOTIFICATION_GROUP_ID_NONE;
762 noti->internal_group_id = NOTIFICATION_GROUP_ID_NONE;
765 ret = _notification_noti_make_query(noti, query, sizeof(query));
766 if (ret != NOTIFICATION_ERROR_NONE) {
770 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
771 if (ret != SQLITE_OK) {
772 NOTIFICATION_ERR("Insert Query : %s", query);
773 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
775 ret = NOTIFICATION_ERROR_FROM_DB;
780 if (noti->b_key != NULL) {
781 snprintf(buf_key, sizeof(buf_key), "%d",
782 NOTIFICATION_TEXT_TYPE_TITLE);
784 title_key = bundle_get_val(noti->b_key, buf_key);
787 if (title_key == NULL && noti->b_text != NULL) {
788 snprintf(buf_key, sizeof(buf_key), "%d",
789 NOTIFICATION_TEXT_TYPE_TITLE);
791 title_key = bundle_get_val(noti->b_text, buf_key);
794 if (title_key == NULL) {
795 title_key = noti->caller_pkgname;
799 ret = _notification_noti_bind_query_text(stmt, "$title_key", title_key);
800 if (ret != NOTIFICATION_ERROR_NONE) {
801 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
804 ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
805 if (ret != NOTIFICATION_ERROR_NONE) {
806 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
808 sqlite3_finalize(stmt);
812 ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
813 if (ret != NOTIFICATION_ERROR_NONE) {
814 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
816 sqlite3_finalize(stmt);
821 ret = sqlite3_step(stmt);
822 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
823 noti->priv_id = (int)sqlite3_last_insert_rowid(db);
824 if (_notification_noti_update_priv_id(db, noti->priv_id) == 0) {
825 ret = NOTIFICATION_ERROR_NONE;
827 ret = NOTIFICATION_ERROR_FROM_DB;
830 ret = NOTIFICATION_ERROR_FROM_DB;
834 sqlite3_finalize(stmt);
839 notification_db_close(&db);
845 int notification_noti_get_by_priv_id(notification_h noti, char *pkgname, int priv_id)
848 sqlite3_stmt *stmt = NULL;
849 char query[NOTIFICATION_QUERY_MAX] = { 0, };
852 if (priv_id < 0 || noti == NULL) {
853 ret = NOTIFICATION_ERROR_INVALID_DATA;
858 db = notification_db_open(DBPATH);
860 return NOTIFICATION_ERROR_FROM_DB;
863 char *base_query = "select "
864 "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
865 "b_text, b_key, b_format_args, num_format_args, "
866 "text_domain, text_dir, time, insert_time, args, group_args, "
867 "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
868 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
869 "flags_for_property, display_applist, progress_size, progress_percentage "
872 if (pkgname != NULL) {
873 snprintf(query, sizeof(query), "%s where caller_pkgname = '%s' and priv_id = %d",
874 base_query ,pkgname, priv_id);
876 snprintf(query, sizeof(query), "%s where priv_id = %d", base_query, priv_id);
879 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
880 if (ret != SQLITE_OK) {
881 NOTIFICATION_ERR("select Query : %s", query);
882 NOTIFICATION_ERR("select DB error(%d) : %s", ret,
884 ret = NOTIFICATION_ERROR_FROM_DB;
888 ret = sqlite3_step(stmt);
889 if (ret == SQLITE_ROW) {
890 _notification_noti_populate_from_stmt(stmt, noti);
891 ret = NOTIFICATION_ERROR_NONE;
893 ret = NOTIFICATION_ERROR_FROM_DB;
897 sqlite3_finalize(stmt);
902 notification_db_close(&db);
908 EXPORT_API int notification_noti_update(notification_h noti)
911 sqlite3_stmt *stmt = NULL;
912 char query[NOTIFICATION_QUERY_MAX] = { 0, };
916 db = notification_db_open(DBPATH);
918 return NOTIFICATION_ERROR_FROM_DB;
921 /* Check private ID is exist */
922 ret = _notification_noti_check_priv_id(noti, db);
923 if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
924 ret = NOTIFICATION_ERROR_NOT_EXIST_ID;
928 /* make update query */
929 ret = _notification_noti_make_update_query(noti, query, sizeof(query));
930 if (ret != NOTIFICATION_ERROR_NONE) {
934 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
935 if (ret != SQLITE_OK) {
936 NOTIFICATION_ERR("Insert Query : %s", query);
937 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
939 ret = NOTIFICATION_ERROR_FROM_DB;
943 ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
944 if (ret != NOTIFICATION_ERROR_NONE) {
945 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
948 ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
949 if (ret != NOTIFICATION_ERROR_NONE) {
950 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
954 ret = sqlite3_step(stmt);
955 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
956 ret = NOTIFICATION_ERROR_NONE;
958 ret = NOTIFICATION_ERROR_FROM_DB;
962 sqlite3_finalize(stmt);
967 notification_db_close(&db);
973 EXPORT_API int notification_noti_delete_all(notification_type_e type, const char *pkgname, int *num_deleted, int **list_deleted_rowid)
975 int ret = NOTIFICATION_ERROR_NONE;
976 int i = 0, data_cnt = 0;
978 sqlite3_stmt *stmt = NULL;
979 char buf[128] = { 0, };
980 char query[NOTIFICATION_QUERY_MAX] = { 0, };
981 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
982 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
985 db = notification_db_open(DBPATH);
987 return NOTIFICATION_ERROR_FROM_DB;
990 if (pkgname == NULL) {
991 if (type != NOTIFICATION_TYPE_NONE) {
992 snprintf(query_where, sizeof(query_where),
993 "where type = %d ", type);
996 if (type == NOTIFICATION_TYPE_NONE) {
997 snprintf(query_where, sizeof(query_where),
998 "where caller_pkgname = '%s' ", pkgname);
1000 snprintf(query_where, sizeof(query_where),
1001 "where caller_pkgname = '%s' and type = %d ",
1006 if (num_deleted != NULL) {
1009 if (list_deleted_rowid != NULL) {
1010 *list_deleted_rowid = NULL;
1011 snprintf(query, sizeof(query),
1012 "select priv_id from noti_list %s ", query_where);
1014 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1015 if (ret != SQLITE_OK) {
1016 NOTIFICATION_ERR("Select Query : %s", query);
1017 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1018 sqlite3_errmsg(db));
1020 ret = NOTIFICATION_ERROR_FROM_DB;
1024 while(sqlite3_step(stmt) == SQLITE_ROW) {
1025 if (data_cnt % 8 == 0) {
1028 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1030 *list_deleted_rowid = tmp;
1032 NOTIFICATION_ERR("Heap: %s\n", strerror(errno));
1035 * How can I handle this?
1037 free(*list_deleted_rowid);
1038 *list_deleted_rowid = NULL;
1039 ret = NOTIFICATION_ERROR_NO_MEMORY;
1043 *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1048 sqlite3_finalize(stmt);
1053 query_where[0] = '\0';
1054 for (i = 0; i < data_cnt ; i++) {
1055 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1056 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1058 snprintf(query_base, sizeof(query_base), "delete from noti_list");
1059 snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1061 NOTIFICATION_ERR("check : %s", query);
1062 ret = notification_db_exec(db, query);
1064 free(*list_deleted_rowid);
1065 *list_deleted_rowid = NULL;
1068 if (num_deleted != NULL) {
1069 *num_deleted = data_cnt;
1072 /* Make main query */
1073 snprintf(query_base, sizeof(query_base), "delete from noti_list ");
1074 snprintf(query, sizeof(query), "%s %s", query_base, query_where);
1076 ret = notification_db_exec(db, query);
1078 if (num_deleted != NULL) {
1079 *num_deleted = sqlite3_changes(db);
1085 sqlite3_finalize(stmt);
1089 notification_db_close(&db);
1095 int notification_noti_delete_group_by_group_id(const char *pkgname,
1096 int group_id, int *num_deleted, int **list_deleted_rowid)
1098 int ret = NOTIFICATION_ERROR_NONE;
1100 int i = 0, data_cnt = 0;
1101 sqlite3_stmt *stmt = NULL;
1102 char buf[128] = { 0, };
1103 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1104 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1105 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1107 /* Check pkgname is valid */
1108 if (pkgname == NULL) {
1109 return NOTIFICATION_ERROR_INVALID_DATA;
1112 snprintf(query_where, sizeof(query_where),
1113 "where caller_pkgname = '%s' and group_id = %d", pkgname, group_id);
1116 db = notification_db_open(DBPATH);
1118 return NOTIFICATION_ERROR_FROM_DB;
1121 if (num_deleted != NULL) {
1124 if (list_deleted_rowid != NULL) {
1125 *list_deleted_rowid = NULL;
1126 snprintf(query, sizeof(query),
1127 "select priv_id from noti_list %s ", query_where);
1129 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1130 if (ret != SQLITE_OK) {
1131 NOTIFICATION_ERR("Select Query : %s", query);
1132 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1133 sqlite3_errmsg(db));
1135 ret = NOTIFICATION_ERROR_FROM_DB;
1139 while(sqlite3_step(stmt) == SQLITE_ROW) {
1140 if (data_cnt % 8 == 0) {
1142 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1144 *list_deleted_rowid = tmp;
1146 free(*list_deleted_rowid);
1147 *list_deleted_rowid = NULL;
1148 ret = NOTIFICATION_ERROR_NO_MEMORY;
1152 *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1157 sqlite3_finalize(stmt);
1162 query_where[0] = '\0';
1163 for (i = 0; i < data_cnt ; i++) {
1164 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1165 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1167 snprintf(query_base, sizeof(query_base), "delete from noti_list");
1168 snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1170 NOTIFICATION_ERR("check : %s", query);
1171 ret = notification_db_exec(db, query);
1173 free(*list_deleted_rowid);
1174 *list_deleted_rowid = NULL;
1177 if (num_deleted != NULL) {
1178 *num_deleted = data_cnt;
1182 snprintf(query, sizeof(query), "delete from noti_list %s", query_where);
1185 ret = notification_db_exec(db, query);
1190 sqlite3_finalize(stmt);
1194 notification_db_close(&db);
1200 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
1203 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1204 int internal_group_id = 0;
1207 /* Check pkgname is valid */
1208 if (pkgname == NULL) {
1209 return NOTIFICATION_ERROR_INVALID_DATA;
1213 db = notification_db_open(DBPATH);
1215 return NOTIFICATION_ERROR_FROM_DB;
1218 /* Get internal group id using priv id */
1220 _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1224 snprintf(query, sizeof(query), "delete from noti_list "
1225 "where caller_pkgname = '%s' and internal_group_id = %d",
1226 pkgname, internal_group_id);
1229 ret = notification_db_exec(db, query);
1232 notification_db_close(&db);
1237 EXPORT_API int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
1240 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1243 /* Check pkgname is valid */
1244 if (pkgname == NULL) {
1245 return NOTIFICATION_ERROR_INVALID_DATA;
1249 db = notification_db_open(DBPATH);
1251 return NOTIFICATION_ERROR_FROM_DB;
1255 snprintf(query, sizeof(query), "delete from noti_list "
1256 "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1260 ret = notification_db_exec(db, query);
1264 notification_db_close(&db);
1270 notification_error_e notification_noti_get_count(notification_type_e type,
1271 const char *pkgname,
1272 int group_id, int priv_id,
1276 sqlite3_stmt *stmt = NULL;
1277 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1278 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1279 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1280 char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
1282 int ret = 0, get_count = 0, internal_group_id = 0;
1283 int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
1285 int flag_where_more = 0;
1289 db = notification_db_open(DBPATH);
1291 return NOTIFICATION_ERROR_FROM_DB;
1294 /* Check current sim status */
1295 ret_vconf = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1298 snprintf(query_base, sizeof(query_base),
1299 "select count(*) from noti_list ");
1301 if (pkgname != NULL) {
1302 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
1303 if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1304 snprintf(query_where, sizeof(query_where),
1305 "where caller_pkgname = '%s' ",
1310 _notification_noti_get_internal_group_id_by_priv_id
1311 (pkgname, priv_id, db);
1312 snprintf(query_where, sizeof(query_where),
1313 "where caller_pkgname = '%s' and internal_group_id = %d ",
1314 pkgname, internal_group_id);
1318 if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1319 snprintf(query_where, sizeof(query_where),
1320 "where caller_pkgname = '%s' and group_id = %d ",
1325 _notification_noti_get_internal_group_id_by_priv_id
1326 (pkgname, priv_id, db);
1327 snprintf(query_where, sizeof(query_where),
1328 "where caller_pkgname = '%s' and internal_group_id = %d ",
1329 pkgname, internal_group_id);
1336 if (ret_vconf == 0 && status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1337 if (type != NOTIFICATION_TYPE_NONE) {
1338 snprintf(query_where_more, sizeof(query_where_more),
1339 "type = %d ", type);
1340 flag_where_more = 1;
1343 if (type != NOTIFICATION_TYPE_NONE) {
1344 snprintf(query_where_more, sizeof(query_where_more),
1345 "type = %d and flag_simmode = 0 ", type);
1346 flag_where_more = 1;
1348 snprintf(query_where_more, sizeof(query_where_more),
1349 "flag_simmode = 0 ");
1350 flag_where_more = 1;
1354 if (flag_where == 1) {
1355 if (flag_where_more == 1) {
1356 snprintf(query, sizeof(query), "%s %s and %s",
1357 query_base, query_where, query_where_more);
1359 snprintf(query, sizeof(query), "%s %s", query_base,
1364 if (flag_where_more == 1) {
1365 snprintf(query, sizeof(query), "%s where %s",
1366 query_base, query_where_more);
1368 snprintf(query, sizeof(query), "%s", query_base);
1372 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1373 if (ret != SQLITE_OK) {
1374 NOTIFICATION_ERR("Select Query : %s", query);
1375 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1376 sqlite3_errmsg(db));
1378 ret = NOTIFICATION_ERROR_FROM_DB;
1382 ret = sqlite3_step(stmt);
1383 if (ret == SQLITE_ROW) {
1384 get_count = sqlite3_column_int(stmt, 0);
1387 ret = NOTIFICATION_ERROR_NONE;
1391 sqlite3_finalize(stmt);
1396 notification_db_close(&db);
1404 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
1406 notification_list_h *
1410 sqlite3_stmt *stmt = NULL;
1411 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1412 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1413 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1416 notification_list_h get_list = NULL;
1417 notification_h noti = NULL;
1418 int internal_count = 0;
1422 db = notification_db_open(DBPATH);
1424 return NOTIFICATION_ERROR_FROM_DB;
1427 /* Check current sim status */
1428 ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1431 snprintf(query_base, sizeof(query_base), "select "
1432 "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1433 "b_text, b_key, b_format_args, num_format_args, "
1434 "text_domain, text_dir, time, insert_time, args, group_args, "
1435 "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1436 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
1437 "flags_for_property, display_applist, progress_size, progress_percentage "
1440 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1441 if (type != NOTIFICATION_TYPE_NONE) {
1442 snprintf(query_where, sizeof(query_where),
1443 "where type = %d ", type);
1446 if (type != NOTIFICATION_TYPE_NONE) {
1447 snprintf(query_where, sizeof(query_where),
1448 "where type = %d and flag_simmode = 0 ", type);
1450 snprintf(query_where, sizeof(query_where),
1451 "where flag_simmode = 0 ");
1455 snprintf(query, sizeof(query),
1457 "group by internal_group_id "
1458 "order by rowid desc, time desc", query_base, query_where);
1460 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1461 if (ret != SQLITE_OK) {
1462 NOTIFICATION_ERR("Select Query : %s", query);
1463 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1464 sqlite3_errmsg(db));
1466 ret = NOTIFICATION_ERROR_FROM_DB;
1470 while (sqlite3_step(stmt) == SQLITE_ROW) {
1471 /* Make notification list */
1472 noti = _notification_noti_get_item(stmt);
1476 get_list = notification_list_append(get_list, noti);
1478 if (count != -1 && internal_count >= count) {
1480 ("internal count %d >= count %d",
1481 internal_count, count);
1487 ret = NOTIFICATION_ERROR_NONE;
1491 sqlite3_finalize(stmt);
1496 notification_db_close(&db);
1499 if (get_list != NULL) {
1500 *list = notification_list_get_head(get_list);
1506 notification_error_e notification_noti_get_detail_list(const char *pkgname,
1508 int priv_id, int count,
1509 notification_list_h *list)
1512 sqlite3_stmt *stmt = NULL;
1513 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1514 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1516 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1518 notification_list_h get_list = NULL;
1519 notification_h noti = NULL;
1520 int internal_count = 0;
1521 int internal_group_id = 0;
1522 int status = 0; /* If the vconf_get_int failed, the status will be the garbage value */
1525 db = notification_db_open(DBPATH);
1527 return NOTIFICATION_ERROR_FROM_DB;
1530 /* Check current sim status */
1531 ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1534 snprintf(query_base, sizeof(query_base), "select "
1535 "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1536 "b_text, b_key, b_format_args, num_format_args, "
1537 "text_domain, text_dir, time, insert_time, args, group_args, "
1538 "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1539 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
1540 "flags_for_property, display_applist, progress_size, progress_percentage "
1543 if (priv_id == NOTIFICATION_PRIV_ID_NONE && group_id == NOTIFICATION_GROUP_ID_NONE) {
1544 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1545 snprintf(query_where, sizeof(query_where),
1546 "where caller_pkgname = '%s' ",
1547 pkgname, internal_group_id);
1549 snprintf(query_where, sizeof(query_where),
1550 "where caller_pkgname = '%s' and flag_simmode = 0 ",
1551 pkgname, internal_group_id);
1555 _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1558 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1559 snprintf(query_where, sizeof(query_where),
1560 "where caller_pkgname = '%s' and internal_group_id = %d ",
1561 pkgname, internal_group_id);
1563 snprintf(query_where, sizeof(query_where),
1564 "where caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
1565 pkgname, internal_group_id);
1569 snprintf(query, sizeof(query),
1571 "order by rowid desc, time desc", query_base, query_where);
1573 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1574 if (ret != SQLITE_OK) {
1575 NOTIFICATION_ERR("Select Query : %s", query);
1576 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1577 sqlite3_errmsg(db));
1579 ret = NOTIFICATION_ERROR_FROM_DB;
1583 while (sqlite3_step(stmt) == SQLITE_ROW) {
1584 /* Make notification list */
1585 noti = _notification_noti_get_item(stmt);
1589 get_list = notification_list_append(get_list, noti);
1591 if (count != -1 && internal_count >= count) {
1593 ("internal count %d >= count %d",
1594 internal_count, count);
1600 ret = NOTIFICATION_ERROR_NONE;
1604 sqlite3_finalize(stmt);
1609 notification_db_close(&db);
1612 if (get_list != NULL) {
1613 *list = notification_list_get_head(get_list);