Release version 0.4.19
[platform/core/api/notification.git] / src / notification_group.c
1 /*
2  * Copyright (c) 2000 - 2017 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 *app_id,
27                                                    int group_id, sqlite3 *db)
28 {
29         sqlite3_stmt *stmt = NULL;
30         char query[NOTIFICATION_QUERY_MAX] = { 0, };
31         int ret;
32         int result;
33
34         snprintf(query, sizeof(query),
35                  "select count(*) from noti_group_data where caller_app_id = '%s' and group_id = %d",
36                  app_id, group_id);
37
38         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
39         if (ret != SQLITE_OK) {
40                 ERR("Get count DB err(%d) : %s", ret, 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         sqlite3_finalize(stmt);
52
53         if (result > 0)
54                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
55
56         INFO("Check Data Inserted appid[%s] group_id[%d] result[%d]",
57                           app_id, group_id, result);
58
59         return NOTIFICATION_ERROR_NONE;
60 }
61 /* LCOV_EXCL_STOP */
62
63 /* LCOV_EXCL_START */
64 int notification_group_set_badge(const char *app_id,
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;
71         int result;
72
73         db = notification_db_open();
74         if (!db)
75                 return get_last_result();
76
77         ret = _notification_group_check_data_inserted(app_id, group_id, db);
78
79         if (ret == NOTIFICATION_ERROR_NONE) {
80                 /* Insert if does not exist */
81                 snprintf(query, sizeof(query), "insert into noti_group_data ("
82                          "caller_app_id, group_id, badge, content, loc_content) values ("
83                          "'%s', %d, %d, '', '')", app_id, group_id, count);
84
85         } else {
86                 /* Update if exist */
87                 snprintf(query, sizeof(query), "update noti_group_data "
88                          "set badge = %d "
89                          "where caller_app_id = '%s' and group_id = %d",
90                          count, app_id, group_id);
91         }
92
93         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
94         if (ret != SQLITE_OK) {
95                 ERR("Failed to insert data app_id[%s] err[%d][%s]",
96                                  app_id, ret, sqlite3_errmsg(db));
97                 if (stmt)
98                         sqlite3_finalize(stmt);
99
100                 if (db)
101                         notification_db_close(&db);
102
103                 return NOTIFICATION_ERROR_FROM_DB;
104         }
105
106         ret = sqlite3_step(stmt);
107         if (ret == SQLITE_OK || ret == SQLITE_DONE)
108                 result = NOTIFICATION_ERROR_NONE;
109         else
110                 result = NOTIFICATION_ERROR_FROM_DB;
111
112         sqlite3_finalize(stmt);
113
114         if (db)
115                 notification_db_close(&db);
116
117         return result;
118 }
119 /* LCOV_EXCL_STOP */
120
121 /* LCOV_EXCL_START */
122 int notification_group_get_badge(const char *app_id,
123                                                   int group_id, int *count)
124 {
125         sqlite3 *db;
126         sqlite3_stmt *stmt = NULL;
127         char query[NOTIFICATION_QUERY_MAX] = { 0, };
128         int ret;
129         int col = 0;
130
131         db = notification_db_open();
132         if (!db)
133                 return get_last_result();
134
135         if (group_id == NOTIFICATION_GROUP_ID_NONE) {
136                 ret = _notification_group_check_data_inserted(app_id, group_id, db);
137                 if (ret == NOTIFICATION_ERROR_NONE)
138                         /* Get all of app_id count if none group id is not exist */
139                         snprintf(query, sizeof(query),
140                                  "select sum(badge) "
141                                  "from noti_group_data "
142                                  "where caller_app_id = '%s'", app_id);
143                 else
144                         /* Get none group id count */
145                         snprintf(query, sizeof(query),
146                                  "select badge "
147                                  "from noti_group_data "
148                                  "where caller_app_id = '%s' and group_id = %d",
149                                  app_id, group_id);
150         } else {
151                 snprintf(query, sizeof(query),
152                          "select badge "
153                          "from noti_group_data "
154                          "where caller_app_id = '%s' and group_id = %d",
155                          app_id, group_id);
156         }
157
158         INFO("Get badge : query[%s]", query);
159
160         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
161         if (ret != SQLITE_OK) {
162                 ERR("Failed to insert data app_id[%s] err[%d][%s]",
163                                 app_id, ret, sqlite3_errmsg(db));
164                 if (db)
165                         notification_db_close(&db);
166
167                 return NOTIFICATION_ERROR_FROM_DB;
168         }
169
170         ret = sqlite3_step(stmt);
171         if (ret == SQLITE_ROW)
172                 *count = sqlite3_column_int(stmt, col++);
173
174         sqlite3_finalize(stmt);
175
176         if (db)
177                 notification_db_close(&db);
178
179         return NOTIFICATION_ERROR_NONE;
180 }
181 /* LCOV_EXCL_STOP */