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