2 * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <notification_debug.h>
22 #include <notification_group.h>
23 #include <notification_db.h>
26 static int _notification_group_check_data_inserted(const char *app_id,
27 int group_id, sqlite3 *db)
29 sqlite3_stmt *stmt = NULL;
30 char query[NOTIFICATION_QUERY_MAX] = { 0, };
34 snprintf(query, sizeof(query),
35 "select count(*) from noti_group_data where caller_app_id = '%s' and group_id = %d",
38 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
39 if (ret != SQLITE_OK) {
40 ERR("Get count DB err(%d) : %s", ret, sqlite3_errmsg(db));
41 return NOTIFICATION_ERROR_FROM_DB;
44 ret = sqlite3_step(stmt);
45 if (ret == SQLITE_ROW)
46 result = sqlite3_column_int(stmt, 0);
51 sqlite3_finalize(stmt);
54 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
56 INFO("Check Data Inserted appid[%s] group_id[%d] result[%d]",
57 app_id, group_id, result);
59 return NOTIFICATION_ERROR_NONE;
64 int notification_group_set_badge(const char *app_id,
65 int group_id, int count)
68 sqlite3_stmt *stmt = NULL;
69 char query[NOTIFICATION_QUERY_MAX] = { 0, };
73 db = notification_db_open();
75 return get_last_result();
77 ret = _notification_group_check_data_inserted(app_id, group_id, db);
79 if (ret == NOTIFICATION_ERROR_NONE) {
80 /* Insert if does not exist */
81 snprintf(query, sizeof(query), "insert into noti_group_data ("
82 "caller_app_id, group_id, badge, content, loc_content) values ("
83 "'%s', %d, %d, '', '')", app_id, group_id, count);
87 snprintf(query, sizeof(query), "update noti_group_data "
89 "where caller_app_id = '%s' and group_id = %d",
90 count, app_id, group_id);
93 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
94 if (ret != SQLITE_OK) {
95 ERR("Failed to insert data app_id[%s] err[%d][%s]",
96 app_id, ret, sqlite3_errmsg(db));
98 sqlite3_finalize(stmt);
101 notification_db_close(&db);
103 return NOTIFICATION_ERROR_FROM_DB;
106 ret = sqlite3_step(stmt);
107 if (ret == SQLITE_OK || ret == SQLITE_DONE)
108 result = NOTIFICATION_ERROR_NONE;
110 result = NOTIFICATION_ERROR_FROM_DB;
112 sqlite3_finalize(stmt);
115 notification_db_close(&db);
121 /* LCOV_EXCL_START */
122 int notification_group_get_badge(const char *app_id,
123 int group_id, int *count)
126 sqlite3_stmt *stmt = NULL;
127 char query[NOTIFICATION_QUERY_MAX] = { 0, };
131 db = notification_db_open();
133 return get_last_result();
135 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
136 ret = _notification_group_check_data_inserted(app_id, group_id, db);
137 if (ret == NOTIFICATION_ERROR_NONE)
138 /* Get all of app_id count if none group id is not exist */
139 snprintf(query, sizeof(query),
141 "from noti_group_data "
142 "where caller_app_id = '%s'", app_id);
144 /* Get none group id count */
145 snprintf(query, sizeof(query),
147 "from noti_group_data "
148 "where caller_app_id = '%s' and group_id = %d",
151 snprintf(query, sizeof(query),
153 "from noti_group_data "
154 "where caller_app_id = '%s' and group_id = %d",
158 INFO("Get badge : query[%s]", query);
160 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
161 if (ret != SQLITE_OK) {
162 ERR("Failed to insert data app_id[%s] err[%d][%s]",
163 app_id, ret, sqlite3_errmsg(db));
165 notification_db_close(&db);
167 return NOTIFICATION_ERROR_FROM_DB;
170 ret = sqlite3_step(stmt);
171 if (ret == SQLITE_ROW)
172 *count = sqlite3_column_int(stmt, col++);
174 sqlite3_finalize(stmt);
177 notification_db_close(&db);
179 return NOTIFICATION_ERROR_NONE;