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.
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 notification_error_e 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 /* Check pkgname & group_id */
80 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
83 if (ret == NOTIFICATION_ERROR_NONE) {
84 /* Insert if does not exist */
85 snprintf(query, sizeof(query), "insert into noti_group_data ("
86 "caller_pkgname, group_id, badge, content, loc_content) values ("
87 "'%s', %d, %d, '', '')", pkgname, group_id, count);
91 snprintf(query, sizeof(query), "update noti_group_data "
93 "where caller_pkgname = '%s' and group_id = %d",
94 count, pkgname, group_id);
97 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
98 if (ret != SQLITE_OK) {
99 NOTIFICATION_ERR("Insert Query : %s", query);
100 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
103 sqlite3_finalize(stmt);
107 notification_db_close(&db);
109 return NOTIFICATION_ERROR_FROM_DB;
112 ret = sqlite3_step(stmt);
113 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
114 result = NOTIFICATION_ERROR_NONE;
116 result = NOTIFICATION_ERROR_FROM_DB;
120 sqlite3_finalize(stmt);
125 notification_db_close(&db);
131 notification_error_e notification_group_get_badge(const char *pkgname,
132 int group_id, int *count)
135 sqlite3_stmt *stmt = NULL;
136 char query[NOTIFICATION_QUERY_MAX] = { 0, };
141 db = notification_db_open(DBPATH);
144 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
145 /* Check Group id None is exist */
147 _notification_group_check_data_inserted(pkgname, group_id,
150 if (ret == NOTIFICATION_ERROR_NONE) {
151 /* Get all of pkgname count if none group id is not exist */
152 snprintf(query, sizeof(query),
154 "from noti_group_data "
155 "where caller_pkgname = '%s'", pkgname);
157 /* Get none group id count */
158 snprintf(query, sizeof(query),
160 "from noti_group_data "
161 "where caller_pkgname = '%s' and group_id = %d",
165 snprintf(query, sizeof(query),
167 "from noti_group_data "
168 "where caller_pkgname = '%s' and group_id = %d",
172 NOTIFICATION_INFO("Get badge : query[%s]", query);
174 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
175 if (ret != SQLITE_OK) {
176 NOTIFICATION_ERR("Select Query : %s", query);
177 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
180 return NOTIFICATION_ERROR_FROM_DB;
183 ret = sqlite3_step(stmt);
184 if (ret == SQLITE_ROW) {
185 *count = sqlite3_column_int(stmt, col++);
188 sqlite3_finalize(stmt);
192 notification_db_close(&db);
195 return NOTIFICATION_ERROR_NONE;