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 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,"
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,"
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->flags_for_property, flag_simmode, noti->display_applist);
421
422         /* Free decoded data */
423         if (args) {
424                 free(args);
425         }
426         if (group_args) {
427                 free(group_args);
428         }
429
430         if (b_execute_option) {
431                 free(b_execute_option);
432         }
433         if (b_service_responding) {
434                 free(b_service_responding);
435         }
436         if (b_service_single_launch) {
437                 free(b_service_single_launch);
438         }
439         if (b_service_multi_launch) {
440                 free(b_service_multi_launch);
441         }
442
443         if (b_text) {
444                 free(b_text);
445         }
446         if (b_key) {
447                 free(b_key);
448         }
449         if (b_format_args) {
450                 free(b_format_args);
451         }
452
453         if (b_image_path) {
454                 free(b_image_path);
455         }
456
457         return NOTIFICATION_ERROR_NONE;
458 }
459
460
461 static int _notification_noti_make_update_query(notification_h noti, char *query,
462                                          int query_size)
463 {
464         char *args = NULL;
465         char *group_args = NULL;
466         char *b_image_path = NULL;
467         char *b_execute_option = NULL;
468         char *b_service_responding = NULL;
469         char *b_service_single_launch = NULL;
470         char *b_service_multi_launch = NULL;
471         char *b_text = NULL;
472         char *b_key = NULL;
473         char *b_format_args = NULL;
474         int flag_simmode = 0;
475
476         /* Decode bundle to update DB */
477         if (noti->args) {
478                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
479         }
480         if (noti->group_args) {
481                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
482                               NULL);
483         }
484
485         if (noti->b_execute_option) {
486                 bundle_encode(noti->b_execute_option,
487                               (bundle_raw **) & b_execute_option, NULL);
488         }
489         if (noti->b_service_responding) {
490                 bundle_encode(noti->b_service_responding,
491                               (bundle_raw **) & b_service_responding, NULL);
492         }
493         if (noti->b_service_single_launch) {
494                 bundle_encode(noti->b_service_single_launch,
495                               (bundle_raw **) & b_service_single_launch, NULL);
496         }
497         if (noti->b_service_multi_launch) {
498                 bundle_encode(noti->b_service_multi_launch,
499                               (bundle_raw **) & b_service_multi_launch, NULL);
500         }
501
502         if (noti->b_text) {
503                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
504         }
505         if (noti->b_key) {
506                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
507         }
508         if (noti->b_format_args) {
509                 bundle_encode(noti->b_format_args,
510                               (bundle_raw **) & b_format_args, NULL);
511         }
512
513         if (noti->b_image_path) {
514                 bundle_encode(noti->b_image_path,
515                               (bundle_raw **) & b_image_path, NULL);
516         }
517
518         /* Check only simmode property is enable */
519         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
520                 flag_simmode = 1;
521         }
522
523         /* Make query */
524         snprintf(query, query_size, "update noti_list set "
525                  "type = %d, "
526                  "layout = %d, "
527                  "launch_pkgname = '%s', "
528                  "image_path = '%s', "
529                  "b_text = '%s', b_key = '%s', "
530                  "b_format_args = '%s', num_format_args = %d, "
531                  "text_domain = '%s', text_dir = '%s', "
532                  "time = %d, insert_time = %d, "
533                  "args = '%s', group_args = '%s', "
534                  "b_execute_option = '%s', "
535                  "b_service_responding = '%s', "
536                  "b_service_single_launch = '%s', "
537                  "b_service_multi_launch = '%s', "
538                  "sound_type = %d, sound_path = '%s', "
539                  "vibration_type = %d, vibration_path = '%s', "
540                  "led_operation = %d, led_argb = %d, "
541                  "flags_for_property = %d, flag_simmode = %d, "
542                  "display_applist = %d, "
543                  "progress_size = $progress_size, progress_percentage = $progress_percentage "
544                  "where priv_id = %d ",
545                  noti->type,
546                  noti->layout,
547                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
548                  NOTIFICATION_CHECK_STR(b_image_path),
549                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
550                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
551                  NOTIFICATION_CHECK_STR(noti->domain),
552                  NOTIFICATION_CHECK_STR(noti->dir),
553                  (int)noti->time, (int)noti->insert_time,
554                  NOTIFICATION_CHECK_STR(args), NOTIFICATION_CHECK_STR(group_args),
555                  NOTIFICATION_CHECK_STR(b_execute_option),
556                  NOTIFICATION_CHECK_STR(b_service_responding),
557                  NOTIFICATION_CHECK_STR(b_service_single_launch),
558                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
559                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
560                  noti->vibration_type,
561                  NOTIFICATION_CHECK_STR(noti->vibration_path),
562                  noti->led_operation,
563                  noti->led_argb,
564                  noti->flags_for_property, flag_simmode, noti->display_applist,
565                  noti->priv_id);
566
567         /* Free decoded data */
568         if (args) {
569                 free(args);
570         }
571         if (group_args) {
572                 free(group_args);
573         }
574
575         if (b_execute_option) {
576                 free(b_execute_option);
577         }
578         if (b_service_responding) {
579                 free(b_service_responding);
580         }
581         if (b_service_single_launch) {
582                 free(b_service_single_launch);
583         }
584         if (b_service_multi_launch) {
585                 free(b_service_multi_launch);
586         }
587
588         if (b_text) {
589                 free(b_text);
590         }
591         if (b_key) {
592                 free(b_key);
593         }
594         if (b_format_args) {
595                 free(b_format_args);
596         }
597
598         if (b_image_path) {
599                 free(b_image_path);
600         }
601
602         return NOTIFICATION_ERROR_NONE;
603 }
604
605 static void _notification_noti_populate_from_stmt(sqlite3_stmt * stmt, notification_h noti) {
606         int col = 0;
607
608         if (stmt == NULL || noti == NULL) {
609                 return ;
610         }
611
612         noti->type = sqlite3_column_int(stmt, col++);
613         noti->layout = sqlite3_column_int(stmt, col++);
614         noti->caller_pkgname = notification_db_column_text(stmt, col++);
615         noti->launch_pkgname = notification_db_column_text(stmt, col++);
616         noti->b_image_path = notification_db_column_bundle(stmt, col++);
617         noti->group_id = sqlite3_column_int(stmt, col++);
618         noti->internal_group_id = 0;
619         noti->priv_id = sqlite3_column_int(stmt, col++);
620
621         noti->b_text = notification_db_column_bundle(stmt, col++);
622         noti->b_key = notification_db_column_bundle(stmt, col++);
623         noti->b_format_args = notification_db_column_bundle(stmt, col++);
624         noti->num_format_args = sqlite3_column_int(stmt, col++);
625
626         noti->domain = notification_db_column_text(stmt, col++);
627         noti->dir = notification_db_column_text(stmt, col++);
628         noti->time = sqlite3_column_int(stmt, col++);
629         noti->insert_time = sqlite3_column_int(stmt, col++);
630         noti->args = notification_db_column_bundle(stmt, col++);
631         noti->group_args = notification_db_column_bundle(stmt, col++);
632
633         noti->b_execute_option = notification_db_column_bundle(stmt, col++);
634         noti->b_service_responding = notification_db_column_bundle(stmt, col++);
635         noti->b_service_single_launch =
636             notification_db_column_bundle(stmt, col++);
637         noti->b_service_multi_launch =
638             notification_db_column_bundle(stmt, col++);
639
640         noti->sound_type = sqlite3_column_int(stmt, col++);
641         noti->sound_path = notification_db_column_text(stmt, col++);
642         noti->vibration_type = sqlite3_column_int(stmt, col++);
643         noti->vibration_path = notification_db_column_text(stmt, col++);
644         noti->led_operation = sqlite3_column_int(stmt, col++);
645         noti->led_argb = sqlite3_column_int(stmt, col++);
646
647         noti->flags_for_property = sqlite3_column_int(stmt, col++);
648         noti->display_applist = sqlite3_column_int(stmt, col++);
649         noti->progress_size = sqlite3_column_double(stmt, col++);
650         noti->progress_percentage = sqlite3_column_double(stmt, col++);
651
652         noti->app_icon_path = NULL;
653         noti->app_name = NULL;
654         noti->temp_title = NULL;
655         noti->temp_content = NULL;
656 }
657
658 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
659 {
660         notification_h noti = NULL;
661
662         noti = malloc(sizeof(struct _notification));
663         if (noti == NULL) {
664                 return NULL;
665         }
666
667         _notification_noti_populate_from_stmt(stmt, noti);
668
669         return noti;
670 }
671
672 int notification_noti_set_tag(const char *tag, char *value, char *buf, int buf_len)
673 {
674         int len_total = 0;
675
676         len_total += (strlen(tag) * 2) + 5 + strlen(value) + 1;
677
678         if (buf_len <= len_total)
679                 return NOTIFICATION_ERROR_INVALID_DATA;
680
681         snprintf(buf, buf_len, "<%s>%s</%s>", tag, value, tag);
682
683         return NOTIFICATION_ERROR_NONE;
684 }
685
686 char *notification_noti_strip_tag(const char *tagged_str)
687 {
688         if (tagged_str == NULL)
689                 return NULL;
690
691         int len_total = strlen(tagged_str);
692
693         if (len_total == 0)
694                 return NULL;
695
696         char *b_f_e = strstr(tagged_str, ">");
697         char *b_e_s = strstr(tagged_str, "</");
698
699         if (b_f_e == NULL || b_e_s == NULL || (b_e_s - b_f_e - 1) <= 0)
700                 return NULL;
701
702         return strndup(b_f_e + 1, b_e_s - b_f_e - 1);
703 }
704
705 int notification_noti_get_tag_type(const char *tagged_str)
706 {
707         if (tagged_str == NULL)
708                 return TAG_TYPE_INVALID;
709
710         if (strlen(tagged_str)== 0)
711                 return TAG_TYPE_INVALID;
712
713         char *b_f_s = strstr(tagged_str, "<");
714         char *b_f_e = strstr(tagged_str, ">");
715
716         if (b_f_s == NULL || b_f_e == NULL || (b_f_e - b_f_s - 1) <= 0)
717                 return TAG_TYPE_INVALID;
718
719         char *start = b_f_s + 1;
720         int len_tag = b_f_e - b_f_s - 1;
721
722         if (strncmp(start,TAG_TIME,len_tag) == 0) {
723                 return TAG_TYPE_TIME;
724         }
725
726         return TAG_TYPE_INVALID;
727 }
728
729 static int _notification_noti_update_priv_id(sqlite3 * db, int rowid)
730 {
731         char query[128] = { 0, };
732
733         if (db == NULL) {
734                 return NOTIFICATION_ERROR_INVALID_DATA;
735         }
736
737         snprintf(query, sizeof(query), "update noti_list set "
738                         "priv_id = %d, internal_group_id = %d where rowid = %d",
739                         rowid, rowid, rowid);
740
741         return notification_db_exec(db, query);
742 }
743
744 EXPORT_API int notification_noti_insert(notification_h noti)
745 {
746         sqlite3 *db = NULL;
747         sqlite3_stmt *stmt = NULL;
748         char query[NOTIFICATION_QUERY_MAX] = { 0, };
749         int ret = 0;
750         char buf_key[32] = { 0, };
751         const char *title_key = NULL;
752
753         /* Open DB */
754         db = notification_db_open(DBPATH);
755         if (!db) {
756                 return NOTIFICATION_ERROR_FROM_DB;
757         }
758
759         /* Initialize private ID */
760         noti->priv_id = NOTIFICATION_PRIV_ID_NONE;
761         noti->group_id = NOTIFICATION_GROUP_ID_NONE;
762         noti->internal_group_id = NOTIFICATION_GROUP_ID_NONE;
763
764         /* make query */
765         ret = _notification_noti_make_query(noti, query, sizeof(query));
766         if (ret != NOTIFICATION_ERROR_NONE) {
767                 goto err;
768         }
769
770         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
771         if (ret != SQLITE_OK) {
772                 NOTIFICATION_ERR("Insert Query : %s", query);
773                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
774                                  sqlite3_errmsg(db));
775                 ret = NOTIFICATION_ERROR_FROM_DB;
776                 goto err;
777         }
778
779         /* Get title key */
780         if (noti->b_key != NULL) {
781                 snprintf(buf_key, sizeof(buf_key), "%d",
782                          NOTIFICATION_TEXT_TYPE_TITLE);
783
784                 title_key = bundle_get_val(noti->b_key, buf_key);
785         }
786
787         if (title_key == NULL && noti->b_text != NULL) {
788                 snprintf(buf_key, sizeof(buf_key), "%d",
789                          NOTIFICATION_TEXT_TYPE_TITLE);
790
791                 title_key = bundle_get_val(noti->b_text, buf_key);
792         }
793
794         if (title_key == NULL) {
795                 title_key = noti->caller_pkgname;
796         }
797
798         /* Bind query */
799         ret = _notification_noti_bind_query_text(stmt, "$title_key", title_key);
800         if (ret != NOTIFICATION_ERROR_NONE) {
801                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
802                 goto err;
803         }
804         ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
805         if (ret != NOTIFICATION_ERROR_NONE) {
806                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
807                 if (stmt) {
808                         sqlite3_finalize(stmt);
809                 }
810                 return ret;
811         }
812         ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
813         if (ret != NOTIFICATION_ERROR_NONE) {
814                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
815                 if (stmt) {
816                         sqlite3_finalize(stmt);
817                 }
818                 return ret;
819         }
820
821         ret = sqlite3_step(stmt);
822         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
823                 noti->priv_id = (int)sqlite3_last_insert_rowid(db);
824                 if (_notification_noti_update_priv_id(db, noti->priv_id) == 0) {
825                         ret = NOTIFICATION_ERROR_NONE;
826                 } else {
827                         ret = NOTIFICATION_ERROR_FROM_DB;
828                 }
829         } else {
830                 ret = NOTIFICATION_ERROR_FROM_DB;
831         }
832 err:
833         if (stmt) {
834                 sqlite3_finalize(stmt);
835         }
836
837         /* Close DB */
838         if (db) {
839                 notification_db_close(&db);
840         }
841
842         return ret;
843 }
844
845 int notification_noti_get_by_priv_id(notification_h noti, char *pkgname, int priv_id)
846 {
847         sqlite3 *db = NULL;
848         sqlite3_stmt *stmt = NULL;
849         char query[NOTIFICATION_QUERY_MAX] = { 0, };
850         int ret = 0;
851
852         if (priv_id < 0 || noti == NULL) {
853                 ret = NOTIFICATION_ERROR_INVALID_DATA;
854                 goto err;
855         }
856
857         /* Open DB */
858         db = notification_db_open(DBPATH);
859         if (!db) {
860                 return NOTIFICATION_ERROR_FROM_DB;
861         }
862
863         char *base_query = "select "
864                          "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
865                          "b_text, b_key, b_format_args, num_format_args, "
866                          "text_domain, text_dir, time, insert_time, args, group_args, "
867                          "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
868                          "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
869                          "flags_for_property, display_applist, progress_size, progress_percentage "
870                          "from noti_list ";
871
872         if (pkgname != NULL) {
873                 snprintf(query, sizeof(query), "%s where caller_pkgname = '%s' and priv_id = %d",
874                                 base_query ,pkgname, priv_id);
875         } else {
876                 snprintf(query, sizeof(query), "%s where priv_id = %d", base_query,  priv_id);
877         }
878
879         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
880         if (ret != SQLITE_OK) {
881                 NOTIFICATION_ERR("select Query : %s", query);
882                 NOTIFICATION_ERR("select DB error(%d) : %s", ret,
883                                  sqlite3_errmsg(db));
884                 ret = NOTIFICATION_ERROR_FROM_DB;
885                 goto err;
886         }
887
888         ret = sqlite3_step(stmt);
889         if (ret == SQLITE_ROW) {
890                 _notification_noti_populate_from_stmt(stmt, noti);
891                 ret = NOTIFICATION_ERROR_NONE;
892         } else {
893                 ret = NOTIFICATION_ERROR_FROM_DB;
894         }
895 err:
896         if (stmt) {
897                 sqlite3_finalize(stmt);
898         }
899
900         /* Close DB */
901         if (db != NULL) {
902                 notification_db_close(&db);
903         }
904
905         return ret;
906 }
907
908 EXPORT_API int notification_noti_update(notification_h noti)
909 {
910         sqlite3 *db;
911         sqlite3_stmt *stmt = NULL;
912         char query[NOTIFICATION_QUERY_MAX] = { 0, };
913         int ret = 0;
914
915         /* Open DB */
916         db = notification_db_open(DBPATH);
917         if (!db) {
918                 return NOTIFICATION_ERROR_FROM_DB;
919         }
920
921         /* Check private ID is exist */
922         ret = _notification_noti_check_priv_id(noti, db);
923         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
924                 ret = NOTIFICATION_ERROR_NOT_EXIST_ID;
925                 goto err;
926         }
927
928         /* make update query */
929         ret = _notification_noti_make_update_query(noti, query, sizeof(query));
930         if (ret != NOTIFICATION_ERROR_NONE) {
931                 goto err;
932         }
933
934         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
935         if (ret != SQLITE_OK) {
936                 NOTIFICATION_ERR("Insert Query : %s", query);
937                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
938                                  sqlite3_errmsg(db));
939                 ret = NOTIFICATION_ERROR_FROM_DB;
940                 goto err;
941         }
942
943         ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
944         if (ret != NOTIFICATION_ERROR_NONE) {
945                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
946                 goto err;
947         }
948         ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
949         if (ret != NOTIFICATION_ERROR_NONE) {
950                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
951                 goto err;
952         }
953
954         ret = sqlite3_step(stmt);
955         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
956                 ret = NOTIFICATION_ERROR_NONE;
957         } else {
958                 ret = NOTIFICATION_ERROR_FROM_DB;
959         }
960 err:
961         if (stmt) {
962                 sqlite3_finalize(stmt);
963         }
964
965         /* Close DB */
966         if (db) {
967                 notification_db_close(&db);
968         }
969
970         return ret;
971 }
972
973 EXPORT_API int notification_noti_delete_all(notification_type_e type, const char *pkgname, int *num_deleted, int **list_deleted_rowid)
974 {
975         int ret = NOTIFICATION_ERROR_NONE;
976         int i = 0, data_cnt = 0;
977         sqlite3 *db = NULL;
978         sqlite3_stmt *stmt = NULL;
979         char buf[128] = { 0, };
980         char query[NOTIFICATION_QUERY_MAX] = { 0, };
981         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
982         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
983
984         /* Open DB */
985         db = notification_db_open(DBPATH);
986         if (!db) {
987                 return NOTIFICATION_ERROR_FROM_DB;
988         }
989
990         if (pkgname == NULL) {
991                 if (type != NOTIFICATION_TYPE_NONE) {
992                         snprintf(query_where, sizeof(query_where),
993                                  "where type = %d ", type);
994                 }
995         } else {
996                 if (type == NOTIFICATION_TYPE_NONE) {
997                         snprintf(query_where, sizeof(query_where),
998                                  "where caller_pkgname = '%s' ", pkgname);
999                 } else {
1000                         snprintf(query_where, sizeof(query_where),
1001                                  "where caller_pkgname = '%s' and type = %d ",
1002                                  pkgname, type);
1003                 }
1004         }
1005
1006         if (num_deleted != NULL) {
1007                 *num_deleted = 0;
1008         }
1009         if (list_deleted_rowid != NULL) {
1010                 *list_deleted_rowid = NULL;
1011                 snprintf(query, sizeof(query),
1012                                 "select priv_id from noti_list %s ", query_where);
1013
1014                 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1015                 if (ret != SQLITE_OK) {
1016                         NOTIFICATION_ERR("Select Query : %s", query);
1017                         NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1018                                          sqlite3_errmsg(db));
1019
1020                         ret = NOTIFICATION_ERROR_FROM_DB;
1021                         goto err;
1022                 }
1023
1024                 while(sqlite3_step(stmt) == SQLITE_ROW) {
1025                         if (data_cnt % 8 == 0) {
1026                                 int *tmp;
1027
1028                                 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1029                                 if (tmp) {
1030                                         *list_deleted_rowid = tmp;
1031                                 } else {
1032                                         NOTIFICATION_ERR("Heap: %s\n", strerror(errno));
1033                                         /*!
1034                                          * \TODO
1035                                          * How can I handle this?
1036                                          */
1037                                         free(*list_deleted_rowid);
1038                                         *list_deleted_rowid = NULL;
1039                                         ret = NOTIFICATION_ERROR_NO_MEMORY;
1040                                         goto err;
1041                                 }
1042                         }
1043                         *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1044                         data_cnt++;
1045                 }
1046
1047                 if (stmt) {
1048                         sqlite3_finalize(stmt);
1049                         stmt = NULL;
1050                 }
1051
1052                 if (data_cnt > 0) {
1053                         query_where[0] = '\0';
1054                         for (i = 0; i < data_cnt ; i++) {
1055                                 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1056                                 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1057                         }
1058                         snprintf(query_base, sizeof(query_base), "delete from noti_list");
1059                         snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1060
1061                         NOTIFICATION_ERR("check : %s", query);
1062                         ret = notification_db_exec(db, query);
1063                 } else {
1064                         free(*list_deleted_rowid);
1065                         *list_deleted_rowid = NULL;
1066                 }
1067
1068                 if (num_deleted != NULL) {
1069                         *num_deleted = data_cnt;
1070                 }
1071         } else {
1072                 /* Make main query */
1073                 snprintf(query_base, sizeof(query_base), "delete from noti_list ");
1074                 snprintf(query, sizeof(query), "%s %s", query_base, query_where);
1075
1076                 ret = notification_db_exec(db, query);
1077
1078                 if (num_deleted != NULL) {
1079                         *num_deleted = sqlite3_changes(db);
1080                 }
1081         }
1082
1083 err:
1084         if (stmt) {
1085                 sqlite3_finalize(stmt);
1086         }
1087         /* Close DB */
1088         if (db) {
1089                 notification_db_close(&db);
1090         }
1091
1092         return ret;
1093 }
1094
1095 int notification_noti_delete_group_by_group_id(const char *pkgname,
1096                                                int group_id, int *num_deleted, int **list_deleted_rowid)
1097 {
1098         int ret = NOTIFICATION_ERROR_NONE;
1099         sqlite3 *db = NULL;
1100         int i = 0, data_cnt = 0;
1101         sqlite3_stmt *stmt = NULL;
1102         char buf[128] = { 0, };
1103         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1104         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1105         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1106
1107         /* Check pkgname is valid */
1108         if (pkgname == NULL) {
1109                 return NOTIFICATION_ERROR_INVALID_DATA;
1110         }
1111
1112         snprintf(query_where, sizeof(query_where),
1113                         "where caller_pkgname = '%s' and group_id = %d", pkgname, group_id);
1114
1115         /* Open DB */
1116         db = notification_db_open(DBPATH);
1117         if (!db) {
1118                 return NOTIFICATION_ERROR_FROM_DB;
1119         }
1120
1121         if (num_deleted != NULL) {
1122                 *num_deleted = 0;
1123         }
1124         if (list_deleted_rowid != NULL) {
1125                 *list_deleted_rowid = NULL;
1126                 snprintf(query, sizeof(query),
1127                                 "select priv_id from noti_list %s ", query_where);
1128
1129                 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1130                 if (ret != SQLITE_OK) {
1131                         NOTIFICATION_ERR("Select Query : %s", query);
1132                         NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1133                                          sqlite3_errmsg(db));
1134
1135                         ret = NOTIFICATION_ERROR_FROM_DB;
1136                         goto err;
1137                 }
1138
1139                 while(sqlite3_step(stmt) == SQLITE_ROW) {
1140                         if (data_cnt % 8 == 0) {
1141                                 int *tmp;
1142                                 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1143                                 if (tmp) {
1144                                         *list_deleted_rowid = tmp;
1145                                 } else {
1146                                         free(*list_deleted_rowid);
1147                                         *list_deleted_rowid = NULL;
1148                                         ret = NOTIFICATION_ERROR_NO_MEMORY;
1149                                         goto err;
1150                                 }
1151                         }
1152                         *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1153                         data_cnt++;
1154                 }
1155
1156                 if (stmt) {
1157                         sqlite3_finalize(stmt);
1158                         stmt = NULL;
1159                 }
1160
1161                 if (data_cnt > 0) {
1162                         query_where[0] = '\0';
1163                         for (i = 0; i < data_cnt ; i++) {
1164                                 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1165                                 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1166                         }
1167                         snprintf(query_base, sizeof(query_base), "delete from noti_list");
1168                         snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1169
1170                         NOTIFICATION_ERR("check : %s", query);
1171                         ret = notification_db_exec(db, query);
1172                 } else {
1173                         free(*list_deleted_rowid);
1174                         *list_deleted_rowid = NULL;
1175                 }
1176
1177                 if (num_deleted != NULL) {
1178                         *num_deleted = data_cnt;
1179                 }
1180         } else {
1181                 /* Make query */
1182                 snprintf(query, sizeof(query), "delete from noti_list %s", query_where);
1183
1184                 /* execute DB */
1185                 ret = notification_db_exec(db, query);
1186         }
1187
1188 err:
1189         if (stmt) {
1190                 sqlite3_finalize(stmt);
1191         }
1192         /* Close DB */
1193         if (db) {
1194                 notification_db_close(&db);
1195         }
1196
1197         return ret;
1198 }
1199
1200 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
1201 {
1202         sqlite3 *db = NULL;
1203         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1204         int internal_group_id = 0;
1205         int ret;
1206
1207         /* Check pkgname is valid */
1208         if (pkgname == NULL) {
1209                 return NOTIFICATION_ERROR_INVALID_DATA;
1210         }
1211
1212         /* Open DB */
1213         db = notification_db_open(DBPATH);
1214         if (!db) {
1215                 return NOTIFICATION_ERROR_FROM_DB;
1216         }
1217
1218         /* Get internal group id using priv id */
1219         internal_group_id =
1220             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1221                                                                 priv_id, db);
1222
1223         /* Make query */
1224         snprintf(query, sizeof(query), "delete from noti_list "
1225                  "where caller_pkgname = '%s' and internal_group_id = %d",
1226                  pkgname, internal_group_id);
1227
1228         /* execute DB */
1229         ret = notification_db_exec(db, query);
1230
1231         /* Close DB */
1232         notification_db_close(&db);
1233
1234         return ret;
1235 }
1236
1237 EXPORT_API int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
1238 {
1239         sqlite3 *db = NULL;
1240         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1241         int ret;
1242
1243         /* Check pkgname is valid */
1244         if (pkgname == NULL) {
1245                 return NOTIFICATION_ERROR_INVALID_DATA;
1246         }
1247
1248         /* Open DB */
1249         db = notification_db_open(DBPATH);
1250         if (!db) {
1251                 return NOTIFICATION_ERROR_FROM_DB;
1252         }
1253
1254         /* Make query */
1255         snprintf(query, sizeof(query), "delete from noti_list "
1256                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1257                  priv_id);
1258
1259         /* execute DB */
1260         ret = notification_db_exec(db, query);
1261
1262         /* Close DB */
1263         if (db) {
1264                 notification_db_close(&db);
1265         }
1266
1267         return ret;
1268 }
1269
1270 notification_error_e notification_noti_get_count(notification_type_e type,
1271                                                  const char *pkgname,
1272                                                  int group_id, int priv_id,
1273                                                  int *count)
1274 {
1275         sqlite3 *db = NULL;
1276         sqlite3_stmt *stmt = NULL;
1277         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1278         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1279         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1280         char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
1281
1282         int ret = 0, get_count = 0, internal_group_id = 0;
1283         int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
1284         int flag_where = 0;
1285         int flag_where_more = 0;
1286         int ret_vconf = 0;
1287
1288         /* Open DB */
1289         db = notification_db_open(DBPATH);
1290         if (!db) {
1291                 return NOTIFICATION_ERROR_FROM_DB;
1292         }
1293
1294         /* Check current sim status */
1295         ret_vconf = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1296
1297         /* Make query */
1298         snprintf(query_base, sizeof(query_base),
1299                  "select count(*) from noti_list ");
1300
1301         if (pkgname != NULL) {
1302                 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
1303                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1304                                 snprintf(query_where, sizeof(query_where),
1305                                          "where caller_pkgname = '%s' ",
1306                                          pkgname);
1307                                 flag_where = 1;
1308                         } else {
1309                                 internal_group_id =
1310                                     _notification_noti_get_internal_group_id_by_priv_id
1311                                     (pkgname, priv_id, db);
1312                                 snprintf(query_where, sizeof(query_where),
1313                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
1314                                          pkgname, internal_group_id);
1315                                 flag_where = 1;
1316                         }
1317                 } else {
1318                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1319                                 snprintf(query_where, sizeof(query_where),
1320                                          "where caller_pkgname = '%s' and group_id = %d ",
1321                                          pkgname, group_id);
1322                                 flag_where = 1;
1323                         } else {
1324                                 internal_group_id =
1325                                     _notification_noti_get_internal_group_id_by_priv_id
1326                                     (pkgname, priv_id, db);
1327                                 snprintf(query_where, sizeof(query_where),
1328                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
1329                                          pkgname, internal_group_id);
1330                                 flag_where = 1;
1331                         }
1332                 }
1333
1334         }
1335
1336         if (ret_vconf == 0 && status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1337                 if (type != NOTIFICATION_TYPE_NONE) {
1338                         snprintf(query_where_more, sizeof(query_where_more),
1339                                  "type = %d ", type);
1340                         flag_where_more = 1;
1341                 }
1342         } else {
1343                 if (type != NOTIFICATION_TYPE_NONE) {
1344                         snprintf(query_where_more, sizeof(query_where_more),
1345                                  "type = %d and flag_simmode = 0 ", type);
1346                         flag_where_more = 1;
1347                 } else {
1348                         snprintf(query_where_more, sizeof(query_where_more),
1349                                  "flag_simmode = 0 ");
1350                         flag_where_more = 1;
1351                 }
1352         }
1353
1354         if (flag_where == 1) {
1355                 if (flag_where_more == 1) {
1356                         snprintf(query, sizeof(query), "%s %s and %s",
1357                                  query_base, query_where, query_where_more);
1358                 } else {
1359                         snprintf(query, sizeof(query), "%s %s", query_base,
1360                                  query_where);
1361                 }
1362
1363         } else {
1364                 if (flag_where_more == 1) {
1365                         snprintf(query, sizeof(query), "%s where %s",
1366                                  query_base, query_where_more);
1367                 } else {
1368                         snprintf(query, sizeof(query), "%s", query_base);
1369                 }
1370         }
1371
1372         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1373         if (ret != SQLITE_OK) {
1374                 NOTIFICATION_ERR("Select Query : %s", query);
1375                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1376                                  sqlite3_errmsg(db));
1377
1378                 ret = NOTIFICATION_ERROR_FROM_DB;
1379                 goto err;
1380         }
1381
1382         ret = sqlite3_step(stmt);
1383         if (ret == SQLITE_ROW) {
1384                 get_count = sqlite3_column_int(stmt, 0);
1385         }
1386
1387         ret = NOTIFICATION_ERROR_NONE;
1388
1389 err:
1390         if (stmt) {
1391                 sqlite3_finalize(stmt);
1392         }
1393
1394         /* Close DB */
1395         if (db) {
1396                 notification_db_close(&db);
1397         }
1398
1399         *count = get_count;
1400
1401         return ret;
1402 }
1403
1404 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
1405                                                          int count,
1406                                                          notification_list_h *
1407                                                          list)
1408 {
1409         sqlite3 *db = NULL;
1410         sqlite3_stmt *stmt = NULL;
1411         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1412         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1413         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1414
1415         int ret = 0;
1416         notification_list_h get_list = NULL;
1417         notification_h noti = NULL;
1418         int internal_count = 0;
1419         int status;
1420
1421         /* Open DB */
1422         db = notification_db_open(DBPATH);
1423         if (!db) {
1424                 return NOTIFICATION_ERROR_FROM_DB;
1425         }
1426
1427         /* Check current sim status */
1428         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1429
1430         /* Make query */
1431         snprintf(query_base, sizeof(query_base), "select "
1432                  "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1433                  "b_text, b_key, b_format_args, num_format_args, "
1434                  "text_domain, text_dir, time, insert_time, args, group_args, "
1435                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1436                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
1437                  "flags_for_property, display_applist, progress_size, progress_percentage "
1438                  "from noti_list ");
1439
1440         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1441                 if (type != NOTIFICATION_TYPE_NONE) {
1442                         snprintf(query_where, sizeof(query_where),
1443                                  "where type = %d ", type);
1444                 }
1445         } else {
1446                 if (type != NOTIFICATION_TYPE_NONE) {
1447                         snprintf(query_where, sizeof(query_where),
1448                                  "where type = %d and flag_simmode = 0 ", type);
1449                 } else {
1450                         snprintf(query_where, sizeof(query_where),
1451                                  "where flag_simmode = 0 ");
1452                 }
1453         }
1454
1455         snprintf(query, sizeof(query),
1456                  "%s %s "
1457                  "group by internal_group_id "
1458                  "order by rowid desc, time desc", query_base, query_where);
1459
1460         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1461         if (ret != SQLITE_OK) {
1462                 NOTIFICATION_ERR("Select Query : %s", query);
1463                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1464                                  sqlite3_errmsg(db));
1465
1466                 ret = NOTIFICATION_ERROR_FROM_DB;
1467                 goto err;
1468         }
1469
1470         while (sqlite3_step(stmt) == SQLITE_ROW) {
1471                 /* Make notification list */
1472                 noti = _notification_noti_get_item(stmt);
1473                 if (noti != NULL) {
1474                         internal_count++;
1475
1476                         get_list = notification_list_append(get_list, noti);
1477
1478                         if (count != -1 && internal_count >= count) {
1479                                 NOTIFICATION_INFO
1480                                     ("internal count %d >= count %d",
1481                                      internal_count, count);
1482                                 break;
1483                         }
1484                 }
1485         }
1486
1487         ret = NOTIFICATION_ERROR_NONE;
1488
1489 err:
1490         if (stmt) {
1491                 sqlite3_finalize(stmt);
1492         }
1493
1494         /* Close DB */
1495         if (db) {
1496                 notification_db_close(&db);
1497         }
1498
1499         if (get_list != NULL) {
1500                 *list = notification_list_get_head(get_list);
1501         }
1502
1503         return ret;
1504 }
1505
1506 notification_error_e notification_noti_get_detail_list(const char *pkgname,
1507                                                        int group_id,
1508                                                        int priv_id, int count,
1509                                                        notification_list_h *list)
1510 {
1511         sqlite3 *db = NULL;
1512         sqlite3_stmt *stmt = NULL;
1513         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1514         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1515
1516         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1517         int ret = 0;
1518         notification_list_h get_list = NULL;
1519         notification_h noti = NULL;
1520         int internal_count = 0;
1521         int internal_group_id = 0;
1522         int status = 0; /* If the vconf_get_int failed, the status will be the garbage value */
1523
1524         /* Open DB */
1525         db = notification_db_open(DBPATH);
1526         if (!db) {
1527                 return NOTIFICATION_ERROR_FROM_DB;
1528         }
1529
1530         /* Check current sim status */
1531         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1532
1533         /* Make query */
1534         snprintf(query_base, sizeof(query_base), "select "
1535                  "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1536                  "b_text, b_key, b_format_args, num_format_args, "
1537                  "text_domain, text_dir, time, insert_time, args, group_args, "
1538                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1539                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb,"
1540                  "flags_for_property, display_applist, progress_size, progress_percentage "
1541                  "from noti_list ");
1542
1543         if (priv_id == NOTIFICATION_PRIV_ID_NONE && group_id == NOTIFICATION_GROUP_ID_NONE) {
1544                 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1545                         snprintf(query_where, sizeof(query_where),
1546                                  "where  caller_pkgname = '%s' ",
1547                                  pkgname, internal_group_id);
1548                 } else {
1549                         snprintf(query_where, sizeof(query_where),
1550                                  "where  caller_pkgname = '%s' and flag_simmode = 0 ",
1551                                  pkgname, internal_group_id);
1552                 }
1553         } else {
1554                 internal_group_id =
1555                     _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1556                                                                         priv_id, db);
1557
1558                 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1559                         snprintf(query_where, sizeof(query_where),
1560                                  "where  caller_pkgname = '%s' and internal_group_id = %d ",
1561                                  pkgname, internal_group_id);
1562                 } else {
1563                         snprintf(query_where, sizeof(query_where),
1564                                  "where  caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
1565                                  pkgname, internal_group_id);
1566                 }
1567         }
1568
1569         snprintf(query, sizeof(query),
1570                  "%s %s "
1571                  "order by rowid desc, time desc", query_base, query_where);
1572
1573         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1574         if (ret != SQLITE_OK) {
1575                 NOTIFICATION_ERR("Select Query : %s", query);
1576                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1577                                  sqlite3_errmsg(db));
1578
1579                 ret = NOTIFICATION_ERROR_FROM_DB;
1580                 goto err;
1581         }
1582
1583         while (sqlite3_step(stmt) == SQLITE_ROW) {
1584                 /* Make notification list */
1585                 noti = _notification_noti_get_item(stmt);
1586                 if (noti != NULL) {
1587                         internal_count++;
1588
1589                         get_list = notification_list_append(get_list, noti);
1590
1591                         if (count != -1 && internal_count >= count) {
1592                                 NOTIFICATION_INFO
1593                                     ("internal count %d >= count %d",
1594                                      internal_count, count);
1595                                 break;
1596                         }
1597                 }
1598         }
1599
1600         ret = NOTIFICATION_ERROR_NONE;
1601
1602 err:
1603         if (stmt) {
1604                 sqlite3_finalize(stmt);
1605         }
1606
1607         /* Close DB */
1608         if (db) {
1609                 notification_db_close(&db);
1610         }
1611
1612         if (get_list != NULL) {
1613                 *list = notification_list_get_head(get_list);
1614         }
1615
1616         return ret;
1617 }