Exclude deprecated API from coverage measurement
[platform/core/api/notification.git] / src / notification_group.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <notification_debug.h>
22 #include <notification_group.h>
23 #include <notification_db.h>
24
25 /* LCOV_EXCL_START */
26 static int _notification_group_check_data_inserted(const char *pkgname,
27                                                    int group_id, sqlite3 *db)
28 {
29         sqlite3_stmt *stmt = NULL;
30         char query[NOTIFICATION_QUERY_MAX] = { 0, };
31         int ret = NOTIFICATION_ERROR_NONE, result = 0;
32
33         snprintf(query, sizeof(query),
34                  "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
35                  pkgname, group_id);
36
37         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
38         if (ret != SQLITE_OK) {
39                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
40                                  sqlite3_errmsg(db));
41                 return NOTIFICATION_ERROR_FROM_DB;
42         }
43
44         ret = sqlite3_step(stmt);
45         if (ret == SQLITE_ROW)
46                 result = sqlite3_column_int(stmt, 0);
47         else
48                 result = 0;
49
50
51         NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
52                           query, result);
53
54         sqlite3_finalize(stmt);
55
56         if (result > 0)
57                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
58
59         return NOTIFICATION_ERROR_NONE;
60 }
61 /* LCOV_EXCL_STOP */
62
63 /* LCOV_EXCL_START */
64 int notification_group_set_badge(const char *pkgname,
65                                                   int group_id, int count)
66 {
67         sqlite3 *db;
68         sqlite3_stmt *stmt = NULL;
69         char query[NOTIFICATION_QUERY_MAX] = { 0, };
70         int ret = 0;
71         int result = NOTIFICATION_ERROR_NONE;
72
73         /* Open DB */
74         db = notification_db_open(DBPATH);
75         if (!db)
76                 return get_last_result();
77
78         /* Check pkgname & group_id */
79         ret = _notification_group_check_data_inserted(pkgname, group_id, db);
80
81         /* Make query */
82         if (ret == NOTIFICATION_ERROR_NONE) {
83                 /* Insert if does not exist */
84                 snprintf(query, sizeof(query), "insert into noti_group_data ("
85                          "caller_pkgname, group_id, badge, content, loc_content) values ("
86                          "'%s', %d, %d, '', '')", pkgname, group_id, count);
87
88         } else {
89                 /* Update if exist */
90                 snprintf(query, sizeof(query), "update noti_group_data "
91                          "set badge = %d "
92                          "where caller_pkgname = '%s' and group_id = %d",
93                          count, pkgname, group_id);
94         }
95
96         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
97         if (ret != SQLITE_OK) {
98                 NOTIFICATION_ERR("Insert Query : %s", query);
99                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
100                                  sqlite3_errmsg(db));
101                 if (stmt)
102                         sqlite3_finalize(stmt);
103
104                 if (db)
105                         notification_db_close(&db);
106
107                 return NOTIFICATION_ERROR_FROM_DB;
108         }
109
110         ret = sqlite3_step(stmt);
111         if (ret == SQLITE_OK || ret == SQLITE_DONE)
112                 result = NOTIFICATION_ERROR_NONE;
113         else
114                 result = NOTIFICATION_ERROR_FROM_DB;
115
116         if (stmt)
117                 sqlite3_finalize(stmt);
118
119         /* Close DB */
120         if (db)
121                 notification_db_close(&db);
122
123         return result;
124 }
125 /* LCOV_EXCL_STOP */
126
127 /* LCOV_EXCL_START */
128 int notification_group_get_badge(const char *pkgname,
129                                                   int group_id, int *count)
130 {
131         sqlite3 *db;
132         sqlite3_stmt *stmt = NULL;
133         char query[NOTIFICATION_QUERY_MAX] = { 0, };
134         int ret = 0;
135         int col = 0;
136
137         /* Open DB */
138         db = notification_db_open(DBPATH);
139         if (!db)
140                 return get_last_result();
141
142         /* Make query */
143         if (group_id == NOTIFICATION_GROUP_ID_NONE) {
144                 /* Check Group id None is exist */
145                 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
146
147                 if (ret == NOTIFICATION_ERROR_NONE)
148                         /* Get all of pkgname count if none group id is not exist */
149                         snprintf(query, sizeof(query),
150                                  "select sum(badge) "
151                                  "from noti_group_data "
152                                  "where caller_pkgname = '%s'", pkgname);
153                 else
154                         /* Get none group id count */
155                         snprintf(query, sizeof(query),
156                                  "select badge "
157                                  "from noti_group_data "
158                                  "where caller_pkgname = '%s' and group_id = %d",
159                                  pkgname, group_id);
160
161         } else {
162                 snprintf(query, sizeof(query),
163                          "select badge "
164                          "from noti_group_data "
165                          "where caller_pkgname = '%s' and group_id = %d",
166                          pkgname, group_id);
167         }
168
169         NOTIFICATION_INFO("Get badge : query[%s]", query);
170
171         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
172         if (ret != SQLITE_OK) {
173                 NOTIFICATION_ERR("Select Query : %s", query);
174                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
175                                  sqlite3_errmsg(db));
176                 if (db)
177                         notification_db_close(&db);
178
179                 return NOTIFICATION_ERROR_FROM_DB;
180         }
181
182         ret = sqlite3_step(stmt);
183         if (ret == SQLITE_ROW)
184                 *count = sqlite3_column_int(stmt, col++);
185
186         sqlite3_finalize(stmt);
187
188         /* db close */
189         if (db)
190                 notification_db_close(&db);
191
192         return NOTIFICATION_ERROR_NONE;
193 }
194 /* LCOV_EXCL_STOP */