507e6bc1a4fa622902aafe9173bca953d5fdb34a
[apps/home/notification.git] / src / notification_group.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <notification_debug.h>
27 #include <notification_group.h>
28 #include <notification_db.h>
29
30 static int _notification_group_check_data_inserted(const char *pkgname,
31                                                    int group_id, sqlite3 * db)
32 {
33         sqlite3_stmt *stmt = NULL;
34         char query[NOTIFICATION_QUERY_MAX] = { 0, };
35         int ret = NOTIFICATION_ERROR_NONE, result = 0;
36
37         snprintf(query, sizeof(query),
38                  "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
39                  pkgname, group_id);
40
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,
44                                  sqlite3_errmsg(db));
45                 return NOTIFICATION_ERROR_FROM_DB;
46         }
47
48         ret = sqlite3_step(stmt);
49         if (ret == SQLITE_ROW) {
50                 result = sqlite3_column_int(stmt, 0);
51         } else {
52                 result = 0;
53         }
54
55         NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
56                           query, result);
57
58         sqlite3_finalize(stmt);
59
60         if (result > 0) {
61                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
62         }
63
64         return NOTIFICATION_ERROR_NONE;
65 }
66
67 notification_error_e notification_group_set_badge(const char *pkgname,
68                                                   int group_id, int count)
69 {
70         sqlite3 *db;
71         sqlite3_stmt *stmt = NULL;
72         char query[NOTIFICATION_QUERY_MAX] = { 0, };
73         int ret = 0;
74         int result = NOTIFICATION_ERROR_NONE;
75
76         /* Open DB */
77         db = notification_db_open(DBPATH);
78
79         /* Check pkgname & group_id */
80         ret = _notification_group_check_data_inserted(pkgname, group_id, db);
81
82         /* Make query */
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);
88
89         } else {
90                 /* Update if exist */
91                 snprintf(query, sizeof(query), "update noti_group_data "
92                          "set badge = %d "
93                          "where caller_pkgname = '%s' and group_id = %d",
94                          count, pkgname, group_id);
95         }
96
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,
101                                  sqlite3_errmsg(db));
102                 if (stmt) {
103                         sqlite3_finalize(stmt);
104                 }
105
106                 if (db) {
107                         notification_db_close(&db);
108                 }
109                 return NOTIFICATION_ERROR_FROM_DB;
110         }
111
112         ret = sqlite3_step(stmt);
113         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
114                 result = NOTIFICATION_ERROR_NONE;
115         } else {
116                 result = NOTIFICATION_ERROR_FROM_DB;
117         }
118
119         if (stmt) {
120                 sqlite3_finalize(stmt);
121         }
122
123         /* Close DB */
124         if (db) {
125                 notification_db_close(&db);
126         }
127
128         return result;
129 }
130
131 notification_error_e notification_group_get_badge(const char *pkgname,
132                                                   int group_id, int *count)
133 {
134         sqlite3 *db;
135         sqlite3_stmt *stmt = NULL;
136         char query[NOTIFICATION_QUERY_MAX] = { 0, };
137         int ret = 0;
138         int col = 0;
139
140         /* Open DB */
141         db = notification_db_open(DBPATH);
142
143         /* Make query */
144         if (group_id == NOTIFICATION_GROUP_ID_NONE) {
145                 /* Check Group id None is exist */
146                 ret =
147                     _notification_group_check_data_inserted(pkgname, group_id,
148                                                             db);
149
150                 if (ret == NOTIFICATION_ERROR_NONE) {
151                         /* Get all of pkgname count if none group id is not exist */
152                         snprintf(query, sizeof(query),
153                                  "select sum(badge) "
154                                  "from noti_group_data "
155                                  "where caller_pkgname = '%s'", pkgname);
156                 } else {
157                         /* Get none group id count */
158                         snprintf(query, sizeof(query),
159                                  "select badge "
160                                  "from noti_group_data "
161                                  "where caller_pkgname = '%s' and group_id = %d",
162                                  pkgname, group_id);
163                 }
164         } else {
165                 snprintf(query, sizeof(query),
166                          "select badge "
167                          "from noti_group_data "
168                          "where caller_pkgname = '%s' and group_id = %d",
169                          pkgname, group_id);
170         }
171
172         NOTIFICATION_INFO("Get badge : query[%s]", query);
173
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,
178                                  sqlite3_errmsg(db));
179
180                 return NOTIFICATION_ERROR_FROM_DB;
181         }
182
183         ret = sqlite3_step(stmt);
184         if (ret == SQLITE_ROW) {
185                 *count = sqlite3_column_int(stmt, col++);
186         }
187
188         sqlite3_finalize(stmt);
189
190         // db close
191         if (db) {
192                 notification_db_close(&db);
193         }
194
195         return NOTIFICATION_ERROR_NONE;
196 }