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>
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.
26 #include <notification_debug.h>
27 #include <notification_group.h>
28 #include <notification_db.h>
30 static int _notification_group_check_data_inserted(const char *pkgname,
31 int group_id, sqlite3 * db)
33 sqlite3_stmt *stmt = NULL;
34 char query[NOTIFICATION_QUERY_MAX] = { 0, };
35 int ret = NOTIFICATION_ERROR_NONE, result = 0;
37 snprintf(query, sizeof(query),
38 "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
41 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
42 if (ret != SQLITE_OK) {
43 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
45 return NOTIFICATION_ERROR_FROM_DB;
48 ret = sqlite3_step(stmt);
49 if (ret == SQLITE_ROW) {
50 result = sqlite3_column_int(stmt, 0);
55 NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
58 sqlite3_finalize(stmt);
61 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
64 return NOTIFICATION_ERROR_NONE;
67 int notification_group_set_badge(const char *pkgname,
68 int group_id, int count)
71 sqlite3_stmt *stmt = NULL;
72 char query[NOTIFICATION_QUERY_MAX] = { 0, };
74 int result = NOTIFICATION_ERROR_NONE;
77 db = notification_db_open(DBPATH);
79 return get_last_result();
82 /* Check pkgname & group_id */
83 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
86 if (ret == NOTIFICATION_ERROR_NONE) {
87 /* Insert if does not exist */
88 snprintf(query, sizeof(query), "insert into noti_group_data ("
89 "caller_pkgname, group_id, badge, content, loc_content) values ("
90 "'%s', %d, %d, '', '')", pkgname, group_id, count);
94 snprintf(query, sizeof(query), "update noti_group_data "
96 "where caller_pkgname = '%s' and group_id = %d",
97 count, pkgname, group_id);
100 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
101 if (ret != SQLITE_OK) {
102 NOTIFICATION_ERR("Insert Query : %s", query);
103 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
106 sqlite3_finalize(stmt);
110 notification_db_close(&db);
112 return NOTIFICATION_ERROR_FROM_DB;
115 ret = sqlite3_step(stmt);
116 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
117 result = NOTIFICATION_ERROR_NONE;
119 result = NOTIFICATION_ERROR_FROM_DB;
123 sqlite3_finalize(stmt);
128 notification_db_close(&db);
134 int notification_group_get_badge(const char *pkgname,
135 int group_id, int *count)
138 sqlite3_stmt *stmt = NULL;
139 char query[NOTIFICATION_QUERY_MAX] = { 0, };
144 db = notification_db_open(DBPATH);
146 return get_last_result();
150 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
151 /* Check Group id None is exist */
153 _notification_group_check_data_inserted(pkgname, group_id,
156 if (ret == NOTIFICATION_ERROR_NONE) {
157 /* Get all of pkgname count if none group id is not exist */
158 snprintf(query, sizeof(query),
160 "from noti_group_data "
161 "where caller_pkgname = '%s'", pkgname);
163 /* Get none group id count */
164 snprintf(query, sizeof(query),
166 "from noti_group_data "
167 "where caller_pkgname = '%s' and group_id = %d",
171 snprintf(query, sizeof(query),
173 "from noti_group_data "
174 "where caller_pkgname = '%s' and group_id = %d",
178 NOTIFICATION_INFO("Get badge : query[%s]", query);
180 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
181 if (ret != SQLITE_OK) {
182 NOTIFICATION_ERR("Select Query : %s", query);
183 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
186 return NOTIFICATION_ERROR_FROM_DB;
189 ret = sqlite3_step(stmt);
190 if (ret == SQLITE_ROW) {
191 *count = sqlite3_column_int(stmt, col++);
194 sqlite3_finalize(stmt);
198 notification_db_close(&db);
201 return NOTIFICATION_ERROR_NONE;