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