c310f8e584d393f0375b01fbe5d853f511e464f9
[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>
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_bind_query(sqlite3_stmt * stmt, const char *name,
31                                           const char *str)
32 {
33         int ret = 0;
34         int index = 0;
35
36         index = sqlite3_bind_parameter_index(stmt, name);
37         if (index == 0) {
38                 NOTIFICATION_ERR("Insert : invalid column name");
39                 return NOTIFICATION_ERROR_FROM_DB;
40         }
41
42         ret =
43             sqlite3_bind_text(stmt, index, NOTIFICATION_CHECK_STR(str), -1,
44                               SQLITE_STATIC);
45         if (ret != SQLITE_OK) {
46                 NOTIFICATION_ERR("Insert text : %s",
47                                  NOTIFICATION_CHECK_STR(str));
48                 return NOTIFICATION_ERROR_FROM_DB;
49         }
50
51         return NOTIFICATION_ERROR_NONE;
52 }
53
54 static int _notification_group_check_data_inserted(const char *pkgname,
55                                                    int group_id, sqlite3 * db)
56 {
57         sqlite3_stmt *stmt = NULL;
58         char query[NOTIFICATION_QUERY_MAX] = { 0, };
59         int ret = NOTIFICATION_ERROR_NONE, result = 0;
60
61         snprintf(query, sizeof(query),
62                  "select count(*) from noti_group_data where caller_pkgname = '%s' and group_id = %d",
63                  pkgname, group_id);
64
65         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
66         if (ret != SQLITE_OK) {
67                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
68                                  sqlite3_errmsg(db));
69                 return NOTIFICATION_ERROR_FROM_DB;
70         }
71
72         ret = sqlite3_step(stmt);
73         if (ret == SQLITE_ROW) {
74                 result = sqlite3_column_int(stmt, 0);
75         } else {
76                 result = 0;
77         }
78
79         NOTIFICATION_INFO("Check Data Inserted : query[%s], result : [%d]",
80                           query, result);
81
82         sqlite3_finalize(stmt);
83
84         if (result > 0) {
85                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
86         }
87
88         return NOTIFICATION_ERROR_NONE;
89 }
90
91 notification_error_e notification_group_set_title(const char *pkgname,
92                                                   int group_id,
93                                                   const char *title,
94                                                   const char *loc_title,
95                                                   notification_count_display_type_e count_display)
96 {
97         sqlite3 *db;
98         sqlite3_stmt *stmt = NULL;
99         char query[NOTIFICATION_QUERY_MAX] = { 0, };
100         int ret = 0;
101         int result = NOTIFICATION_ERROR_NONE;
102
103         // db open
104         db = notification_db_open(DBPATH);
105
106         // Check pkgname & group_id
107         ret = _notification_group_check_data_inserted(pkgname, group_id, db);
108         if (ret == NOTIFICATION_ERROR_NONE) {
109                 // not exist -> insert
110                 snprintf(query, sizeof(query), "insert into noti_group_data ("
111                          "caller_pkgname, group_id, unread_count, title, loc_title, count_display_title) values ("
112                          "'%s', %d, 0, $title, $loc_title, %d)",
113                          pkgname, group_id, count_display);
114
115         } else {
116                 // exist -> update
117                 // not exist -> insert
118                 snprintf(query, sizeof(query), "update noti_group_data "
119                          "set title = $title, loc_title = $loc_title, count_display_title = %d "
120                          "where caller_pkgname = '%s' and group_id = %d",
121                          count_display, pkgname, group_id);
122         }
123
124         // insert
125         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
126         if (ret != SQLITE_OK) {
127                 NOTIFICATION_ERR("Insert Query : %s", query);
128                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
129                                  sqlite3_errmsg(db));
130                 if (stmt) {
131                         sqlite3_finalize(stmt);
132                 }
133
134                 if (db) {
135                         notification_db_close(&db);
136                 }
137                 return NOTIFICATION_ERROR_FROM_DB;
138         }
139
140         ret = _notification_group_bind_query(stmt, "$title", title);
141         if (ret != NOTIFICATION_ERROR_NONE) {
142                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
143                 return ret;
144         }
145
146         ret = _notification_group_bind_query(stmt, "$loc_title", loc_title);
147         if (ret != NOTIFICATION_ERROR_NONE) {
148                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
149                 return ret;
150         }
151
152         ret = sqlite3_step(stmt);
153         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
154                 result = NOTIFICATION_ERROR_NONE;
155         } else {
156                 result = NOTIFICATION_ERROR_FROM_DB;
157         }
158
159         if (stmt) {
160                 sqlite3_finalize(stmt);
161         }
162
163         if (db) {
164                 notification_db_close(&db);
165         }
166
167         return result;
168 }
169
170 notification_error_e notification_group_get_title(const char *pkgname,
171                                                   int group_id,
172                                                   char **ret_title,
173                                                   char **ret_loc_title,
174                                                   notification_count_display_type_e *count_display)
175 {
176         sqlite3 *db;
177         sqlite3_stmt *stmt = NULL;
178         char query[NOTIFICATION_QUERY_MAX] = { 0, };
179         int ret = 0;
180         int col = 0;
181
182         // db open
183         db = notification_db_open(DBPATH);
184
185         snprintf(query, sizeof(query), "select "
186                  "title, loc_title, count_display_title "
187                  "from noti_group_data "
188                  "where caller_pkgname = '%s' and group_id = %d",
189                  pkgname, group_id);
190
191         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
192         if (ret != SQLITE_OK) {
193                 NOTIFICATION_ERR("Select Query : %s", query);
194                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
195                                  sqlite3_errmsg(db));
196
197                 return NOTIFICATION_ERROR_FROM_DB;
198         }
199
200         ret = sqlite3_step(stmt);
201         if (ret == SQLITE_ROW) {
202                 *ret_title = notification_db_column_text(stmt, col++);
203                 *ret_loc_title = notification_db_column_text(stmt, col++);
204                 *count_display = sqlite3_column_int(stmt, col++);
205         }
206
207         sqlite3_finalize(stmt);
208
209         // db close
210         if (db) {
211                 notification_db_close(&db);
212         }
213
214         return NOTIFICATION_ERROR_NONE;
215 }
216
217 notification_error_e notification_group_set_content(const char *pkgname,
218                                                     int group_id,
219                                                     const char *content,
220                                                     const char *loc_content,
221                                                     notification_count_display_type_e count_display)
222 {
223         sqlite3 *db;
224         sqlite3_stmt *stmt = NULL;
225         char query[NOTIFICATION_QUERY_MAX] = { 0, };
226         int ret = 0;
227         int result = NOTIFICATION_ERROR_NONE;
228
229         // db open
230         db = notification_db_open(DBPATH);
231
232         // Check pkgname & group_id
233         ret = _notification_group_check_data_inserted(pkgname, group_id, db);
234         if (ret == NOTIFICATION_ERROR_NONE) {
235                 // not exist -> insert
236                 snprintf(query, sizeof(query), "insert into noti_group_data ("
237                          "caller_pkgname, group_id, unread_count, content, loc_content, count_display_content) values ("
238                          "'%s', %d, 0, $content, $loc_content, %d)",
239                          pkgname, group_id, count_display);
240 //              NOTIFICATION_INFO("Insert Query : %s", query);
241         } else {
242                 // exist -> update
243                 // not exist -> insert
244                 snprintf(query, sizeof(query), "update noti_group_data "
245                          "set content = $content, loc_content = $loc_content, count_display_content = %d "
246                          "where caller_pkgname = '%s' and group_id = %d",
247                          count_display, pkgname, group_id);
248 //              NOTIFICATION_INFO("Insert Query : %s", query);
249         }
250
251         // insert
252         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
253         if (ret != SQLITE_OK) {
254                 NOTIFICATION_ERR("Insert Query : %s", query);
255                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
256                                  sqlite3_errmsg(db));
257                 if (stmt) {
258                         sqlite3_finalize(stmt);
259                 }
260
261                 if (db) {
262                         notification_db_close(&db);
263                 }
264                 return NOTIFICATION_ERROR_FROM_DB;
265         }
266
267         ret = _notification_group_bind_query(stmt, "$content", content);
268         if (ret != NOTIFICATION_ERROR_NONE) {
269                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
270                 return ret;
271         }
272
273         ret = _notification_group_bind_query(stmt, "$loc_content", loc_content);
274         if (ret != NOTIFICATION_ERROR_NONE) {
275                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
276                 return ret;
277         }
278
279         ret = sqlite3_step(stmt);
280         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
281                 result = NOTIFICATION_ERROR_NONE;
282         } else {
283                 result = NOTIFICATION_ERROR_FROM_DB;
284         }
285
286         if (stmt) {
287                 sqlite3_finalize(stmt);
288         }
289
290         if (db) {
291                 notification_db_close(&db);
292         }
293
294         return result;
295 }
296
297 notification_error_e notification_group_get_content(const char *pkgname,
298                                                     int group_id,
299                                                     char **ret_content,
300                                                     char **ret_loc_content,
301                                                     notification_count_display_type_e *count_display)
302 {
303         sqlite3 *db;
304         sqlite3_stmt *stmt = NULL;
305         char query[NOTIFICATION_QUERY_MAX] = { 0, };
306         int ret = 0;
307         int col = 0;
308
309         // db open
310         db = notification_db_open(DBPATH);
311
312         snprintf(query, sizeof(query), "select "
313                  "content, loc_content, count_display_content "
314                  "from noti_group_data "
315                  "where caller_pkgname = '%s' and group_id = %d",
316                  pkgname, group_id);
317
318         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
319         if (ret != SQLITE_OK) {
320                 NOTIFICATION_ERR("Select Query : %s", query);
321                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
322                                  sqlite3_errmsg(db));
323
324                 return NOTIFICATION_ERROR_FROM_DB;
325         }
326
327         ret = sqlite3_step(stmt);
328         if (ret == SQLITE_ROW) {
329                 *ret_content = notification_db_column_text(stmt, col++);
330                 *ret_loc_content = notification_db_column_text(stmt, col++);
331                 *count_display = sqlite3_column_int(stmt, col++);
332         }
333
334         sqlite3_finalize(stmt);
335
336         // db close
337         if (db) {
338                 notification_db_close(&db);
339         }
340
341         return NOTIFICATION_ERROR_NONE;
342 }
343
344 notification_error_e notification_group_set_badge(const char *pkgname,
345                                                   int group_id, int count)
346 {
347         sqlite3 *db;
348         sqlite3_stmt *stmt = NULL;
349         char query[NOTIFICATION_QUERY_MAX] = { 0, };
350         int ret = 0;
351         int result = NOTIFICATION_ERROR_NONE;
352
353         /* Open DB */
354         db = notification_db_open(DBPATH);
355
356         /* Check pkgname & group_id */
357         ret = _notification_group_check_data_inserted(pkgname, group_id, db);
358
359         /* Make query */
360         if (ret == NOTIFICATION_ERROR_NONE) {
361                 /* Insert if does not exist */
362                 snprintf(query, sizeof(query), "insert into noti_group_data ("
363                          "caller_pkgname, group_id, badge, content, loc_content) values ("
364                          "'%s', %d, %d, '', '')", pkgname, group_id, count);
365
366         } else {
367                 /* Update if exist */
368                 snprintf(query, sizeof(query), "update noti_group_data "
369                          "set badge = %d "
370                          "where caller_pkgname = '%s' and group_id = %d",
371                          count, pkgname, group_id);
372         }
373
374         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
375         if (ret != SQLITE_OK) {
376                 NOTIFICATION_ERR("Insert Query : %s", query);
377                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
378                                  sqlite3_errmsg(db));
379                 if (stmt) {
380                         sqlite3_finalize(stmt);
381                 }
382
383                 if (db) {
384                         notification_db_close(&db);
385                 }
386                 return NOTIFICATION_ERROR_FROM_DB;
387         }
388
389         ret = sqlite3_step(stmt);
390         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
391                 result = NOTIFICATION_ERROR_NONE;
392         } else {
393                 result = NOTIFICATION_ERROR_FROM_DB;
394         }
395
396         if (stmt) {
397                 sqlite3_finalize(stmt);
398         }
399
400         /* Close DB */
401         if (db) {
402                 notification_db_close(&db);
403         }
404
405         return result;
406 }
407
408 notification_error_e notification_group_get_badge(const char *pkgname,
409                                                   int group_id, int *count)
410 {
411         sqlite3 *db;
412         sqlite3_stmt *stmt = NULL;
413         char query[NOTIFICATION_QUERY_MAX] = { 0, };
414         int ret = 0;
415         int col = 0;
416
417         /* Open DB */
418         db = notification_db_open(DBPATH);
419
420         /* Make query */
421         if (group_id == NOTIFICATION_GROUP_ID_NONE) {
422                 /* Check Group id None is exist */
423                 ret =
424                     _notification_group_check_data_inserted(pkgname, group_id,
425                                                             db);
426
427                 if (ret == NOTIFICATION_ERROR_NONE) {
428                         /* Get all of pkgname count if none group id is not exist */
429                         snprintf(query, sizeof(query),
430                                  "select sum(badge) "
431                                  "from noti_group_data "
432                                  "where caller_pkgname = '%s'", pkgname);
433                 } else {
434                         /* Get none group id count */
435                         snprintf(query, sizeof(query),
436                                  "select badge "
437                                  "from noti_group_data "
438                                  "where caller_pkgname = '%s' and group_id = %d",
439                                  pkgname, group_id);
440                 }
441         } else {
442                 snprintf(query, sizeof(query),
443                          "select badge "
444                          "from noti_group_data "
445                          "where caller_pkgname = '%s' and group_id = %d",
446                          pkgname, group_id);
447         }
448
449         NOTIFICATION_INFO("Get badge : query[%s]", query);
450
451         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
452         if (ret != SQLITE_OK) {
453                 NOTIFICATION_ERR("Select Query : %s", query);
454                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
455                                  sqlite3_errmsg(db));
456
457                 return NOTIFICATION_ERROR_FROM_DB;
458         }
459
460         ret = sqlite3_step(stmt);
461         if (ret == SQLITE_ROW) {
462                 *count = sqlite3_column_int(stmt, col++);
463         }
464
465         sqlite3_finalize(stmt);
466
467         // db close
468         if (db) {
469                 notification_db_close(&db);
470         }
471
472         return NOTIFICATION_ERROR_NONE;
473 }