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