Release notification lib for Tizen2.0 beta(tagging)
[apps/home/notification.git] / src / notification_noti.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>, Youngsub Ko <ys4610.ko@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 <string.h>
24 #include <stdlib.h>
25
26 #include <vconf.h>
27
28 #include <notification.h>
29 #include <notification_db.h>
30 #include <notification_noti.h>
31 #include <notification_debug.h>
32 #include <notification_internal.h>
33
34 static int _notification_noti_bind_query(sqlite3_stmt * stmt, const char *name,
35                                          const char *str)
36 {
37         int ret = 0;
38         int index = 0;
39
40         index = sqlite3_bind_parameter_index(stmt, name);
41         if (index == 0) {
42                 NOTIFICATION_ERR("Insert : invalid column name");
43                 return NOTIFICATION_ERROR_FROM_DB;
44         }
45
46         ret =
47             sqlite3_bind_text(stmt, index, NOTIFICATION_CHECK_STR(str), -1,
48                               SQLITE_STATIC);
49         if (ret != SQLITE_OK) {
50                 NOTIFICATION_ERR("Insert text : %s",
51                                  NOTIFICATION_CHECK_STR(str));
52                 return NOTIFICATION_ERROR_FROM_DB;
53         }
54
55         return NOTIFICATION_ERROR_NONE;
56 }
57
58 static int _notification_noti_check_priv_id(notification_h noti, sqlite3 * db)
59 {
60         sqlite3_stmt *stmt = NULL;
61         char query[NOTIFICATION_QUERY_MAX] = { 0, };
62         int ret = NOTIFICATION_ERROR_NONE, result = 0;
63
64         /* Make query to check priv_id exist */
65         snprintf(query, sizeof(query),
66                  "select count(*) from noti_list where caller_pkgname = '%s' and priv_id = %d",
67                  noti->caller_pkgname, noti->priv_id);
68
69         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
70         if (ret != SQLITE_OK) {
71                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
72                                  sqlite3_errmsg(db));
73                 return NOTIFICATION_ERROR_FROM_DB;
74         }
75
76         ret = sqlite3_step(stmt);
77         if (ret == SQLITE_ROW) {
78                 result = sqlite3_column_int(stmt, 0);
79         } else {
80                 result = 0;
81         }
82
83         sqlite3_finalize(stmt);
84
85         /* If result > 0, there is priv_id in DB */
86         if (result > 0) {
87                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
88         }
89
90         return NOTIFICATION_ERROR_NONE;
91 }
92
93 static int _notification_noti_get_priv_id(notification_h noti, sqlite3 * db)
94 {
95         sqlite3_stmt *stmt = NULL;
96         char query[NOTIFICATION_QUERY_MAX] = { 0, };
97         int ret = NOTIFICATION_ERROR_NONE, result = 0;
98
99         /* Make query to get max priv_id */
100         snprintf(query, sizeof(query),
101                  "select max(priv_id) from noti_list where caller_pkgname = '%s'",
102                  noti->caller_pkgname);
103
104         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
105         if (ret != SQLITE_OK) {
106                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
107                                  sqlite3_errmsg(db));
108                 return NOTIFICATION_ERROR_FROM_DB;
109         }
110
111         ret = sqlite3_step(stmt);
112         if (ret == SQLITE_ROW) {
113                 result = sqlite3_column_int(stmt, 0);
114         } else {
115                 result = 0;
116         }
117
118         sqlite3_finalize(stmt);
119
120         if (result < 0) {
121                 return NOTIFICATION_ERROR_FROM_DB;
122         }
123
124         /* Increase result(max priv_id value) for next priv_id */
125         noti->priv_id = result + 1;
126
127         return NOTIFICATION_ERROR_NONE;
128 }
129
130 static int _notification_noti_get_internal_group_id_by_priv_id(const char *pkgname,
131                                                                int priv_id,
132                                                                sqlite3 * db)
133 {
134         sqlite3_stmt *stmt = NULL;
135         char query[NOTIFICATION_QUERY_MAX] = { 0, };
136         int ret = NOTIFICATION_ERROR_NONE, result = 0;
137
138         snprintf(query, sizeof(query),
139                  "select internal_group_id from noti_list where caller_pkgname = '%s' and priv_id = %d",
140                  pkgname, priv_id);
141
142         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
143         if (ret != SQLITE_OK) {
144                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
145                                  sqlite3_errmsg(db));
146                 return NOTIFICATION_ERROR_FROM_DB;
147         }
148
149         ret = sqlite3_step(stmt);
150         if (ret == SQLITE_ROW) {
151                 result = sqlite3_column_int(stmt, 0);
152         } else {
153                 result = 0;
154         }
155
156         sqlite3_finalize(stmt);
157
158         return result;
159 }
160
161 static int _notification_noti_get_max_internal_group_id(notification_h noti,
162                                                         sqlite3 * db)
163 {
164         sqlite3_stmt *stmt = NULL;
165         char query[NOTIFICATION_QUERY_MAX] = { 0, };
166         int ret = NOTIFICATION_ERROR_NONE, result = 0;
167
168         /* Get max internal group id */
169         snprintf(query, sizeof(query),
170                  "select max(internal_group_id) from noti_list");
171
172         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
173         if (ret != SQLITE_OK) {
174                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
175                                  sqlite3_errmsg(db));
176                 return NOTIFICATION_ERROR_FROM_DB;
177         }
178
179         ret = sqlite3_step(stmt);
180         if (ret == SQLITE_ROW) {
181                 result = sqlite3_column_int(stmt, 0);
182         } else {
183                 result = 0;
184         }
185
186         sqlite3_finalize(stmt);
187
188         return result;
189 }
190
191 static int _notification_noti_get_internal_group_id(notification_h noti,
192                                                     sqlite3 * db)
193 {
194         sqlite3_stmt *stmt = NULL;
195         char query[NOTIFICATION_QUERY_MAX] = { 0, };
196         int ret = NOTIFICATION_ERROR_NONE, result = 0;
197         const char *ret_title = NULL;
198         char buf_key[32] = { 0, };
199
200         if (noti->group_id == NOTIFICATION_GROUP_ID_NONE) {
201                 /* If Group ID is NONE Get max internal group ID */
202                 result = _notification_noti_get_max_internal_group_id(noti, db);
203                 if (result < 0) {
204                         return NOTIFICATION_ERROR_FROM_DB;
205                 }
206
207                 /* Internal Group ID is max internal group ID + 1 */
208                 noti->internal_group_id = result + 1;
209
210                 return NOTIFICATION_ERROR_NONE;
211         } else if (noti->group_id == NOTIFICATION_GROUP_ID_DEFAULT) {
212                 /* If Group ID is DEFAULT, Get internal group id if it exist */
213                 if (noti->b_key != NULL) {
214                         snprintf(buf_key, sizeof(buf_key), "%d",
215                                  NOTIFICATION_TEXT_TYPE_TITLE);
216
217                         ret_title = bundle_get_val(noti->b_key, buf_key);
218                 }
219
220                 if (ret_title == NULL && noti->b_text != NULL) {
221                         snprintf(buf_key, sizeof(buf_key), "%d",
222                                  NOTIFICATION_TEXT_TYPE_TITLE);
223
224                         ret_title = bundle_get_val(noti->b_text, buf_key);
225                 }
226
227                 if (ret_title == NULL) {
228                         ret_title = noti->caller_pkgname;
229                 }
230
231                 snprintf(query, sizeof(query),
232                          "select internal_group_id from noti_list where title_key = $title_key and group_id = %d",
233                          NOTIFICATION_GROUP_ID_DEFAULT);
234         } else {
235                 /* If Group ID is > DEFAULT, Get internal group id if it exit */
236                 snprintf(query, sizeof(query),
237                          "select internal_group_id from noti_list where caller_pkgname = '%s' and group_id = %d",
238                          NOTIFICATION_CHECK_STR(noti->caller_pkgname),
239                          noti->group_id);
240         }
241
242         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
243         if (ret != SQLITE_OK) {
244                 NOTIFICATION_ERR("Select Query : %s", query);
245                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
246                                  sqlite3_errmsg(db));
247                 if (stmt) {
248                         sqlite3_finalize(stmt);
249                 }
250                 return NOTIFICATION_ERROR_FROM_DB;
251         }
252
253         /* Bind query */
254         if (ret_title != NULL) {
255                 ret =
256                     _notification_noti_bind_query(stmt, "$title_key",
257                                                   NOTIFICATION_CHECK_STR
258                                                   (ret_title));
259                 if (ret != NOTIFICATION_ERROR_NONE) {
260                         NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
261                         if (stmt) {
262                                 sqlite3_finalize(stmt);
263                         }
264                         return ret;
265                 }
266         }
267
268         ret = sqlite3_step(stmt);
269         if (ret == SQLITE_ROW) {
270                 result = sqlite3_column_int(stmt, 0);
271         } else {
272                 /* If there is not internal_group_id, create new one */
273                 result = _notification_noti_get_max_internal_group_id(noti, db);
274                 result++;
275         }
276
277         sqlite3_finalize(stmt);
278
279         noti->internal_group_id = result;
280
281         return NOTIFICATION_ERROR_NONE;
282 }
283
284 static int _notification_noti_make_query(notification_h noti, char *query,
285                                          int query_size)
286 {
287         char *args = NULL;
288         char *group_args = NULL;
289         char *b_image_path = NULL;
290         char *b_execute_option = NULL;
291         char *b_service_responding = NULL;
292         char *b_service_single_launch = NULL;
293         char *b_service_multi_launch = NULL;
294         char *b_text = NULL;
295         char *b_key = NULL;
296         char *b_format_args = NULL;
297         int flag_simmode = 0;
298
299         /* Decode bundle to insert DB */
300         if (noti->args) {
301                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
302         }
303         if (noti->group_args) {
304                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
305                               NULL);
306         }
307
308         if (noti->b_execute_option) {
309                 bundle_encode(noti->b_execute_option,
310                               (bundle_raw **) & b_execute_option, NULL);
311         }
312         if (noti->b_service_responding) {
313                 bundle_encode(noti->b_service_responding,
314                               (bundle_raw **) & b_service_responding, NULL);
315         }
316         if (noti->b_service_single_launch) {
317                 bundle_encode(noti->b_service_single_launch,
318                               (bundle_raw **) & b_service_single_launch, NULL);
319         }
320         if (noti->b_service_multi_launch) {
321                 bundle_encode(noti->b_service_multi_launch,
322                               (bundle_raw **) & b_service_multi_launch, NULL);
323         }
324
325         if (noti->b_text) {
326                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
327         }
328         if (noti->b_key) {
329                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
330         }
331         if (noti->b_format_args) {
332                 bundle_encode(noti->b_format_args,
333                               (bundle_raw **) & b_format_args, NULL);
334         }
335
336         if (noti->b_image_path) {
337                 bundle_encode(noti->b_image_path,
338                               (bundle_raw **) & b_image_path, NULL);
339         }
340
341         /* Check only simmode property is enable */
342         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
343                 flag_simmode = 1;
344         }
345
346         /* Make query */
347         snprintf(query, query_size, "insert into noti_list ("
348                  "type, "
349                  "caller_pkgname, launch_pkgname, "
350                  "image_path, "
351                  "group_id, internal_group_id, priv_id, "
352                  "title_key, "
353                  "b_text, b_key, b_format_args, num_format_args, "
354                  "text_domain, text_dir, "
355                  "time, insert_time, "
356                  "args, group_args, "
357                  "b_execute_option, "
358                  "b_service_responding, b_service_single_launch, b_service_multi_launch, "
359                  "sound_type, sound_path, vibration_type, vibration_path, "
360                  "flags_for_property, flag_simmode, display_applist, "
361                  "progress_size, progress_percentage) values ("
362                  "%d, "
363                  "'%s', '%s', "
364                  "'%s', "
365                  "%d, %d, %d, "
366                  "$title_key, "
367                  "'%s', '%s', '%s', %d, "
368                  "'%s', '%s', "
369                  "%d, %d, "
370                  "'%s', '%s', "
371                  "'%s', "
372                  "'%s', '%s', '%s', "
373                  "%d, '%s', %d, '%s', "
374                  "%d, %d, %d, "
375                  "%f, %f)",
376                  noti->type,
377                  NOTIFICATION_CHECK_STR(noti->caller_pkgname),
378                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
379                  NOTIFICATION_CHECK_STR(b_image_path), noti->group_id,
380                  noti->internal_group_id, noti->priv_id,
381                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
382                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
383                  NOTIFICATION_CHECK_STR(noti->domain),
384                  NOTIFICATION_CHECK_STR(noti->dir), (int)noti->time,
385                  (int)noti->insert_time, NOTIFICATION_CHECK_STR(args),
386                  NOTIFICATION_CHECK_STR(group_args),
387                  NOTIFICATION_CHECK_STR(b_execute_option),
388                  NOTIFICATION_CHECK_STR(b_service_responding),
389                  NOTIFICATION_CHECK_STR(b_service_single_launch),
390                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
391                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
392                  noti->vibration_type,
393                  NOTIFICATION_CHECK_STR(noti->vibration_path),
394                  noti->flags_for_property, flag_simmode, noti->display_applist,
395                  noti->progress_size, noti->progress_percentage);
396
397         /* Free decoded data */
398         if (args) {
399                 free(args);
400         }
401         if (group_args) {
402                 free(group_args);
403         }
404
405         if (b_execute_option) {
406                 free(b_execute_option);
407         }
408         if (b_service_responding) {
409                 free(b_service_responding);
410         }
411         if (b_service_single_launch) {
412                 free(b_service_single_launch);
413         }
414         if (b_service_multi_launch) {
415                 free(b_service_multi_launch);
416         }
417
418         if (b_text) {
419                 free(b_text);
420         }
421         if (b_key) {
422                 free(b_key);
423         }
424         if (b_format_args) {
425                 free(b_format_args);
426         }
427
428         if (b_image_path) {
429                 free(b_image_path);
430         }
431
432         return NOTIFICATION_ERROR_NONE;
433 }
434
435
436 static int _notification_noti_make_update_query(notification_h noti, char *query,
437                                          int query_size)
438 {
439         char *args = NULL;
440         char *group_args = NULL;
441         char *b_image_path = NULL;
442         char *b_execute_option = NULL;
443         char *b_service_responding = NULL;
444         char *b_service_single_launch = NULL;
445         char *b_service_multi_launch = NULL;
446         char *b_text = NULL;
447         char *b_key = NULL;
448         char *b_format_args = NULL;
449         int flag_simmode = 0;
450
451         /* Decode bundle to update DB */
452         if (noti->args) {
453                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
454         }
455         if (noti->group_args) {
456                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
457                               NULL);
458         }
459
460         if (noti->b_execute_option) {
461                 bundle_encode(noti->b_execute_option,
462                               (bundle_raw **) & b_execute_option, NULL);
463         }
464         if (noti->b_service_responding) {
465                 bundle_encode(noti->b_service_responding,
466                               (bundle_raw **) & b_service_responding, NULL);
467         }
468         if (noti->b_service_single_launch) {
469                 bundle_encode(noti->b_service_single_launch,
470                               (bundle_raw **) & b_service_single_launch, NULL);
471         }
472         if (noti->b_service_multi_launch) {
473                 bundle_encode(noti->b_service_multi_launch,
474                               (bundle_raw **) & b_service_multi_launch, NULL);
475         }
476
477         if (noti->b_text) {
478                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
479         }
480         if (noti->b_key) {
481                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
482         }
483         if (noti->b_format_args) {
484                 bundle_encode(noti->b_format_args,
485                               (bundle_raw **) & b_format_args, NULL);
486         }
487
488         if (noti->b_image_path) {
489                 bundle_encode(noti->b_image_path,
490                               (bundle_raw **) & b_image_path, NULL);
491         }
492
493         /* Check only simmode property is enable */
494         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
495                 flag_simmode = 1;
496         }
497
498         /* Make query */
499         snprintf(query, query_size, "update noti_list set "
500                  "type = %d, "
501                  "launch_pkgname = '%s', "
502                  "image_path = '%s', "
503                  "b_text = '%s', b_key = '%s', "
504                  "b_format_args = '%s', num_format_args = %d, "
505                  "text_domain = '%s', text_dir = '%s', "
506                  "time = %d, insert_time = %d, "
507                  "args = '%s', group_args = '%s', "
508                  "b_execute_option = '%s', "
509                  "b_service_responding = '%s', "
510                  "b_service_single_launch = '%s', "
511                  "b_service_multi_launch = '%s', "
512                  "sound_type = %d, sound_path = '%s', "
513                  "vibration_type = %d, vibration_path = '%s', "
514                  "flags_for_property = %d, flag_simmode = %d, "
515                  "display_applist = %d, "
516                  "progress_size = %f, progress_percentage = %f "
517                  "where priv_id = %d ",
518                  noti->type,
519                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
520                  NOTIFICATION_CHECK_STR(b_image_path),
521                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
522                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
523                  NOTIFICATION_CHECK_STR(noti->domain),
524                  NOTIFICATION_CHECK_STR(noti->dir),
525                  (int)noti->time, (int)noti->insert_time,
526                  NOTIFICATION_CHECK_STR(args), NOTIFICATION_CHECK_STR(group_args),
527                  NOTIFICATION_CHECK_STR(b_execute_option),
528                  NOTIFICATION_CHECK_STR(b_service_responding),
529                  NOTIFICATION_CHECK_STR(b_service_single_launch),
530                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
531                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
532                  noti->vibration_type,
533                  NOTIFICATION_CHECK_STR(noti->vibration_path),
534                  noti->flags_for_property, flag_simmode, noti->display_applist,
535                  noti->progress_size, noti->progress_percentage,
536                  noti->priv_id);
537
538         /* Free decoded data */
539         if (args) {
540                 free(args);
541         }
542         if (group_args) {
543                 free(group_args);
544         }
545
546         if (b_execute_option) {
547                 free(b_execute_option);
548         }
549         if (b_service_responding) {
550                 free(b_service_responding);
551         }
552         if (b_service_single_launch) {
553                 free(b_service_single_launch);
554         }
555         if (b_service_multi_launch) {
556                 free(b_service_multi_launch);
557         }
558
559         if (b_text) {
560                 free(b_text);
561         }
562         if (b_key) {
563                 free(b_key);
564         }
565         if (b_format_args) {
566                 free(b_format_args);
567         }
568
569         if (b_image_path) {
570                 free(b_image_path);
571         }
572
573         return NOTIFICATION_ERROR_NONE;
574 }
575
576 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
577 {
578         notification_h noti = NULL;
579         int col = 0;
580
581         noti = malloc(sizeof(struct _notification));
582         if (noti == NULL) {
583                 return NULL;
584         }
585
586         noti->type = sqlite3_column_int(stmt, col++);
587         noti->caller_pkgname = notification_db_column_text(stmt, col++);
588         noti->launch_pkgname = notification_db_column_text(stmt, col++);
589         noti->b_image_path = notification_db_column_bundle(stmt, col++);
590         noti->group_id = sqlite3_column_int(stmt, col++);
591         noti->internal_group_id = 0;
592         noti->priv_id = sqlite3_column_int(stmt, col++);
593
594         noti->b_text = notification_db_column_bundle(stmt, col++);
595         noti->b_key = notification_db_column_bundle(stmt, col++);
596         noti->b_format_args = notification_db_column_bundle(stmt, col++);
597         noti->num_format_args = sqlite3_column_int(stmt, col++);
598
599         noti->domain = notification_db_column_text(stmt, col++);
600         noti->dir = notification_db_column_text(stmt, col++);
601         noti->time = sqlite3_column_int(stmt, col++);
602         noti->insert_time = sqlite3_column_int(stmt, col++);
603         noti->args = notification_db_column_bundle(stmt, col++);
604         noti->group_args = notification_db_column_bundle(stmt, col++);
605
606         noti->b_execute_option = notification_db_column_bundle(stmt, col++);
607         noti->b_service_responding = notification_db_column_bundle(stmt, col++);
608         noti->b_service_single_launch =
609             notification_db_column_bundle(stmt, col++);
610         noti->b_service_multi_launch =
611             notification_db_column_bundle(stmt, col++);
612
613         noti->sound_type = sqlite3_column_int(stmt, col++);
614         noti->sound_path = notification_db_column_text(stmt, col++);
615         noti->vibration_type = sqlite3_column_int(stmt, col++);
616         noti->vibration_path = notification_db_column_text(stmt, col++);
617
618         noti->flags_for_property = sqlite3_column_int(stmt, col++);
619         noti->display_applist = sqlite3_column_int(stmt, col++);
620         noti->progress_size = sqlite3_column_double(stmt, col++);
621         noti->progress_percentage = sqlite3_column_double(stmt, col++);
622
623         noti->app_icon_path = NULL;
624         noti->app_name = NULL;
625         noti->temp_title = NULL;
626         noti->temp_content = NULL;
627         return noti;
628 }
629
630 int notification_noti_insert(notification_h noti)
631 {
632         sqlite3 *db = NULL;
633         sqlite3_stmt *stmt = NULL;
634         char query[NOTIFICATION_QUERY_MAX] = { 0, };
635         int ret = 0;
636         char buf_key[32] = { 0, };
637         const char *title_key = NULL;
638
639         /* Open DB */
640         db = notification_db_open(DBPATH);
641
642         /* Get private ID */
643         if (noti->priv_id == NOTIFICATION_PRIV_ID_NONE) {
644                 ret = _notification_noti_get_priv_id(noti, db);
645         } else {
646                 ret = _notification_noti_check_priv_id(noti, db);
647                 if (ret != NOTIFICATION_ERROR_NONE) {
648                         goto err;
649                 }
650         }
651
652         /* Get internal group ID */
653         ret = _notification_noti_get_internal_group_id(noti, db);
654         if (ret != NOTIFICATION_ERROR_NONE) {
655                 goto err;
656         }
657
658         /* make query */
659         ret = _notification_noti_make_query(noti, query, sizeof(query));
660         if (ret != NOTIFICATION_ERROR_NONE) {
661                 goto err;
662         }
663
664         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
665         if (ret != SQLITE_OK) {
666                 NOTIFICATION_ERR("Insert Query : %s", query);
667                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
668                                  sqlite3_errmsg(db));
669                 ret = NOTIFICATION_ERROR_FROM_DB;
670                 goto err;
671         }
672
673         /* Get title key */
674         if (noti->b_key != NULL) {
675                 snprintf(buf_key, sizeof(buf_key), "%d",
676                          NOTIFICATION_TEXT_TYPE_TITLE);
677
678                 title_key = bundle_get_val(noti->b_key, buf_key);
679         }
680
681         if (title_key == NULL && noti->b_text != NULL) {
682                 snprintf(buf_key, sizeof(buf_key), "%d",
683                          NOTIFICATION_TEXT_TYPE_TITLE);
684
685                 title_key = bundle_get_val(noti->b_text, buf_key);
686         }
687
688         if (title_key == NULL) {
689                 title_key = noti->caller_pkgname;
690         }
691
692         /* Bind query */
693         ret = _notification_noti_bind_query(stmt, "$title_key", title_key);
694         if (ret != NOTIFICATION_ERROR_NONE) {
695                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
696                 goto err;
697         }
698
699         ret = sqlite3_step(stmt);
700         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
701                 ret = NOTIFICATION_ERROR_NONE;
702         } else {
703                 ret = NOTIFICATION_ERROR_FROM_DB;
704         }
705 err:
706         if (stmt) {
707                 sqlite3_finalize(stmt);
708         }
709
710         /* Close DB */
711         if (db) {
712                 notification_db_close(&db);
713         }
714
715         return ret;
716 }
717
718
719 int notification_noti_update(notification_h noti)
720 {
721         sqlite3 *db;
722         sqlite3_stmt *stmt = NULL;
723         char query[NOTIFICATION_QUERY_MAX] = { 0, };
724         int ret = 0;
725
726         /* Open DB */
727         db = notification_db_open(DBPATH);
728
729         /* Check private ID is exist */
730         ret = _notification_noti_check_priv_id(noti, db);
731         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
732                 ret = NOTIFICATION_ERROR_NOT_EXIST_ID;
733                 goto err;
734         }
735
736         /* make update query */
737         ret = _notification_noti_make_update_query(noti, query, sizeof(query));
738         if (ret != NOTIFICATION_ERROR_NONE) {
739                 goto err;
740         }
741
742         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
743         if (ret != SQLITE_OK) {
744                 NOTIFICATION_ERR("Insert Query : %s", query);
745                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
746                                  sqlite3_errmsg(db));
747                 ret = NOTIFICATION_ERROR_FROM_DB;
748                 goto err;
749         }
750
751         ret = sqlite3_step(stmt);
752         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
753                 ret = NOTIFICATION_ERROR_NONE;
754         } else {
755                 ret = NOTIFICATION_ERROR_FROM_DB;
756         }
757 err:
758         if (stmt) {
759                 sqlite3_finalize(stmt);
760         }
761
762         /* Close DB */
763         if (db) {
764                 notification_db_close(&db);
765         }
766
767         return ret;
768 }
769
770 int notification_noti_delete_all(notification_type_e type, const char *pkgname)
771 {
772         sqlite3 *db = NULL;
773         char query[NOTIFICATION_QUERY_MAX] = { 0, };
774         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
775         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
776
777         /* Open DB */
778         db = notification_db_open(DBPATH);
779
780         /* Make query */
781         snprintf(query_base, sizeof(query_base), "delete from noti_list ");
782
783         if (pkgname == NULL) {
784                 if (type != NOTIFICATION_TYPE_NONE) {
785                         snprintf(query_where, sizeof(query_where),
786                                  "where type = %d ", type);
787                 }
788         } else {
789                 if (type == NOTIFICATION_TYPE_NONE) {
790                         snprintf(query_where, sizeof(query_where),
791                                  "where caller_pkgname = '%s' ", pkgname);
792                 } else {
793                         snprintf(query_where, sizeof(query_where),
794                                  "where caller_pkgname = '%s' and type = %d ",
795                                  pkgname, type);
796                 }
797         }
798
799         snprintf(query, sizeof(query), "%s %s", query_base, query_where);
800
801         //NOTIFICATION_INFO("Delete All : [%s]", query);
802
803         /* execute DB */
804         notification_db_exec(db, query);
805
806         /* Close DB */
807         if (db) {
808                 notification_db_close(&db);
809         }
810
811         return NOTIFICATION_ERROR_NONE;
812 }
813
814 int notification_noti_delete_group_by_group_id(const char *pkgname,
815                                                int group_id)
816 {
817         sqlite3 *db = NULL;
818         char query[NOTIFICATION_QUERY_MAX] = { 0, };
819
820         /* Check pkgname is valid */
821         if (pkgname == NULL) {
822                 return NOTIFICATION_ERROR_INVALID_DATA;
823         }
824
825         /* Open DB */
826         db = notification_db_open(DBPATH);
827
828         /* Make query */
829         snprintf(query, sizeof(query), "delete from noti_list "
830                  "where caller_pkgname = '%s' and group_id = %d", pkgname,
831                  group_id);
832
833         /* execute DB */
834         notification_db_exec(db, query);
835
836         /* Close DB */
837         if (db) {
838                 notification_db_close(&db);
839         }
840
841         return NOTIFICATION_ERROR_NONE;
842 }
843
844 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
845 {
846         sqlite3 *db = NULL;
847         char query[NOTIFICATION_QUERY_MAX] = { 0, };
848         int internal_group_id = 0;
849
850         /* Check pkgname is valid */
851         if (pkgname == NULL) {
852                 return NOTIFICATION_ERROR_INVALID_DATA;
853         }
854
855         /* Open DB */
856         db = notification_db_open(DBPATH);
857
858         /* Get internal group id using priv id */
859         internal_group_id =
860             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
861                                                                 priv_id, db);
862
863         /* Make query */
864         snprintf(query, sizeof(query), "delete from noti_list "
865                  "where caller_pkgname = '%s' and internal_group_id = %d",
866                  pkgname, internal_group_id);
867
868         /* execute DB */
869         notification_db_exec(db, query);
870
871         /* Close DB */
872         if (db) {
873                 notification_db_close(&db);
874         }
875
876         return NOTIFICATION_ERROR_NONE;
877 }
878
879 int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
880 {
881         sqlite3 *db = NULL;
882         char query[NOTIFICATION_QUERY_MAX] = { 0, };
883
884         /* Check pkgname is valid */
885         if (pkgname == NULL) {
886                 return NOTIFICATION_ERROR_INVALID_DATA;
887         }
888
889         /* Open DB */
890         db = notification_db_open(DBPATH);
891
892         /* Make query */
893         snprintf(query, sizeof(query), "delete from noti_list "
894                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
895                  priv_id);
896
897         /* execute DB */
898         notification_db_exec(db, query);
899
900         /* Close DB */
901         if (db) {
902                 notification_db_close(&db);
903         }
904
905         return NOTIFICATION_ERROR_NONE;
906 }
907
908 notification_error_e notification_noti_get_count(notification_type_e type,
909                                                  const char *pkgname,
910                                                  int group_id, int priv_id,
911                                                  int *count)
912 {
913         sqlite3 *db = NULL;
914         sqlite3_stmt *stmt = NULL;
915         char query[NOTIFICATION_QUERY_MAX] = { 0, };
916         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
917         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
918         char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
919
920         int ret = 0, get_count = 0, internal_group_id = 0;
921         int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
922         int flag_where = 0;
923         int flag_where_more = 0;
924
925         /* Open DB */
926         db = notification_db_open(DBPATH);
927
928         /* Check current sim status */
929         vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
930
931         /* Make query */
932         snprintf(query_base, sizeof(query_base),
933                  "select count(*) from noti_list ");
934
935         if (pkgname != NULL) {
936                 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
937                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
938                                 snprintf(query_where, sizeof(query_where),
939                                          "where caller_pkgname = '%s' ",
940                                          pkgname);
941                                 flag_where = 1;
942                         } else {
943                                 internal_group_id =
944                                     _notification_noti_get_internal_group_id_by_priv_id
945                                     (pkgname, priv_id, db);
946                                 snprintf(query_where, sizeof(query_where),
947                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
948                                          pkgname, internal_group_id);
949                                 flag_where = 1;
950                         }
951                 } else {
952                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
953                                 snprintf(query_where, sizeof(query_where),
954                                          "where caller_pkgname = '%s' and group_id = %d ",
955                                          pkgname, group_id);
956                                 flag_where = 1;
957                         } else {
958                                 internal_group_id =
959                                     _notification_noti_get_internal_group_id_by_priv_id
960                                     (pkgname, priv_id, db);
961                                 snprintf(query_where, sizeof(query_where),
962                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
963                                          pkgname, internal_group_id);
964                                 flag_where = 1;
965                         }
966                 }
967
968         }
969
970         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
971                 if (type != NOTIFICATION_TYPE_NONE) {
972                         snprintf(query_where_more, sizeof(query_where_more),
973                                  "type = %d ", type);
974                         flag_where_more = 1;
975                 }
976         } else {
977                 if (type != NOTIFICATION_TYPE_NONE) {
978                         snprintf(query_where_more, sizeof(query_where_more),
979                                  "type = %d and flag_simmode = 0 ", type);
980                         flag_where_more = 1;
981                 } else {
982                         snprintf(query_where_more, sizeof(query_where_more),
983                                  "flag_simmode = 0 ");
984                         flag_where_more = 1;
985                 }
986         }
987
988         if (flag_where == 1) {
989                 if (flag_where_more == 1) {
990                         snprintf(query, sizeof(query), "%s %s and %s",
991                                  query_base, query_where, query_where_more);
992                 } else {
993                         snprintf(query, sizeof(query), "%s %s", query_base,
994                                  query_where);
995                 }
996
997         } else {
998                 if (flag_where_more == 1) {
999                         snprintf(query, sizeof(query), "%s where %s",
1000                                  query_base, query_where_more);
1001                 } else {
1002                         snprintf(query, sizeof(query), "%s", query_base);
1003                 }
1004         }
1005
1006         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1007         if (ret != SQLITE_OK) {
1008                 NOTIFICATION_ERR("Select Query : %s", query);
1009                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1010                                  sqlite3_errmsg(db));
1011
1012                 ret = NOTIFICATION_ERROR_FROM_DB;
1013                 goto err;
1014         }
1015
1016         ret = sqlite3_step(stmt);
1017         if (ret == SQLITE_ROW) {
1018                 get_count = sqlite3_column_int(stmt, 0);
1019         }
1020
1021         ret = NOTIFICATION_ERROR_NONE;
1022
1023 err:
1024         if (stmt) {
1025                 sqlite3_finalize(stmt);
1026         }
1027
1028         /* Close DB */
1029         if (db) {
1030                 notification_db_close(&db);
1031         }
1032
1033         *count = get_count;
1034
1035         return ret;
1036 }
1037
1038 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
1039                                                          int count,
1040                                                          notification_list_h *
1041                                                          list)
1042 {
1043         sqlite3 *db = NULL;
1044         sqlite3_stmt *stmt = NULL;
1045         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1046         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1047         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1048
1049         int ret = 0;
1050         notification_list_h get_list = NULL;
1051         notification_h noti = NULL;
1052         int internal_count = 0;
1053         int status;
1054
1055         /* Open DB */
1056         db = notification_db_open(DBPATH);
1057
1058         /* Check current sim status */
1059         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1060
1061         /* Make query */
1062         snprintf(query_base, sizeof(query_base), "select "
1063                  "type, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1064                  "b_text, b_key, b_format_args, num_format_args, "
1065                  "text_domain, text_dir, time, insert_time, args, group_args, "
1066                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1067                  "sound_type, sound_path, vibration_type, vibration_path, "
1068                  "flags_for_property, display_applist, progress_size, progress_percentage "
1069                  "from noti_list ");
1070
1071         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1072                 if (type != NOTIFICATION_TYPE_NONE) {
1073                         snprintf(query_where, sizeof(query_where),
1074                                  "where type = %d ", type);
1075                 }
1076         } else {
1077                 if (type != NOTIFICATION_TYPE_NONE) {
1078                         snprintf(query_where, sizeof(query_where),
1079                                  "where type = %d and flag_simmode = 0 ", type);
1080                 } else {
1081                         snprintf(query_where, sizeof(query_where),
1082                                  "where flag_simmode = 0 ");
1083                 }
1084         }
1085
1086         snprintf(query, sizeof(query),
1087                  "%s %s "
1088                  "group by internal_group_id "
1089                  "order by rowid desc, time desc", query_base, query_where);
1090
1091         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1092         if (ret != SQLITE_OK) {
1093                 NOTIFICATION_ERR("Select Query : %s", query);
1094                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1095                                  sqlite3_errmsg(db));
1096
1097                 ret = NOTIFICATION_ERROR_FROM_DB;
1098                 goto err;
1099         }
1100
1101         ret = sqlite3_step(stmt);
1102         while (ret == SQLITE_ROW) {
1103                 /* Make notification list */
1104                 noti = _notification_noti_get_item(stmt);
1105                 if (noti != NULL) {
1106                         internal_count++;
1107
1108                         get_list = notification_list_append(get_list, noti);
1109
1110                         if (count != -1 && internal_count >= count) {
1111                                 NOTIFICATION_INFO
1112                                     ("internal count %d >= count %d",
1113                                      internal_count, count);
1114                                 break;
1115                         }
1116                 }
1117
1118                 ret = sqlite3_step(stmt);
1119         }
1120
1121         ret = NOTIFICATION_ERROR_NONE;
1122
1123 err:
1124         if (stmt) {
1125                 sqlite3_finalize(stmt);
1126         }
1127
1128         /* Close DB */
1129         if (db) {
1130                 notification_db_close(&db);
1131         }
1132
1133         if (get_list != NULL) {
1134                 *list = notification_list_get_head(get_list);
1135         }
1136
1137         return ret;
1138 }
1139
1140 notification_error_e notification_noti_get_detail_list(const char *pkgname,
1141                                                        int group_id,
1142                                                        int priv_id, int count,
1143                                                        notification_list_h *list)
1144 {
1145         sqlite3 *db = NULL;
1146         sqlite3_stmt *stmt = NULL;
1147         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1148         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1149
1150         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1151         int ret = 0;
1152         notification_list_h get_list = NULL;
1153         notification_h noti = NULL;
1154         int internal_count = 0;
1155         int internal_group_id = 0;
1156         int status;
1157
1158         /* Open DB */
1159         db = notification_db_open(DBPATH);
1160
1161         /* Check current sim status */
1162         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1163
1164         /* Make query */
1165         snprintf(query_base, sizeof(query_base), "select "
1166                  "type, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1167                  "b_text, b_key, b_format_args, num_format_args, "
1168                  "text_domain, text_dir, time, insert_time, args, group_args, "
1169                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1170                  "sound_type, sound_path, vibration_type, vibration_path, "
1171                  "flags_for_property, display_applist, progress_size, progress_percentage "
1172                  "from noti_list ");
1173
1174         internal_group_id =
1175             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1176                                                                 priv_id, db);
1177
1178         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1179                 snprintf(query_where, sizeof(query_where),
1180                          "where  caller_pkgname = '%s' and internal_group_id = %d ",
1181                          pkgname, internal_group_id);
1182         } else {
1183                 snprintf(query_where, sizeof(query_where),
1184                          "where  caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
1185                          pkgname, internal_group_id);
1186         }
1187
1188         snprintf(query, sizeof(query),
1189                  "%s %s "
1190                  "order by rowid desc, time desc", query_base, query_where);
1191
1192         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1193         if (ret != SQLITE_OK) {
1194                 NOTIFICATION_ERR("Select Query : %s", query);
1195                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1196                                  sqlite3_errmsg(db));
1197
1198                 ret = NOTIFICATION_ERROR_FROM_DB;
1199                 goto err;
1200         }
1201
1202         ret = sqlite3_step(stmt);
1203         while (ret == SQLITE_ROW) {
1204                 /* Make notification list */
1205                 noti = _notification_noti_get_item(stmt);
1206                 if (noti != NULL) {
1207                         internal_count++;
1208
1209                         get_list = notification_list_append(get_list, noti);
1210
1211                         if (count != -1 && internal_count >= count) {
1212                                 NOTIFICATION_INFO
1213                                     ("internal count %d >= count %d",
1214                                      internal_count, count);
1215                                 break;
1216                         }
1217                 }
1218
1219                 ret = sqlite3_step(stmt);
1220         }
1221
1222         ret = NOTIFICATION_ERROR_NONE;
1223
1224 err:
1225         if (stmt) {
1226                 sqlite3_finalize(stmt);
1227         }
1228
1229         /* Close DB */
1230         if (db) {
1231                 notification_db_close(&db);
1232         }
1233
1234         if (get_list != NULL) {
1235                 *list = notification_list_get_head(get_list);
1236         }
1237
1238         return ret;
1239 }