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_bind_query(sqlite3_stmt * stmt, const char *name,
36 index = sqlite3_bind_parameter_index(stmt, name);
38 NOTIFICATION_ERR("Insert : invalid column name");
39 return NOTIFICATION_ERROR_FROM_DB;
43 sqlite3_bind_text(stmt, index, NOTIFICATION_CHECK_STR(str), -1,
45 if (ret != SQLITE_OK) {
46 NOTIFICATION_ERR("Insert text : %s",
47 NOTIFICATION_CHECK_STR(str));
48 return NOTIFICATION_ERROR_FROM_DB;
51 return NOTIFICATION_ERROR_NONE;
54 static int _notification_group_check_data_inserted(const char *pkgname,
55 int group_id, sqlite3 * db)
57 sqlite3_stmt *stmt = NULL;
58 char query[NOTIFICATION_QUERY_MAX] = { 0, };
59 int ret = NOTIFICATION_ERROR_NONE, result = 0;
61 snprintf(query, sizeof(query),
62 "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
65 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
66 if (ret != SQLITE_OK) {
67 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
69 return NOTIFICATION_ERROR_FROM_DB;
72 ret = sqlite3_step(stmt);
73 if (ret == SQLITE_ROW) {
74 result = sqlite3_column_int(stmt, 0);
79 NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
82 sqlite3_finalize(stmt);
85 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
88 return NOTIFICATION_ERROR_NONE;
91 notification_error_e notification_group_set_title(const char *pkgname,
94 const char *loc_title,
95 notification_count_display_type_e count_display)
98 sqlite3_stmt *stmt = NULL;
99 char query[NOTIFICATION_QUERY_MAX] = { 0, };
101 int result = NOTIFICATION_ERROR_NONE;
104 db = notification_db_open(DBPATH);
106 // Check pkgname & group_id
107 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
108 if (ret == NOTIFICATION_ERROR_NONE) {
109 // not exist -> insert
110 snprintf(query, sizeof(query), "insert into noti_group_data ("
111 "caller_pkgname, group_id, unread_count, title, loc_title, count_display_title) values ("
112 "'%s', %d, 0, $title, $loc_title, %d)",
113 pkgname, group_id, count_display);
117 // not exist -> insert
118 snprintf(query, sizeof(query), "update noti_group_data "
119 "set title = $title, loc_title = $loc_title, count_display_title = %d "
120 "where caller_pkgname = '%s' and group_id = %d",
121 count_display, pkgname, group_id);
125 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
126 if (ret != SQLITE_OK) {
127 NOTIFICATION_ERR("Insert Query : %s", query);
128 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
131 sqlite3_finalize(stmt);
135 notification_db_close(&db);
137 return NOTIFICATION_ERROR_FROM_DB;
140 ret = _notification_group_bind_query(stmt, "$title", title);
141 if (ret != NOTIFICATION_ERROR_NONE) {
142 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
146 ret = _notification_group_bind_query(stmt, "$loc_title", loc_title);
147 if (ret != NOTIFICATION_ERROR_NONE) {
148 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
152 ret = sqlite3_step(stmt);
153 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
154 result = NOTIFICATION_ERROR_NONE;
156 result = NOTIFICATION_ERROR_FROM_DB;
160 sqlite3_finalize(stmt);
164 notification_db_close(&db);
170 notification_error_e notification_group_get_title(const char *pkgname,
173 char **ret_loc_title,
174 notification_count_display_type_e *count_display)
177 sqlite3_stmt *stmt = NULL;
178 char query[NOTIFICATION_QUERY_MAX] = { 0, };
183 db = notification_db_open(DBPATH);
185 snprintf(query, sizeof(query), "select "
186 "title, loc_title, count_display_title "
187 "from noti_group_data "
188 "where caller_pkgname = '%s' and group_id = %d",
191 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
192 if (ret != SQLITE_OK) {
193 NOTIFICATION_ERR("Select Query : %s", query);
194 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
197 return NOTIFICATION_ERROR_FROM_DB;
200 ret = sqlite3_step(stmt);
201 if (ret == SQLITE_ROW) {
202 *ret_title = notification_db_column_text(stmt, col++);
203 *ret_loc_title = notification_db_column_text(stmt, col++);
204 *count_display = sqlite3_column_int(stmt, col++);
207 sqlite3_finalize(stmt);
211 notification_db_close(&db);
214 return NOTIFICATION_ERROR_NONE;
217 notification_error_e notification_group_set_content(const char *pkgname,
220 const char *loc_content,
221 notification_count_display_type_e count_display)
224 sqlite3_stmt *stmt = NULL;
225 char query[NOTIFICATION_QUERY_MAX] = { 0, };
227 int result = NOTIFICATION_ERROR_NONE;
230 db = notification_db_open(DBPATH);
232 // Check pkgname & group_id
233 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
234 if (ret == NOTIFICATION_ERROR_NONE) {
235 // not exist -> insert
236 snprintf(query, sizeof(query), "insert into noti_group_data ("
237 "caller_pkgname, group_id, unread_count, content, loc_content, count_display_content) values ("
238 "'%s', %d, 0, $content, $loc_content, %d)",
239 pkgname, group_id, count_display);
240 // NOTIFICATION_INFO("Insert Query : %s", query);
243 // not exist -> insert
244 snprintf(query, sizeof(query), "update noti_group_data "
245 "set content = $content, loc_content = $loc_content, count_display_content = %d "
246 "where caller_pkgname = '%s' and group_id = %d",
247 count_display, pkgname, group_id);
248 // NOTIFICATION_INFO("Insert Query : %s", query);
252 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
253 if (ret != SQLITE_OK) {
254 NOTIFICATION_ERR("Insert Query : %s", query);
255 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
258 sqlite3_finalize(stmt);
262 notification_db_close(&db);
264 return NOTIFICATION_ERROR_FROM_DB;
267 ret = _notification_group_bind_query(stmt, "$content", content);
268 if (ret != NOTIFICATION_ERROR_NONE) {
269 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
273 ret = _notification_group_bind_query(stmt, "$loc_content", loc_content);
274 if (ret != NOTIFICATION_ERROR_NONE) {
275 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
279 ret = sqlite3_step(stmt);
280 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
281 result = NOTIFICATION_ERROR_NONE;
283 result = NOTIFICATION_ERROR_FROM_DB;
287 sqlite3_finalize(stmt);
291 notification_db_close(&db);
297 notification_error_e notification_group_get_content(const char *pkgname,
300 char **ret_loc_content,
301 notification_count_display_type_e *count_display)
304 sqlite3_stmt *stmt = NULL;
305 char query[NOTIFICATION_QUERY_MAX] = { 0, };
310 db = notification_db_open(DBPATH);
312 snprintf(query, sizeof(query), "select "
313 "content, loc_content, count_display_content "
314 "from noti_group_data "
315 "where caller_pkgname = '%s' and group_id = %d",
318 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
319 if (ret != SQLITE_OK) {
320 NOTIFICATION_ERR("Select Query : %s", query);
321 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
324 return NOTIFICATION_ERROR_FROM_DB;
327 ret = sqlite3_step(stmt);
328 if (ret == SQLITE_ROW) {
329 *ret_content = notification_db_column_text(stmt, col++);
330 *ret_loc_content = notification_db_column_text(stmt, col++);
331 *count_display = sqlite3_column_int(stmt, col++);
334 sqlite3_finalize(stmt);
338 notification_db_close(&db);
341 return NOTIFICATION_ERROR_NONE;
344 notification_error_e notification_group_set_badge(const char *pkgname,
345 int group_id, int count)
348 sqlite3_stmt *stmt = NULL;
349 char query[NOTIFICATION_QUERY_MAX] = { 0, };
351 int result = NOTIFICATION_ERROR_NONE;
354 db = notification_db_open(DBPATH);
356 /* Check pkgname & group_id */
357 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
360 if (ret == NOTIFICATION_ERROR_NONE) {
361 /* Insert if does not exist */
362 snprintf(query, sizeof(query), "insert into noti_group_data ("
363 "caller_pkgname, group_id, badge, content, loc_content) values ("
364 "'%s', %d, %d, '', '')", pkgname, group_id, count);
367 /* Update if exist */
368 snprintf(query, sizeof(query), "update noti_group_data "
370 "where caller_pkgname = '%s' and group_id = %d",
371 count, pkgname, group_id);
374 ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
375 if (ret != SQLITE_OK) {
376 NOTIFICATION_ERR("Insert Query : %s", query);
377 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
380 sqlite3_finalize(stmt);
384 notification_db_close(&db);
386 return NOTIFICATION_ERROR_FROM_DB;
389 ret = sqlite3_step(stmt);
390 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
391 result = NOTIFICATION_ERROR_NONE;
393 result = NOTIFICATION_ERROR_FROM_DB;
397 sqlite3_finalize(stmt);
402 notification_db_close(&db);
408 notification_error_e notification_group_get_badge(const char *pkgname,
409 int group_id, int *count)
412 sqlite3_stmt *stmt = NULL;
413 char query[NOTIFICATION_QUERY_MAX] = { 0, };
418 db = notification_db_open(DBPATH);
421 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
422 /* Check Group id None is exist */
424 _notification_group_check_data_inserted(pkgname, group_id,
427 if (ret == NOTIFICATION_ERROR_NONE) {
428 /* Get all of pkgname count if none group id is not exist */
429 snprintf(query, sizeof(query),
431 "from noti_group_data "
432 "where caller_pkgname = '%s'", pkgname);
434 /* Get none group id count */
435 snprintf(query, sizeof(query),
437 "from noti_group_data "
438 "where caller_pkgname = '%s' and group_id = %d",
442 snprintf(query, sizeof(query),
444 "from noti_group_data "
445 "where caller_pkgname = '%s' and group_id = %d",
449 NOTIFICATION_INFO("Get badge : query[%s]", query);
451 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
452 if (ret != SQLITE_OK) {
453 NOTIFICATION_ERR("Select Query : %s", query);
454 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
457 return NOTIFICATION_ERROR_FROM_DB;
460 ret = sqlite3_step(stmt);
461 if (ret == SQLITE_ROW) {
462 *count = sqlite3_column_int(stmt, col++);
465 sqlite3_finalize(stmt);
469 notification_db_close(&db);
472 return NOTIFICATION_ERROR_NONE;