sync with private git
[platform/core/api/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 #define NOTI_BURST_DELETE_UNIT 10
35
36 static void __free_and_set(void **target_ptr, void *new_ptr) {
37         if (target_ptr != NULL) {
38                 if (*target_ptr != NULL) {
39                         free(*target_ptr);
40                 }
41                 *target_ptr = new_ptr;
42         }
43 }
44
45 static int _notification_noti_bind_query_text(sqlite3_stmt * stmt, const char *name,
46                                          const char *str)
47 {
48         int ret = 0;
49         int index = 0;
50
51         index = sqlite3_bind_parameter_index(stmt, name);
52         if (index == 0) {
53                 NOTIFICATION_ERR("Insert : invalid column name");
54                 return NOTIFICATION_ERROR_FROM_DB;
55         }
56
57         ret =
58             sqlite3_bind_text(stmt, index, NOTIFICATION_CHECK_STR(str), -1,
59                               SQLITE_STATIC);
60         if (ret != SQLITE_OK) {
61                 NOTIFICATION_ERR("Insert text : %s",
62                                  NOTIFICATION_CHECK_STR(str));
63                 return NOTIFICATION_ERROR_FROM_DB;
64         }
65
66         return NOTIFICATION_ERROR_NONE;
67 }
68
69 static int _notification_noti_bind_query_double(sqlite3_stmt * stmt, const char *name,
70                                          double val)
71 {
72         int ret = 0;
73         int index = 0;
74
75         index = sqlite3_bind_parameter_index(stmt, name);
76         if (index == 0) {
77                 NOTIFICATION_ERR("Insert : invalid column name");
78                 return NOTIFICATION_ERROR_FROM_DB;
79         }
80
81         ret = sqlite3_bind_double(stmt, index, val);
82         if (ret != SQLITE_OK) {
83                 NOTIFICATION_ERR("Insert double : %f", val);
84                 return NOTIFICATION_ERROR_FROM_DB;
85         }
86
87         return NOTIFICATION_ERROR_NONE;
88 }
89
90 static int _notification_noti_check_priv_id(notification_h noti, sqlite3 * db)
91 {
92         sqlite3_stmt *stmt = NULL;
93         char query[NOTIFICATION_QUERY_MAX] = { 0, };
94         int ret = NOTIFICATION_ERROR_NONE, result = 0;
95
96         /* Make query to check priv_id exist */
97         snprintf(query, sizeof(query),
98                  "select count(*) from noti_list where caller_pkgname = '%s' and priv_id = %d",
99                  noti->caller_pkgname, noti->priv_id);
100
101         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
102         if (ret != SQLITE_OK) {
103                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
104                                  sqlite3_errmsg(db));
105                 return NOTIFICATION_ERROR_FROM_DB;
106         }
107
108         ret = sqlite3_step(stmt);
109         if (ret == SQLITE_ROW) {
110                 result = sqlite3_column_int(stmt, 0);
111         } else {
112                 result = 0;
113         }
114
115         sqlite3_finalize(stmt);
116
117         /* If result > 0, there is priv_id in DB */
118         if (result > 0) {
119                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
120         }
121
122         return NOTIFICATION_ERROR_NONE;
123 }
124
125 static int _notification_noti_get_internal_group_id_by_priv_id(const char *pkgname,
126                                                                int priv_id,
127                                                                sqlite3 * db)
128 {
129         sqlite3_stmt *stmt = NULL;
130         char query[NOTIFICATION_QUERY_MAX] = { 0, };
131         int ret = NOTIFICATION_ERROR_NONE, result = 0;
132
133         snprintf(query, sizeof(query),
134                  "select internal_group_id from noti_list where caller_pkgname = '%s' and priv_id = %d",
135                  pkgname, priv_id);
136
137         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
138         if (ret != SQLITE_OK) {
139                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
140                                  sqlite3_errmsg(db));
141                 return NOTIFICATION_ERROR_FROM_DB;
142         }
143
144         ret = sqlite3_step(stmt);
145         if (ret == SQLITE_ROW) {
146                 result = sqlite3_column_int(stmt, 0);
147         } else {
148                 result = 0;
149         }
150
151         sqlite3_finalize(stmt);
152
153         return result;
154 }
155
156 static int _notification_noti_make_query(notification_h noti, char *query,
157                                          int query_size)
158 {
159         char *args = NULL;
160         char *group_args = NULL;
161         char *b_image_path = NULL;
162         char *b_execute_option = NULL;
163         char *b_service_responding = NULL;
164         char *b_service_single_launch = NULL;
165         char *b_service_multi_launch = NULL;
166         char *b_text = NULL;
167         char *b_key = NULL;
168         char *b_format_args = NULL;
169         int flag_simmode = 0;
170
171         /* Decode bundle to insert DB */
172         if (noti->args) {
173                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
174         }
175         if (noti->group_args) {
176                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
177                               NULL);
178         }
179
180         if (noti->b_execute_option) {
181                 bundle_encode(noti->b_execute_option,
182                               (bundle_raw **) & b_execute_option, NULL);
183         }
184         if (noti->b_service_responding) {
185                 bundle_encode(noti->b_service_responding,
186                               (bundle_raw **) & b_service_responding, NULL);
187         }
188         if (noti->b_service_single_launch) {
189                 bundle_encode(noti->b_service_single_launch,
190                               (bundle_raw **) & b_service_single_launch, NULL);
191         }
192         if (noti->b_service_multi_launch) {
193                 bundle_encode(noti->b_service_multi_launch,
194                               (bundle_raw **) & b_service_multi_launch, NULL);
195         }
196
197         if (noti->b_text) {
198                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
199         }
200         if (noti->b_key) {
201                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
202         }
203         if (noti->b_format_args) {
204                 bundle_encode(noti->b_format_args,
205                               (bundle_raw **) & b_format_args, NULL);
206         }
207
208         if (noti->b_image_path) {
209                 bundle_encode(noti->b_image_path,
210                               (bundle_raw **) & b_image_path, NULL);
211         }
212
213         /* Check only simmode property is enable */
214         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
215                 flag_simmode = 1;
216         }
217
218         /* Make query */
219         snprintf(query, query_size, "insert into noti_list ("
220                  "type, "
221                  "layout, "
222                  "caller_pkgname, launch_pkgname, "
223                  "image_path, "
224                  "group_id, internal_group_id, priv_id, "
225                  "title_key, "
226                  "b_text, b_key, b_format_args, num_format_args, "
227                  "text_domain, text_dir, "
228                  "time, insert_time, "
229                  "args, group_args, "
230                  "b_execute_option, "
231                  "b_service_responding, b_service_single_launch, b_service_multi_launch, "
232                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
233                  "flags_for_property, flag_simmode, display_applist, "
234                  "progress_size, progress_percentage) values ("
235                  "%d, "
236                  "%d, "
237                  "'%s', '%s', "
238                  "'%s', "
239                  "%d, %d, %d, "
240                  "$title_key, "
241                  "'%s', '%s', '%s', %d, "
242                  "'%s', '%s', "
243                  "%d, %d, "
244                  "'%s', '%s', "
245                  "'%s', "
246                  "'%s', '%s', '%s', "
247                  "%d, '%s', %d, '%s', %d, %d, %d, %d,"
248                  "%d, %d, %d, "
249                  "$progress_size, $progress_percentage)",
250                  noti->type,
251                  noti->layout,
252                  NOTIFICATION_CHECK_STR(noti->caller_pkgname),
253                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
254                  NOTIFICATION_CHECK_STR(b_image_path), noti->group_id,
255                  noti->internal_group_id, noti->priv_id,
256                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
257                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
258                  NOTIFICATION_CHECK_STR(noti->domain),
259                  NOTIFICATION_CHECK_STR(noti->dir), (int)noti->time,
260                  (int)noti->insert_time, NOTIFICATION_CHECK_STR(args),
261                  NOTIFICATION_CHECK_STR(group_args),
262                  NOTIFICATION_CHECK_STR(b_execute_option),
263                  NOTIFICATION_CHECK_STR(b_service_responding),
264                  NOTIFICATION_CHECK_STR(b_service_single_launch),
265                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
266                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
267                  noti->vibration_type,
268                  NOTIFICATION_CHECK_STR(noti->vibration_path),
269                  noti->led_operation,
270                  noti->led_argb,
271                  noti->led_on_ms,
272                  noti->led_off_ms,
273                  noti->flags_for_property, flag_simmode, noti->display_applist);
274
275         /* Free decoded data */
276         if (args) {
277                 free(args);
278         }
279         if (group_args) {
280                 free(group_args);
281         }
282
283         if (b_execute_option) {
284                 free(b_execute_option);
285         }
286         if (b_service_responding) {
287                 free(b_service_responding);
288         }
289         if (b_service_single_launch) {
290                 free(b_service_single_launch);
291         }
292         if (b_service_multi_launch) {
293                 free(b_service_multi_launch);
294         }
295
296         if (b_text) {
297                 free(b_text);
298         }
299         if (b_key) {
300                 free(b_key);
301         }
302         if (b_format_args) {
303                 free(b_format_args);
304         }
305
306         if (b_image_path) {
307                 free(b_image_path);
308         }
309
310         return NOTIFICATION_ERROR_NONE;
311 }
312
313
314 static int _notification_noti_make_update_query(notification_h noti, char *query,
315                                          int query_size)
316 {
317         char *args = NULL;
318         char *group_args = NULL;
319         char *b_image_path = NULL;
320         char *b_execute_option = NULL;
321         char *b_service_responding = NULL;
322         char *b_service_single_launch = NULL;
323         char *b_service_multi_launch = NULL;
324         char *b_text = NULL;
325         char *b_key = NULL;
326         char *b_format_args = NULL;
327         int flag_simmode = 0;
328
329         /* Decode bundle to update DB */
330         if (noti->args) {
331                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
332         }
333         if (noti->group_args) {
334                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
335                               NULL);
336         }
337
338         if (noti->b_execute_option) {
339                 bundle_encode(noti->b_execute_option,
340                               (bundle_raw **) & b_execute_option, NULL);
341         }
342         if (noti->b_service_responding) {
343                 bundle_encode(noti->b_service_responding,
344                               (bundle_raw **) & b_service_responding, NULL);
345         }
346         if (noti->b_service_single_launch) {
347                 bundle_encode(noti->b_service_single_launch,
348                               (bundle_raw **) & b_service_single_launch, NULL);
349         }
350         if (noti->b_service_multi_launch) {
351                 bundle_encode(noti->b_service_multi_launch,
352                               (bundle_raw **) & b_service_multi_launch, NULL);
353         }
354
355         if (noti->b_text) {
356                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
357         }
358         if (noti->b_key) {
359                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
360         }
361         if (noti->b_format_args) {
362                 bundle_encode(noti->b_format_args,
363                               (bundle_raw **) & b_format_args, NULL);
364         }
365
366         if (noti->b_image_path) {
367                 bundle_encode(noti->b_image_path,
368                               (bundle_raw **) & b_image_path, NULL);
369         }
370
371         /* Check only simmode property is enable */
372         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
373                 flag_simmode = 1;
374         }
375
376         /* Make query */
377         snprintf(query, query_size, "update noti_list set "
378                  "type = %d, "
379                  "layout = %d, "
380                  "launch_pkgname = '%s', "
381                  "image_path = '%s', "
382                  "b_text = '%s', b_key = '%s', "
383                  "b_format_args = '%s', num_format_args = %d, "
384                  "text_domain = '%s', text_dir = '%s', "
385                  "time = %d, insert_time = %d, "
386                  "args = '%s', group_args = '%s', "
387                  "b_execute_option = '%s', "
388                  "b_service_responding = '%s', "
389                  "b_service_single_launch = '%s', "
390                  "b_service_multi_launch = '%s', "
391                  "sound_type = %d, sound_path = '%s', "
392                  "vibration_type = %d, vibration_path = '%s', "
393                  "led_operation = %d, led_argb = %d, "
394                  "led_on_ms = %d, led_off_ms = %d, "
395                  "flags_for_property = %d, flag_simmode = %d, "
396                  "display_applist = %d, "
397                  "progress_size = $progress_size, progress_percentage = $progress_percentage "
398                  "where priv_id = %d ",
399                  noti->type,
400                  noti->layout,
401                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
402                  NOTIFICATION_CHECK_STR(b_image_path),
403                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
404                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
405                  NOTIFICATION_CHECK_STR(noti->domain),
406                  NOTIFICATION_CHECK_STR(noti->dir),
407                  (int)noti->time, (int)noti->insert_time,
408                  NOTIFICATION_CHECK_STR(args), NOTIFICATION_CHECK_STR(group_args),
409                  NOTIFICATION_CHECK_STR(b_execute_option),
410                  NOTIFICATION_CHECK_STR(b_service_responding),
411                  NOTIFICATION_CHECK_STR(b_service_single_launch),
412                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
413                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
414                  noti->vibration_type,
415                  NOTIFICATION_CHECK_STR(noti->vibration_path),
416                  noti->led_operation,
417                  noti->led_argb,
418                  noti->led_on_ms,
419                  noti->led_off_ms,
420                  noti->flags_for_property, flag_simmode, noti->display_applist,
421                  noti->priv_id);
422
423         /* Free decoded data */
424         if (args) {
425                 free(args);
426         }
427         if (group_args) {
428                 free(group_args);
429         }
430
431         if (b_execute_option) {
432                 free(b_execute_option);
433         }
434         if (b_service_responding) {
435                 free(b_service_responding);
436         }
437         if (b_service_single_launch) {
438                 free(b_service_single_launch);
439         }
440         if (b_service_multi_launch) {
441                 free(b_service_multi_launch);
442         }
443
444         if (b_text) {
445                 free(b_text);
446         }
447         if (b_key) {
448                 free(b_key);
449         }
450         if (b_format_args) {
451                 free(b_format_args);
452         }
453
454         if (b_image_path) {
455                 free(b_image_path);
456         }
457
458         return NOTIFICATION_ERROR_NONE;
459 }
460
461 static void _notification_noti_populate_from_stmt(sqlite3_stmt * stmt, notification_h noti) {
462         int col = 0;
463
464         if (stmt == NULL || noti == NULL) {
465                 return ;
466         }
467
468         noti->type = sqlite3_column_int(stmt, col++);
469         noti->layout = sqlite3_column_int(stmt, col++);
470         __free_and_set((void **)&(noti->caller_pkgname), notification_db_column_text(stmt, col++));
471         __free_and_set((void **)&(noti->launch_pkgname), notification_db_column_text(stmt, col++));
472         noti->b_image_path = notification_db_column_bundle(stmt, col++);
473         noti->group_id = sqlite3_column_int(stmt, col++);
474         noti->internal_group_id = 0;
475         noti->priv_id = sqlite3_column_int(stmt, col++);
476
477         noti->b_text = notification_db_column_bundle(stmt, col++);
478         noti->b_key = notification_db_column_bundle(stmt, col++);
479         noti->b_format_args = notification_db_column_bundle(stmt, col++);
480         noti->num_format_args = sqlite3_column_int(stmt, col++);
481
482         __free_and_set((void **)&(noti->domain), notification_db_column_text(stmt, col++));
483         __free_and_set((void **)&(noti->dir), notification_db_column_text(stmt, col++));
484         noti->time = sqlite3_column_int(stmt, col++);
485         noti->insert_time = sqlite3_column_int(stmt, col++);
486         noti->args = notification_db_column_bundle(stmt, col++);
487         noti->group_args = notification_db_column_bundle(stmt, col++);
488
489         noti->b_execute_option = notification_db_column_bundle(stmt, col++);
490         noti->b_service_responding = notification_db_column_bundle(stmt, col++);
491         noti->b_service_single_launch =
492             notification_db_column_bundle(stmt, col++);
493         noti->b_service_multi_launch =
494             notification_db_column_bundle(stmt, col++);
495
496         noti->sound_type = sqlite3_column_int(stmt, col++);
497         __free_and_set((void **)&(noti->sound_path), notification_db_column_text(stmt, col++));
498         noti->vibration_type = sqlite3_column_int(stmt, col++);
499         __free_and_set((void **)&(noti->vibration_path), notification_db_column_text(stmt, col++));
500         noti->led_operation = sqlite3_column_int(stmt, col++);
501         noti->led_argb = sqlite3_column_int(stmt, col++);
502         noti->led_on_ms = sqlite3_column_int(stmt, col++);
503         noti->led_off_ms = sqlite3_column_int(stmt, col++);
504
505         noti->flags_for_property = sqlite3_column_int(stmt, col++);
506         noti->display_applist = sqlite3_column_int(stmt, col++);
507         noti->progress_size = sqlite3_column_double(stmt, col++);
508         noti->progress_percentage = sqlite3_column_double(stmt, col++);
509
510         noti->app_icon_path = NULL;
511         noti->app_name = NULL;
512         noti->temp_title = NULL;
513         noti->temp_content = NULL;
514 }
515
516 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
517 {
518         notification_h noti = NULL;
519
520         noti = (notification_h) calloc(1, sizeof(struct _notification));
521         if (noti == NULL) {
522                 return NULL;
523         }
524
525         _notification_noti_populate_from_stmt(stmt, noti);
526
527         return noti;
528 }
529
530 int notification_noti_set_tag(const char *tag, char *value, char *buf, int buf_len)
531 {
532         int len_total = 0;
533
534         len_total += (strlen(tag) * 2) + 5 + strlen(value) + 1;
535
536         if (buf_len <= len_total)
537                 return NOTIFICATION_ERROR_INVALID_DATA;
538
539         snprintf(buf, buf_len, "<%s>%s</%s>", tag, value, tag);
540
541         return NOTIFICATION_ERROR_NONE;
542 }
543
544 char *notification_noti_strip_tag(const char *tagged_str)
545 {
546         if (tagged_str == NULL)
547                 return NULL;
548
549         int len_total = strlen(tagged_str);
550
551         if (len_total == 0)
552                 return NULL;
553
554         char *b_f_e = strstr(tagged_str, ">");
555         char *b_e_s = strstr(tagged_str, "</");
556
557         if (b_f_e == NULL || b_e_s == NULL || (b_e_s - b_f_e - 1) <= 0)
558                 return NULL;
559
560         return strndup(b_f_e + 1, b_e_s - b_f_e - 1);
561 }
562
563 int notification_noti_get_tag_type(const char *tagged_str)
564 {
565         if (tagged_str == NULL)
566                 return TAG_TYPE_INVALID;
567
568         if (strlen(tagged_str)== 0)
569                 return TAG_TYPE_INVALID;
570
571         char *b_f_s = strstr(tagged_str, "<");
572         char *b_f_e = strstr(tagged_str, ">");
573
574         if (b_f_s == NULL || b_f_e == NULL || (b_f_e - b_f_s - 1) <= 0)
575                 return TAG_TYPE_INVALID;
576
577         char *start = b_f_s + 1;
578         int len_tag = b_f_e - b_f_s - 1;
579
580         if (strncmp(start,TAG_TIME,len_tag) == 0) {
581                 return TAG_TYPE_TIME;
582         }
583
584         return TAG_TYPE_INVALID;
585 }
586
587 static int _notification_noti_update_priv_id(sqlite3 * db, int rowid)
588 {
589         char query[128] = { 0, };
590
591         if (db == NULL) {
592                 return NOTIFICATION_ERROR_INVALID_DATA;
593         }
594
595         snprintf(query, sizeof(query), "update noti_list set "
596                         "priv_id = %d, internal_group_id = %d where rowid = %d",
597                         rowid, rowid, rowid);
598
599         return notification_db_exec(db, query, NULL);
600 }
601
602 EXPORT_API int notification_noti_insert(notification_h noti)
603 {
604         sqlite3 *db = NULL;
605         sqlite3_stmt *stmt = NULL;
606         char query[NOTIFICATION_QUERY_MAX] = { 0, };
607         int ret = 0;
608         char buf_key[32] = { 0, };
609         const char *title_key = NULL;
610
611         /* Open DB */
612         db = notification_db_open(DBPATH);
613         if (!db) {
614                 return NOTIFICATION_ERROR_FROM_DB;
615         }
616
617         /* Initialize private ID */
618         noti->priv_id = NOTIFICATION_PRIV_ID_NONE;
619         noti->group_id = NOTIFICATION_GROUP_ID_NONE;
620         noti->internal_group_id = NOTIFICATION_GROUP_ID_NONE;
621
622         /* make query */
623         ret = _notification_noti_make_query(noti, query, sizeof(query));
624         if (ret != NOTIFICATION_ERROR_NONE) {
625                 goto err;
626         }
627
628         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
629         if (ret != SQLITE_OK) {
630                 NOTIFICATION_ERR("Insert Query : %s", query);
631                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
632                                  sqlite3_errmsg(db));
633                 ret = NOTIFICATION_ERROR_FROM_DB;
634                 goto err;
635         }
636
637         /* Get title key */
638         if (noti->b_key != NULL) {
639                 snprintf(buf_key, sizeof(buf_key), "%d",
640                          NOTIFICATION_TEXT_TYPE_TITLE);
641
642                 title_key = bundle_get_val(noti->b_key, buf_key);
643         }
644
645         if (title_key == NULL && noti->b_text != NULL) {
646                 snprintf(buf_key, sizeof(buf_key), "%d",
647                          NOTIFICATION_TEXT_TYPE_TITLE);
648
649                 title_key = bundle_get_val(noti->b_text, buf_key);
650         }
651
652         if (title_key == NULL) {
653                 title_key = noti->caller_pkgname;
654         }
655
656         /* Bind query */
657         ret = _notification_noti_bind_query_text(stmt, "$title_key", title_key);
658         if (ret != NOTIFICATION_ERROR_NONE) {
659                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
660                 goto err;
661         }
662         ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
663         if (ret != NOTIFICATION_ERROR_NONE) {
664                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
665                 if (stmt) {
666                         sqlite3_finalize(stmt);
667                 }
668                 return ret;
669         }
670         ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
671         if (ret != NOTIFICATION_ERROR_NONE) {
672                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
673                 if (stmt) {
674                         sqlite3_finalize(stmt);
675                 }
676                 return ret;
677         }
678
679         ret = sqlite3_step(stmt);
680         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
681                 noti->priv_id = (int)sqlite3_last_insert_rowid(db);
682                 if (_notification_noti_update_priv_id(db, noti->priv_id) == 0) {
683                         ret = NOTIFICATION_ERROR_NONE;
684                 } else {
685                         ret = NOTIFICATION_ERROR_FROM_DB;
686                 }
687         } else {
688                 ret = NOTIFICATION_ERROR_FROM_DB;
689         }
690 err:
691         if (stmt) {
692                 sqlite3_finalize(stmt);
693         }
694
695         /* Close DB */
696         if (db) {
697                 notification_db_close(&db);
698         }
699
700         return ret;
701 }
702
703 int notification_noti_get_by_priv_id(notification_h noti, char *pkgname, int priv_id)
704 {
705         sqlite3 *db = NULL;
706         sqlite3_stmt *stmt = NULL;
707         char query[NOTIFICATION_QUERY_MAX] = { 0, };
708         int ret = 0;
709
710         if (priv_id < 0 || noti == NULL) {
711                 ret = NOTIFICATION_ERROR_INVALID_DATA;
712                 goto err;
713         }
714
715         /* Open DB */
716         db = notification_db_open(DBPATH);
717         if (!db) {
718                 return NOTIFICATION_ERROR_FROM_DB;
719         }
720
721         char *base_query = "select "
722                          "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
723                          "b_text, b_key, b_format_args, num_format_args, "
724                          "text_domain, text_dir, time, insert_time, args, group_args, "
725                          "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
726                          "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
727                          "flags_for_property, display_applist, progress_size, progress_percentage "
728                          "from noti_list ";
729
730         if (pkgname != NULL) {
731                 snprintf(query, sizeof(query), "%s where caller_pkgname = '%s' and priv_id = %d",
732                                 base_query ,pkgname, priv_id);
733         } else {
734                 snprintf(query, sizeof(query), "%s where priv_id = %d", base_query,  priv_id);
735         }
736
737         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
738         if (ret != SQLITE_OK) {
739                 NOTIFICATION_ERR("select Query : %s", query);
740                 NOTIFICATION_ERR("select DB error(%d) : %s", ret,
741                                  sqlite3_errmsg(db));
742                 ret = NOTIFICATION_ERROR_FROM_DB;
743                 goto err;
744         }
745
746         ret = sqlite3_step(stmt);
747         if (ret == SQLITE_ROW) {
748                 _notification_noti_populate_from_stmt(stmt, noti);
749                 ret = NOTIFICATION_ERROR_NONE;
750         } else {
751                 ret = NOTIFICATION_ERROR_FROM_DB;
752         }
753 err:
754         if (stmt) {
755                 sqlite3_finalize(stmt);
756         }
757
758         /* Close DB */
759         if (db != NULL) {
760                 notification_db_close(&db);
761         }
762
763         return ret;
764 }
765
766 EXPORT_API int notification_noti_update(notification_h noti)
767 {
768         sqlite3 *db;
769         sqlite3_stmt *stmt = NULL;
770         char query[NOTIFICATION_QUERY_MAX] = { 0, };
771         int ret = 0;
772
773         /* Open DB */
774         db = notification_db_open(DBPATH);
775         if (!db) {
776                 return NOTIFICATION_ERROR_FROM_DB;
777         }
778
779         /* Check private ID is exist */
780         ret = _notification_noti_check_priv_id(noti, db);
781         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
782                 ret = NOTIFICATION_ERROR_NOT_EXIST_ID;
783                 goto err;
784         }
785
786         /* make update query */
787         ret = _notification_noti_make_update_query(noti, query, sizeof(query));
788         if (ret != NOTIFICATION_ERROR_NONE) {
789                 goto err;
790         }
791
792         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
793         if (ret != SQLITE_OK) {
794                 NOTIFICATION_ERR("Insert Query : %s", query);
795                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
796                                  sqlite3_errmsg(db));
797                 ret = NOTIFICATION_ERROR_FROM_DB;
798                 goto err;
799         }
800
801         ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
802         if (ret != NOTIFICATION_ERROR_NONE) {
803                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
804                 goto err;
805         }
806         ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
807         if (ret != NOTIFICATION_ERROR_NONE) {
808                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
809                 goto err;
810         }
811
812         ret = sqlite3_step(stmt);
813         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
814                 ret = NOTIFICATION_ERROR_NONE;
815         } else {
816                 ret = NOTIFICATION_ERROR_FROM_DB;
817         }
818 err:
819         if (stmt) {
820                 sqlite3_finalize(stmt);
821         }
822
823         /* Close DB */
824         if (db) {
825                 notification_db_close(&db);
826         }
827
828         return ret;
829 }
830
831 EXPORT_API int notification_noti_delete_all(notification_type_e type, const char *pkgname, int *num_deleted, int **list_deleted_rowid)
832 {
833         int ret = NOTIFICATION_ERROR_NONE;
834         int ret_tmp = NOTIFICATION_ERROR_NONE;
835         int i = 0, data_cnt = 0;
836         sqlite3 *db = NULL;
837         sqlite3_stmt *stmt = NULL;
838         char buf[128] = { 0, };
839         char query[NOTIFICATION_QUERY_MAX] = { 0, };
840         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
841         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
842
843         /* Open DB */
844         db = notification_db_open(DBPATH);
845         if (!db) {
846                 return NOTIFICATION_ERROR_FROM_DB;
847         }
848
849         if (pkgname == NULL) {
850                 if (type != NOTIFICATION_TYPE_NONE) {
851                         snprintf(query_where, sizeof(query_where),
852                                  "where type = %d ", type);
853                 }
854         } else {
855                 if (type == NOTIFICATION_TYPE_NONE) {
856                         snprintf(query_where, sizeof(query_where),
857                                  "where caller_pkgname = '%s' ", pkgname);
858                 } else {
859                         snprintf(query_where, sizeof(query_where),
860                                  "where caller_pkgname = '%s' and type = %d ",
861                                  pkgname, type);
862                 }
863         }
864
865         if (num_deleted != NULL) {
866                 *num_deleted = 0;
867         }
868         if (list_deleted_rowid != NULL) {
869                 *list_deleted_rowid = NULL;
870                 snprintf(query, sizeof(query),
871                                 "select priv_id from noti_list %s ", query_where);
872
873                 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
874                 if (ret != SQLITE_OK) {
875                         NOTIFICATION_ERR("Select Query : %s", query);
876                         NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
877                                          sqlite3_errmsg(db));
878
879                         ret = NOTIFICATION_ERROR_FROM_DB;
880                         goto err;
881                 }
882
883                 while(sqlite3_step(stmt) == SQLITE_ROW) {
884                         if (data_cnt % 8 == 0) {
885                                 int *tmp;
886
887                                 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
888                                 if (tmp) {
889                                         *list_deleted_rowid = tmp;
890                                 } else {
891                                         NOTIFICATION_ERR("Heap: %s\n", strerror(errno));
892                                         /*!
893                                          * \TODO
894                                          * How can I handle this?
895                                          */
896                                         free(*list_deleted_rowid);
897                                         *list_deleted_rowid = NULL;
898                                         ret = NOTIFICATION_ERROR_NO_MEMORY;
899                                         goto err;
900                                 }
901                         }
902                         *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
903                         data_cnt++;
904                 }
905
906                 if (stmt) {
907                         sqlite3_finalize(stmt);
908                         stmt = NULL;
909                 }
910
911                 if (data_cnt > 0) {
912                         query_where[0] = '\0';
913                         snprintf(query_base, sizeof(query_base) - 1, "delete from noti_list");
914                         for (i = 0; i < data_cnt ; i++) {
915                                 if (i % NOTI_BURST_DELETE_UNIT == 0 && i != 0) {
916                                         snprintf(query, sizeof(query) - 1, "%s where priv_id in (%s)", query_base, query_where);
917                                         ret_tmp = notification_db_exec(db, query, NULL);
918                                         query_where[0] = '\0';
919                                         if (ret == NOTIFICATION_ERROR_NONE) {
920                                                 ret = ret_tmp;
921                                         }
922                                 }
923                                 snprintf(buf, sizeof(buf) - 1, "%s%d", (i % NOTI_BURST_DELETE_UNIT == 0) ? "" : ",", *((*list_deleted_rowid) + i));
924                                 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
925                         }
926                         if ((i <= NOTI_BURST_DELETE_UNIT) || ((i % NOTI_BURST_DELETE_UNIT) > 0) ) {
927                                 snprintf(query, sizeof(query) - 1, "%s where priv_id in (%s)", query_base, query_where);
928                                 ret_tmp = notification_db_exec(db, query, NULL);
929                                 if (ret == NOTIFICATION_ERROR_NONE) {
930                                         ret = ret_tmp;
931                                 }
932                         }
933                 } else {
934                         free(*list_deleted_rowid);
935                         *list_deleted_rowid = NULL;
936                 }
937
938                 if (num_deleted != NULL) {
939                         *num_deleted = data_cnt;
940                 }
941         } else {
942                 /* Make main query */
943                 snprintf(query_base, sizeof(query_base), "delete from noti_list ");
944                 snprintf(query, sizeof(query), "%s %s", query_base, query_where);
945
946                 ret = notification_db_exec(db, query, NULL);
947
948                 if (num_deleted != NULL) {
949                         *num_deleted = sqlite3_changes(db);
950                 }
951         }
952
953 err:
954         if (stmt) {
955                 sqlite3_finalize(stmt);
956         }
957         /* Close DB */
958         if (db) {
959                 notification_db_close(&db);
960         }
961
962         return ret;
963 }
964
965 int notification_noti_delete_group_by_group_id(const char *pkgname,
966                                                int group_id, int *num_deleted, int **list_deleted_rowid)
967 {
968         int ret = NOTIFICATION_ERROR_NONE;
969         int ret_tmp = NOTIFICATION_ERROR_NONE;
970         sqlite3 *db = NULL;
971         int i = 0, data_cnt = 0;
972         sqlite3_stmt *stmt = NULL;
973         char buf[128] = { 0, };
974         char query[NOTIFICATION_QUERY_MAX] = { 0, };
975         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
976         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
977
978         /* Check pkgname is valid */
979         if (pkgname == NULL) {
980                 return NOTIFICATION_ERROR_INVALID_DATA;
981         }
982
983         snprintf(query_where, sizeof(query_where),
984                         "where caller_pkgname = '%s' and group_id = %d", pkgname, group_id);
985
986         /* Open DB */
987         db = notification_db_open(DBPATH);
988         if (!db) {
989                 return NOTIFICATION_ERROR_FROM_DB;
990         }
991
992         if (num_deleted != NULL) {
993                 *num_deleted = 0;
994         }
995         if (list_deleted_rowid != NULL) {
996                 *list_deleted_rowid = NULL;
997                 snprintf(query, sizeof(query),
998                                 "select priv_id from noti_list %s ", query_where);
999
1000                 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1001                 if (ret != SQLITE_OK) {
1002                         NOTIFICATION_ERR("Select Query : %s", query);
1003                         NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1004                                          sqlite3_errmsg(db));
1005
1006                         ret = NOTIFICATION_ERROR_FROM_DB;
1007                         goto err;
1008                 }
1009
1010                 while(sqlite3_step(stmt) == SQLITE_ROW) {
1011                         if (data_cnt % 8 == 0) {
1012                                 int *tmp;
1013                                 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1014                                 if (tmp) {
1015                                         *list_deleted_rowid = tmp;
1016                                 } else {
1017                                         free(*list_deleted_rowid);
1018                                         *list_deleted_rowid = NULL;
1019                                         ret = NOTIFICATION_ERROR_NO_MEMORY;
1020                                         goto err;
1021                                 }
1022                         }
1023                         *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1024                         data_cnt++;
1025                 }
1026
1027                 if (stmt) {
1028                         sqlite3_finalize(stmt);
1029                         stmt = NULL;
1030                 }
1031
1032                 if (data_cnt > 0) {
1033                         query_where[0] = '\0';
1034                         snprintf(query_base, sizeof(query_base) - 1, "delete from noti_list");
1035                         for (i = 0; i < data_cnt ; i++) {
1036                                 if (i % NOTI_BURST_DELETE_UNIT == 0 && i != 0) {
1037                                         snprintf(query, sizeof(query) - 1, "%s where priv_id in (%s)", query_base, query_where);
1038                                         ret_tmp = notification_db_exec(db, query, NULL);
1039                                         query_where[0] = '\0';
1040                                         if (ret == NOTIFICATION_ERROR_NONE) {
1041                                                 ret = ret_tmp;
1042                                         }
1043                                 }
1044                                 snprintf(buf, sizeof(buf) - 1, "%s%d", (i % NOTI_BURST_DELETE_UNIT == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1045                                 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1046                         }
1047                         if ((i <= NOTI_BURST_DELETE_UNIT) || ((i % NOTI_BURST_DELETE_UNIT) > 0) ) {
1048                                 snprintf(query, sizeof(query) - 1, "%s where priv_id in (%s)", query_base, query_where);
1049                                 ret_tmp = notification_db_exec(db, query, NULL);
1050                                 if (ret == NOTIFICATION_ERROR_NONE) {
1051                                         ret = ret_tmp;
1052                                 }
1053                         }
1054                 } else {
1055                         free(*list_deleted_rowid);
1056                         *list_deleted_rowid = NULL;
1057                 }
1058
1059                 if (num_deleted != NULL) {
1060                         *num_deleted = data_cnt;
1061                 }
1062         } else {
1063                 /* Make query */
1064                 snprintf(query, sizeof(query), "delete from noti_list %s", query_where);
1065
1066                 /* execute DB */
1067                 ret = notification_db_exec(db, query, NULL);
1068         }
1069
1070 err:
1071         if (stmt) {
1072                 sqlite3_finalize(stmt);
1073         }
1074         /* Close DB */
1075         if (db) {
1076                 notification_db_close(&db);
1077         }
1078
1079         return ret;
1080 }
1081
1082 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
1083 {
1084         sqlite3 *db = NULL;
1085         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1086         int internal_group_id = 0;
1087         int ret;
1088
1089         /* Check pkgname is valid */
1090         if (pkgname == NULL) {
1091                 return NOTIFICATION_ERROR_INVALID_DATA;
1092         }
1093
1094         /* Open DB */
1095         db = notification_db_open(DBPATH);
1096         if (!db) {
1097                 return NOTIFICATION_ERROR_FROM_DB;
1098         }
1099
1100         /* Get internal group id using priv id */
1101         internal_group_id =
1102             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1103                                                                 priv_id, db);
1104
1105         /* Make query */
1106         snprintf(query, sizeof(query), "delete from noti_list "
1107                  "where caller_pkgname = '%s' and internal_group_id = %d",
1108                  pkgname, internal_group_id);
1109
1110         /* execute DB */
1111         ret = notification_db_exec(db, query, NULL);
1112
1113         /* Close DB */
1114         notification_db_close(&db);
1115
1116         return ret;
1117 }
1118
1119 EXPORT_API int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
1120 {
1121         sqlite3 *db = NULL;
1122         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1123         int ret;
1124
1125         /* Check pkgname is valid */
1126         if (pkgname == NULL) {
1127                 return NOTIFICATION_ERROR_INVALID_DATA;
1128         }
1129
1130         /* Open DB */
1131         db = notification_db_open(DBPATH);
1132         if (!db) {
1133                 return NOTIFICATION_ERROR_FROM_DB;
1134         }
1135
1136         /* Make query */
1137         snprintf(query, sizeof(query), "delete from noti_list "
1138                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1139                  priv_id);
1140
1141         /* execute DB */
1142         ret = notification_db_exec(db, query, NULL);
1143
1144         /* Close DB */
1145         if (db) {
1146                 notification_db_close(&db);
1147         }
1148
1149         return ret;
1150 }
1151
1152 EXPORT_API int notification_noti_delete_by_priv_id_get_changes(const char *pkgname, int priv_id, int *num_changes)
1153 {
1154         sqlite3 *db = NULL;
1155         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1156         int ret;
1157
1158         /* Check pkgname is valid */
1159         if (pkgname == NULL) {
1160                 return NOTIFICATION_ERROR_INVALID_DATA;
1161         }
1162
1163         /* Open DB */
1164         db = notification_db_open(DBPATH);
1165         if (!db) {
1166                 return NOTIFICATION_ERROR_FROM_DB;
1167         }
1168
1169         /* Make query */
1170         snprintf(query, sizeof(query), "delete from noti_list "
1171                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1172                  priv_id);
1173
1174         /* execute DB */
1175         ret = notification_db_exec(db, query, num_changes);
1176
1177         if (num_changes != NULL) {
1178                 NOTIFICATION_DBG("deleted num:%d", *num_changes);
1179         }
1180
1181         /* Close DB */
1182         if (db) {
1183                 notification_db_close(&db);
1184         }
1185
1186         return ret;
1187 }
1188
1189 notification_error_e notification_noti_get_count(notification_type_e type,
1190                                                  const char *pkgname,
1191                                                  int group_id, int priv_id,
1192                                                  int *count)
1193 {
1194         sqlite3 *db = NULL;
1195         sqlite3_stmt *stmt = NULL;
1196         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1197         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1198         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1199         char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
1200
1201         int ret = 0, get_count = 0, internal_group_id = 0;
1202         int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
1203         int flag_where = 0;
1204         int flag_where_more = 0;
1205         int ret_vconf = 0;
1206
1207         /* Open DB */
1208         db = notification_db_open(DBPATH);
1209         if (!db) {
1210                 return NOTIFICATION_ERROR_FROM_DB;
1211         }
1212
1213         /* Check current sim status */
1214         ret_vconf = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1215
1216         /* Make query */
1217         snprintf(query_base, sizeof(query_base),
1218                  "select count(*) from noti_list ");
1219
1220         if (pkgname != NULL) {
1221                 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
1222                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1223                                 snprintf(query_where, sizeof(query_where),
1224                                          "where caller_pkgname = '%s' ",
1225                                          pkgname);
1226                                 flag_where = 1;
1227                         } else {
1228                                 internal_group_id =
1229                                     _notification_noti_get_internal_group_id_by_priv_id
1230                                     (pkgname, priv_id, db);
1231                                 snprintf(query_where, sizeof(query_where),
1232                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
1233                                          pkgname, internal_group_id);
1234                                 flag_where = 1;
1235                         }
1236                 } else {
1237                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1238                                 snprintf(query_where, sizeof(query_where),
1239                                          "where caller_pkgname = '%s' and group_id = %d ",
1240                                          pkgname, group_id);
1241                                 flag_where = 1;
1242                         } else {
1243                                 internal_group_id =
1244                                     _notification_noti_get_internal_group_id_by_priv_id
1245                                     (pkgname, priv_id, db);
1246                                 snprintf(query_where, sizeof(query_where),
1247                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
1248                                          pkgname, internal_group_id);
1249                                 flag_where = 1;
1250                         }
1251                 }
1252
1253         }
1254
1255         if (ret_vconf == 0 && status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1256                 if (type != NOTIFICATION_TYPE_NONE) {
1257                         snprintf(query_where_more, sizeof(query_where_more),
1258                                  "type = %d ", type);
1259                         flag_where_more = 1;
1260                 }
1261         } else {
1262                 if (type != NOTIFICATION_TYPE_NONE) {
1263                         snprintf(query_where_more, sizeof(query_where_more),
1264                                  "type = %d and flag_simmode = 0 ", type);
1265                         flag_where_more = 1;
1266                 } else {
1267                         snprintf(query_where_more, sizeof(query_where_more),
1268                                  "flag_simmode = 0 ");
1269                         flag_where_more = 1;
1270                 }
1271         }
1272
1273         if (flag_where == 1) {
1274                 if (flag_where_more == 1) {
1275                         snprintf(query, sizeof(query), "%s %s and %s",
1276                                  query_base, query_where, query_where_more);
1277                 } else {
1278                         snprintf(query, sizeof(query), "%s %s", query_base,
1279                                  query_where);
1280                 }
1281
1282         } else {
1283                 if (flag_where_more == 1) {
1284                         snprintf(query, sizeof(query), "%s where %s",
1285                                  query_base, query_where_more);
1286                 } else {
1287                         snprintf(query, sizeof(query), "%s", query_base);
1288                 }
1289         }
1290
1291         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1292         if (ret != SQLITE_OK) {
1293                 NOTIFICATION_ERR("Select Query : %s", query);
1294                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1295                                  sqlite3_errmsg(db));
1296
1297                 ret = NOTIFICATION_ERROR_FROM_DB;
1298                 goto err;
1299         }
1300
1301         ret = sqlite3_step(stmt);
1302         if (ret == SQLITE_ROW) {
1303                 get_count = sqlite3_column_int(stmt, 0);
1304         }
1305
1306         ret = NOTIFICATION_ERROR_NONE;
1307
1308 err:
1309         if (stmt) {
1310                 sqlite3_finalize(stmt);
1311         }
1312
1313         /* Close DB */
1314         if (db) {
1315                 notification_db_close(&db);
1316         }
1317
1318         *count = get_count;
1319
1320         return ret;
1321 }
1322
1323 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
1324                                                          int count,
1325                                                          notification_list_h *
1326                                                          list)
1327 {
1328         sqlite3 *db = NULL;
1329         sqlite3_stmt *stmt = NULL;
1330         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1331         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1332         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1333
1334         int ret = 0;
1335         notification_list_h get_list = NULL;
1336         notification_h noti = NULL;
1337         int internal_count = 0;
1338         int status;
1339
1340         /* Open DB */
1341         db = notification_db_open(DBPATH);
1342         if (!db) {
1343                 return NOTIFICATION_ERROR_FROM_DB;
1344         }
1345
1346         /* Check current sim status */
1347         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1348
1349         /* Make query */
1350         snprintf(query_base, sizeof(query_base), "select "
1351                  "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1352                  "b_text, b_key, b_format_args, num_format_args, "
1353                  "text_domain, text_dir, time, insert_time, args, group_args, "
1354                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1355                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
1356                  "flags_for_property, display_applist, progress_size, progress_percentage "
1357                  "from noti_list ");
1358
1359         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1360                 if (type != NOTIFICATION_TYPE_NONE) {
1361                         snprintf(query_where, sizeof(query_where),
1362                                  "where type = %d ", type);
1363                 }
1364         } else {
1365                 if (type != NOTIFICATION_TYPE_NONE) {
1366                         snprintf(query_where, sizeof(query_where),
1367                                  "where type = %d and flag_simmode = 0 ", type);
1368                 } else {
1369                         snprintf(query_where, sizeof(query_where),
1370                                  "where flag_simmode = 0 ");
1371                 }
1372         }
1373
1374         snprintf(query, sizeof(query),
1375                  "%s %s "
1376                  "group by internal_group_id "
1377                  "order by rowid desc, time desc", query_base, query_where);
1378
1379         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1380         if (ret != SQLITE_OK) {
1381                 NOTIFICATION_ERR("Select Query : %s", query);
1382                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1383                                  sqlite3_errmsg(db));
1384
1385                 ret = NOTIFICATION_ERROR_FROM_DB;
1386                 goto err;
1387         }
1388
1389         while (sqlite3_step(stmt) == SQLITE_ROW) {
1390                 /* Make notification list */
1391                 noti = _notification_noti_get_item(stmt);
1392                 if (noti != NULL) {
1393                         internal_count++;
1394
1395                         get_list = notification_list_append(get_list, noti);
1396
1397                         if (count != -1 && internal_count >= count) {
1398                                 NOTIFICATION_INFO
1399                                     ("internal count %d >= count %d",
1400                                      internal_count, count);
1401                                 break;
1402                         }
1403                 }
1404         }
1405
1406         ret = NOTIFICATION_ERROR_NONE;
1407
1408 err:
1409         if (stmt) {
1410                 sqlite3_finalize(stmt);
1411         }
1412
1413         /* Close DB */
1414         if (db) {
1415                 notification_db_close(&db);
1416         }
1417
1418         if (get_list != NULL) {
1419                 *list = notification_list_get_head(get_list);
1420         }
1421
1422         return ret;
1423 }
1424
1425 notification_error_e notification_noti_get_detail_list(const char *pkgname,
1426                                                        int group_id,
1427                                                        int priv_id, int count,
1428                                                        notification_list_h *list)
1429 {
1430         sqlite3 *db = NULL;
1431         sqlite3_stmt *stmt = NULL;
1432         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1433         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1434
1435         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1436         int ret = 0;
1437         notification_list_h get_list = NULL;
1438         notification_h noti = NULL;
1439         int internal_count = 0;
1440         int internal_group_id = 0;
1441         int status = 0; /* If the vconf_get_int failed, the status will be the garbage value */
1442
1443         /* Open DB */
1444         db = notification_db_open(DBPATH);
1445         if (!db) {
1446                 return NOTIFICATION_ERROR_FROM_DB;
1447         }
1448
1449         /* Check current sim status */
1450         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1451
1452         /* Make query */
1453         snprintf(query_base, sizeof(query_base), "select "
1454                  "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1455                  "b_text, b_key, b_format_args, num_format_args, "
1456                  "text_domain, text_dir, time, insert_time, args, group_args, "
1457                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1458                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
1459                  "flags_for_property, display_applist, progress_size, progress_percentage "
1460                  "from noti_list ");
1461
1462         if (priv_id == NOTIFICATION_PRIV_ID_NONE && group_id == NOTIFICATION_GROUP_ID_NONE) {
1463                 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1464                         snprintf(query_where, sizeof(query_where),
1465                                  "where  caller_pkgname = '%s' ", pkgname);
1466                 } else {
1467                         snprintf(query_where, sizeof(query_where),
1468                                  "where  caller_pkgname = '%s' and flag_simmode = 0 ", pkgname);
1469                 }
1470         } else {
1471                 internal_group_id =
1472                     _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1473                                                                         priv_id, db);
1474
1475                 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1476                         snprintf(query_where, sizeof(query_where),
1477                                  "where  caller_pkgname = '%s' and internal_group_id = %d ",
1478                                  pkgname, internal_group_id);
1479                 } else {
1480                         snprintf(query_where, sizeof(query_where),
1481                                  "where  caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
1482                                  pkgname, internal_group_id);
1483                 }
1484         }
1485
1486         snprintf(query, sizeof(query),
1487                  "%s %s "
1488                  "order by rowid desc, time desc", query_base, query_where);
1489
1490         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1491         if (ret != SQLITE_OK) {
1492                 NOTIFICATION_ERR("Select Query : %s", query);
1493                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1494                                  sqlite3_errmsg(db));
1495
1496                 ret = NOTIFICATION_ERROR_FROM_DB;
1497                 goto err;
1498         }
1499
1500         while (sqlite3_step(stmt) == SQLITE_ROW) {
1501                 /* Make notification list */
1502                 noti = _notification_noti_get_item(stmt);
1503                 if (noti != NULL) {
1504                         internal_count++;
1505
1506                         get_list = notification_list_append(get_list, noti);
1507
1508                         if (count != -1 && internal_count >= count) {
1509                                 NOTIFICATION_INFO
1510                                     ("internal count %d >= count %d",
1511                                      internal_count, count);
1512                                 break;
1513                         }
1514                 }
1515         }
1516
1517         ret = NOTIFICATION_ERROR_NONE;
1518
1519 err:
1520         if (stmt) {
1521                 sqlite3_finalize(stmt);
1522         }
1523
1524         /* Close DB */
1525         if (db) {
1526                 notification_db_close(&db);
1527         }
1528
1529         if (get_list != NULL) {
1530                 *list = notification_list_get_head(get_list);
1531         }
1532
1533         return ret;
1534 }