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, led_on_ms, led_off_ms, "
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, %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),
422 noti->flags_for_property, flag_simmode, noti->display_applist);
424 /* Free decoded data */
432 if (b_execute_option) {
433 free(b_execute_option);
435 if (b_service_responding) {
436 free(b_service_responding);
438 if (b_service_single_launch) {
439 free(b_service_single_launch);
441 if (b_service_multi_launch) {
442 free(b_service_multi_launch);
459 return NOTIFICATION_ERROR_NONE;
463 static int _notification_noti_make_update_query(notification_h noti, char *query,
467 char *group_args = NULL;
468 char *b_image_path = NULL;
469 char *b_execute_option = NULL;
470 char *b_service_responding = NULL;
471 char *b_service_single_launch = NULL;
472 char *b_service_multi_launch = NULL;
475 char *b_format_args = NULL;
476 int flag_simmode = 0;
478 /* Decode bundle to update DB */
480 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
482 if (noti->group_args) {
483 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
487 if (noti->b_execute_option) {
488 bundle_encode(noti->b_execute_option,
489 (bundle_raw **) & b_execute_option, NULL);
491 if (noti->b_service_responding) {
492 bundle_encode(noti->b_service_responding,
493 (bundle_raw **) & b_service_responding, NULL);
495 if (noti->b_service_single_launch) {
496 bundle_encode(noti->b_service_single_launch,
497 (bundle_raw **) & b_service_single_launch, NULL);
499 if (noti->b_service_multi_launch) {
500 bundle_encode(noti->b_service_multi_launch,
501 (bundle_raw **) & b_service_multi_launch, NULL);
505 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
508 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
510 if (noti->b_format_args) {
511 bundle_encode(noti->b_format_args,
512 (bundle_raw **) & b_format_args, NULL);
515 if (noti->b_image_path) {
516 bundle_encode(noti->b_image_path,
517 (bundle_raw **) & b_image_path, NULL);
520 /* Check only simmode property is enable */
521 if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
526 snprintf(query, query_size, "update noti_list set "
529 "launch_pkgname = '%s', "
530 "image_path = '%s', "
531 "b_text = '%s', b_key = '%s', "
532 "b_format_args = '%s', num_format_args = %d, "
533 "text_domain = '%s', text_dir = '%s', "
534 "time = %d, insert_time = %d, "
535 "args = '%s', group_args = '%s', "
536 "b_execute_option = '%s', "
537 "b_service_responding = '%s', "
538 "b_service_single_launch = '%s', "
539 "b_service_multi_launch = '%s', "
540 "sound_type = %d, sound_path = '%s', "
541 "vibration_type = %d, vibration_path = '%s', "
542 "led_operation = %d, led_argb = %d, "
543 "led_on_ms = %d, led_off_ms = %d, "
544 "flags_for_property = %d, flag_simmode = %d, "
545 "display_applist = %d, "
546 "progress_size = $progress_size, progress_percentage = $progress_percentage "
547 "where priv_id = %d ",
550 NOTIFICATION_CHECK_STR(noti->launch_pkgname),
551 NOTIFICATION_CHECK_STR(b_image_path),
552 NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
553 NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
554 NOTIFICATION_CHECK_STR(noti->domain),
555 NOTIFICATION_CHECK_STR(noti->dir),
556 (int)noti->time, (int)noti->insert_time,
557 NOTIFICATION_CHECK_STR(args), NOTIFICATION_CHECK_STR(group_args),
558 NOTIFICATION_CHECK_STR(b_execute_option),
559 NOTIFICATION_CHECK_STR(b_service_responding),
560 NOTIFICATION_CHECK_STR(b_service_single_launch),
561 NOTIFICATION_CHECK_STR(b_service_multi_launch),
562 noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
563 noti->vibration_type,
564 NOTIFICATION_CHECK_STR(noti->vibration_path),
569 noti->flags_for_property, flag_simmode, noti->display_applist,
572 /* Free decoded data */
580 if (b_execute_option) {
581 free(b_execute_option);
583 if (b_service_responding) {
584 free(b_service_responding);
586 if (b_service_single_launch) {
587 free(b_service_single_launch);
589 if (b_service_multi_launch) {
590 free(b_service_multi_launch);
607 return NOTIFICATION_ERROR_NONE;
610 static void _notification_noti_populate_from_stmt(sqlite3_stmt * stmt, notification_h noti) {
613 if (stmt == NULL || noti == NULL) {
617 noti->type = sqlite3_column_int(stmt, col++);
618 noti->layout = sqlite3_column_int(stmt, col++);
619 noti->caller_pkgname = notification_db_column_text(stmt, col++);
620 noti->launch_pkgname = notification_db_column_text(stmt, col++);
621 noti->b_image_path = notification_db_column_bundle(stmt, col++);
622 noti->group_id = sqlite3_column_int(stmt, col++);
623 noti->internal_group_id = 0;
624 noti->priv_id = sqlite3_column_int(stmt, col++);
626 noti->b_text = notification_db_column_bundle(stmt, col++);
627 noti->b_key = notification_db_column_bundle(stmt, col++);
628 noti->b_format_args = notification_db_column_bundle(stmt, col++);
629 noti->num_format_args = sqlite3_column_int(stmt, col++);
631 noti->domain = notification_db_column_text(stmt, col++);
632 noti->dir = notification_db_column_text(stmt, col++);
633 noti->time = sqlite3_column_int(stmt, col++);
634 noti->insert_time = sqlite3_column_int(stmt, col++);
635 noti->args = notification_db_column_bundle(stmt, col++);
636 noti->group_args = notification_db_column_bundle(stmt, col++);
638 noti->b_execute_option = notification_db_column_bundle(stmt, col++);
639 noti->b_service_responding = notification_db_column_bundle(stmt, col++);
640 noti->b_service_single_launch =
641 notification_db_column_bundle(stmt, col++);
642 noti->b_service_multi_launch =
643 notification_db_column_bundle(stmt, col++);
645 noti->sound_type = sqlite3_column_int(stmt, col++);
646 noti->sound_path = notification_db_column_text(stmt, col++);
647 noti->vibration_type = sqlite3_column_int(stmt, col++);
648 noti->vibration_path = notification_db_column_text(stmt, col++);
649 noti->led_operation = sqlite3_column_int(stmt, col++);
650 noti->led_argb = sqlite3_column_int(stmt, col++);
651 noti->led_on_ms = sqlite3_column_int(stmt, col++);
652 noti->led_off_ms = sqlite3_column_int(stmt, col++);
654 noti->flags_for_property = sqlite3_column_int(stmt, col++);
655 noti->display_applist = sqlite3_column_int(stmt, col++);
656 noti->progress_size = sqlite3_column_double(stmt, col++);
657 noti->progress_percentage = sqlite3_column_double(stmt, col++);
659 noti->app_icon_path = NULL;
660 noti->app_name = NULL;
661 noti->temp_title = NULL;
662 noti->temp_content = NULL;
665 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
667 notification_h noti = NULL;
669 noti = malloc(sizeof(struct _notification));
674 _notification_noti_populate_from_stmt(stmt, noti);
679 int notification_noti_set_tag(const char *tag, char *value, char *buf, int buf_len)
683 len_total += (strlen(tag) * 2) + 5 + strlen(value) + 1;
685 if (buf_len <= len_total)
686 return NOTIFICATION_ERROR_INVALID_DATA;
688 snprintf(buf, buf_len, "<%s>%s</%s>", tag, value, tag);
690 return NOTIFICATION_ERROR_NONE;
693 char *notification_noti_strip_tag(const char *tagged_str)
695 if (tagged_str == NULL)
698 int len_total = strlen(tagged_str);
703 char *b_f_e = strstr(tagged_str, ">");
704 char *b_e_s = strstr(tagged_str, "</");
706 if (b_f_e == NULL || b_e_s == NULL || (b_e_s - b_f_e - 1) <= 0)
709 return strndup(b_f_e + 1, b_e_s - b_f_e - 1);
712 int notification_noti_get_tag_type(const char *tagged_str)
714 if (tagged_str == NULL)
715 return TAG_TYPE_INVALID;
717 if (strlen(tagged_str)== 0)
718 return TAG_TYPE_INVALID;
720 char *b_f_s = strstr(tagged_str, "<");
721 char *b_f_e = strstr(tagged_str, ">");
723 if (b_f_s == NULL || b_f_e == NULL || (b_f_e - b_f_s - 1) <= 0)
724 return TAG_TYPE_INVALID;
726 char *start = b_f_s + 1;
727 int len_tag = b_f_e - b_f_s - 1;
729 if (strncmp(start,TAG_TIME,len_tag) == 0) {
730 return TAG_TYPE_TIME;
733 return TAG_TYPE_INVALID;
736 static int _notification_noti_update_priv_id(sqlite3 * db, int rowid)
738 char query[128] = { 0, };
741 return NOTIFICATION_ERROR_INVALID_DATA;
744 snprintf(query, sizeof(query), "update noti_list set "
745 "priv_id = %d, internal_group_id = %d where rowid = %d",
746 rowid, rowid, rowid);
748 return notification_db_exec(db, query);
751 EXPORT_API int notification_noti_insert(notification_h noti)
754 sqlite3_stmt *stmt = NULL;
755 char query[NOTIFICATION_QUERY_MAX] = { 0, };
757 char buf_key[32] = { 0, };
758 const char *title_key = NULL;
761 db = notification_db_open(DBPATH);
763 return NOTIFICATION_ERROR_FROM_DB;
766 /* Initialize private ID */
767 noti->priv_id = NOTIFICATION_PRIV_ID_NONE;
768 noti->group_id = NOTIFICATION_GROUP_ID_NONE;
769 noti->internal_group_id = NOTIFICATION_GROUP_ID_NONE;
772 ret = _notification_noti_make_query(noti, query, sizeof(query));
773 if (ret != NOTIFICATION_ERROR_NONE) {
777 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
778 if (ret != SQLITE_OK) {
779 NOTIFICATION_ERR("Insert Query : %s", query);
780 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
782 ret = NOTIFICATION_ERROR_FROM_DB;
787 if (noti->b_key != NULL) {
788 snprintf(buf_key, sizeof(buf_key), "%d",
789 NOTIFICATION_TEXT_TYPE_TITLE);
791 title_key = bundle_get_val(noti->b_key, buf_key);
794 if (title_key == NULL && noti->b_text != NULL) {
795 snprintf(buf_key, sizeof(buf_key), "%d",
796 NOTIFICATION_TEXT_TYPE_TITLE);
798 title_key = bundle_get_val(noti->b_text, buf_key);
801 if (title_key == NULL) {
802 title_key = noti->caller_pkgname;
806 ret = _notification_noti_bind_query_text(stmt, "$title_key", title_key);
807 if (ret != NOTIFICATION_ERROR_NONE) {
808 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
811 ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
812 if (ret != NOTIFICATION_ERROR_NONE) {
813 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
815 sqlite3_finalize(stmt);
819 ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
820 if (ret != NOTIFICATION_ERROR_NONE) {
821 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
823 sqlite3_finalize(stmt);
828 ret = sqlite3_step(stmt);
829 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
830 noti->priv_id = (int)sqlite3_last_insert_rowid(db);
831 if (_notification_noti_update_priv_id(db, noti->priv_id) == 0) {
832 ret = NOTIFICATION_ERROR_NONE;
834 ret = NOTIFICATION_ERROR_FROM_DB;
837 ret = NOTIFICATION_ERROR_FROM_DB;
841 sqlite3_finalize(stmt);
846 notification_db_close(&db);
852 int notification_noti_get_by_priv_id(notification_h noti, char *pkgname, int priv_id)
855 sqlite3_stmt *stmt = NULL;
856 char query[NOTIFICATION_QUERY_MAX] = { 0, };
859 if (priv_id < 0 || noti == NULL) {
860 ret = NOTIFICATION_ERROR_INVALID_DATA;
865 db = notification_db_open(DBPATH);
867 return NOTIFICATION_ERROR_FROM_DB;
870 char *base_query = "select "
871 "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
872 "b_text, b_key, b_format_args, num_format_args, "
873 "text_domain, text_dir, time, insert_time, args, group_args, "
874 "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
875 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
876 "flags_for_property, display_applist, progress_size, progress_percentage "
879 if (pkgname != NULL) {
880 snprintf(query, sizeof(query), "%s where caller_pkgname = '%s' and priv_id = %d",
881 base_query ,pkgname, priv_id);
883 snprintf(query, sizeof(query), "%s where priv_id = %d", base_query, priv_id);
886 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
887 if (ret != SQLITE_OK) {
888 NOTIFICATION_ERR("select Query : %s", query);
889 NOTIFICATION_ERR("select DB error(%d) : %s", ret,
891 ret = NOTIFICATION_ERROR_FROM_DB;
895 ret = sqlite3_step(stmt);
896 if (ret == SQLITE_ROW) {
897 _notification_noti_populate_from_stmt(stmt, noti);
898 ret = NOTIFICATION_ERROR_NONE;
900 ret = NOTIFICATION_ERROR_FROM_DB;
904 sqlite3_finalize(stmt);
909 notification_db_close(&db);
915 EXPORT_API int notification_noti_update(notification_h noti)
918 sqlite3_stmt *stmt = NULL;
919 char query[NOTIFICATION_QUERY_MAX] = { 0, };
923 db = notification_db_open(DBPATH);
925 return NOTIFICATION_ERROR_FROM_DB;
928 /* Check private ID is exist */
929 ret = _notification_noti_check_priv_id(noti, db);
930 if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
931 ret = NOTIFICATION_ERROR_NOT_EXIST_ID;
935 /* make update query */
936 ret = _notification_noti_make_update_query(noti, query, sizeof(query));
937 if (ret != NOTIFICATION_ERROR_NONE) {
941 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
942 if (ret != SQLITE_OK) {
943 NOTIFICATION_ERR("Insert Query : %s", query);
944 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
946 ret = NOTIFICATION_ERROR_FROM_DB;
950 ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
951 if (ret != NOTIFICATION_ERROR_NONE) {
952 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
955 ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
956 if (ret != NOTIFICATION_ERROR_NONE) {
957 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
961 ret = sqlite3_step(stmt);
962 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
963 ret = NOTIFICATION_ERROR_NONE;
965 ret = NOTIFICATION_ERROR_FROM_DB;
969 sqlite3_finalize(stmt);
974 notification_db_close(&db);
980 EXPORT_API int notification_noti_delete_all(notification_type_e type, const char *pkgname, int *num_deleted, int **list_deleted_rowid)
982 int ret = NOTIFICATION_ERROR_NONE;
983 int i = 0, data_cnt = 0;
985 sqlite3_stmt *stmt = NULL;
986 char buf[128] = { 0, };
987 char query[NOTIFICATION_QUERY_MAX] = { 0, };
988 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
989 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
992 db = notification_db_open(DBPATH);
994 return NOTIFICATION_ERROR_FROM_DB;
997 if (pkgname == NULL) {
998 if (type != NOTIFICATION_TYPE_NONE) {
999 snprintf(query_where, sizeof(query_where),
1000 "where type = %d ", type);
1003 if (type == NOTIFICATION_TYPE_NONE) {
1004 snprintf(query_where, sizeof(query_where),
1005 "where caller_pkgname = '%s' ", pkgname);
1007 snprintf(query_where, sizeof(query_where),
1008 "where caller_pkgname = '%s' and type = %d ",
1013 if (num_deleted != NULL) {
1016 if (list_deleted_rowid != NULL) {
1017 *list_deleted_rowid = NULL;
1018 snprintf(query, sizeof(query),
1019 "select priv_id from noti_list %s ", query_where);
1021 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1022 if (ret != SQLITE_OK) {
1023 NOTIFICATION_ERR("Select Query : %s", query);
1024 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1025 sqlite3_errmsg(db));
1027 ret = NOTIFICATION_ERROR_FROM_DB;
1031 while(sqlite3_step(stmt) == SQLITE_ROW) {
1032 if (data_cnt % 8 == 0) {
1035 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1037 *list_deleted_rowid = tmp;
1039 NOTIFICATION_ERR("Heap: %s\n", strerror(errno));
1042 * How can I handle this?
1044 free(*list_deleted_rowid);
1045 *list_deleted_rowid = NULL;
1046 ret = NOTIFICATION_ERROR_NO_MEMORY;
1050 *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1055 sqlite3_finalize(stmt);
1060 query_where[0] = '\0';
1061 for (i = 0; i < data_cnt ; i++) {
1062 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1063 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1065 snprintf(query_base, sizeof(query_base), "delete from noti_list");
1066 snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1068 NOTIFICATION_ERR("check : %s", query);
1069 ret = notification_db_exec(db, query);
1071 free(*list_deleted_rowid);
1072 *list_deleted_rowid = NULL;
1075 if (num_deleted != NULL) {
1076 *num_deleted = data_cnt;
1079 /* Make main query */
1080 snprintf(query_base, sizeof(query_base), "delete from noti_list ");
1081 snprintf(query, sizeof(query), "%s %s", query_base, query_where);
1083 ret = notification_db_exec(db, query);
1085 if (num_deleted != NULL) {
1086 *num_deleted = sqlite3_changes(db);
1092 sqlite3_finalize(stmt);
1096 notification_db_close(&db);
1102 int notification_noti_delete_group_by_group_id(const char *pkgname,
1103 int group_id, int *num_deleted, int **list_deleted_rowid)
1105 int ret = NOTIFICATION_ERROR_NONE;
1107 int i = 0, data_cnt = 0;
1108 sqlite3_stmt *stmt = NULL;
1109 char buf[128] = { 0, };
1110 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1111 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1112 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1114 /* Check pkgname is valid */
1115 if (pkgname == NULL) {
1116 return NOTIFICATION_ERROR_INVALID_DATA;
1119 snprintf(query_where, sizeof(query_where),
1120 "where caller_pkgname = '%s' and group_id = %d", pkgname, group_id);
1123 db = notification_db_open(DBPATH);
1125 return NOTIFICATION_ERROR_FROM_DB;
1128 if (num_deleted != NULL) {
1131 if (list_deleted_rowid != NULL) {
1132 *list_deleted_rowid = NULL;
1133 snprintf(query, sizeof(query),
1134 "select priv_id from noti_list %s ", query_where);
1136 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1137 if (ret != SQLITE_OK) {
1138 NOTIFICATION_ERR("Select Query : %s", query);
1139 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1140 sqlite3_errmsg(db));
1142 ret = NOTIFICATION_ERROR_FROM_DB;
1146 while(sqlite3_step(stmt) == SQLITE_ROW) {
1147 if (data_cnt % 8 == 0) {
1149 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1151 *list_deleted_rowid = tmp;
1153 free(*list_deleted_rowid);
1154 *list_deleted_rowid = NULL;
1155 ret = NOTIFICATION_ERROR_NO_MEMORY;
1159 *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1164 sqlite3_finalize(stmt);
1169 query_where[0] = '\0';
1170 for (i = 0; i < data_cnt ; i++) {
1171 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1172 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1174 snprintf(query_base, sizeof(query_base), "delete from noti_list");
1175 snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1177 NOTIFICATION_ERR("check : %s", query);
1178 ret = notification_db_exec(db, query);
1180 free(*list_deleted_rowid);
1181 *list_deleted_rowid = NULL;
1184 if (num_deleted != NULL) {
1185 *num_deleted = data_cnt;
1189 snprintf(query, sizeof(query), "delete from noti_list %s", query_where);
1192 ret = notification_db_exec(db, query);
1197 sqlite3_finalize(stmt);
1201 notification_db_close(&db);
1207 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
1210 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1211 int internal_group_id = 0;
1214 /* Check pkgname is valid */
1215 if (pkgname == NULL) {
1216 return NOTIFICATION_ERROR_INVALID_DATA;
1220 db = notification_db_open(DBPATH);
1222 return NOTIFICATION_ERROR_FROM_DB;
1225 /* Get internal group id using priv id */
1227 _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1231 snprintf(query, sizeof(query), "delete from noti_list "
1232 "where caller_pkgname = '%s' and internal_group_id = %d",
1233 pkgname, internal_group_id);
1236 ret = notification_db_exec(db, query);
1239 notification_db_close(&db);
1244 EXPORT_API int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
1247 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1250 /* Check pkgname is valid */
1251 if (pkgname == NULL) {
1252 return NOTIFICATION_ERROR_INVALID_DATA;
1256 db = notification_db_open(DBPATH);
1258 return NOTIFICATION_ERROR_FROM_DB;
1262 snprintf(query, sizeof(query), "delete from noti_list "
1263 "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1267 ret = notification_db_exec(db, query);
1271 notification_db_close(&db);
1277 notification_error_e notification_noti_get_count(notification_type_e type,
1278 const char *pkgname,
1279 int group_id, int priv_id,
1283 sqlite3_stmt *stmt = NULL;
1284 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1285 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1286 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1287 char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
1289 int ret = 0, get_count = 0, internal_group_id = 0;
1290 int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
1292 int flag_where_more = 0;
1296 db = notification_db_open(DBPATH);
1298 return NOTIFICATION_ERROR_FROM_DB;
1301 /* Check current sim status */
1302 ret_vconf = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1305 snprintf(query_base, sizeof(query_base),
1306 "select count(*) from noti_list ");
1308 if (pkgname != NULL) {
1309 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
1310 if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1311 snprintf(query_where, sizeof(query_where),
1312 "where caller_pkgname = '%s' ",
1317 _notification_noti_get_internal_group_id_by_priv_id
1318 (pkgname, priv_id, db);
1319 snprintf(query_where, sizeof(query_where),
1320 "where caller_pkgname = '%s' and internal_group_id = %d ",
1321 pkgname, internal_group_id);
1325 if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1326 snprintf(query_where, sizeof(query_where),
1327 "where caller_pkgname = '%s' and group_id = %d ",
1332 _notification_noti_get_internal_group_id_by_priv_id
1333 (pkgname, priv_id, db);
1334 snprintf(query_where, sizeof(query_where),
1335 "where caller_pkgname = '%s' and internal_group_id = %d ",
1336 pkgname, internal_group_id);
1343 if (ret_vconf == 0 && status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1344 if (type != NOTIFICATION_TYPE_NONE) {
1345 snprintf(query_where_more, sizeof(query_where_more),
1346 "type = %d ", type);
1347 flag_where_more = 1;
1350 if (type != NOTIFICATION_TYPE_NONE) {
1351 snprintf(query_where_more, sizeof(query_where_more),
1352 "type = %d and flag_simmode = 0 ", type);
1353 flag_where_more = 1;
1355 snprintf(query_where_more, sizeof(query_where_more),
1356 "flag_simmode = 0 ");
1357 flag_where_more = 1;
1361 if (flag_where == 1) {
1362 if (flag_where_more == 1) {
1363 snprintf(query, sizeof(query), "%s %s and %s",
1364 query_base, query_where, query_where_more);
1366 snprintf(query, sizeof(query), "%s %s", query_base,
1371 if (flag_where_more == 1) {
1372 snprintf(query, sizeof(query), "%s where %s",
1373 query_base, query_where_more);
1375 snprintf(query, sizeof(query), "%s", query_base);
1379 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1380 if (ret != SQLITE_OK) {
1381 NOTIFICATION_ERR("Select Query : %s", query);
1382 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1383 sqlite3_errmsg(db));
1385 ret = NOTIFICATION_ERROR_FROM_DB;
1389 ret = sqlite3_step(stmt);
1390 if (ret == SQLITE_ROW) {
1391 get_count = sqlite3_column_int(stmt, 0);
1394 ret = NOTIFICATION_ERROR_NONE;
1398 sqlite3_finalize(stmt);
1403 notification_db_close(&db);
1411 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
1413 notification_list_h *
1417 sqlite3_stmt *stmt = NULL;
1418 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1419 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1420 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1423 notification_list_h get_list = NULL;
1424 notification_h noti = NULL;
1425 int internal_count = 0;
1429 db = notification_db_open(DBPATH);
1431 return NOTIFICATION_ERROR_FROM_DB;
1434 /* Check current sim status */
1435 ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1438 snprintf(query_base, sizeof(query_base), "select "
1439 "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1440 "b_text, b_key, b_format_args, num_format_args, "
1441 "text_domain, text_dir, time, insert_time, args, group_args, "
1442 "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1443 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
1444 "flags_for_property, display_applist, progress_size, progress_percentage "
1447 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1448 if (type != NOTIFICATION_TYPE_NONE) {
1449 snprintf(query_where, sizeof(query_where),
1450 "where type = %d ", type);
1453 if (type != NOTIFICATION_TYPE_NONE) {
1454 snprintf(query_where, sizeof(query_where),
1455 "where type = %d and flag_simmode = 0 ", type);
1457 snprintf(query_where, sizeof(query_where),
1458 "where flag_simmode = 0 ");
1462 snprintf(query, sizeof(query),
1464 "group by internal_group_id "
1465 "order by rowid desc, time desc", query_base, query_where);
1467 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1468 if (ret != SQLITE_OK) {
1469 NOTIFICATION_ERR("Select Query : %s", query);
1470 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1471 sqlite3_errmsg(db));
1473 ret = NOTIFICATION_ERROR_FROM_DB;
1477 while (sqlite3_step(stmt) == SQLITE_ROW) {
1478 /* Make notification list */
1479 noti = _notification_noti_get_item(stmt);
1483 get_list = notification_list_append(get_list, noti);
1485 if (count != -1 && internal_count >= count) {
1487 ("internal count %d >= count %d",
1488 internal_count, count);
1494 ret = NOTIFICATION_ERROR_NONE;
1498 sqlite3_finalize(stmt);
1503 notification_db_close(&db);
1506 if (get_list != NULL) {
1507 *list = notification_list_get_head(get_list);
1513 notification_error_e notification_noti_get_detail_list(const char *pkgname,
1515 int priv_id, int count,
1516 notification_list_h *list)
1519 sqlite3_stmt *stmt = NULL;
1520 char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1521 char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1523 char query[NOTIFICATION_QUERY_MAX] = { 0, };
1525 notification_list_h get_list = NULL;
1526 notification_h noti = NULL;
1527 int internal_count = 0;
1528 int internal_group_id = 0;
1529 int status = 0; /* If the vconf_get_int failed, the status will be the garbage value */
1532 db = notification_db_open(DBPATH);
1534 return NOTIFICATION_ERROR_FROM_DB;
1537 /* Check current sim status */
1538 ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1541 snprintf(query_base, sizeof(query_base), "select "
1542 "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1543 "b_text, b_key, b_format_args, num_format_args, "
1544 "text_domain, text_dir, time, insert_time, args, group_args, "
1545 "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1546 "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
1547 "flags_for_property, display_applist, progress_size, progress_percentage "
1550 if (priv_id == NOTIFICATION_PRIV_ID_NONE && group_id == NOTIFICATION_GROUP_ID_NONE) {
1551 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1552 snprintf(query_where, sizeof(query_where),
1553 "where caller_pkgname = '%s' ",
1554 pkgname, internal_group_id);
1556 snprintf(query_where, sizeof(query_where),
1557 "where caller_pkgname = '%s' and flag_simmode = 0 ",
1558 pkgname, internal_group_id);
1562 _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1565 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1566 snprintf(query_where, sizeof(query_where),
1567 "where caller_pkgname = '%s' and internal_group_id = %d ",
1568 pkgname, internal_group_id);
1570 snprintf(query_where, sizeof(query_where),
1571 "where caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
1572 pkgname, internal_group_id);
1576 snprintf(query, sizeof(query),
1578 "order by rowid desc, time desc", query_base, query_where);
1580 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1581 if (ret != SQLITE_OK) {
1582 NOTIFICATION_ERR("Select Query : %s", query);
1583 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1584 sqlite3_errmsg(db));
1586 ret = NOTIFICATION_ERROR_FROM_DB;
1590 while (sqlite3_step(stmt) == SQLITE_ROW) {
1591 /* Make notification list */
1592 noti = _notification_noti_get_item(stmt);
1596 get_list = notification_list_append(get_list, noti);
1598 if (count != -1 && internal_count >= count) {
1600 ("internal count %d >= count %d",
1601 internal_count, count);
1607 ret = NOTIFICATION_ERROR_NONE;
1611 sqlite3_finalize(stmt);
1616 notification_db_close(&db);
1619 if (get_list != NULL) {
1620 *list = notification_list_get_head(get_list);