2 * Copyright (c) 2000 - 2016 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 *pkgname,
27 int group_id, sqlite3 *db)
29 sqlite3_stmt *stmt = NULL;
30 char query[NOTIFICATION_QUERY_MAX] = { 0, };
31 int ret = NOTIFICATION_ERROR_NONE, result = 0;
33 snprintf(query, sizeof(query),
34 "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
37 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
38 if (ret != SQLITE_OK) {
39 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
41 return NOTIFICATION_ERROR_FROM_DB;
44 ret = sqlite3_step(stmt);
45 if (ret == SQLITE_ROW)
46 result = sqlite3_column_int(stmt, 0);
51 NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
54 sqlite3_finalize(stmt);
57 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
59 return NOTIFICATION_ERROR_NONE;
64 int notification_group_set_badge(const char *pkgname,
65 int group_id, int count)
68 sqlite3_stmt *stmt = NULL;
69 char query[NOTIFICATION_QUERY_MAX] = { 0, };
71 int result = NOTIFICATION_ERROR_NONE;
73 db = notification_db_open(DBPATH);
75 return get_last_result();
77 ret = _notification_group_check_data_inserted(pkgname, 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_pkgname, group_id, badge, content, loc_content) values ("
83 "'%s', %d, %d, '', '')", pkgname, group_id, count);
87 snprintf(query, sizeof(query), "update noti_group_data "
89 "where caller_pkgname = '%s' and group_id = %d",
90 count, pkgname, group_id);
93 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
94 if (ret != SQLITE_OK) {
95 NOTIFICATION_ERR("Insert Query : %s", query);
96 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
99 sqlite3_finalize(stmt);
102 notification_db_close(&db);
104 return NOTIFICATION_ERROR_FROM_DB;
107 ret = sqlite3_step(stmt);
108 if (ret == SQLITE_OK || ret == SQLITE_DONE)
109 result = NOTIFICATION_ERROR_NONE;
111 result = NOTIFICATION_ERROR_FROM_DB;
114 sqlite3_finalize(stmt);
117 notification_db_close(&db);
123 /* LCOV_EXCL_START */
124 int notification_group_get_badge(const char *pkgname,
125 int group_id, int *count)
128 sqlite3_stmt *stmt = NULL;
129 char query[NOTIFICATION_QUERY_MAX] = { 0, };
133 db = notification_db_open(DBPATH);
135 return get_last_result();
137 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
138 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
139 if (ret == NOTIFICATION_ERROR_NONE)
140 /* Get all of pkgname count if none group id is not exist */
141 snprintf(query, sizeof(query),
143 "from noti_group_data "
144 "where caller_pkgname = '%s'", pkgname);
146 /* Get none group id count */
147 snprintf(query, sizeof(query),
149 "from noti_group_data "
150 "where caller_pkgname = '%s' and group_id = %d",
153 snprintf(query, sizeof(query),
155 "from noti_group_data "
156 "where caller_pkgname = '%s' and group_id = %d",
160 NOTIFICATION_INFO("Get badge : query[%s]", query);
162 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
163 if (ret != SQLITE_OK) {
164 NOTIFICATION_ERR("Select Query : %s", query);
165 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
168 notification_db_close(&db);
170 return NOTIFICATION_ERROR_FROM_DB;
173 ret = sqlite3_step(stmt);
174 if (ret == SQLITE_ROW)
175 *count = sqlite3_column_int(stmt, col++);
177 sqlite3_finalize(stmt);
180 notification_db_close(&db);
182 return NOTIFICATION_ERROR_NONE;