Move notification_get_text_input_max_length to internal
[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         db = notification_db_open(DBPATH);
74         if (!db)
75                 return get_last_result();
76
77         ret = _notification_group_check_data_inserted(pkgname, 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_pkgname, group_id, badge, content, loc_content) values ("
83                          "'%s', %d, %d, '', '')", pkgname, 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_pkgname = '%s' and group_id = %d",
90                          count, pkgname, group_id);
91         }
92
93         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
94         if (ret != SQLITE_OK) {
95                 NOTIFICATION_ERR("Insert Query : %s", query);
96                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
97                                  sqlite3_errmsg(db));
98                 if (stmt)
99                         sqlite3_finalize(stmt);
100
101                 if (db)
102                         notification_db_close(&db);
103
104                 return NOTIFICATION_ERROR_FROM_DB;
105         }
106
107         ret = sqlite3_step(stmt);
108         if (ret == SQLITE_OK || ret == SQLITE_DONE)
109                 result = NOTIFICATION_ERROR_NONE;
110         else
111                 result = NOTIFICATION_ERROR_FROM_DB;
112
113         if (stmt)
114                 sqlite3_finalize(stmt);
115
116         if (db)
117                 notification_db_close(&db);
118
119         return result;
120 }
121 /* LCOV_EXCL_STOP */
122
123 /* LCOV_EXCL_START */
124 int notification_group_get_badge(const char *pkgname,
125                                                   int group_id, int *count)
126 {
127         sqlite3 *db;
128         sqlite3_stmt *stmt = NULL;
129         char query[NOTIFICATION_QUERY_MAX] = { 0, };
130         int ret = 0;
131         int col = 0;
132
133         db = notification_db_open(DBPATH);
134         if (!db)
135                 return get_last_result();
136
137         if (group_id == NOTIFICATION_GROUP_ID_NONE) {
138                 ret = _notification_group_check_data_inserted(pkgname, group_id, db);
139                 if (ret == NOTIFICATION_ERROR_NONE)
140                         /* Get all of pkgname count if none group id is not exist */
141                         snprintf(query, sizeof(query),
142                                  "select sum(badge) "
143                                  "from noti_group_data "
144                                  "where caller_pkgname = '%s'", pkgname);
145                 else
146                         /* Get none group id count */
147                         snprintf(query, sizeof(query),
148                                  "select badge "
149                                  "from noti_group_data "
150                                  "where caller_pkgname = '%s' and group_id = %d",
151                                  pkgname, group_id);
152         } else {
153                 snprintf(query, sizeof(query),
154                          "select badge "
155                          "from noti_group_data "
156                          "where caller_pkgname = '%s' and group_id = %d",
157                          pkgname, group_id);
158         }
159
160         NOTIFICATION_INFO("Get badge : query[%s]", query);
161
162         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
163         if (ret != SQLITE_OK) {
164                 NOTIFICATION_ERR("Select Query : %s", query);
165                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
166                                  sqlite3_errmsg(db));
167                 if (db)
168                         notification_db_close(&db);
169
170                 return NOTIFICATION_ERROR_FROM_DB;
171         }
172
173         ret = sqlite3_step(stmt);
174         if (ret == SQLITE_ROW)
175                 *count = sqlite3_column_int(stmt, col++);
176
177         sqlite3_finalize(stmt);
178
179         if (db)
180                 notification_db_close(&db);
181
182         return NOTIFICATION_ERROR_NONE;
183 }
184 /* LCOV_EXCL_STOP */