Give detail license information
[platform/core/api/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>
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 int 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         if (!db) {
79                 return get_last_result();
80         }
81
82         /* Check pkgname & group_id */
83         ret = _notification_group_check_data_inserted(pkgname, group_id, db);
84
85         /* Make query */
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);
91
92         } else {
93                 /* Update if exist */
94                 snprintf(query, sizeof(query), "update noti_group_data "
95                          "set badge = %d "
96                          "where caller_pkgname = '%s' and group_id = %d",
97                          count, pkgname, group_id);
98         }
99
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,
104                                  sqlite3_errmsg(db));
105                 if (stmt) {
106                         sqlite3_finalize(stmt);
107                 }
108
109                 if (db) {
110                         notification_db_close(&db);
111                 }
112                 return NOTIFICATION_ERROR_FROM_DB;
113         }
114
115         ret = sqlite3_step(stmt);
116         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
117                 result = NOTIFICATION_ERROR_NONE;
118         } else {
119                 result = NOTIFICATION_ERROR_FROM_DB;
120         }
121
122         if (stmt) {
123                 sqlite3_finalize(stmt);
124         }
125
126         /* Close DB */
127         if (db) {
128                 notification_db_close(&db);
129         }
130
131         return result;
132 }
133
134 int notification_group_get_badge(const char *pkgname,
135                                                   int group_id, int *count)
136 {
137         sqlite3 *db;
138         sqlite3_stmt *stmt = NULL;
139         char query[NOTIFICATION_QUERY_MAX] = { 0, };
140         int ret = 0;
141         int col = 0;
142
143         /* Open DB */
144         db = notification_db_open(DBPATH);
145         if (!db) {
146                 return get_last_result();
147         }
148
149         /* Make query */
150         if (group_id == NOTIFICATION_GROUP_ID_NONE) {
151                 /* Check Group id None is exist */
152                 ret =
153                     _notification_group_check_data_inserted(pkgname, group_id,
154                                                             db);
155
156                 if (ret == NOTIFICATION_ERROR_NONE) {
157                         /* Get all of pkgname count if none group id is not exist */
158                         snprintf(query, sizeof(query),
159                                  "select sum(badge) "
160                                  "from noti_group_data "
161                                  "where caller_pkgname = '%s'", pkgname);
162                 } else {
163                         /* Get none group id count */
164                         snprintf(query, sizeof(query),
165                                  "select badge "
166                                  "from noti_group_data "
167                                  "where caller_pkgname = '%s' and group_id = %d",
168                                  pkgname, group_id);
169                 }
170         } else {
171                 snprintf(query, sizeof(query),
172                          "select badge "
173                          "from noti_group_data "
174                          "where caller_pkgname = '%s' and group_id = %d",
175                          pkgname, group_id);
176         }
177
178         NOTIFICATION_INFO("Get badge : query[%s]", query);
179
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,
184                                  sqlite3_errmsg(db));
185
186                 return NOTIFICATION_ERROR_FROM_DB;
187         }
188
189         ret = sqlite3_step(stmt);
190         if (ret == SQLITE_ROW) {
191                 *count = sqlite3_column_int(stmt, col++);
192         }
193
194         sqlite3_finalize(stmt);
195
196         // db close
197         if (db) {
198                 notification_db_close(&db);
199         }
200
201         return NOTIFICATION_ERROR_NONE;
202 }